Skip to content

Commit

Permalink
fix #2776 Tweak exception handling in PluginHelper for better clarity.
Browse files Browse the repository at this point in the history
  • Loading branch information
marevol committed Oct 25, 2023
1 parent 2d07323 commit 2bc9269
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 32 deletions.
54 changes: 28 additions & 26 deletions src/main/java/org/codelibs/fess/helper/PluginHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,34 +121,36 @@ protected List<Artifact> processRepository(final ArtifactType artifactType, fina
while (matcher.find()) {
final String name = matcher.group(1);
final String pluginUrl = url + (url.endsWith("/") ? name + "/" : "/" + name + "/");
final String pluginMetaContent = getRepositoryContent(pluginUrl + "maven-metadata.xml");
try (final InputStream is = new ByteArrayInputStream(pluginMetaContent.getBytes(Constants.UTF_8_CHARSET))) {
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setFeature(Constants.FEATURE_SECURE_PROCESSING, true);
factory.setFeature(Constants.FEATURE_EXTERNAL_GENERAL_ENTITIES, false);
factory.setFeature(Constants.FEATURE_EXTERNAL_PARAMETER_ENTITIES, false);
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, StringUtil.EMPTY);
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, StringUtil.EMPTY);
final DocumentBuilder builder = factory.newDocumentBuilder();
final Document document = builder.parse(is);
final NodeList nodeList = document.getElementsByTagName("version");
for (int i = 0; i < nodeList.getLength(); i++) {
final String version = nodeList.item(i).getTextContent();
if (isTargetPluginVersion(version)) {
if (version.endsWith("SNAPSHOT")) {
final String snapshotVersion = getSnapshotActualVersion(builder, pluginUrl, version);
if (StringUtil.isNotBlank(snapshotVersion)) {
final String actualVersion = version.replace("SNAPSHOT", snapshotVersion);
list.add(
new Artifact(name, actualVersion, pluginUrl + version + "/" + name + "-" + actualVersion + ".jar"));
} else if (logger.isDebugEnabled()) {
logger.debug("Snapshot name is not found: {}/{}", name, version);
try {
final String pluginMetaContent = getRepositoryContent(pluginUrl + "maven-metadata.xml");
try (final InputStream is = new ByteArrayInputStream(pluginMetaContent.getBytes(Constants.UTF_8_CHARSET))) {
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setFeature(Constants.FEATURE_SECURE_PROCESSING, true);
factory.setFeature(Constants.FEATURE_EXTERNAL_GENERAL_ENTITIES, false);
factory.setFeature(Constants.FEATURE_EXTERNAL_PARAMETER_ENTITIES, false);
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, StringUtil.EMPTY);
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, StringUtil.EMPTY);
final DocumentBuilder builder = factory.newDocumentBuilder();
final Document document = builder.parse(is);
final NodeList nodeList = document.getElementsByTagName("version");
for (int i = 0; i < nodeList.getLength(); i++) {
final String version = nodeList.item(i).getTextContent();
if (isTargetPluginVersion(version)) {
if (version.endsWith("SNAPSHOT")) {
final String snapshotVersion = getSnapshotActualVersion(builder, pluginUrl, version);
if (StringUtil.isNotBlank(snapshotVersion)) {
final String actualVersion = version.replace("SNAPSHOT", snapshotVersion);
list.add(new Artifact(name, actualVersion,
pluginUrl + version + "/" + name + "-" + actualVersion + ".jar"));
} else if (logger.isDebugEnabled()) {
logger.debug("Snapshot name is not found: {}/{}", name, version);
}
} else {
list.add(new Artifact(name, version, pluginUrl + version + "/" + name + "-" + version + ".jar"));
}
} else {
list.add(new Artifact(name, version, pluginUrl + version + "/" + name + "-" + version + ".jar"));
} else if (logger.isDebugEnabled()) {
logger.debug("{}:{} is ignored.", name, version);
}
} else if (logger.isDebugEnabled()) {
logger.debug("{}:{} is ignored.", name, version);
}
}
} catch (final Exception e) {
Expand Down
14 changes: 8 additions & 6 deletions src/test/java/org/codelibs/fess/helper/PluginHelperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.codelibs.fess.helper;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

Expand All @@ -25,6 +26,7 @@
import org.codelibs.fess.helper.PluginHelper.Artifact;
import org.codelibs.fess.helper.PluginHelper.ArtifactType;
import org.codelibs.fess.unit.UnitFessTestCase;
import org.lastaflute.di.exception.IORuntimeException;

public class PluginHelperTest extends UnitFessTestCase {
private PluginHelper pluginHelper;
Expand All @@ -44,20 +46,20 @@ protected String getRepositoryContent(String url) {
if (url.contains("plugin/repo1")) {
try (InputStream is = ResourceUtil.getResourceAsStream(url)) {
return new String(InputStreamUtil.getBytes(is), Constants.UTF_8);
} catch (Exception e) {
return "";
} catch (IOException e) {
throw new IORuntimeException(e);
}
} else if (url.contains("plugin/repo2")) {
try (InputStream is = ResourceUtil.getResourceAsStream(url)) {
return new String(InputStreamUtil.getBytes(is), Constants.UTF_8);
} catch (Exception e) {
return "";
} catch (IOException e) {
throw new IORuntimeException(e);
}
} else if (url.contains("plugin/repo.yaml")) {
try (InputStream is = ResourceUtil.getResourceAsStream(url)) {
return new String(InputStreamUtil.getBytes(is), Constants.UTF_8);
} catch (Exception e) {
return "";
} catch (IOException e) {
throw new IORuntimeException(e);
}
}
throw new FessSystemException("unknown");
Expand Down

0 comments on commit 2bc9269

Please sign in to comment.