Bläddra i källkod

fix #2776 Tweak exception handling in PluginHelper for better clarity.

Shinsuke Sugaya 1 år sedan
förälder
incheckning
2bc92694bc

+ 28 - 26
src/main/java/org/codelibs/fess/helper/PluginHelper.java

@@ -121,34 +121,36 @@ public class PluginHelper {
         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) {

+ 8 - 6
src/test/java/org/codelibs/fess/helper/PluginHelperTest.java

@@ -15,6 +15,7 @@
  */
 package org.codelibs.fess.helper;
 
+import java.io.IOException;
 import java.io.InputStream;
 import java.util.List;
 
@@ -25,6 +26,7 @@ import org.codelibs.fess.exception.FessSystemException;
 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;
@@ -44,20 +46,20 @@ public class PluginHelperTest extends UnitFessTestCase {
                 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");