#2213 add ApiAdminPluginAction

This commit is contained in:
igarashi 2019-08-19 15:54:32 +09:00
parent ead2ba74fe
commit 3382a60f50
5 changed files with 146 additions and 32 deletions

View file

@ -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<Map<String, String>> getAllAvailableArtifacts() {
public static List<Map<String, String>> getAllAvailableArtifacts() {
final PluginHelper pluginHelper = ComponentUtil.getComponent(PluginHelper.class);
final List<Map<String, String>> 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<Map<String, String>> getAllInstalledArtifacts() {
public static List<Map<String, String>> getAllInstalledArtifacts() {
final PluginHelper pluginHelper = ComponentUtil.getComponent(PluginHelper.class);
final List<Map<String, String>> 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<String, String> beanToMap(final Artifact artifact) {
public static Map<String, String> beanToMap(final Artifact artifact) {
final Map<String, String> 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();
}
}

View file

@ -347,4 +347,18 @@ public class ApiResult {
return this;
}
}
public static class ApiPluginResponse extends ApiResponse {
protected List<Map<String, String>> plugins;
public ApiPluginResponse plugins(final List<Map<String, String>> plugins) {
this.plugins = plugins;
return this;
}
@Override
public ApiResult result() {
return new ApiResult(this);
}
}
}

View file

@ -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<ApiResult> installedplugins() {
final List<Map<String, String>> list = getAllInstalledArtifacts();
return asJson(new ApiResult.ApiPluginResponse().plugins(list).status(ApiResult.Status.OK).result());
}
@Execute
public JsonResponse<ApiResult> availableplugins() {
final List<Map<String, String>> list = getAllAvailableArtifacts();
return asJson(new ApiResult.ApiPluginResponse().plugins(list).status(ApiResult.Status.OK).result());
}
@Execute
public JsonResponse<ApiResult> 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<ApiResult> 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());
}
}

View file

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

View file

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