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.cache;
17  
18  import java.util.Map;
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.app.web.base.FessSearchAction;
24  import org.codelibs.fess.app.web.error.ErrorAction;
25  import org.codelibs.fess.util.DocumentUtil;
26  import org.lastaflute.web.Execute;
27  import org.lastaflute.web.response.ActionResponse;
28  import org.lastaflute.web.response.StreamResponse;
29  
30  public class CacheAction extends FessSearchAction {
31  
32      // ===================================================================================
33      //                                                                            Constant
34      //
35      private static final Logger logger = LogManager.getLogger(CacheAction.class);
36  
37      // ===================================================================================
38      //                                                                           Attribute
39      //
40  
41      // ===================================================================================
42      //                                                                               Hook
43      //                                                                              ======
44  
45      // ===================================================================================
46      //                                                                      Search Execute
47      //                                                                      ==============
48      @Execute
49      public ActionResponse index(final CacheForm form) {
50          validate(form, messages -> {}, () -> asHtml(virtualHost(path_Error_ErrorJsp)));
51          if (isLoginRequired()) {
52              return redirectToLogin();
53          }
54  
55          Map<String, Object> doc = null;
56          try {
57              doc = searchHelper.getDocumentByDocId(form.docId, queryHelper.getCacheResponseFields(), getUserBean()).orElse(null);
58          } catch (final Exception e) {
59              logger.warn("Failed to request: " + form.docId, e);
60          }
61          if (doc == null) {
62              saveError(messages -> messages.addErrorsDocidNotFound(GLOBAL, form.docId));
63              return redirect(ErrorAction.class);
64          }
65  
66          final String content = viewHelper.createCacheContent(doc, form.hq);
67          if (content == null) {
68              saveError(messages -> messages.addErrorsDocidNotFound(GLOBAL, form.docId));
69              return redirect(ErrorAction.class);
70          }
71  
72          final StreamResponse response =
73                  asStream(DocumentUtil.getValue(doc, fessConfig.getIndexFieldDocId(), String.class)).contentType("text/html; charset=UTF-8")
74                          .data(content.getBytes(Constants.CHARSET_UTF_8));
75          response.headerContentDispositionInline(); // TODO will be fixed in lastaflute
76          return response;
77      }
78  
79  }