瀏覽代碼

add #972 Admin API: /api/admin/fileauth

Keiichi Watanabe 8 年之前
父節點
當前提交
afea01fcdb

+ 5 - 3
src/main/java/org/codelibs/fess/app/web/admin/fileauth/AdminFileauthAction.java

@@ -31,6 +31,7 @@ import org.codelibs.fess.app.web.CrudMode;
 import org.codelibs.fess.app.web.base.FessAdminAction;
 import org.codelibs.fess.es.config.exentity.FileAuthentication;
 import org.codelibs.fess.es.config.exentity.FileConfig;
+import org.codelibs.fess.helper.SystemHelper;
 import org.codelibs.fess.util.ComponentUtil;
 import org.codelibs.fess.util.RenderDataUtil;
 import org.dbflute.optional.OptionalEntity;
@@ -241,7 +242,7 @@ public class AdminFileauthAction extends FessAdminAction {
     //===================================================================================
     //                                                                        Assist Logic
     //                                                                        ============
-    private OptionalEntity<FileAuthentication> getEntity(final CreateForm form, final String username, final long currentTime) {
+    static public OptionalEntity<FileAuthentication> getEntity(final CreateForm form, final String username, final long currentTime) {
         switch (form.crudMode) {
         case CrudMode.CREATE:
             return OptionalEntity.of(new FileAuthentication()).map(entity -> {
@@ -251,7 +252,7 @@ public class AdminFileauthAction extends FessAdminAction {
             });
         case CrudMode.EDIT:
             if (form instanceof EditForm) {
-                return fileAuthenticationService.getFileAuthentication(((EditForm) form).id);
+                return ComponentUtil.getComponent(FileAuthenticationService.class).getFileAuthentication(((EditForm) form).id);
             }
             break;
         default:
@@ -260,7 +261,8 @@ public class AdminFileauthAction extends FessAdminAction {
         return OptionalEntity.empty();
     }
 
-    protected OptionalEntity<FileAuthentication> getFileAuthentication(final CreateForm form) {
+    static public OptionalEntity<FileAuthentication> getFileAuthentication(final CreateForm form) {
+        final SystemHelper systemHelper = ComponentUtil.getSystemHelper();
         final String username = systemHelper.getUsername();
         final long currentTime = systemHelper.getCurrentTimeAsLong();
         return getEntity(form, username, currentTime).map(entity -> {

+ 151 - 0
src/main/java/org/codelibs/fess/app/web/api/admin/fileauth/ApiAdminFileauthAction.java

@@ -0,0 +1,151 @@
+/*
+ * Copyright 2012-2017 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.fileauth;
+
+import static org.codelibs.fess.app.web.admin.fileauth.AdminFileauthAction.getFileAuthentication;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+import javax.annotation.Resource;
+
+import org.codelibs.fess.Constants;
+import org.codelibs.fess.app.pager.FileAuthPager;
+import org.codelibs.fess.app.service.FileAuthenticationService;
+import org.codelibs.fess.app.service.FileConfigService;
+import org.codelibs.fess.app.web.CrudMode;
+import org.codelibs.fess.app.web.api.ApiResult;
+import org.codelibs.fess.app.web.api.ApiResult.ApiConfigResponse;
+import org.codelibs.fess.app.web.api.ApiResult.ApiErrorResponse;
+import org.codelibs.fess.app.web.api.ApiResult.ApiResponse;
+import org.codelibs.fess.app.web.api.ApiResult.ApiUpdateResponse;
+import org.codelibs.fess.app.web.api.ApiResult.Status;
+import org.codelibs.fess.app.web.api.admin.FessApiAdminAction;
+import org.codelibs.fess.es.config.exentity.FileAuthentication;
+import org.lastaflute.web.Execute;
+import org.lastaflute.web.response.JsonResponse;
+
+/**
+ * @author Keiichi Watanabe
+ */
+public class ApiAdminFileauthAction extends FessApiAdminAction {
+
+    // ===================================================================================
+    //                                                                           Attribute
+    //                                                                           =========
+    @Resource
+    private FileAuthenticationService fileAuthService;
+    @Resource
+    private FileConfigService fileConfigService;
+
+    // ===================================================================================
+    //                                                                      Search Execute
+    //                                                                      ==============
+
+    // GET /api/admin/fileauth/settings
+    // POST /api/admin/fileauth/settings
+    @Execute
+    public JsonResponse<ApiResult> settings(final SearchBody body) {
+        validateApi(body, messages -> {});
+        final FileAuthPager pager = new FileAuthPager();
+        copyBeanToBean(body, pager, op -> op.exclude(Constants.PAGER_CONVERSION_RULE));
+        final List<FileAuthentication> list = fileAuthService.getFileAuthenticationList(pager);
+        return asJson(new ApiResult.ApiConfigsResponse<EditBody>()
+                .settings(list.stream().map(entity -> createEditBody(entity)).collect(Collectors.toList()))
+                .total(pager.getAllRecordCount()).status(ApiResult.Status.OK).result());
+    }
+
+    // GET /api/admin/fileauth/setting/{id}
+    @Execute
+    public JsonResponse<ApiResult> get$setting(final String id) {
+        return asJson(new ApiConfigResponse()
+                .setting(fileAuthService.getFileAuthentication(id).map(entity -> createEditBody(entity)).orElseGet(() -> {
+                    throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
+                    return null;
+                })).status(Status.OK).result());
+    }
+
+    // PUT /api/admin/fileauth/setting
+    @Execute
+    public JsonResponse<ApiResult> put$setting(final CreateBody body) {
+        validateApi(body, messages -> {});
+        if (!isValidFileConfigId(body.fileConfigId)) {
+            return asJson(new ApiErrorResponse().message("invalid fileConfigId").status(Status.BAD_REQUEST).result());
+        }
+
+        body.crudMode = CrudMode.CREATE;
+        final FileAuthentication fileAuth = getFileAuthentication(body).map(entity -> {
+            try {
+                fileAuthService.store(entity);
+            } catch (final Exception e) {
+                throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateCrudTable(GLOBAL, buildThrowableMessage(e)));
+            }
+            return entity;
+        }).orElseGet(() -> {
+            throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateInstance(GLOBAL));
+            return null;
+        });
+
+        return asJson(new ApiUpdateResponse().id(fileAuth.getId()).created(true).status(Status.OK).result());
+    }
+
+    // POST /api/admin/fileauth/setting
+    @Execute
+    public JsonResponse<ApiResult> post$setting(final EditBody body) {
+        validateApi(body, messages -> {});
+        body.crudMode = CrudMode.EDIT;
+        final FileAuthentication fileAuth = getFileAuthentication(body).map(entity -> {
+            try {
+                fileAuthService.store(entity);
+            } catch (final Exception e) {
+                throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToUpdateCrudTable(GLOBAL, buildThrowableMessage(e)));
+            }
+            return entity;
+        }).orElseGet(() -> {
+            throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, body.id));
+            return null;
+        });
+        return asJson(new ApiUpdateResponse().id(fileAuth.getId()).created(false).status(Status.OK).result());
+    }
+
+    // DELETE /api/admin/fileauth/setting/{id}
+    @Execute
+    public JsonResponse<ApiResult> delete$setting(final String id) {
+        fileAuthService.getFileAuthentication(id).ifPresent(entity -> {
+            try {
+                fileAuthService.delete(entity);
+                saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
+            } catch (final Exception e) {
+                throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)));
+            }
+        }).orElse(() -> {
+            throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
+        });
+        return asJson(new ApiResponse().status(Status.OK).result());
+    }
+
+    protected EditBody createEditBody(final FileAuthentication entity) {
+        final EditBody body = new EditBody();
+        copyBeanToBean(entity, body, copyOp -> {
+            copyOp.excludeNull();
+        });
+        return body;
+    }
+
+    protected Boolean isValidFileConfigId(String fileconfigId) {
+        return fileConfigService.getFileConfig(fileconfigId).isPresent();
+    }
+}

+ 22 - 0
src/main/java/org/codelibs/fess/app/web/api/admin/fileauth/CreateBody.java

@@ -0,0 +1,22 @@
+/*
+ * Copyright 2012-2017 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.fileauth;
+
+import org.codelibs.fess.app.web.admin.fileauth.CreateForm;
+
+public class CreateBody extends CreateForm {
+
+}

+ 22 - 0
src/main/java/org/codelibs/fess/app/web/api/admin/fileauth/EditBody.java

@@ -0,0 +1,22 @@
+/*
+ * Copyright 2012-2017 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.fileauth;
+
+import org.codelibs.fess.app.web.admin.fileauth.EditForm;
+
+public class EditBody extends EditForm {
+
+}

+ 22 - 0
src/main/java/org/codelibs/fess/app/web/api/admin/fileauth/SearchBody.java

@@ -0,0 +1,22 @@
+/*
+ * Copyright 2012-2017 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.fileauth;
+
+import org.codelibs.fess.app.web.admin.fileauth.SearchForm;
+
+public class SearchBody extends SearchForm {
+
+}