1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.rewriter;
18
19 import java.util.StringTokenizer;
20
21 import javax.portlet.PortletURL;
22
23 /***
24 * WebContentRewriter
25 *
26 * @author <a href="mailto:rogerrutr@apache.org">Roger Ruttimann </a>
27 * @version $Id: WebContentRewriter.java 516448 2007-03-09 16:25:47Z ate $
28 */
29 public class WebContentRewriter extends RulesetRewriterImpl implements Rewriter
30 {
31
32
33
34 public void enterConvertTagEvent(String tagid, MutableAttributes attributes) {
35 super.enterConvertTagEvent(tagid, attributes);
36 }
37
38 /*** parameters that need to be propagated in the action URL (since HTTP request parameters will not be available) */
39 public static final String ACTION_PARAMETER_URL = "_AP_URL";
40 public static final String ACTION_PARAMETER_METHOD = "_AP_METHOD";
41
42
43
44
45 private PortletURL actionURL = null;
46
47 /***
48 * Setters/getters for members
49 */
50 public void setActionURL(PortletURL action)
51 {
52 this.actionURL = action;
53 }
54
55 public PortletURL getActionURL()
56 {
57 return this.actionURL;
58 }
59
60 /***
61 * rewriteURL
62 *
63 * @param url
64 * @param tag
65 * @param attribute
66 * @param otherAttributes
67 * @return the modified url which is a portlet action
68 *
69 * Rewrites all urls HREFS with a portlet action
70 */
71 public String rewriteUrl(String url, String tag, String attribute, MutableAttributes otherAttributes)
72 {
73 String modifiedURL = url;
74 modifiedURL = getModifiedURL(url);
75
76
77
78
79 if (( tag.equalsIgnoreCase("A") && attribute.equalsIgnoreCase("href")) ||
80 ( tag.equalsIgnoreCase("FORM") && attribute.equalsIgnoreCase("action")))
81
82 {
83
84 if (this.actionURL != null)
85 {
86
87 actionURL.setParameter(ACTION_PARAMETER_URL, modifiedURL);
88 if (tag.equalsIgnoreCase("FORM"))
89 {
90 String httpMethod = otherAttributes.getValue("method");
91 if (httpMethod != null)
92 actionURL.setParameter(ACTION_PARAMETER_METHOD, httpMethod);
93 }
94 modifiedURL = actionURL.toString();
95 }
96 }
97
98
99 if (attribute.equalsIgnoreCase("onclick"))
100 {
101
102 for (int i=0; i < otherAttributes.getLength(); i++) {
103
104 String name = otherAttributes.getQName(i);
105
106 if (name.equalsIgnoreCase("onclick")) {
107
108 String value = otherAttributes.getValue(i);
109
110 int index = value.indexOf(".location=");
111 if (index >= 0) {
112 String oldLocation = value.substring(index + ".location=".length());
113 StringTokenizer tokenizer = new StringTokenizer(oldLocation, "\'\"");
114 oldLocation = tokenizer.nextToken();
115
116 modifiedURL = oldLocation;
117 url = oldLocation;
118 modifiedURL = getModifiedURL(url);
119
120
121 if (this.actionURL != null)
122 {
123
124 actionURL.setParameter(ACTION_PARAMETER_URL, modifiedURL);
125 modifiedURL = actionURL.toString();
126 }
127
128
129 modifiedURL = value.replaceAll(oldLocation, modifiedURL);
130 }
131 }
132 }
133
134
135 }
136
137
138
139
140 return modifiedURL;
141 }
142
143 private String getModifiedURL(String url) {
144 String modifiedURL = url;
145
146 if (url.startsWith("/") || (!url.startsWith("http:") && !url.startsWith("https:")))
147 {
148 if (this.getBaseUrl() != null)
149 {
150 modifiedURL = getBaseRelativeUrl(url) ;
151
152 }
153 else
154 {
155 modifiedURL = url;
156 }
157 }
158 return modifiedURL;
159 }
160 }