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.base;
17  
18  import javax.annotation.Resource;
19  import javax.servlet.ServletContext;
20  
21  import org.codelibs.core.lang.StringUtil;
22  import org.codelibs.fess.exception.UserRoleLoginException;
23  import org.codelibs.fess.helper.CrawlingConfigHelper;
24  import org.dbflute.optional.OptionalThing;
25  import org.lastaflute.di.util.LdiFileUtil;
26  import org.lastaflute.web.login.LoginManager;
27  import org.lastaflute.web.response.ActionResponse;
28  import org.lastaflute.web.ruts.process.ActionRuntime;
29  import org.lastaflute.web.util.LaServletContextUtil;
30  
31  /**
32   * @author codelibs
33   * @author jflute
34   */
35  public abstract class FessAdminAction extends FessBaseAction {
36  
37      public static final String VIEW = "-view";
38  
39      // ===================================================================================
40      //                                                                           Attribute
41      //                                                                           =========
42      @Resource
43      protected CrawlingConfigHelper crawlingConfigHelper;
44  
45      // ===================================================================================
46      //                                                                        Small Helper
47      //                                                                        ============
48  
49      @Override
50      protected void setupHtmlData(final ActionRuntime runtime) {
51          super.setupHtmlData(runtime);
52          systemHelper.setupAdminHtmlData(this, runtime);
53  
54          final Boolean editable = getUserBean()
55                  .map(user -> user.hasRoles(fessConfig.getAuthenticationAdminRolesAsArray()) || user.hasRole(getActionRole())).orElse(false);
56          runtime.registerData("editable", editable);
57          runtime.registerData("editableClass", editable ? StringUtil.EMPTY : "disabled");
58          runtime.registerData("fesenType", fessConfig.getFesenType());
59          final String forumLink = systemHelper.getForumLink();
60          if (StringUtil.isNotBlank(forumLink)) {
61              runtime.registerData("forumLink", forumLink);
62          }
63      }
64  
65      protected abstract String getActionRole();
66  
67      protected void write(final String path, final byte[] data) {
68          LdiFileUtil.write(path, data);
69      }
70  
71      protected ServletContext getServletContext() {
72          return LaServletContextUtil.getServletContext();
73      }
74  
75      // ===================================================================================
76      //                                                                            Document
77      //                                                                            ========
78      /**
79       * {@inheritDoc} <br>
80       * Application Origin Methods:
81       * <pre>
82       * <span style="font-size: 130%; color: #553000">[Small Helper]</span>
83       * o saveInfo() <span style="color: #3F7E5E">// save messages to session</span>
84       * o write() <span style="color: #3F7E5E">// write text to specified file</span>
85       * o copyBeanToBean() <span style="color: #3F7E5E">// copy bean to bean by BeanUtil</span>
86       * o getServletContext() <span style="color: #3F7E5E">// get servlet context</span>
87       * </pre>
88       */
89      @Override
90      public void document1_CallableSuperMethod() {
91          super.document1_CallableSuperMethod();
92      }
93  
94      // ===================================================================================
95      //                                                                           User Info
96      //                                                                           =========
97      @Override
98      protected OptionalThing<LoginManager> myLoginManager() {
99          return OptionalThing.of(fessLoginAssist);
100     }
101 
102     // ===================================================================================
103     //                                                                               Hook
104     //                                                                              ======
105     @Override
106     public ActionResponse godHandPrologue(final ActionRuntime runtime) {
107         try {
108             return super.godHandPrologue(runtime);
109         } catch (final UserRoleLoginException e) {
110             return redirect(e.getActionClass());
111         }
112     }
113 
114     @Override
115     public ActionResponse hookBefore(final ActionRuntime runtime) {
116         final String requestPath = runtime.getRequestPath();
117         final String executeName = runtime.getExecuteMethod().getName();
118         activityHelper.access(getUserBean(), requestPath, executeName);
119         return super.hookBefore(runtime);
120     }
121 
122     @Override
123     public void hookFinally(final ActionRuntime runtime) {
124         super.hookFinally(runtime);
125     }
126 
127 }