-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCrossingResponse.java
62 lines (52 loc) · 1.65 KB
/
CrossingResponse.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.servletgarden.railsxing;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
* @author Yoko Harada <[email protected]>
*/
public class CrossingResponse {
String context_path;
int status;
Map<String, String> responseHeader;
String body;
Map flash;
CrossingResponse() {}
public String getBody() {
return addContextPath();
}
public Map getFlash() {
return flash;
}
public Map<String, String> getResponseHeader() {
return responseHeader;
}
public int getStatus() {
return status;
}
private static Pattern pattern =
Pattern.compile("(<\\s*link\\s+href\\s*=\\s*(\"|')\\/stylesheets/)|(<\\s*script\\s+src\\s*=\\s*(\"|')\\/javascripts/)");
private String addContextPath() {
Matcher matcher = pattern.matcher(body);
boolean result = matcher.find();
StringBuffer sb = new StringBuffer();
while(result) {
String matched = matcher.group();
String replacement = "";
if (matched.contains("/stylesheets/")) {
replacement = matched.replace("/stylesheets/", context_path + "/stylesheets/");
} else if (matched.contains("/javascripts/")) {
replacement = matched.replace("/javascripts/", context_path + "/javascripts/");
}
matcher.appendReplacement(sb, replacement);
result = matcher.find();
}
matcher.appendTail(sb);
return new String(sb);
}
}