This commit is contained in:
Shinsuke Sugaya 2014-05-30 09:42:36 +09:00
parent 13fcdad905
commit e7f0f82d83
4 changed files with 48 additions and 4 deletions

View file

@ -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) {

View file

@ -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 {

View file

@ -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>

View file

@ -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));
}
}