Browse Source

fix #484 : display cached page on browser

Shinsuke Sugaya 9 years ago
parent
commit
d6dd5f92ee

+ 6 - 2
src/main/java/org/codelibs/fess/app/web/cache/CacheAction.java

@@ -25,6 +25,7 @@ import org.elasticsearch.index.query.QueryBuilders;
 import org.elasticsearch.index.query.TermQueryBuilder;
 import org.elasticsearch.index.query.TermQueryBuilder;
 import org.lastaflute.web.Execute;
 import org.lastaflute.web.Execute;
 import org.lastaflute.web.response.ActionResponse;
 import org.lastaflute.web.response.ActionResponse;
+import org.lastaflute.web.response.StreamResponse;
 import org.slf4j.Logger;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.slf4j.LoggerFactory;
 
 
@@ -78,8 +79,11 @@ public class CacheAction extends FessSearchAction {
             return redirect(ErrorAction.class);
             return redirect(ErrorAction.class);
         }
         }
 
 
-        return asStream(DocumentUtil.getValue(doc, fessConfig.getIndexFieldDocId(), String.class)).contentType("text/html; charset=UTF-8")
-                .data(content.getBytes(Constants.CHARSET_UTF_8));
+        StreamResponse response =
+                asStream(DocumentUtil.getValue(doc, fessConfig.getIndexFieldDocId(), String.class)).contentType("text/html; charset=UTF-8")
+                        .data(content.getBytes(Constants.CHARSET_UTF_8));
+        response.headerContentDispositionInline(); // TODO will be fixed in lastaflute
+        return response;
     }
     }
 
 
 }
 }

+ 18 - 19
src/main/java/org/codelibs/fess/helper/ViewHelper.java

@@ -78,6 +78,12 @@ import com.ibm.icu.text.SimpleDateFormat;
 
 
 public class ViewHelper implements Serializable {
 public class ViewHelper implements Serializable {
 
 
+    private static final String HL_CACHE = "hl_cache";
+
+    private static final String QUERIES = "queries";
+
+    private static final String CACHE_MSG = "cache_msg";
+
     private static final Pattern LOCAL_PATH_PATTERN = Pattern.compile("^file:/+[a-zA-Z]:");
     private static final Pattern LOCAL_PATH_PATTERN = Pattern.compile("^file:/+[a-zA-Z]:");
 
 
     private static final Pattern SHARED_FOLDER_PATTERN = Pattern.compile("^file:/+[^/]\\.");
     private static final Pattern SHARED_FOLDER_PATTERN = Pattern.compile("^file:/+[^/]\\.");
@@ -141,22 +147,14 @@ public class ViewHelper implements Serializable {
         }
         }
     }
     }
 
 
-    private String getString(final Map<String, Object> doc, final String key) {
-        final Object value = doc.get(key);
-        if (value == null) {
-            return null;
-        }
-        return value.toString();
-    }
-
     public String getContentTitle(final Map<String, Object> document) {
     public String getContentTitle(final Map<String, Object> document) {
         final int size = titleLength;
         final int size = titleLength;
         final FessConfig fessConfig = ComponentUtil.getFessConfig();
         final FessConfig fessConfig = ComponentUtil.getFessConfig();
         String title;
         String title;
-        if (StringUtil.isNotBlank(getString(document, fessConfig.getIndexFieldTitle()))) {
-            title = getString(document, fessConfig.getIndexFieldTitle());
+        if (StringUtil.isNotBlank(DocumentUtil.getValue(document, fessConfig.getIndexFieldTitle(), String.class))) {
+            title = DocumentUtil.getValue(document, fessConfig.getIndexFieldTitle(), String.class);
         } else {
         } else {
-            title = getString(document, fessConfig.getIndexFieldUrl());
+            title = DocumentUtil.getValue(document, fessConfig.getIndexFieldUrl(), String.class);
         }
         }
         return StringUtils.abbreviate(title, size);
         return StringUtils.abbreviate(title, size);
     }
     }
