View Javadoc
1   /*
2    * Copyright 2012-2025 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  /**
34   * API action for admin plugin management.
35   */
36  public class ApiAdminPluginAction extends FessApiAdminAction {
37  
38      // ===================================================================================
39      //                                                                         Constructor
40      //                                                                         ===========
41      /**
42       * Default constructor.
43       */
44      public ApiAdminPluginAction() {
45          super();
46      }
47  
48      // ===================================================================================
49      //                                                                      Search Execute
50      //                                                                      ==============
51  
52      /**
53       * Retrieves the list of installed plugins.
54       *
55       * @return JSON response containing installed plugin list
56       */
57      // GET /api/admin/plugin/installed
58      @Execute
59      public JsonResponse<ApiResult> get$installed() {
60          final List<Map<String, String>> list = getAllInstalledArtifacts();
61          return asJson(new ApiResult.ApiPluginResponse().plugins(list).status(ApiResult.Status.OK).result());
62      }
63  
64      /**
65       * Retrieves the list of available plugins for installation.
66       *
67       * @return JSON response containing available plugin list
68       */
69      // GET /api/admin/plugin/available
70      @Execute
71      public JsonResponse<ApiResult> get$available() {
72          final List<Map<String, String>> list = getAllAvailableArtifacts();
73          return asJson(new ApiResult.ApiPluginResponse().plugins(list).status(ApiResult.Status.OK).result());
74      }
75  
76      /**
77       * Installs a plugin with the specified name and version.
78       *
79       * @param body the plugin installation data containing name and version
80       * @return JSON response indicating success or failure
81       */
82      // POST /api/admin/plugin
83      @Execute
84      public JsonResponse<ApiResult> post$index(final InstallBody body) {
85          validateApi(body, messages -> {});
86          final Artifact artifact = ComponentUtil.getPluginHelper().getArtifact(body.name, body.version);
87          if (artifact == null) {
88              return asJson(
89                      new ApiResult.ApiErrorResponse().message("invalid name or version").status(ApiResult.Status.BAD_REQUEST).result());
90          }
91          installArtifact(artifact);
92          return asJson(new ApiResult.ApiResponse().status(ApiResult.Status.OK).result());
93      }
94  
95      /**
96       * Deletes a plugin with the specified name and version.
97       *
98       * @param body the plugin deletion data containing name and version
99       * @return JSON response indicating success or failure
100      */
101     // DELETE /api/admin/plugin
102     @Execute
103     public JsonResponse<ApiResult> delete$index(final DeleteBody body) {
104         validateApi(body, messages -> {});
105         deleteArtifact(new Artifact(body.name, body.version));
106         return asJson(new ApiResult.ApiResponse().status(ApiResult.Status.OK).result());
107     }
108 
109 }