|
@@ -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;
|