Shinsuke Sugaya 11 years ago
parent
commit
8dba6984af

+ 27 - 2
src/main/java/jp/sf/fess/action/IndexAction.java

@@ -22,7 +22,9 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
 import java.net.URLDecoder;
+import java.net.URLEncoder;
 import java.sql.Timestamp;
 import java.text.NumberFormat;
 import java.util.ArrayList;
@@ -89,6 +91,8 @@ import org.seasar.framework.container.annotation.tiger.Binding;
 import org.seasar.framework.container.annotation.tiger.BindingType;
 import org.seasar.framework.util.InputStreamUtil;
 import org.seasar.framework.util.OutputStreamUtil;
+import org.seasar.framework.util.URLUtil;
+import org.seasar.robot.util.CharUtil;
 import org.seasar.struts.annotation.ActionForm;
 import org.seasar.struts.annotation.Execute;
 import org.seasar.struts.taglib.S2Functions;
@@ -396,6 +400,27 @@ public class IndexAction {
             }
         }
 
+        String hash;
+        if (StringUtil.isNotBlank(indexForm.hash)) {
+            String value = URLUtil.decode(indexForm.hash, Constants.UTF_8);
+            final StringBuilder buf = new StringBuilder(value.length() + 100);
+            for (final char c : value.toCharArray()) {
+                if (CharUtil.isUrlChar(c) || c == ' ') {
+                    buf.append(c);
+                } else {
+                    try {
+                        buf.append(URLEncoder.encode(String.valueOf(c),
+                                Constants.UTF_8));
+                    } catch (final UnsupportedEncodingException e) {
+                        // NOP
+                    }
+                }
+            }
+            hash = buf.toString();
+        } else {
+            hash = StringUtil.EMPTY;
+        }
+
         if (isFileSystemPath(url)) {
             if (Constants.TRUE.equals(crawlerProperties.getProperty(
                     Constants.SEARCH_FILE_PROXY_PROPERTY, Constants.TRUE))) {
@@ -441,10 +466,10 @@ public class IndexAction {
                         RequestUtil.getRequest().getContextPath()
                                 + "/applet/launcher?uri=" + S2Functions.u(url));
             } else {
-                ResponseUtil.getResponse().sendRedirect(url);
+                ResponseUtil.getResponse().sendRedirect(url + hash);
             }
         } else {
-            ResponseUtil.getResponse().sendRedirect(url);
+            ResponseUtil.getResponse().sendRedirect(url + hash);
         }
         return null;
     }

+ 3 - 0
src/main/java/jp/sf/fess/form/IndexForm.java

@@ -67,6 +67,9 @@ public class IndexForm implements Serializable {
 
     public String[] hq;
 
+    @Maxbytelength(maxbytelength = 1000)
+    public String hash;
+
     // xml/json
 
     @Maxbytelength(maxbytelength = 20)

+ 14 - 13
src/main/java/jp/sf/fess/helper/ViewHelper.java

@@ -289,7 +289,7 @@ public class ViewHelper implements Serializable {
             final String mimetype = getString(document, "mimetype");
             if (StringUtil.isNotBlank(mimetype)) {
                 if ("application/pdf".equals(mimetype)) {
-                    return appendSearchWord(url, "search");
+                    return appendPDFSearchWord(url);
                 } else {
                     // TODO others..
                     return url;
@@ -299,19 +299,20 @@ public class ViewHelper implements Serializable {
         return url;
     }
 
-    protected String appendSearchWord(final String url,
-            final String searchWordKey) {
-        final String query = RequestUtil.getRequest().getParameter("query");
-        if (StringUtil.isNotBlank(query)) {
-            String separator;
-            if (url.indexOf('?') >= 0) {
-                separator = "&";
-            } else {
-                separator = "?";
+    protected String appendPDFSearchWord(final String url) {
+        final String[] queries = (String[]) RequestUtil.getRequest()
+                .getAttribute(Constants.HIGHLIGHT_QUERIES);
+        if (queries != null) {
+            StringBuilder buf = new StringBuilder(url.length() + 100);
+            buf.append(url).append("#search=%22");
+            for (int i = 0; i < queries.length; i++) {
+                if (i != 0) {
+                    buf.append(' ');
+                }
+                buf.append(URLUtil.encode(queries[i], urlLinkEncoding));
             }
-            return url + separator + searchWordKey + "="
-                    + URLUtil.encode(query, urlLinkEncoding);
-
+            buf.append("%22");
+            return buf.toString();
         }
         return url;
     }

+ 19 - 0
src/main/webapp/js/search.js

@@ -25,11 +25,21 @@ $(function(){
 	$result.on('mousedown', 'a.link', function(e){
 		var docId = $(this).attr('data-id'),
 			rt = $('#rt').val(),
+			url = $(this).attr('href'),
 			buf = [];
 		buf.push('go?rt=');
 		buf.push(rt);
 		buf.push('&docId=');
 		buf.push(docId);
+
+		var hashIndex = url.indexOf('#');
+		if (hashIndex >= 0) {
+			var hashStr = url.substring(hashIndex);
+			buf.push('&hash=');
+			buf.push(encodeURIComponent(hashStr));
+			buf.push(hashStr);
+		}
+
 		$(this).attr('href', buf.join(''));
 	});
 
@@ -37,6 +47,7 @@ $(function(){
 		if($screenshot.size() > 0) {
 			var docId = $(this).attr('data-id'),
 				rt = $('#rt').val(),
+				url = $(this).attr('href'),
 				queryId = $queryId.val(),
 				buf = [];
 			buf.push('go?rt=');
@@ -44,6 +55,14 @@ $(function(){
 			buf.push('&docId=');
 			buf.push(docId);
 
+			var hashIndex = url.indexOf('#');
+			if (hashIndex >= 0) {
+				var hashStr = url.substring(hashIndex);
+				buf.push('&hash=');
+				buf.push(encodeURIComponent(hashStr));
+				buf.push(hashStr);
+			}
+
 			$screenshot.children().remove();
 			
 			var content = '<a href="' + buf.join('') + '"><img src="screenshot?queryId='