|
@@ -21,6 +21,7 @@ import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
import java.io.StringReader;
|
|
|
+import java.nio.file.Files;
|
|
|
import java.nio.file.Path;
|
|
|
import java.nio.file.Paths;
|
|
|
import java.util.ArrayList;
|
|
@@ -28,9 +29,12 @@ import java.util.List;
|
|
|
import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
+import javax.servlet.ServletContext;
|
|
|
+
|
|
|
import org.codelibs.core.io.CopyUtil;
|
|
|
import org.codelibs.curl.Curl;
|
|
|
import org.codelibs.curl.CurlResponse;
|
|
|
+import org.codelibs.fess.exception.PluginException;
|
|
|
import org.codelibs.fess.util.ComponentUtil;
|
|
|
import org.codelibs.fess.util.ResourceUtil;
|
|
|
import org.codelibs.nekohtml.parsers.DOMParser;
|
|
@@ -41,8 +45,6 @@ import org.w3c.dom.Document;
|
|
|
import org.w3c.dom.NodeList;
|
|
|
import org.xml.sax.InputSource;
|
|
|
|
|
|
-import javax.servlet.ServletContext;
|
|
|
-
|
|
|
// TODO: refactoring, exception handling, improving codes
|
|
|
public class PluginHelper {
|
|
|
private static final Logger logger = LoggerFactory.getLogger(PluginHelper.class);
|
|
@@ -64,12 +66,12 @@ public class PluginHelper {
|
|
|
|
|
|
protected List<Artifact> processRepository(final ArtifactType artifactType, final String url) {
|
|
|
final List<Artifact> list = new ArrayList<>();
|
|
|
- final String repoContent = getRepositoryContentAsString(url);
|
|
|
+ final String repoContent = getRepositoryContent(url);
|
|
|
final Matcher matcher = Pattern.compile("href=\"[^\"]*(" + artifactType.getId() + "[a-zA-Z0-9\\-]+)/?\"").matcher(repoContent);
|
|
|
while (matcher.find()) {
|
|
|
final String name = matcher.group(1);
|
|
|
final String pluginUrl = url + (url.endsWith("/") ? name + "/" : "/" + name + "/");
|
|
|
- final String mavenMetadata = getRepositoryContentAsString(pluginUrl + "maven-metadata.xml");
|
|
|
+ final String mavenMetadata = getRepositoryContent(pluginUrl + "maven-metadata.xml");
|
|
|
try (final StringReader reader = new StringReader(mavenMetadata)) {
|
|
|
final DOMParser parser = new DOMParser();
|
|
|
parser.parse(new InputSource(reader));
|
|
@@ -86,7 +88,7 @@ public class PluginHelper {
|
|
|
return list;
|
|
|
}
|
|
|
|
|
|
- protected String getRepositoryContentAsString(final String url) {
|
|
|
+ protected String getRepositoryContent(final String url) {
|
|
|
try (final CurlResponse response = Curl.get(url).execute()) {
|
|
|
return response.getContentAsString();
|
|
|
} catch (final IOException e) {
|
|
@@ -94,16 +96,8 @@ public class PluginHelper {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- protected InputStream getRepositoryContentAsStream(final String url) {
|
|
|
- try (final CurlResponse response = Curl.get(url).execute()) {
|
|
|
- return response.getContentAsStream();
|
|
|
- } catch (final IOException e) {
|
|
|
- throw new IORuntimeException(e);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
public Artifact[] getInstalledArtifacts(final ArtifactType artifactType) {
|
|
|
- final File[] jarFiles = ResourceUtil.getJarFiles(artifactType.getId());
|
|
|
+ final File[] jarFiles = ResourceUtil.getPluginJarFiles(artifactType.getId());
|
|
|
final List<Artifact> list = new ArrayList<>(jarFiles.length);
|
|
|
for (final File file : jarFiles) {
|
|
|
list.add(getArtifactFromFileName(artifactType, file.getName()));
|
|
@@ -119,39 +113,32 @@ public class PluginHelper {
|
|
|
return new Artifact(artifactName, artifactVersion, null);
|
|
|
}
|
|
|
|
|
|
- public boolean installArtifact(Artifact artifact) {
|
|
|
- try (final InputStream in = getRepositoryContentAsStream(artifact.getUrl())) {
|
|
|
- final String fileName = artifact.getName() + "-" + artifact.getVersion() + ".jar";
|
|
|
- final File file = Paths.get(getLibPath().toString(), fileName).toFile();
|
|
|
- if (!file.exists()) {
|
|
|
- file.createNewFile();
|
|
|
- CopyUtil.copy(in, file);
|
|
|
- } else {
|
|
|
- final File tempFile = File.createTempFile(fileName, "bk");
|
|
|
- CopyUtil.copy(file, tempFile);
|
|
|
- CopyUtil.copy(in, file);
|
|
|
- tempFile.delete();
|
|
|
+ public void installArtifact(Artifact artifact) {
|
|
|
+ final String fileName = artifact.getFileName();
|
|
|
+ try (final CurlResponse response = Curl.get(artifact.getUrl()).execute()) {
|
|
|
+ try (final InputStream in = response.getContentAsStream()) {
|
|
|
+ CopyUtil.copy(in, ResourceUtil.getPluginPath(fileName).toFile());
|
|
|
}
|
|
|
- return file.exists();
|
|
|
} catch (final Exception e) {
|
|
|
- logger.warn("Failed to install the artifact " + artifact.getName(), e);
|
|
|
- return false;
|
|
|
+ throw new PluginException("Failed to install the artifact " + artifact.getName(), e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public boolean deleteInstalledArtifact(Artifact artifact) {
|
|
|
- final String fileName = artifact.getName() + "-" + artifact.getVersion() + ".jar";
|
|
|
- final File file = Paths.get(getLibPath().toString(), fileName).toFile();
|
|
|
- if (file != null && file.exists() && file.delete()) {
|
|
|
- return true;
|
|
|
- } else {
|
|
|
- logger.warn("Failed to delete the artifact " + file.getAbsolutePath());
|
|
|
- return false;
|
|
|
+ public void deleteInstalledArtifact(Artifact artifact) {
|
|
|
+ final String fileName = artifact.getFileName();
|
|
|
+ final Path jarPath = Paths.get(getPluginPath().toString(), fileName);
|
|
|
+ if (!Files.exists(jarPath)) {
|
|
|
+ throw new PluginException(fileName + " does not exist.");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ Files.delete(jarPath);
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new PluginException("Failed to delete the artifact " + fileName, e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- protected Path getLibPath() {
|
|
|
- return Paths.get(ComponentUtil.getComponent(ServletContext.class).getRealPath("/WEB-INF/lib/"));
|
|
|
+ protected Path getPluginPath() {
|
|
|
+ return Paths.get(ComponentUtil.getComponent(ServletContext.class).getRealPath("/WEB-INF/plugin"));
|
|
|
}
|
|
|
|
|
|
public static class Artifact {
|
|
@@ -173,6 +160,10 @@ public class PluginHelper {
|
|
|
return version;
|
|
|
}
|
|
|
|
|
|
+ public String getFileName() {
|
|
|
+ return name + "-" + version + ".jar";
|
|
|
+ }
|
|
|
+
|
|
|
public String getUrl() {
|
|
|
return url;
|
|
|
}
|