This is a cache of https://fess.codelibs.org/xref/org/codelibs/fess/app/web/admin/general/AdminGeneralAction.html. It is a snapshot of the page at 2020-10-25T01:18:26.614+0000.
AdminGeneralAction xref
View Javadoc
1   /*
2    * Copyright 2012-2020 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.admin.general;
17  
18  import static org.codelibs.core.stream.StreamUtil.stream;
19  
20  import java.util.ArrayList;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import javax.annotation.Resource;
26  
27  import org.apache.logging.log4j.LogManager;
28  import org.apache.logging.log4j.Logger;
29  import org.codelibs.core.beans.util.BeanUtil;
30  import org.codelibs.core.lang.StringUtil;
31  import org.codelibs.core.misc.DynamicProperties;
32  import org.codelibs.fess.Constants;
33  import org.codelibs.fess.annotation.Secured;
34  import org.codelibs.fess.app.web.base.FessAdminAction;
35  import org.codelibs.fess.helper.SystemHelper;
36  import org.codelibs.fess.mylasta.direction.FessConfig;
37  import org.codelibs.fess.mylasta.mail.testmailPostcard;
38  import org.codelibs.fess.util.ComponentUtil;
39  import org.lastaflute.core.mail.Postbox;
40  import org.lastaflute.web.Execute;
41  import org.lastaflute.web.response.HtmlResponse;
42  import org.lastaflute.web.ruts.process.ActionRuntime;
43  
44  /**
45   * @author shinsuke
46   * @author Shunji Makino
47   */
48  public class AdminGeneralAction extends FessAdminAction {
49  
50      public static final String ROLE = "admin-general";
51  
52      private static final String DUMMY_PASSWORD = "**********";
53  
54      private static final Logger logger = LogManager.getLogger(AdminGeneralAction.class);
55  
56      // ===================================================================================
57      //                                                                           Attribute
58      //                                                                           =========
59      @Resource
60      protected DynamicProperties systemProperties;
61  
62      // ===================================================================================
63      //                                                                               Hook
64      //                                                                              ======
65      @Override
66      protected void setupHtmlData(final ActionRuntime runtime) {
67          super.setupHtmlData(runtime);
68          runtime.registerData("helpLink", systemHelper.getHelpLink(fessConfig.getOnlineHelpNameGeneral()));
69          runtime.registerData("dayItems", getDayItems());
70      }
71  
72      @Override
73      protected String getActionRole() {
74          return ROLE;
75      }
76  
77      // ===================================================================================
78      //
79  
80      @Execute
81      @Secured({ ROLE, ROLE + VIEW })
82      public HtmlResponse index() {
83          saveToken();
84          return asHtml(path_AdminGeneral_AdminGeneralJsp).useForm(EditForm.class, setup -> {
85              setup.setup(form -> {
86                  updateForm(fessConfig, form);
87              });
88          });
89      }
90  
91      @Execute
92      @Secured({ ROLE })
93      public HtmlResponse sendmail(final MailForm form) {
94          validate(form, messages -> {}, () -> asHtml(path_AdminGeneral_AdminGeneralJsp));
95  
96          final String[] toAddresses = form.notificationTo.split(",");
97          final Map<String, Object> dataMap = new HashMap<>();
98          dataMap.put("hostname", systemHelper.getHostname());
99  
100         final FessConfig fessConfig = ComponentUtil.getFessConfig();
101         final Postbox postbox = ComponentUtil.getComponent(Postbox.class);
102         try {
103             testmailPostcard.droppedInto(postbox, postcard -> {
104                 postcard.setFrom(fessConfig.getMailFromAddress(), fessConfig.getMailFromName());
105                 postcard.addReplyTo(fessConfig.getMailReturnPath());
106                 stream(toAddresses).of(stream -> stream.forEach(address -> {
107                     postcard.addTo(address);
108                 }));
109                 BeanUtil.copyMapToBean(dataMap, postcard);
110             });
111             saveInfo(messages -> messages.addSuccessSendtestmail(GLOBAL));
112             updateProperty(Constants.NOTIFICATION_TO_PROPERTY, form.notificationTo);
113             systemProperties.store();
114         } catch (final Exception e) {
115             logger.warn("Failed to send a test mail.", e);
116             saveError(messages -> messages.addErrorsFailedToSendtestmail(GLOBAL));
117         }
118 
119         return redirectByParam(AdminGeneralAction.class, "notificationTo", form.notificationTo);
120     }
121 
122     @Execute
123     @Secured({ ROLE })
124     public HtmlResponse update(final EditForm form) {
125         validate(form, messages -> {}, () -> asHtml(path_AdminGeneral_AdminGeneralJsp));
126         verifyToken(() -> asHtml(path_AdminGeneral_AdminGeneralJsp));
127 
128         updateConfig(fessConfig, form);
129         saveInfo(messages -> messages.addSuccessUpdateCrawlerParams(GLOBAL));
130         return redirect(getClass());
131     }
132 
133     public static void updateConfig(final FessConfig fessConfig, final EditForm form) {
134         fessConfig.setLoginRequired(isCheckboxEnabled(form.loginRequired));
135         fessConfig.setResultCollapsed(isCheckboxEnabled(form.resultCollapsed));
136         fessConfig.setLoginLinkEnabled(isCheckboxEnabled(form.loginLink));
137         fessConfig.setThumbnailEnabled(isCheckboxEnabled(form.thumbnail));
138         fessConfig.setIncrementalCrawling(isCheckboxEnabled(form.incrementalCrawling));
139         fessConfig.setDayForCleanup(form.dayForCleanup);
140         fessConfig.setCrawlingThreadCount(form.crawlingThreadCount);
141         fessConfig.setSearchLog(isCheckboxEnabled(form.searchLog));
142         fessConfig.setUserInfo(isCheckboxEnabled(form.userInfo));
143         fessConfig.setUserFavorite(isCheckboxEnabled(form.userFavorite));
144         fessConfig.setWebApiJson(isCheckboxEnabled(form.webApiJson));
145         fessConfig.setAppValue(form.appValue);
146         fessConfig.setDefaultLabelValue(form.defaultLabelValue);
147         fessConfig.setDefaultSortValue(form.defaultSortValue);
148         fessConfig.setVirtualHostValue(form.virtualHostValue);
149         fessConfig.setAppendQueryParameter(isCheckboxEnabled(form.appendQueryParameter));
150         fessConfig.setIgnoreFailureType(form.ignoreFailureType);
151         fessConfig.setFailureCountThreshold(form.failureCountThreshold);
152         fessConfig.setWebApiPopularWord(isCheckboxEnabled(form.popularWord));
153         fessConfig.setCsvFileEncoding(form.csvFileEncoding);
154         fessConfig.setPurgeSearchLogDay(form.purgeSearchLogDay);
155         fessConfig.setPurgeJobLogDay(form.purgeJobLogDay);
156         fessConfig.setPurgeUserInfoDay(form.purgeUserInfoDay);
157         fessConfig.setPurgeByBots(form.purgeByBots);
158         fessConfig.setNotificationTo(form.notificationTo);
159         fessConfig.setSuggestSearchLog(isCheckboxEnabled(form.suggestSearchLog));
160         fessConfig.setSuggestDocuments(isCheckboxEnabled(form.suggestDocuments));
161         fessConfig.setPurgeSuggestSearchLogDay(form.purgeSuggestSearchLogDay);
162         fessConfig.setLdapProviderUrl(form.ldapProviderUrl);
163         fessConfig.setLdapSecurityPrincipal(form.ldapSecurityPrincipal);
164         fessConfig.setLdapAdminSecurityPrincipal(form.ldapAdminSecurityPrincipal);
165         if (form.ldapAdminSecurityCredentials != null && StringUtil.isNotBlank(form.ldapAdminSecurityCredentials.replace("*", " "))) {
166             fessConfig.setLdapAdminSecurityCredentials(form.ldapAdminSecurityCredentials);
167         }
168         fessConfig.setLdapBaseDn(form.ldapBaseDn);
169         fessConfig.setLdapAccountFilter(form.ldapAccountFilter);
170         fessConfig.setLdapGroupFilter(form.ldapGroupFilter);
171         fessConfig.setLdapMemberofAttribute(form.ldapMemberofAttribute);
172         fessConfig.setNotificationLogin(form.notificationLogin);
173         fessConfig.setNotificationSearchTop(form.notificationSearchTop);
174         fessConfig.setStorageEndpoint(form.storageEndpoint);
175         if (form.storageAccessKey != null && StringUtil.isNotBlank(form.storageAccessKey.replace("*", " "))) {
176             fessConfig.setStorageAccessKey(form.storageAccessKey);
177         }
178         if (form.storageSecretKey != null && StringUtil.isNotBlank(form.storageSecretKey.replace("*", " "))) {
179             fessConfig.setStorageSecretKey(form.storageSecretKey);
180         }
181         fessConfig.setStorageBucket(form.storageBucket);
182 
183         fessConfig.storeSystemProperties();
184         ComponentUtil.getLdapManager().updateConfig();
185         final SystemHelper systemHelper = ComponentUtil.getSystemHelper();
186         systemHelper.refreshDesignJspFiles();
187         systemHelper.updateSystemProperties();
188 
189         if (StringUtil.isNotBlank(form.logLevel)) {
190             systemHelper.setLogLevel(form.logLevel);
191         }
192     }
193 
194     public static void updateForm(final FessConfig fessConfig, final EditForm form) {
195         form.loginRequired = fessConfig.isLoginRequired() ? Constants.TRUE : Constants.FALSE;
196         form.resultCollapsed = fessConfig.isResultCollapsed() ? Constants.TRUE : Constants.FALSE;
197         form.loginLink = fessConfig.isLoginLinkEnabled() ? Constants.TRUE : Constants.FALSE;
198         form.thumbnail = fessConfig.isThumbnailEnabled() ? Constants.TRUE : Constants.FALSE;
199         form.incrementalCrawling = fessConfig.isIncrementalCrawling() ? Constants.TRUE : Constants.FALSE;
200         form.dayForCleanup = fessConfig.getDayForCleanup();
201         form.crawlingThreadCount = fessConfig.getCrawlingThreadCount();
202         form.searchLog = fessConfig.isSearchLog() ? Constants.TRUE : Constants.FALSE;
203         form.userInfo = fessConfig.isUserInfo() ? Constants.TRUE : Constants.FALSE;
204         form.userFavorite = fessConfig.isUserFavorite() ? Constants.TRUE : Constants.FALSE;
205         form.webApiJson = fessConfig.isWebApiJson() ? Constants.TRUE : Constants.FALSE;
206         form.appValue = fessConfig.getAppValue();
207         form.defaultLabelValue = fessConfig.getDefaultLabelValue();
208         form.defaultSortValue = fessConfig.getDefaultSortValue();
209         form.virtualHostValue = fessConfig.getVirtualHostValue();
210         form.appendQueryParameter = fessConfig.isAppendQueryParameter() ? Constants.TRUE : Constants.FALSE;
211         form.ignoreFailureType = fessConfig.getIgnoreFailureType();
212         form.failureCountThreshold = fessConfig.getFailureCountThreshold();
213         form.popularWord = fessConfig.isWebApiPopularWord() ? Constants.TRUE : Constants.FALSE;
214         form.csvFileEncoding = fessConfig.getCsvFileEncoding();
215         form.purgeSearchLogDay = fessConfig.getPurgeSearchLogDay();
216         form.purgeJobLogDay = fessConfig.getPurgeJobLogDay();
217         form.purgeUserInfoDay = fessConfig.getPurgeUserInfoDay();
218         form.purgeByBots = fessConfig.getPurgeByBots();
219         form.notificationTo = fessConfig.getNotificationTo();
220         form.suggestSearchLog = fessConfig.isSuggestSearchLog() ? Constants.TRUE : Constants.FALSE;
221         form.suggestDocuments = fessConfig.isSuggestDocuments() ? Constants.TRUE : Constants.FALSE;
222         form.purgeSuggestSearchLogDay = fessConfig.getPurgeSuggestSearchLogDay();
223         form.ldapProviderUrl = fessConfig.getLdapProviderUrl();
224         form.ldapSecurityPrincipal = fessConfig.getLdapSecurityPrincipal();
225         form.ldapAdminSecurityPrincipal = fessConfig.getLdapAdminSecurityPrincipal();
226         form.ldapAdminSecurityCredentials =
227                 StringUtil.isNotBlank(fessConfig.getLdapAdminSecurityCredentials()) ? DUMMY_PASSWORD : StringUtil.EMPTY;
228         form.ldapBaseDn = fessConfig.getLdapBaseDn();
229         form.ldapAccountFilter = fessConfig.getLdapAccountFilter();
230         form.ldapGroupFilter = fessConfig.getLdapGroupFilter();
231         form.ldapMemberofAttribute = fessConfig.getLdapMemberofAttribute();
232         form.notificationLogin = fessConfig.getNotificationLogin();
233         form.notificationSearchTop = fessConfig.getNotificationSearchTop();
234         form.storageEndpoint = fessConfig.getStorageEndpoint();
235         form.storageAccessKey = StringUtil.isNotBlank(fessConfig.getStorageAccessKey()) ? DUMMY_PASSWORD : StringUtil.EMPTY;
236         form.storageSecretKey = StringUtil.isNotBlank(fessConfig.getStorageSecretKey()) ? DUMMY_PASSWORD : StringUtil.EMPTY;
237         form.storageBucket = fessConfig.getStorageBucket();
238         form.logLevel = ComponentUtil.getSystemHelper().getLogLevel().toUpperCase();
239     }
240 
241     private void updateProperty(final String key, final String value) {
242         systemProperties.setProperty(key, value == null ? StringUtil.EMPTY : value);
243     }
244 
245     private List<String> getDayItems() {
246         final List<String> items = new ArrayList<>();
247         for (int i = 0; i < 32; i++) {
248             items.add(Integer.toString(i));
249         }
250         for (int i = 40; i < 370; i += 10) {
251             items.add(Integer.toString(i));
252         }
253         items.add(Integer.toString(365));
254         return items;
255     }
256 
257 }