@@ -173,7 +171,7 @@ public class ViewHelper implements Serializable {
         final int size = descriptionLength;
         final int size = descriptionLength;
 
 
         for (final String field : highlightedFields) {
         for (final String field : highlightedFields) {
-            final String text = getString(document, field);
+            final String text = DocumentUtil.getValue(document, field, String.class);
             if (StringUtil.isNotBlank(text)) {
             if (StringUtil.isNotBlank(text)) {
                 if (useHighlight) {
                 if (useHighlight) {
                     return escapeHighlight(text);
                     return escapeHighlight(text);
@@ -211,7 +209,7 @@ public class ViewHelper implements Serializable {
 
 
     public String getUrlLink(final Map<String, Object> document) {
     public String getUrlLink(final Map<String, Object> document) {
         // file protocol
         // file protocol
-        String url = getString(document, "url");
+        String url = DocumentUtil.getValue(document, "url", String.class);
 
 
         if (url == null) {
         if (url == null) {
             // TODO should redirect to a invalid page?
             // TODO should redirect to a invalid page?
@@ -310,7 +308,7 @@ public class ViewHelper implements Serializable {
     protected String appendQueryParameter(final Map<String, Object> document, final String url) {
     protected String appendQueryParameter(final Map<String, Object> document, final String url) {
         if (Constants.TRUE.equals(systemProperties.get(Constants.APPEND_QUERY_PARAMETER_PROPERTY))) {
         if (Constants.TRUE.equals(systemProperties.get(Constants.APPEND_QUERY_PARAMETER_PROPERTY))) {
             final FessConfig fessConfig = ComponentUtil.getFessConfig();
             final FessConfig fessConfig = ComponentUtil.getFessConfig();
-            final String mimetype = getString(document, fessConfig.getIndexFieldMimetype());
+            final String mimetype = DocumentUtil.getValue(document, fessConfig.getIndexFieldMimetype(), String.class);
             if (StringUtil.isNotBlank(mimetype)) {
             if (StringUtil.isNotBlank(mimetype)) {
                 if ("application/pdf".equals(mimetype)) {
                 if ("application/pdf".equals(mimetype)) {
                     return appendPDFSearchWord(url);
                     return appendPDFSearchWord(url);
@@ -406,6 +404,7 @@ public class ViewHelper implements Serializable {
         if (url == null) {
         if (url == null) {
             url = ComponentUtil.getMessageManager().getMessage(locale, "labels.search_unknown");
             url = ComponentUtil.getMessageManager().getMessage(locale, "labels.search_unknown");
         }
         }
+        doc.put(fessConfig.getResponseFieldUrlLink(), getUrlLink(doc));
         String createdStr;
         String createdStr;
         final Date created = DocumentUtil.getValue(doc, fessConfig.getIndexFieldCreated(), Date.class);
         final Date created = DocumentUtil.getValue(doc, fessConfig.getIndexFieldCreated(), Date.class);
         if (created != null) {
         if (created != null) {
@@ -414,10 +413,10 @@ public class ViewHelper implements Serializable {
         } else {
         } else {
             createdStr = ComponentUtil.getMessageManager().getMessage(locale, "labels.search_unknown");
             createdStr = ComponentUtil.getMessageManager().getMessage(locale, "labels.search_unknown");
         }
         }
-        doc.put("cacheMsg",
+        doc.put(CACHE_MSG,
                 ComponentUtil.getMessageManager().getMessage(locale, "labels.search_cache_msg", new Object[] { url, createdStr }));
                 ComponentUtil.getMessageManager().getMessage(locale, "labels.search_cache_msg", new Object[] { url, createdStr }));
 
 
-        doc.put("queries", queries);
+        doc.put(QUERIES, queries);
 
 
         String cache = DocumentUtil.getValue(doc, fessConfig.getIndexFieldCache(), String.class);
         String cache = DocumentUtil.getValue(doc, fessConfig.getIndexFieldCache(), String.class);
         if (cache != null) {
         if (cache != null) {
@@ -427,13 +426,13 @@ public class ViewHelper implements Serializable {
             }
             }
             cache = pathMappingHelper.replaceUrls(cache);
             cache = pathMappingHelper.replaceUrls(cache);
             if (queries != null && queries.length > 0) {
             if (queries != null && queries.length > 0) {
-                doc.put("hlCache", replaceHighlightQueries(cache, queries));
+                doc.put(HL_CACHE, replaceHighlightQueries(cache, queries));
             } else {
             } else {
-                doc.put("hlCache", cache);
+                doc.put(HL_CACHE, cache);
             }
             }
         } else {
         } else {
             doc.put(fessConfig.getIndexFieldCache(), StringUtil.EMPTY);
             doc.put(fessConfig.getIndexFieldCache(), StringUtil.EMPTY);
-            doc.put("hlCache", StringUtil.EMPTY);
+            doc.put(HL_CACHE, StringUtil.EMPTY);
         }
         }
 
 
         try {
         try {

+ 3 - 3
src/main/webapp/WEB-INF/orig/view/cache.hbs

@@ -1,7 +1,7 @@
 <!DOCTYPE html>
 <!DOCTYPE html>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
-<base href="{{urlLink}}">
+<base href="{{url_link}}">
 <div style="border:1px solid #999;margin:5px -1px;padding:0;">
 <div style="border:1px solid #999;margin:5px -1px;padding:0;">
-<div style="color:#000;background:#ddd;border:1px solid #666;margin:10px 15px;padding:5px;text-align:left;">{{cacheMsg}}</div>
+<div style="color:#000;background:#ddd;border:1px solid #666;margin:10px 15px;padding:5px;text-align:left;">{{cache_msg}}</div>
 </div>
 </div>
-{{{hlCache}}}
+{{{hl_cache}}}

+ 3 - 3
src/main/webapp/WEB-INF/view/cache.hbs

@@ -1,7 +1,7 @@
 <!DOCTYPE html>
 <!DOCTYPE html>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
-<base href="{{urlLink}}">
+<base href="{{url_link}}">
 <div style="border:1px solid #999;margin:5px -1px;padding:0;">
 <div style="border:1px solid #999;margin:5px -1px;padding:0;">
-<div style="color:#000;background:#ddd;border:1px solid #666;margin:10px 15px;padding:5px;text-align:left;">{{cacheMsg}}</div>
+<div style="color:#000;background:#ddd;border:1px solid #666;margin:10px 15px;padding:5px;text-align:left;">{{cache_msg}}</div>
 </div>
 </div>
-{{{hlCache}}}
+{{{hl_cache}}}