/* * 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.admin.webauth; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; import javax.annotation.Resource; import org.codelibs.fess.Constants; import org.codelibs.fess.app.pager.WebAuthPager; import org.codelibs.fess.app.service.WebAuthenticationService; import org.codelibs.fess.app.service.WebConfigService; import org.codelibs.fess.app.web.CrudMode; import org.codelibs.fess.app.web.base.FessAdminAction; import org.codelibs.fess.es.config.exentity.WebAuthentication; import org.codelibs.fess.es.config.exentity.WebConfig; import org.codelibs.fess.helper.SystemHelper; import org.codelibs.fess.util.ComponentUtil; import org.codelibs.fess.util.RenderDataUtil; import org.dbflute.optional.OptionalEntity; import org.dbflute.optional.OptionalThing; import org.lastaflute.web.Execute; import org.lastaflute.web.response.HtmlResponse; import org.lastaflute.web.response.render.RenderData; import org.lastaflute.web.ruts.process.ActionRuntime; /** * @author shinsuke * @author Shunji Makino */ public class AdminWebauthAction extends FessAdminAction { // =================================================================================== // Attribute // ========= @Resource private WebAuthenticationService webAuthenticationService; @Resource private WebAuthPager webAuthPager; @Resource protected WebConfigService webConfigService; // =================================================================================== // Hook // ====== @Override protected void setupHtmlData(final ActionRuntime runtime) { super.setupHtmlData(runtime); runtime.registerData("helpLink", systemHelper.getHelpLink(fessConfig.getOnlineHelpNameWebauth())); } // =================================================================================== // Search Execute // ============== @Execute public HtmlResponse index(final SearchForm form) { return asListHtml(); } @Execute public HtmlResponse list(final OptionalThing<Integer> pageNumber, final SearchForm form) { pageNumber.ifPresent(num -> { webAuthPager.setCurrentPageNumber(pageNumber.get()); }).orElse(() -> { webAuthPager.setCurrentPageNumber(0); }); return asHtml(path_AdminWebauth_AdminWebauthJsp).renderWith(data -> { searchPaging(data, form); }); } @Execute public HtmlResponse search(final SearchForm form) { copyBeanToBean(form, webAuthPager, op -> op.exclude(Constants.PAGER_CONVERSION_RULE)); return asHtml(path_AdminWebauth_AdminWebauthJsp).renderWith(data -> { searchPaging(data, form); }); } @Execute public HtmlResponse reset(final SearchForm form) { webAuthPager.clear(); return asHtml(path_AdminWebauth_AdminWebauthJsp).renderWith(data -> { searchPaging(data, form); }); } protected void searchPaging(final RenderData data, final SearchForm form) { RenderDataUtil.register(data, "webAuthenticationItems", webAuthenticationService.getWebAuthenticationList(webAuthPager)); // page navi RenderDataUtil.register(data, "displayCreateLink", !webConfigService.getAllWebConfigList(false, false, false, null).isEmpty()); // restore from pager copyBeanToBean(webAuthPager, form, op -> op.include("id")); } // =================================================================================== // Edit Execute // ============ // ----------------------------------------------------- // Entry Page // ---------- @Execute public HtmlResponse createnew() { saveToken(); return asHtml(path_AdminWebauth_AdminWebauthEditJsp).useForm(CreateForm.class, op -> { op.setup(form -> { form.initialize(); form.crudMode = CrudMode.CREATE; }); }).renderWith(data -> { registerProtocolSchemeItems(data); registerWebConfigItems(data); }); } @Execute public HtmlResponse edit(final EditForm form) { validate(form, messages -> {}, () -> asListHtml()); final String id = form.id; webAuthenticationService.getWebAuthentication(id).ifPresent(entity -> { copyBeanToBean(entity, form, op -> {}); }).orElse(() -> { throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), () -> asListHtml()); }); saveToken(); if (form.crudMode.intValue() == CrudMode.EDIT) { // back form.crudMode = CrudMode.DETAILS; return asDetailsHtml(); } else { form.crudMode = CrudMode.EDIT; return asEditHtml(); } } // ----------------------------------------------------- // Details // ------- @Execute public HtmlResponse details(final int crudMode, final String id) { verifyCrudMode(crudMode, CrudMode.DETAILS); saveToken(); return asHtml(path_AdminWebauth_AdminWebauthDetailsJsp).useForm(EditForm.class, op -> { op.setup(form -> { webAuthenticationService.getWebAuthentication(id).ifPresent(entity -> { copyBeanToBean(entity, form, copyOp -> { copyOp.excludeNull(); }); form.crudMode = crudMode; }).orElse(() -> { throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), () -> asListHtml()); }); }); }).renderWith(data -> { registerProtocolSchemeItems(data); registerWebConfigItems(data); }); } // ----------------------------------------------------- // Actually Crud // ------------- @Execute public HtmlResponse create(final CreateForm form) { verifyCrudMode(form.crudMode, CrudMode.CREATE); validate(form, messages -> {}, () -> asEditHtml()); verifyToken(() -> asEditHtml()); getWebAuthentication(form).ifPresent( entity -> { try { webAuthenticationService.store(entity); saveInfo(messages -> messages.addSuccessCrudCreateCrudTable(GLOBAL)); } catch (final Exception e) { throwValidationError(messages -> messages.addErrorsCrudFailedToCreateCrudTable(GLOBAL, buildThrowableMessage(e)), () -> asEditHtml()); } }).orElse(() -> { throwValidationError(messages -> messages.addErrorsCrudFailedToCreateInstance(GLOBAL), () -> asEditHtml()); }); return redirect(getClass()); } @Execute public HtmlResponse update(final EditForm form) { verifyCrudMode(form.crudMode, CrudMode.EDIT); validate(form, messages -> {}, () -> asEditHtml()); verifyToken(() -> asEditHtml()); getWebAuthentication(form).ifPresent( entity -> { try { webAuthenticationService.store(entity); saveInfo(messages -> messages.addSuccessCrudUpdateCrudTable(GLOBAL)); } catch (final Exception e) { throwValidationError(messages -> messages.addErrorsCrudFailedToUpdateCrudTable(GLOBAL, buildThrowableMessage(e)), () -> asEditHtml()); } }).orElse(() -> { throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, form.id), () -> asEditHtml()); }); return redirect(getClass()); } @Execute public HtmlResponse delete(final EditForm form) { verifyCrudMode(form.crudMode, CrudMode.DETAILS); validate(form, messages -> {}, () -> asDetailsHtml()); verifyToken(() -> asDetailsHtml()); final String id = form.id; webAuthenticationService .getWebAuthentication(id) .ifPresent( entity -> { try { webAuthenticationService.delete(entity); saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL)); } catch (final Exception e) { throwValidationError( messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)), () -> asEditHtml()); } }).orElse(() -> { throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), () -> asDetailsHtml()); }); return redirect(getClass()); } //=================================================================================== // Assist Logic // ============ public static OptionalEntity<WebAuthentication> getEntity(final CreateForm form, final String username, final long currentTime) { switch (form.crudMode) { case CrudMode.CREATE: return OptionalEntity.of(new WebAuthentication()).map(entity -> { entity.setCreatedBy(username); entity.setCreatedTime(currentTime); return entity; }); case CrudMode.EDIT: if (form instanceof EditForm) { return ComponentUtil.getComponent(WebAuthenticationService.class).getWebAuthentication(((EditForm) form).id); } break; default: break; } return OptionalEntity.empty(); } public static OptionalEntity<WebAuthentication> getWebAuthentication(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 -> { entity.setUpdatedBy(username); entity.setUpdatedTime(currentTime); copyBeanToBean(form, entity, op -> op.exclude(Constants.COMMON_CONVERSION_RULE)); return entity; }); } protected void registerProtocolSchemeItems(final RenderData data) { final List<Map<String, String>> itemList = new ArrayList<>(); final Locale locale = ComponentUtil.getRequestManager().getUserLocale(); itemList.add(createItem(ComponentUtil.getMessageManager().getMessage(locale, "labels.webauth_scheme_basic"), Constants.BASIC)); itemList.add(createItem(ComponentUtil.getMessageManager().getMessage(locale, "labels.webauth_scheme_digest"), Constants.DIGEST)); itemList.add(createItem(ComponentUtil.getMessageManager().getMessage(locale, "labels.webauth_scheme_ntlm"), Constants.NTLM)); itemList.add(createItem(ComponentUtil.getMessageManager().getMessage(locale, "labels.webauth_scheme_form"), Constants.FORM)); RenderDataUtil.register(data, "protocolSchemeItems", itemList); } protected void registerWebConfigItems(final RenderData data) { final List<Map<String, String>> itemList = new ArrayList<>(); final List<WebConfig> webConfigList = webConfigService.getAllWebConfigList(false, false, false, null); for (final WebConfig webConfig : webConfigList) { itemList.add(createItem(webConfig.getName(), webConfig.getId().toString())); } RenderDataUtil.register(data, "webConfigItems", itemList); } protected Map<String, String> createItem(final String label, final String value) { final Map<String, String> map = new HashMap<>(2); map.put(Constants.ITEM_LABEL, label); map.put(Constants.ITEM_VALUE, value); return map; } // =================================================================================== // Small Helper // ============ protected void verifyCrudMode(final int crudMode, final int expectedMode) { if (crudMode != expectedMode) { throwValidationError(messages -> { messages.addErrorsCrudInvalidMode(GLOBAL, String.valueOf(expectedMode), String.valueOf(crudMode)); }, () -> asListHtml()); } } // =================================================================================== // JSP // ========= private HtmlResponse asListHtml() { return asHtml(path_AdminWebauth_AdminWebauthJsp).renderWith(data -> { RenderDataUtil.register(data, "webAuthenticationItems", webAuthenticationService.getWebAuthenticationList(webAuthPager)); // page navi RenderDataUtil.register(data, "displayCreateLink", !webConfigService.getAllWebConfigList(false, false, false, null) .isEmpty()); }).useForm(SearchForm.class, setup -> { setup.setup(form -> { copyBeanToBean(webAuthPager, form, op -> op.include("id")); }); }); } private HtmlResponse asEditHtml() { return asHtml(path_AdminWebauth_AdminWebauthEditJsp).renderWith(data -> { registerProtocolSchemeItems(data); registerWebConfigItems(data); }); } private HtmlResponse asDetailsHtml() { return asHtml(path_AdminWebauth_AdminWebauthDetailsJsp).renderWith(data -> { registerProtocolSchemeItems(data); registerWebConfigItems(data); }); } }