1 /*
2 * Copyright 2012-2025 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 /**
31 * Action class for handling cached document content requests.
32 * Provides functionality to retrieve and display cached versions of crawled documents.
33 */
34 public class CacheAction extends FessSearchAction {
35
36 /**
37 * Creates a new instance of CacheAction.
38 */
39 public CacheAction() {
40 super();
41 }
42
43 // ===================================================================================
44 // Constant
45 //
46 private static final Logger logger = LogManager.getLogger(CacheAction.class);
47
48 // ===================================================================================
49 // Attribute
50 //
51
52 // ===================================================================================
53 // Hook
54 // ======
55
56 // ===================================================================================
57 // Search Execute
58 // ==============
59 /**
60 * Retrieves and displays cached content for a specific document.
61 *
62 * @param form the cache form containing document ID and highlight query
63 * @return ActionResponse containing the cached document content or error redirect
64 */
65 @Execute
66 public ActionResponse index(final CacheForm form) {
67 validate(form, messages -> {}, () -> asHtml(virtualHost(path_Error_ErrorJsp)));
68 if (isLoginRequired()) {
69 return redirectToLogin();
70 }
71
72 Map<String, Object> doc = null;
73 try {
74 doc = searchHelper.getDocumentByDocId(form.docId, queryFieldConfig.getCacheResponseFields(), getUserBean()).orElse(null);
75 } catch (final Exception e) {
76 logger.warn("Failed to request: {}", form.docId, e);
77 }
78 if (doc == null) {
79 saveError(messages -> messages.addErrorsDocidNotFound(GLOBAL, form.docId));
80 return redirect(ErrorAction.class);
81 }
82
83 final String content = viewHelper.createCacheContent(doc, form.hq);
84 if (content == null) {
85 saveError(messages -> messages.addErrorsDocidNotFound(GLOBAL, form.docId));
86 return redirect(ErrorAction.class);
87 }
88
89 final StreamResponse response =
90 asStream(DocumentUtil.getValue(doc, fessConfig.getIndexFieldDocId(), String.class)).contentType("text/html; charset=UTF-8")
91 .data(content.getBytes(Constants.CHARSET_UTF_8));
92 response.headerContentDispositionInline(); // TODO will be fixed in lastaflute
93 return response;
94 }
95
96 }