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.setAppendQueryParameter(Constants.ON.equalsIgnoreCase(form.appendQueryParameter));
142         fessConfig.setIgnoreFailureType(form.ignoreFailureType);
143         fessConfig.setFailureCountThreshold(form.failureCountThreshold);
144         fessConfig.setWebApiPopularWord(Constants.ON.equalsIgnoreCase(form.popularWord));
145         fessConfig.setCsvFileEncoding(form.csvFileEncoding);
146         fessConfig.setPurgeSearchLogDay(form.purgeSearchLogDay);
147         fessConfig.setPurgeJobLogDay(form.purgeJobLogDay);
148         fessConfig.setPurgeUserInfoDay(form.purgeUserInfoDay);
149         fessConfig.setPurgeByBots(form.purgeByBots);
150         fessConfig.setNotificationTo(form.notificationTo);
151         fessConfig.setSuggestSearchLog(Constants.ON.equalsIgnoreCase(form.suggestSearchLog));
152         fessConfig.setSuggestDocuments(Constants.ON.equalsIgnoreCase(form.suggestDocuments));
153         fessConfig.setPurgeSuggestSearchLogDay(form.purgeSuggestSearchLogDay);
154         fessConfig.setLdapProviderUrl(form.ldapProviderUrl);
155         fessConfig.setLdapSecurityPrincipal(form.ldapSecurityPrincipal);
156         fessConfig.setLdapAdminSecurityPrincipal(form.ldapAdminSecurityPrincipal);
157         if (form.ldapAdminSecurityCredentials != null && StringUtil.isNotBlank(form.ldapAdminSecurityCredentials.replace("*", " "))) {
158             fessConfig.setLdapAdminSecurityCredentials(form.ldapAdminSecurityCredentials);
159         }
160         fessConfig.setLdapBaseDn(form.ldapBaseDn);
161         fessConfig.setLdapAccountFilter(form.ldapAccountFilter);
162         fessConfig.setLdapMemberofAttribute(form.ldapMemberofAttribute);
163         fessConfig.setNotificationLogin(form.notificationLogin);
164         fessConfig.setNotificationSearchTop(form.notificationSearchTop);
165 
166         fessConfig.storeSystemProperties();
167         ComponentUtil.getLdapManager().updateConfig();
168     }
169 
170     public static void updateForm(final FessConfig fessConfig, final EditForm form) {
171         form.loginRequired = fessConfig.isLoginRequired() ? Constants.TRUE : Constants.FALSE;
172         form.resultCollapsed = fessConfig.isResultCollapsed() ? Constants.TRUE : Constants.FALSE;
173         form.loginLink = fessConfig.isLoginLinkEnabled() ? Constants.TRUE : Constants.FALSE;
174         form.thumbnail = fessConfig.isThumbnailEnabled() ? Constants.TRUE : Constants.FALSE;
175         form.incrementalCrawling = fessConfig.isIncrementalCrawling() ? Constants.TRUE : Constants.FALSE;
176         form.dayForCleanup = fessConfig.getDayForCleanup();
177         form.crawlingThreadCount = fessConfig.getCrawlingThreadCount();
178         form.searchLog = fessConfig.isSearchLog() ? Constants.TRUE : Constants.FALSE;
179         form.userInfo = fessConfig.isUserInfo() ? Constants.TRUE : Constants.FALSE;
180         form.userFavorite = fessConfig.isUserFavorite() ? Constants.TRUE : Constants.FALSE;
181         form.webApiJson = fessConfig.isWebApiJson() ? Constants.TRUE : Constants.FALSE;
182         form.defaultLabelValue = fessConfig.getDefaultLabelValue();
183         form.defaultSortValue = fessConfig.getDefaultSortValue();
184         form.appendQueryParameter = fessConfig.isAppendQueryParameter() ? Constants.TRUE : Constants.FALSE;
185         form.ignoreFailureType = fessConfig.getIgnoreFailureType();
186         form.failureCountThreshold = fessConfig.getFailureCountThreshold();
187         form.popularWord = fessConfig.isWebApiPopularWord() ? Constants.TRUE : Constants.FALSE;
188         form.csvFileEncoding = fessConfig.getCsvFileEncoding();
189         form.purgeSearchLogDay = fessConfig.getPurgeSearchLogDay();
190         form.purgeJobLogDay = fessConfig.getPurgeJobLogDay();
191         form.purgeUserInfoDay = fessConfig.getPurgeUserInfoDay();
192         form.purgeByBots = fessConfig.getPurgeByBots();
193         form.notificationTo = fessConfig.getNotificationTo();
194         form.suggestSearchLog = fessConfig.isSuggestSearchLog() ? Constants.TRUE : Constants.FALSE;
195         form.suggestDocuments = fessConfig.isSuggestDocuments() ? Constants.TRUE : Constants.FALSE;
196         form.purgeSuggestSearchLogDay = fessConfig.getPurgeSuggestSearchLogDay();
197         form.ldapProviderUrl = fessConfig.getLdapProviderUrl();
198         form.ldapSecurityPrincipal = fessConfig.getLdapSecurityPrincipal();
199         form.ldapAdminSecurityPrincipal = fessConfig.getLdapAdminSecurityPrincipal();
200         form.ldapAdminSecurityCredentials = DUMMY_PASSWORD;//fessConfig.getLdapAdminSecurityCredentials();
201         form.ldapBaseDn = fessConfig.getLdapBaseDn();
202         form.ldapAccountFilter = fessConfig.getLdapAccountFilter();
203         form.ldapMemberofAttribute = fessConfig.getLdapMemberofAttribute();
204         form.notificationLogin = fessConfig.getNotificationLogin();
205         form.notificationSearchTop = fessConfig.getNotificationSearchTop();
206     }
207 
208     private void updateProperty(final String key, final String value) {
209         systemProperties.setProperty(key, value == null ? StringUtil.EMPTY : value);
210     }
211 
212     private List<String> getDayItems() {
213         final List<String> items = new ArrayList<>();
214         for (int i = 0; i < 32; i++) {
215             items.add(Integer.toString(i));
216         }
217         for (int i = 40; i < 370; i += 10) {
218             items.add(Integer.toString(i));
219         }
220         items.add(Integer.toString(365));
221         return items;
222     }
223 
224 }