-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix #2819 add ProtocolHelper for URL protocol management and update r…
…elated classes
- Loading branch information
Showing
7 changed files
with
207 additions
and
5 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
src/main/java/org/codelibs/fess/helper/ProtocolHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright 2012-2024 CodeLibs Project and the Others. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
* either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
package org.codelibs.fess.helper; | ||
|
||
import static org.codelibs.core.stream.StreamUtil.split; | ||
import static org.codelibs.core.stream.StreamUtil.stream; | ||
|
||
import java.util.Arrays; | ||
|
||
import javax.annotation.PostConstruct; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.codelibs.core.lang.StringUtil; | ||
import org.codelibs.fess.mylasta.direction.FessConfig; | ||
import org.codelibs.fess.util.ComponentUtil; | ||
|
||
public class ProtocolHelper { | ||
private static final Logger logger = LogManager.getLogger(ProtocolHelper.class); | ||
|
||
protected String[] webProtocols = StringUtil.EMPTY_STRINGS; | ||
|
||
protected String[] fileProtocols = StringUtil.EMPTY_STRINGS; | ||
|
||
@PostConstruct | ||
public void init() { | ||
final FessConfig fessConfig = ComponentUtil.getFessConfig(); | ||
webProtocols = split(fessConfig.getCrawlerWebProtocols(), ",") | ||
.get(stream -> stream.filter(StringUtil::isNotBlank).map(s -> s.trim() + ":").toArray(n -> new String[n])); | ||
fileProtocols = split(fessConfig.getCrawlerFileProtocols(), ",") | ||
.get(stream -> stream.filter(StringUtil::isNotBlank).map(s -> s.trim() + ":").toArray(n -> new String[n])); | ||
if (logger.isDebugEnabled()) { | ||
logger.debug("web protocols: {}", Arrays.toString(webProtocols)); | ||
logger.debug("file protocols: {}", Arrays.toString(fileProtocols)); | ||
} | ||
} | ||
|
||
public String[] getWebProtocols() { | ||
return webProtocols; | ||
} | ||
|
||
public String[] getFileProtocols() { | ||
return fileProtocols; | ||
} | ||
|
||
public boolean isValidWebProtocol(final String url) { | ||
return stream(webProtocols).get(stream -> stream.anyMatch(s -> url.startsWith(s))); | ||
} | ||
|
||
public boolean isValidFileProtocol(final String url) { | ||
return stream(fileProtocols).get(stream -> stream.anyMatch(s -> url.startsWith(s))); | ||
} | ||
|
||
public void addWebProtocol(final String protocol) { | ||
final String prefix = protocol + ":"; | ||
if (stream(webProtocols).get(stream -> stream.anyMatch(s -> s.equals(prefix)))) { | ||
logger.debug("web protocols contains {}.", protocol); | ||
return; | ||
} | ||
webProtocols = Arrays.copyOf(webProtocols, webProtocols.length + 1); | ||
webProtocols[webProtocols.length - 1] = prefix; | ||
} | ||
|
||
public void addFileProtocol(final String protocol) { | ||
final String prefix = protocol + ":"; | ||
if (stream(fileProtocols).get(stream -> stream.anyMatch(s -> s.equals(prefix)))) { | ||
logger.debug("file protocols contains {}.", protocol); | ||
return; | ||
} | ||
fileProtocols = Arrays.copyOf(fileProtocols, fileProtocols.length + 1); | ||
fileProtocols[fileProtocols.length - 1] = prefix; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
src/test/java/org/codelibs/fess/helper/ProtocolHelperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright 2012-2024 CodeLibs Project and the Others. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
* either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
package org.codelibs.fess.helper; | ||
|
||
import org.codelibs.fess.mylasta.direction.FessConfig; | ||
import org.codelibs.fess.unit.UnitFessTestCase; | ||
import org.codelibs.fess.util.ComponentUtil; | ||
|
||
public class ProtocolHelperTest extends UnitFessTestCase { | ||
public void test_add_httpx() { | ||
ComponentUtil.setFessConfig(new FessConfig.SimpleImpl() { | ||
@Override | ||
public String getCrawlerWebProtocols() { | ||
return "http,https"; | ||
} | ||
|
||
@Override | ||
public String getCrawlerFileProtocols() { | ||
return "file,smb"; | ||
} | ||
}); | ||
|
||
final ProtocolHelper protocolHelper = new ProtocolHelper(); | ||
protocolHelper.init(); | ||
assertEquals(2, protocolHelper.getWebProtocols().length); | ||
assertEquals("http:", protocolHelper.getWebProtocols()[0]); | ||
assertEquals("https:", protocolHelper.getWebProtocols()[1]); | ||
assertEquals(2, protocolHelper.getFileProtocols().length); | ||
assertEquals("file:", protocolHelper.getFileProtocols()[0]); | ||
assertEquals("smb:", protocolHelper.getFileProtocols()[1]); | ||
|
||
assertFalse(protocolHelper.isValidWebProtocol("httpx://test")); | ||
|
||
protocolHelper.addWebProtocol("httpx"); | ||
assertEquals(3, protocolHelper.getWebProtocols().length); | ||
assertEquals("http:", protocolHelper.getWebProtocols()[0]); | ||
assertEquals("https:", protocolHelper.getWebProtocols()[1]); | ||
assertEquals("httpx:", protocolHelper.getWebProtocols()[2]); | ||
assertEquals(2, protocolHelper.getFileProtocols().length); | ||
assertEquals("file:", protocolHelper.getFileProtocols()[0]); | ||
assertEquals("smb:", protocolHelper.getFileProtocols()[1]); | ||
|
||
assertTrue(protocolHelper.isValidWebProtocol("httpx://test")); | ||
|
||
protocolHelper.addWebProtocol("httpx"); | ||
assertEquals(3, protocolHelper.getWebProtocols().length); | ||
assertEquals(2, protocolHelper.getFileProtocols().length); | ||
} | ||
|
||
public void test_add_smbx() { | ||
ComponentUtil.setFessConfig(new FessConfig.SimpleImpl() { | ||
@Override | ||
public String getCrawlerWebProtocols() { | ||
return "http,https"; | ||
} | ||
|
||
@Override | ||
public String getCrawlerFileProtocols() { | ||
return "file,smb"; | ||
} | ||
}); | ||
|
||
final ProtocolHelper protocolHelper = new ProtocolHelper(); | ||
protocolHelper.init(); | ||
assertEquals(2, protocolHelper.getWebProtocols().length); | ||
assertEquals("http:", protocolHelper.getWebProtocols()[0]); | ||
assertEquals("https:", protocolHelper.getWebProtocols()[1]); | ||
assertEquals(2, protocolHelper.getFileProtocols().length); | ||
assertEquals("file:", protocolHelper.getFileProtocols()[0]); | ||
assertEquals("smb:", protocolHelper.getFileProtocols()[1]); | ||
|
||
assertFalse(protocolHelper.isValidFileProtocol("smbx://test")); | ||
|
||
protocolHelper.addFileProtocol("smbx"); | ||
assertEquals(2, protocolHelper.getWebProtocols().length); | ||
assertEquals("http:", protocolHelper.getWebProtocols()[0]); | ||
assertEquals("https:", protocolHelper.getWebProtocols()[1]); | ||
assertEquals(3, protocolHelper.getFileProtocols().length); | ||
assertEquals("file:", protocolHelper.getFileProtocols()[0]); | ||
assertEquals("smb:", protocolHelper.getFileProtocols()[1]); | ||
assertEquals("smbx:", protocolHelper.getFileProtocols()[2]); | ||
|
||
assertTrue(protocolHelper.isValidFileProtocol("smbx://test")); | ||
|
||
protocolHelper.addFileProtocol("smbx"); | ||
assertEquals(2, protocolHelper.getWebProtocols().length); | ||
assertEquals(3, protocolHelper.getFileProtocols().length); | ||
} | ||
} |