diff --git a/src/main/java/org/codelibs/fess/app/web/admin/plugin/AdminPluginAction.java b/src/main/java/org/codelibs/fess/app/web/admin/plugin/AdminPluginAction.java index 1508786cd..850776bab 100644 --- a/src/main/java/org/codelibs/fess/app/web/admin/plugin/AdminPluginAction.java +++ b/src/main/java/org/codelibs/fess/app/web/admin/plugin/AdminPluginAction.java @@ -22,12 +22,11 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; -import javax.annotation.Resource; - import org.codelibs.fess.app.web.base.FessAdminAction; import org.codelibs.fess.helper.PluginHelper; import org.codelibs.fess.helper.PluginHelper.Artifact; import org.codelibs.fess.helper.PluginHelper.ArtifactType; +import org.codelibs.fess.util.ComponentUtil; import org.codelibs.fess.util.RenderDataUtil; import org.lastaflute.web.Execute; import org.lastaflute.web.response.HtmlResponse; @@ -39,9 +38,6 @@ public class AdminPluginAction extends FessAdminAction { private static final Logger logger = LoggerFactory.getLogger(AdminPluginAction.class); - @Resource - private PluginHelper pluginHelper; - @Override protected void setupHtmlData(final ActionRuntime runtime) { super.setupHtmlData(runtime); @@ -59,13 +55,7 @@ public class AdminPluginAction extends FessAdminAction { validate(form, messages -> {}, () -> asHtml(path_AdminPlugin_AdminPluginJsp)); verifyToken(() -> asHtml(path_AdminPlugin_AdminPluginJsp)); final Artifact artifact = new Artifact(form.name, form.version, null); - new Thread(() -> { - try { - pluginHelper.deleteInstalledArtifact(artifact); - } catch (final Exception e) { - logger.warn("Failed to delete " + artifact.getFileName(), e); - } - }).start(); + deleteArtifact(artifact); saveInfo(messages -> messages.addSuccessDeletePlugin(GLOBAL, artifact.getFileName())); return redirect(getClass()); } @@ -75,23 +65,7 @@ public class AdminPluginAction extends FessAdminAction { validate(form, messages -> {}, () -> asHtml(path_AdminPlugin_AdminPluginInstallpluginJsp)); verifyToken(() -> asHtml(path_AdminPlugin_AdminPluginInstallpluginJsp)); final Artifact artifact = getArtifactFromInstallForm(form); - new Thread(() -> { - final Artifact[] artifacts = pluginHelper.getInstalledArtifacts(ArtifactType.getType(artifact)); - try { - pluginHelper.installArtifact(artifact); - } catch (final Exception e) { - logger.warn("Failed to install " + artifact.getFileName(), e); - } - for (final Artifact a : artifacts) { - if (a.getName().equals(artifact.getName()) && !a.getVersion().equals(artifact.getVersion())) { - try { - pluginHelper.deleteInstalledArtifact(a); - } catch (final Exception e) { - logger.warn("Failed to uninstall " + a.getFileName(), e); - } - } - } - }).start(); + installArtifact(artifact); saveInfo(messages -> messages.addSuccessInstallPlugin(GLOBAL, artifact.getFileName())); return redirect(getClass()); } @@ -109,7 +83,8 @@ public class AdminPluginAction extends FessAdminAction { data -> data.register("installedArtifactItems", getAllInstalledArtifacts())).useForm(DeleteForm.class); } - private List> getAllAvailableArtifacts() { + public static List> getAllAvailableArtifacts() { + final PluginHelper pluginHelper = ComponentUtil.getComponent(PluginHelper.class); final List> result = new ArrayList<>(); for (final PluginHelper.ArtifactType artifactType : PluginHelper.ArtifactType.values()) { result.addAll(Arrays.stream(pluginHelper.getAvailableArtifacts(artifactType)).map(artifact -> beanToMap(artifact)) @@ -118,7 +93,8 @@ public class AdminPluginAction extends FessAdminAction { return result; } - private List> getAllInstalledArtifacts() { + public static List> getAllInstalledArtifacts() { + final PluginHelper pluginHelper = ComponentUtil.getComponent(PluginHelper.class); final List> result = new ArrayList<>(); for (final PluginHelper.ArtifactType artifactType : PluginHelper.ArtifactType.values()) { result.addAll(Arrays.stream(pluginHelper.getInstalledArtifacts(artifactType)).map(artifact -> beanToMap(artifact)) @@ -127,7 +103,7 @@ public class AdminPluginAction extends FessAdminAction { return result; } - private Map beanToMap(final Artifact artifact) { + public static Map beanToMap(final Artifact artifact) { final Map item = new HashMap<>(); item.put("type", ArtifactType.getType(artifact).getId()); item.put("name", artifact.getName()); @@ -140,4 +116,36 @@ public class AdminPluginAction extends FessAdminAction { final String[] values = form.selectedArtifact.split("\\|"); return new Artifact(values[0], values[1], values[2]); } + + public static void installArtifact(final Artifact artifact) { + final PluginHelper pluginHelper = ComponentUtil.getComponent(PluginHelper.class); + new Thread(() -> { + final Artifact[] artifacts = pluginHelper.getInstalledArtifacts(ArtifactType.getType(artifact)); + try { + pluginHelper.installArtifact(artifact); + } catch (final Exception e) { + logger.warn("Failed to install " + artifact.getFileName(), e); + } + for (final Artifact a : artifacts) { + if (a.getName().equals(artifact.getName()) && !a.getVersion().equals(artifact.getVersion())) { + try { + pluginHelper.deleteInstalledArtifact(a); + } catch (final Exception e) { + logger.warn("Failed to delete " + a.getFileName(), e); + } + } + } + }).start(); + } + + public static void deleteArtifact(final Artifact artifact) { + final PluginHelper pluginHelper = ComponentUtil.getComponent(PluginHelper.class); + new Thread(() -> { + try { + pluginHelper.deleteInstalledArtifact(artifact); + } catch (final Exception e) { + logger.warn("Failed to delete " + artifact.getFileName(), e); + } + }).start(); + } } diff --git a/src/main/java/org/codelibs/fess/app/web/api/ApiResult.java b/src/main/java/org/codelibs/fess/app/web/api/ApiResult.java index f510685c3..dfff8614b 100644 --- a/src/main/java/org/codelibs/fess/app/web/api/ApiResult.java +++ b/src/main/java/org/codelibs/fess/app/web/api/ApiResult.java @@ -347,4 +347,18 @@ public class ApiResult { return this; } } + + public static class ApiPluginResponse extends ApiResponse { + protected List> plugins; + + public ApiPluginResponse plugins(final List> plugins) { + this.plugins = plugins; + return this; + } + + @Override + public ApiResult result() { + return new ApiResult(this); + } + } } diff --git a/src/main/java/org/codelibs/fess/app/web/api/admin/plugin/ApiAdminPluginAction.java b/src/main/java/org/codelibs/fess/app/web/api/admin/plugin/ApiAdminPluginAction.java new file mode 100644 index 000000000..63f176a7f --- /dev/null +++ b/src/main/java/org/codelibs/fess/app/web/api/admin/plugin/ApiAdminPluginAction.java @@ -0,0 +1,58 @@ +/* + * Copyright 2012-2019 CodeLibs Project and the Others. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ +package org.codelibs.fess.app.web.api.admin.plugin; + +import static org.codelibs.fess.app.web.admin.plugin.AdminPluginAction.getAllInstalledArtifacts; +import static org.codelibs.fess.app.web.admin.plugin.AdminPluginAction.getAllAvailableArtifacts; +import static org.codelibs.fess.app.web.admin.plugin.AdminPluginAction.installArtifact; +import static org.codelibs.fess.app.web.admin.plugin.AdminPluginAction.deleteArtifact; + +import org.codelibs.fess.app.web.api.ApiResult; +import org.codelibs.fess.helper.PluginHelper.Artifact; +import org.codelibs.fess.app.web.api.admin.FessApiAdminAction; +import org.lastaflute.web.Execute; +import org.lastaflute.web.response.JsonResponse; + +import java.util.List; +import java.util.Map; + +public class ApiAdminPluginAction extends FessApiAdminAction { + @Execute + public JsonResponse installedplugins() { + final List> list = getAllInstalledArtifacts(); + return asJson(new ApiResult.ApiPluginResponse().plugins(list).status(ApiResult.Status.OK).result()); + } + + @Execute + public JsonResponse availableplugins() { + final List> list = getAllAvailableArtifacts(); + return asJson(new ApiResult.ApiPluginResponse().plugins(list).status(ApiResult.Status.OK).result()); + } + + @Execute + public JsonResponse install(final InstallBody body) { + validateApi(body, messages -> {}); + installArtifact(new Artifact(body.name, body.version, body.url)); + return asJson(new ApiResult.ApiResponse().status(ApiResult.Status.OK).result()); + } + + @Execute + public JsonResponse delete(final DeleteBody body) { + validateApi(body, messages -> {}); + deleteArtifact(new Artifact(body.name, body.version, null)); + return asJson(new ApiResult.ApiResponse().status(ApiResult.Status.OK).result()); + } +} diff --git a/src/main/java/org/codelibs/fess/app/web/api/admin/plugin/DeleteBody.java b/src/main/java/org/codelibs/fess/app/web/api/admin/plugin/DeleteBody.java new file mode 100644 index 000000000..59d3cca47 --- /dev/null +++ b/src/main/java/org/codelibs/fess/app/web/api/admin/plugin/DeleteBody.java @@ -0,0 +1,15 @@ +package org.codelibs.fess.app.web.api.admin.plugin; + +import org.lastaflute.web.validation.Required; + +import javax.validation.constraints.Size; + +public class DeleteBody { + @Required + @Size(max = 100) + public String name; + + @Required + @Size(max = 100) + public String version; +} diff --git a/src/main/java/org/codelibs/fess/app/web/api/admin/plugin/InstallBody.java b/src/main/java/org/codelibs/fess/app/web/api/admin/plugin/InstallBody.java new file mode 100644 index 000000000..bd32edffd --- /dev/null +++ b/src/main/java/org/codelibs/fess/app/web/api/admin/plugin/InstallBody.java @@ -0,0 +1,19 @@ +package org.codelibs.fess.app.web.api.admin.plugin; + +import org.lastaflute.web.validation.Required; + +import javax.validation.constraints.Size; + +public class InstallBody { + @Required + @Size(max = 100) + public String name; + + @Required + @Size(max = 100) + public String version; + + @Required + @Size(max = 200) + public String url; +}