ソースを参照

#2203 update PluginHelper

igarashi 6 年 前
コミット
c24b9b0559

+ 56 - 14
src/main/java/org/codelibs/fess/helper/PluginHelper.java

@@ -17,7 +17,9 @@ package org.codelibs.fess.helper;
 
 import static org.codelibs.core.stream.StreamUtil.split;
 
+import java.io.File;
 import java.io.IOException;
+import java.io.StringReader;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.regex.Matcher;
@@ -26,14 +28,25 @@ import java.util.regex.Pattern;
 import org.codelibs.curl.Curl;
 import org.codelibs.curl.CurlResponse;
 import org.codelibs.fess.util.ComponentUtil;
+import org.codelibs.fess.util.ResourceUtil;
+import org.codelibs.nekohtml.parsers.DOMParser;
 import org.lastaflute.di.exception.IORuntimeException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.w3c.dom.Document;
+import org.w3c.dom.NodeList;
+import org.xml.sax.InputSource;
 
+// TODO: refactoring
 public class PluginHelper {
+    private static final Logger logger = LoggerFactory.getLogger(PluginHelper.class);
 
-    public Artifact[] getArtifacts(PluginType pluginType) {
-        List<Artifact> list = new ArrayList<>();
-        for (String url : getRepositories()) {
-            list.addAll(processRepository(pluginType, url));
+    protected static final String VERSION = "version";
+
+    public Artifact[] getArtifacts(final PluginType pluginType) {
+        final List<Artifact> list = new ArrayList<>();
+        for (final String url : getRepositories()) {
+            list.addAll(processRepository(pluginType, url, ""));
         }
         return list.toArray(new Artifact[list.size()]);
     }
@@ -43,26 +56,55 @@ public class PluginHelper {
                 stream -> stream.map(s -> s.trim()).toArray(n -> new String[n]));
     }
 
-    protected List<Artifact> processRepository(PluginType pluginType, String url) {
-        List<Artifact> list = new ArrayList<>();
-        String repoContent = getRepositoryContent(url);
-        Matcher matcher = Pattern.compile("href=\"[^\"]*(" + pluginType.getId() + "[a-zA-Z0-9\\-]+)/?\"").matcher(repoContent);
+    protected List<Artifact> processRepository(final PluginType pluginType, final String url, final String index) {
+        final List<Artifact> list = new ArrayList<>();
+        final String repoContent = getRepositoryContent(url + "/" + index);
+        final Matcher matcher = Pattern.compile("href=\"[^\"]*(" + pluginType.getId() + "[a-zA-Z0-9\\-]+)/?\"").matcher(repoContent);
         while (matcher.find()) {
-            String name = matcher.group(1);
-            // TODO parse maven-metadata.xml from url/name to set version and url
-            list.add(new Artifact(name, "TODO", "TODO"));
+            final String name = matcher.group(1);
+            try {
+                final String pluginUrl = url + "/" + name + "/";
+                final String mavenMetadata = getRepositoryContent(pluginUrl + "maven-metadata.xml");
+                final DOMParser parser = new DOMParser();
+                parser.parse(new InputSource(new StringReader(mavenMetadata)));
+                final Document document = parser.getDocument();
+                final NodeList nodeList = document.getElementsByTagName(VERSION);
+                for (int i = 0; i < nodeList.getLength(); i++) {
+                    final String version = nodeList.item(i).getTextContent();
+                    list.add(new Artifact(name, version, pluginUrl + version + "/" + name + "-" + version + ".jar"));
+                }
+            } catch (final Exception e) {
+                logger.warn("Failed to parse maven-metadata.xml.", e);
+            }
         }
         return list;
     }
 
-    protected String getRepositoryContent(String url) {
-        try (CurlResponse response = Curl.get(url).execute()) {
+    protected String getRepositoryContent(final String url) {
+        try (final CurlResponse response = Curl.get(url).execute()) {
             return response.getContentAsString();
-        } catch (IOException e) {
+        } catch (final IOException e) {
             throw new IORuntimeException(e);
         }
     }
 
+    protected Artifact[] getArtifactsAlreadyInstalled(final PluginType pluginType) {
+        final List<Artifact> list = new ArrayList<>();
+        File[] jarFiles = ResourceUtil.getJarFiles(pluginType.getId());
+        for (final File file : jarFiles) {
+            list.add(getArtifactFromFileName(pluginType, file.getName()));
+        }
+        return list.toArray(new Artifact[list.size()]);
+    }
+
+    protected Artifact getArtifactFromFileName(final PluginType pluginType, final String fileName) {
+        final String convertedFileName = fileName.substring(pluginType.getId().length() + 1, fileName.lastIndexOf('.'));
+        final int firstIndexOfDash = convertedFileName.indexOf("-");
+        final String artifactName = pluginType.getId() + "-" + convertedFileName.substring(0, firstIndexOfDash);
+        final String artifactVersion = convertedFileName.substring(firstIndexOfDash + 1);
+        return new Artifact(artifactName, artifactVersion, "NONE");
+    }
+
     public static class Artifact {
         protected final String name;
         protected final String version;

+ 23 - 7
src/test/java/org/codelibs/fess/helper/PluginHelperTest.java

@@ -36,17 +36,17 @@ public class PluginHelperTest extends UnitFessTestCase {
         super.setUp();
         pluginHelper = new PluginHelper() {
             protected String[] getRepositories() {
-                return new String[] { "plugin/repo1.html", "plugin/repo2.html" };
+                return new String[] { "plugin/repo1/", "plugin/repo2/" };
             }
 
             protected String getRepositoryContent(String url) {
-                if (url.contains("repo1")) {
+                if (url.contains("plugin/repo1")) {
                     try (InputStream is = ResourceUtil.getResourceAsStream(url)) {
                         return new String(InputStreamUtil.getBytes(is), Constants.UTF_8);
                     } catch (IOException e) {
                         throw new IORuntimeException(e);
                     }
-                } else if (url.contains("repo2")) {
+                } else if (url.contains("plugin/repo2")) {
                     try (InputStream is = ResourceUtil.getResourceAsStream(url)) {
                         return new String(InputStreamUtil.getBytes(is), Constants.UTF_8);
                     } catch (IOException e) {
@@ -59,14 +59,30 @@ public class PluginHelperTest extends UnitFessTestCase {
     }
 
     public void test_processRepository1() {
-        List<Artifact> list = pluginHelper.processRepository(PluginType.DATA_STORE, "plugin/repo1.html");
-        assertEquals(11, list.size());
+        List<Artifact> list = pluginHelper.processRepository(PluginType.DATA_STORE, "plugin/repo1/", "index.html");
+        assertEquals(7, list.size());
         assertEquals("fess-ds-atlassian", list.get(0).getName());
+        assertEquals("12.2.0", list.get(0).getVersion());
+        assertEquals("plugin/repo1/fess-ds-atlassian/12.2.0/fess-ds-atlassian-12.2.0.jar", list.get(0).getUrl().replace("//", "/"));
     }
 
     public void test_processRepository2() {
-        List<Artifact> list = pluginHelper.processRepository(PluginType.DATA_STORE, "plugin/repo2.html");
-        assertEquals(14, list.size());
+        List<Artifact> list = pluginHelper.processRepository(PluginType.DATA_STORE, "plugin/repo2/", "index.html");
+        assertEquals(7, list.size());
         assertEquals("fess-ds-atlassian", list.get(0).getName());
+        assertEquals("12.2.0", list.get(0).getVersion());
+        assertEquals("plugin/repo2/fess-ds-atlassian/12.2.0/fess-ds-atlassian-12.2.0.jar", list.get(0).getUrl().replace("//", "/"));
+    }
+
+    public void test_getArtifactFromFileName1() {
+        Artifact artifact = pluginHelper.getArtifactFromFileName(PluginType.DATA_STORE, "fess-ds-atlassian-13.2.0.jar");
+        assertEquals("fess-ds-atlassian", artifact.getName());
+        assertEquals("13.2.0", artifact.getVersion());
+    }
+
+    public void test_getArtifactFromFileName2() {
+        Artifact artifact = pluginHelper.getArtifactFromFileName(PluginType.DATA_STORE, "fess-ds-atlassian-13.2.1-20190708.212247-1.jar");
+        assertEquals("fess-ds-atlassian", artifact.getName());
+        assertEquals("13.2.1-20190708.212247-1", artifact.getVersion());
     }
 }

+ 19 - 0
src/test/resources/plugin/repo1/fess-ds-atlassian/maven-metadata.xml

@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<metadata>
+    <groupId>org.codelibs.fess</groupId>
+    <artifactId>fess-ds-atlassian</artifactId>
+    <versioning>
+        <latest>13.2.0</latest>
+        <release>13.2.0</release>
+        <versions>
+            <version>12.2.0</version>
+            <version>12.5.0</version>
+            <version>12.6.0</version>
+            <version>12.7.0</version>
+            <version>13.0.0</version>
+            <version>13.1.0</version>
+            <version>13.2.0</version>
+        </versions>
+        <lastUpdated>20190704004757</lastUpdated>
+    </versioning>
+</metadata>

+ 2 - 2
src/test/resources/plugin/repo1.html → src/test/resources/plugin/repo1/index.html

@@ -18,7 +18,7 @@ body {
 	<hr/>
 	<main>
 		<pre id="contents">
-<a href="../">../</a>
+<a href="../plugin/plugin">../</a>
 <a href="fess/" title="fess/">fess/</a>                                                            -         -      
 <a href="fess-crawler/" title="fess-crawler/">fess-crawler/</a>                                                    -         -      
 <a href="fess-crawler-db/" title="fess-crawler-db/">fess-crawler-db/</a>                                                 -         -      
@@ -28,7 +28,7 @@ body {
 <a href="fess-crawler-lasta/" title="fess-crawler-lasta/">fess-crawler-lasta/</a>                                              -         -      
 <a href="fess-crawler-parent/" title="fess-crawler-parent/">fess-crawler-parent/</a>                                             -         -      
 <a href="fess-crawler-webdriver/" title="fess-crawler-webdriver/">fess-crawler-webdriver/</a>                                          -         -      
-<a href="fess-ds-atlassian/" title="fess-ds-atlassian/">fess-ds-atlassian/</a>                                               -         -      
+<a href="fess-ds-atlassian" title="fess-ds-atlassian/">fess-ds-atlassian/</a>                                               -         -
 <a href="fess-ds-box/" title="fess-ds-box/">fess-ds-box/</a>                                                     -         -      
 <a href="fess-ds-csv/" title="fess-ds-csv/">fess-ds-csv/</a>                                                     -         -      
 <a href="fess-ds-db/" title="fess-ds-db/">fess-ds-db/</a>                                                      -         -      

+ 19 - 0
src/test/resources/plugin/repo2/fess-ds-atlassian/maven-metadata.xml

@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<metadata>
+    <groupId>org.codelibs.fess</groupId>
+    <artifactId>fess-ds-atlassian</artifactId>
+    <versioning>
+        <latest>13.2.0</latest>
+        <release>13.2.0</release>
+        <versions>
+            <version>12.2.0</version>
+            <version>12.5.0</version>
+            <version>12.6.0</version>
+            <version>12.7.0</version>
+            <version>13.0.0</version>
+            <version>13.1.0</version>
+            <version>13.2.0</version>
+        </versions>
+        <lastUpdated>20190704004757</lastUpdated>
+    </versioning>
+</metadata>

+ 1 - 1
src/test/resources/plugin/repo2.html → src/test/resources/plugin/repo2/index.html

@@ -20,7 +20,7 @@
         <th>Description</th>
       </tr>
       <tr>
-        <td><a href="../">Parent Directory</a></td>
+        <td><a href="../..">Parent Directory</a></td>
       </tr>
                   <tr>
             <td><a href="https://oss.sonatype.org/content/repositories/snapshots/org/codelibs/fess/fess-crawler-db-h2/">fess-crawler-db-h2/</a></td>