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.mylasta.direction;
17  
18  import java.util.List;
19  import java.util.function.Consumer;
20  
21  import javax.annotation.Resource;
22  
23  import org.codelibs.fess.app.web.base.FessAdminAction;
24  import org.codelibs.fess.mylasta.direction.sponsor.FessActionAdjustmentProvider;
25  import org.codelibs.fess.mylasta.direction.sponsor.FessApiFailureHook;
26  import org.codelibs.fess.mylasta.direction.sponsor.FessCookieResourceProvider;
27  import org.codelibs.fess.mylasta.direction.sponsor.FessCurtainBeforeHook;
28  import org.codelibs.fess.mylasta.direction.sponsor.FessCurtainFinallyHook;
29  import org.codelibs.fess.mylasta.direction.sponsor.FessJsonResourceProvider;
30  import org.codelibs.fess.mylasta.direction.sponsor.FessListedClassificationProvider;
31  import org.codelibs.fess.mylasta.direction.sponsor.FessMailDeliveryDepartmentCreator;
32  import org.codelibs.fess.mylasta.direction.sponsor.FessMultipartRequestHandler;
33  import org.codelibs.fess.mylasta.direction.sponsor.FessSecurityResourceProvider;
34  import org.codelibs.fess.mylasta.direction.sponsor.FessTimeResourceProvider;
35  import org.codelibs.fess.mylasta.direction.sponsor.FessUserLocaleProcessProvider;
36  import org.codelibs.fess.mylasta.direction.sponsor.FessUserTimeZoneProcessProvider;
37  import org.lastaflute.core.direction.CachedFwAssistantDirector;
38  import org.lastaflute.core.direction.FwAssistDirection;
39  import org.lastaflute.core.direction.FwCoreDirection;
40  import org.lastaflute.core.security.InvertibleCryptographer;
41  import org.lastaflute.core.security.OneWayCryptographer;
42  import org.lastaflute.db.dbflute.classification.ListedClassificationProvider;
43  import org.lastaflute.db.direction.FwDbDirection;
44  import org.lastaflute.web.direction.FwWebDirection;
45  import org.lastaflute.web.ruts.process.ActionRuntime;
46  import org.lastaflute.web.ruts.renderer.JspHtmlRenderingProvider;
47  
48  /**
49   * @author jflute
50   */
51  public class FessFwAssistantDirector extends CachedFwAssistantDirector {
52  
53      // ===================================================================================
54      //                                                                           Attribute
55      //                                                                           =========
56      @Resource
57      protected FessConfig fessConfig;
58  
59      // ===================================================================================
60      //                                                                              Assist
61      //                                                                              ======
62      @Override
63      protected void prepareAssistDirection(final FwAssistDirection direction) {
64          direction.directConfig(nameList -> nameList.add("fess_config.properties"), "fess_env.properties");
65      }
66  
67      // ===================================================================================
68      //                                                                               Core
69      //                                                                              ======
70      @Override
71      protected void prepareCoreDirection(final FwCoreDirection direction) {
72          // this configuration is on fess_env.properties because this is true only when development
73          direction.directDevelopmentHere(fessConfig.isDevelopmentHere());
74  
75          // titles of the application for logging are from configurations
76          direction.directLoggingTitle(fessConfig.getDomainTitle(), fessConfig.getEnvironmentTitle());
77  
78          // this configuration is on sea_env.properties because it has no influence to production
79          // even if you set true manually and forget to set false back
80          direction.directFrameworkDebug(fessConfig.isFrameworkDebug()); // basically false
81  
82          // you can add your own process when your application is booting or closing
83          direction.directCurtainBefore(createCurtainBeforeHook());
84          direction.directCurtainFinally(createCurtainFinallyHook()); // when destroy
85  
86          direction.directSecurity(createSecurityResourceProvider());
87          direction.directTime(createTimeResourceProvider());
88          direction.directMail(createFessMailDeliveryDepartmentCreator().create());
89          direction.directJson(createJsonResourceProvider());
90      }
91  
92      protected FessJsonResourceProvider createJsonResourceProvider() {
93          return new FessJsonResourceProvider();
94      }
95  
96      protected FessCurtainBeforeHook createCurtainBeforeHook() {
97          return new FessCurtainBeforeHook();
98      }
99  
100     protected FessCurtainFinallyHook createCurtainFinallyHook() {
101         return new FessCurtainFinallyHook();
102     }
103 
104     protected FessSecurityResourceProvider createSecurityResourceProvider() {
105         final InvertibleCryptographer inver;
106         final String cipherAlgorism = fessConfig.getAppCipherAlgorism();
107         if ("blowfish".equalsIgnoreCase(cipherAlgorism)) {
108             inver = InvertibleCryptographer.createBlowfishCipher(fessConfig.getAppCipherKey());
109         } else if ("des".equalsIgnoreCase(cipherAlgorism)) {
110             inver = InvertibleCryptographer.createDesCipher(fessConfig.getAppCipherKey());
111         } else if ("rsa".equalsIgnoreCase(cipherAlgorism)) {
112             inver = InvertibleCryptographer.createRsaCipher(fessConfig.getAppCipherKey());
113         } else {
114             inver = InvertibleCryptographer.createAesCipher(fessConfig.getAppCipherKey());
115         }
116 
117         final OneWayCryptographer oneWay;
118         final String digestAlgorism = fessConfig.getAppDigestAlgorism();
119         if ("sha512".equalsIgnoreCase(digestAlgorism)) {
120             oneWay = OneWayCryptographer.createSha512Cryptographer();
121         } else if ("md5".equalsIgnoreCase(digestAlgorism)) {
122             oneWay = new OneWayCryptographer("MD5", OneWayCryptographer.ENCODING_UTF8);
123         } else {
124             oneWay = OneWayCryptographer.createSha256Cryptographer();
125         }
126 
127         return new FessSecurityResourceProvider(inver, oneWay);
128     }
129 
130     protected FessTimeResourceProvider createTimeResourceProvider() {
131         return new FessTimeResourceProvider(fessConfig);
132     }
133 
134     protected FessMailDeliveryDepartmentCreator createFessMailDeliveryDepartmentCreator() {
135         return new FessMailDeliveryDepartmentCreator(fessConfig);
136     }
137 
138     // ===================================================================================
139     //                                                                                 DB
140     //                                                                                ====
141     @Override
142     protected void prepareDbDirection(final FwDbDirection direction) {
143         direction.directClassification(createListedClassificationProvider());
144     }
145 
146     protected ListedClassificationProvider createListedClassificationProvider() {
147         return new FessListedClassificationProvider();
148     }
149 
150     // ===================================================================================
151     //                                                                                Web
152     //                                                                               =====
153     @Override
154     protected void prepareWebDirection(final FwWebDirection direction) {
155         direction.directRequest(createUserLocaleProcessProvider(), createUserTimeZoneProcessProvider());
156         direction.directCookie(createCookieResourceProvider());
157         direction.directAdjustment(createActionAdjustmentProvider());
158         direction.directMessage(createMessageNameList(), "fess_label");
159         direction.directApiCall(createApiFailureHook());
160         direction.directMultipart(FessMultipartRequestHandler::new);
161         direction.directHtmlRendering(new JspHtmlRenderingProvider() {
162             @Override
163             protected String getShowErrorsForwardPath(final ActionRuntime runtime) {
164                 if (FessAdminAction.class.isAssignableFrom(runtime.getActionType())) {
165                     return "/admin/error/error.jsp";
166                 }
167                 return "/error/system.jsp";
168             }
169         });
170     }
171 
172     protected Consumer<List<String>> createMessageNameList() {
173         return nameList -> nameList.add("fess_message");
174     }
175 
176     protected FessUserLocaleProcessProvider createUserLocaleProcessProvider() {
177         return new FessUserLocaleProcessProvider();
178     }
179 
180     protected FessUserTimeZoneProcessProvider createUserTimeZoneProcessProvider() {
181         return new FessUserTimeZoneProcessProvider();
182     }
183 
184     protected FessCookieResourceProvider createCookieResourceProvider() { // #change_it_first
185         final InvertibleCryptographer cr = InvertibleCryptographer.createAesCipher("*unused@");
186         return new FessCookieResourceProvider(fessConfig, cr);
187     }
188 
189     protected FessActionAdjustmentProvider createActionAdjustmentProvider() {
190         return new FessActionAdjustmentProvider();
191     }
192 
193     protected FessApiFailureHook createApiFailureHook() {
194         return new FessApiFailureHook();
195     }
196 }