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