Shinsuke Sugaya 11 лет назад
Родитель
Сommit
e7f0f82d83

+ 17 - 1
src/main/java/jp/sf/fess/helper/PathMappingHelper.java

@@ -42,7 +42,7 @@ public class PathMappingHelper implements Serializable {
 
     private final Map<String, List<PathMapping>> pathMappingMap = new HashMap<String, List<PathMapping>>();
 
-    private volatile List<PathMapping> cachedPathMappingList = null;
+    volatile List<PathMapping> cachedPathMappingList = null;
 
     @InitMethod
     public void init() {
@@ -95,6 +95,22 @@ public class PathMappingHelper implements Serializable {
         return replaceUrl(pathMappingList, url);
     }
 
+    public String replaceUrls(final String text) {
+        if (cachedPathMappingList == null) {
+            synchronized (this) {
+                if (cachedPathMappingList == null) {
+                    init();
+                }
+            }
+        }
+        String result = text;
+        for (final PathMapping pathMapping : cachedPathMappingList) {
+            result = result.replaceAll("(\"[^\"]*)" + pathMapping.getRegex()
+                    + "([^\"]*\")", "$1" + pathMapping.getReplacement() + "$2");
+        }
+        return result;
+    }
+
     public String replaceUrl(final String url) {
         if (cachedPathMappingList == null) {
             synchronized (this) {

+ 3 - 2
src/main/java/jp/sf/fess/helper/ViewHelper.java

@@ -416,7 +416,7 @@ public class ViewHelper implements Serializable {
         if (locale == null) {
             locale = Locale.ENGLISH;
         }
-        String url = (String) doc.get("url");
+        String url = (String) doc.get("urlLink");
         if (url == null) {
             url = MessageResourcesUtil.getMessage(locale,
                     "labels.search_unknown");
@@ -435,8 +435,9 @@ public class ViewHelper implements Serializable {
 
         doc.put("queries", queries);
 
-        final String cache = (String) doc.get("cache");
+        String cache = (String) doc.get("cache");
         if (cache != null) {
+            cache = pathMappingHelper.replaceUrls(cache);
             if (queries != null && queries.length > 0) {
                 doc.put("hlCache", replaceHighlightQueries(cache, queries));
             } else {

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

@@ -1,6 +1,6 @@
 <!DOCTYPE html>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
-<base href="{{url}}">
+<base href="{{urlLink}}">
 <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>

+ 27 - 0
src/test/java/jp/sf/fess/helper/PathMappingHelperTest.java

@@ -61,4 +61,31 @@ public class PathMappingHelperTest extends S2TestCase {
         assertEquals("http://localhost/taro/",
                 pathMappingHelper.replaceUrl(sessionId, url));
     }
+
+    public void test_replaceUrls() {
+        final List<PathMapping> pathMappingList = new ArrayList<PathMapping>();
+        final PathMapping pathMapping = new PathMapping();
+        pathMapping.setRegex("file:///home/");
+        pathMapping.setReplacement("http://localhost/");
+        pathMappingList.add(pathMapping);
+
+        pathMappingHelper.cachedPathMappingList = pathMappingList;
+
+        String text = "\"file:///home/\"";
+        assertEquals("\"http://localhost/\"",
+                pathMappingHelper.replaceUrls(text));
+
+        text = "\"file:///home/taro/\"";
+        assertEquals("\"http://localhost/taro/\"",
+                pathMappingHelper.replaceUrls(text));
+
+        text = "\"aaafile:///home/taro/\"";
+        assertEquals("\"aaahttp://localhost/taro/\"",
+                pathMappingHelper.replaceUrls(text));
+
+        text = "aaa\"file:///home/taro/\"bbb";
+        assertEquals("aaa\"http://localhost/taro/\"bbb",
+                pathMappingHelper.replaceUrls(text));
+
+    }
 }