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.admin.crawlinginfo;
17  
18  import javax.annotation.Resource;
19  
20  import org.apache.logging.log4j.LogManager;
21  import org.apache.logging.log4j.Logger;
22  import org.codelibs.fess.Constants;
23  import org.codelibs.fess.annotation.Secured;
24  import org.codelibs.fess.app.pager.CrawlingInfoPager;
25  import org.codelibs.fess.app.service.CrawlingInfoService;
26  import org.codelibs.fess.app.web.CrudMode;
27  import org.codelibs.fess.app.web.base.FessAdminAction;
28  import org.codelibs.fess.helper.ProcessHelper;
29  import org.codelibs.fess.util.RenderDataUtil;
30  import org.lastaflute.web.Execute;
31  import org.lastaflute.web.response.HtmlResponse;
32  import org.lastaflute.web.response.render.RenderData;
33  import org.lastaflute.web.ruts.process.ActionRuntime;
34  
35  /**
36   * @author shinsuke
37   * @author Shunji Makino
38   */
39  public class AdminCrawlinginfoAction extends FessAdminAction {
40  
41      public static final String ROLE = "admin-crawlinginfo";
42  
43      private static final Logger logger = LogManager.getLogger(AdminCrawlinginfoAction.class);
44  
45      // ===================================================================================
46      //                                                                           Attribute
47      //                                                                           =========
48      @Resource
49      private CrawlingInfoService crawlingInfoService;
50      @Resource
51      private CrawlingInfoPager crawlingInfoPager;
52      @Resource
53      protected ProcessHelper processHelper;
54  
55      // ===================================================================================
56      //                                                                               Hook
57      //                                                                              ======
58      @Override
59      protected void setupHtmlData(final ActionRuntime runtime) {
60          super.setupHtmlData(runtime);
61          runtime.registerData("helpLink", systemHelper.getHelpLink(fessConfig.getOnlineHelpNameCrawlinginfo()));
62      }
63  
64      @Override
65      protected String getActionRole() {
66          return ROLE;
67      }
68  
69      // ===================================================================================
70      //                                                                      Search Execute
71      //                                                                      ==============
72      @Execute
73      @Secured({ ROLE, ROLE + VIEW })
74      public HtmlResponse index() {
75          saveToken();
76          return asListHtml();
77      }
78  
79      @Execute
80      @Secured({ ROLE, ROLE + VIEW })
81      public HtmlResponse list(final Integer pageNumber, final SearchForm form) {
82          saveToken();
83          crawlingInfoPager.setCurrentPageNumber(pageNumber);
84          return asHtml(path_AdminCrawlinginfo_AdminCrawlinginfoJsp).renderWith(data -> {
85              searchPaging(data, form);
86          });
87      }
88  
89      @Execute
90      @Secured({ ROLE, ROLE + VIEW })
91      public HtmlResponse search(final SearchForm form) {
92          saveToken();
93          copyBeanToBean(form, crawlingInfoPager, op -> op.exclude(Constants.PAGER_CONVERSION_RULE));
94          return asHtml(path_AdminCrawlinginfo_AdminCrawlinginfoJsp).renderWith(data -> {
95              searchPaging(data, form);
96          });
97      }
98  
99      @Execute
100     @Secured({ ROLE, ROLE + VIEW })
101     public HtmlResponse reset(final SearchForm form) {
102         saveToken();
103         crawlingInfoPager.clear();
104         return asHtml(path_AdminCrawlinginfo_AdminCrawlinginfoJsp).renderWith(data -> {
105             searchPaging(data, form);
106         });
107     }
108 
109     @Execute
110     @Secured({ ROLE, ROLE + VIEW })
111     public HtmlResponse back(final SearchForm form) {
112         saveToken();
113         return asHtml(path_AdminCrawlinginfo_AdminCrawlinginfoJsp).renderWith(data -> {
114             searchPaging(data, form);
115         });
116     }
117 
118     protected void searchPaging(final RenderData data, final SearchForm form) {
119         RenderDataUtil.register(data, "crawlingInfoItems", crawlingInfoService.getCrawlingInfoList(crawlingInfoPager)); // page navi
120 
121         // restore from pager
122         copyBeanToBean(crawlingInfoPager, form, op -> op.include("sessionId"));
123     }
124 
125     // ===================================================================================
126     //                                                                        Edit Execute
127     //                                                                        ============
128 
129     // -----------------------------------------------------
130     //                                               Details
131     //                                               -------
132     @Execute
133     @Secured({ ROLE, ROLE + VIEW })
134     public HtmlResponse details(final int crudMode, final String id) {
135         verifyCrudMode(crudMode, CrudMode.DETAILS);
136         saveToken();
137         return crawlingInfoService.getCrawlingInfo(id)
138                 .map(entity -> asHtml(path_AdminCrawlinginfo_AdminCrawlinginfoDetailsJsp).useForm(EditForm.class, op -> {
139                     op.setup(form -> {
140                         copyBeanToBean(entity, form, copyOp -> {
141                             copyOp.excludeNull();
142                         });
143                         form.crudMode = crudMode;
144                     });
145                 }).renderWith(data -> {
146                     RenderDataUtil.register(data, "crawlingInfoParamItems", crawlingInfoService.getCrawlingInfoParamList(id));
147                     RenderDataUtil.register(data, "running", processHelper.isProcessRunning(entity.getSessionId()));
148                 })).orElseGet(() -> {
149                     throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), this::asListHtml);
150                     return null;
151                 });
152     }
153 
154     @Execute
155     @Secured({ ROLE })
156     public HtmlResponse threaddump(final EditForm form) {
157         verifyCrudMode(form.crudMode, CrudMode.DETAILS);
158         validate(form, messages -> {}, this::asDetailsHtml);
159         verifyToken(this::asDetailsHtml);
160         final String id = form.id;
161         crawlingInfoService.getCrawlingInfo(id).ifPresent(entity -> {
162             try {
163                 processHelper.sendCommand(entity.getSessionId(), Constants.CRAWLER_PROCESS_COMMAND_THREAD_DUMP);
164                 saveInfo(messages -> messages.addSuccessPrintThreadDump(GLOBAL));
165             } catch (final Exception e) {
166                 logger.warn("Failed to print a thread dump.", e);
167                 throwValidationError(messages -> messages.addErrorsFailedToPrintThreadDump(GLOBAL), this::asListHtml);
168             }
169         }).orElse(() -> {
170             throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), this::asListHtml);
171         });
172         return redirect(getClass());
173     }
174 
175     // -----------------------------------------------------
176     //                                         Actually Crud
177     //                                         -------------
178     @Execute
179     @Secured({ ROLE })
180     public HtmlResponse delete(final EditForm form) {
181         verifyCrudMode(form.crudMode, CrudMode.DETAILS);
182         validate(form, messages -> {}, this::asDetailsHtml);
183         verifyToken(this::asDetailsHtml);
184         final String id = form.id;
185         crawlingInfoService.getCrawlingInfo(id).alwaysPresent(entity -> {
186             crawlingInfoService.delete(entity);
187             saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
188         });
189         return redirect(getClass());
190     }
191 
192     @Execute
193     @Secured({ ROLE, ROLE + VIEW })
194     public HtmlResponse deleteall() {
195         verifyToken(this::asListHtml);
196         crawlingInfoService.deleteOldSessions(processHelper.getRunningSessionIdSet());
197         crawlingInfoPager.clear();
198         saveInfo(messages -> messages.addSuccessCrawlingInfoDeleteAll(GLOBAL));
199         return redirect(getClass());
200     }
201 
202     // ===================================================================================
203     //                                                                        Assist Logic
204     //                                                                        ============
205 
206     // ===================================================================================
207     //                                                                        Small Helper
208     //                                                                        ============
209     protected void verifyCrudMode(final int crudMode, final int expectedMode) {
210         if (crudMode != expectedMode) {
211             throwValidationError(messages -> {
212                 messages.addErrorsCrudInvalidMode(GLOBAL, String.valueOf(expectedMode), String.valueOf(crudMode));
213             }, this::asListHtml);
214         }
215     }
216 
217     // ===================================================================================
218     //                                                                              JSP
219     //                                                                           =========
220 
221     private HtmlResponse asListHtml() {
222         return asHtml(path_AdminCrawlinginfo_AdminCrawlinginfoJsp).renderWith(data -> {
223             RenderDataUtil.register(data, "crawlingInfoItems", crawlingInfoService.getCrawlingInfoList(crawlingInfoPager)); // page navi
224         }).useForm(SearchForm.class, setup -> {
225             setup.setup(form -> {
226                 copyBeanToBean(crawlingInfoPager, form, op -> op.include("id"));
227             });
228         });
229     }
230 
231     private HtmlResponse asDetailsHtml() {
232         return asHtml(path_AdminCrawlinginfo_AdminCrawlinginfoDetailsJsp);
233     }
234 }