View Javadoc
1   /*
2    * Copyright 2012-2021 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.app.web.api.admin.plugin;
17  
18  import static org.codelibs.fess.app.web.admin.plugin.AdminPluginAction.deleteArtifact;
19  import static org.codelibs.fess.app.web.admin.plugin.AdminPluginAction.getAllAvailableArtifacts;
20  import static org.codelibs.fess.app.web.admin.plugin.AdminPluginAction.getAllInstalledArtifacts;
21  import static org.codelibs.fess.app.web.admin.plugin.AdminPluginAction.installArtifact;
22  
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.codelibs.fess.app.web.api.ApiResult;
27  import org.codelibs.fess.app.web.api.admin.FessApiAdminAction;
28  import org.codelibs.fess.helper.PluginHelper.Artifact;
29  import org.codelibs.fess.util.ComponentUtil;
30  import org.lastaflute.web.Execute;
31  import org.lastaflute.web.response.JsonResponse;
32  
33  public class ApiAdminPluginAction extends FessApiAdminAction {
34  
35      // GET /api/admin/plugin/installed
36      @Execute
37      public JsonResponse<ApiResult> get$installed() {
38          final List<Map<String, String>> list = getAllInstalledArtifacts();
39          return asJson(new ApiResult.ApiPluginResponse().plugins(list).status(ApiResult.Status.OK).result());
40      }
41  
42      // GET /api/admin/plugin/available
43      @Execute
44      public JsonResponse<ApiResult> get$available() {
45          final List<Map<String, String>> list = getAllAvailableArtifacts();
46          return asJson(new ApiResult.ApiPluginResponse().plugins(list).status(ApiResult.Status.OK).result());
47      }
48  
49      // PUT /api/admin/plugin
50      @Execute
51      public JsonResponse<ApiResult> put$index(final InstallBody body) {
52          validateApi(body, messages -> {});
53          final Artifact artifact = ComponentUtil.getPluginHelper().getArtifact(body.name, body.version);
54          if (artifact == null) {
55              return asJson(
56                      new ApiResult.ApiErrorResponse().message("invalid name or version").status(ApiResult.Status.BAD_REQUEST).result());
57          }
58          installArtifact(artifact);
59          return asJson(new ApiResult.ApiResponse().status(ApiResult.Status.OK).result());
60      }
61  
62      // DELETE /api/admin/plugin
63      @Execute
64      public JsonResponse<ApiResult> delete$index(final DeleteBody body) {
65          validateApi(body, messages -> {});
66          deleteArtifact(new Artifact(body.name, body.version));
67          return asJson(new ApiResult.ApiResponse().status(ApiResult.Status.OK).result());
68      }
69  
70  }