1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.engine; |
16 |
|
|
17 |
|
import org.apache.commons.codec.net.URLCodec; |
18 |
|
import org.apache.hivemind.ApplicationRuntimeException; |
19 |
|
import org.apache.hivemind.util.Defense; |
20 |
|
import org.apache.tapestry.IRequestCycle; |
21 |
|
import org.apache.tapestry.Tapestry; |
22 |
|
import org.apache.tapestry.util.QueryParameterMap; |
23 |
|
import org.apache.tapestry.web.WebRequest; |
24 |
|
|
25 |
|
import java.io.UnsupportedEncodingException; |
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
public class EngineServiceLink implements ILink |
39 |
|
{ |
40 |
|
private static final int DEFAULT_HTTP_PORT = 80; |
41 |
|
|
42 |
|
private static final int DEFAULT_HTTPS_PORT = 443; |
43 |
|
|
44 |
|
private final String _servletPath; |
45 |
|
|
46 |
|
private final URLCodec _codec; |
47 |
|
|
48 |
|
private IRequestCycle _cycle; |
49 |
|
|
50 |
|
private boolean _stateful; |
51 |
|
|
52 |
|
private String _encoding; |
53 |
|
|
54 |
|
|
55 |
|
private final QueryParameterMap _parameters; |
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
private final WebRequest _request; |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
public EngineServiceLink(String servletPath, String encoding, |
79 |
|
URLCodec codec, WebRequest request, QueryParameterMap parameters, boolean stateful) |
80 |
9 |
{ |
81 |
9 |
Defense.notNull(servletPath, "servletPath"); |
82 |
9 |
Defense.notNull(encoding, "encoding"); |
83 |
9 |
Defense.notNull(codec, "codec"); |
84 |
9 |
Defense.notNull(request, "request"); |
85 |
9 |
Defense.notNull(parameters, "parameters"); |
86 |
|
|
87 |
9 |
_servletPath = servletPath; |
88 |
9 |
_encoding = encoding; |
89 |
9 |
_codec = codec; |
90 |
9 |
_request = request; |
91 |
9 |
_stateful = stateful; |
92 |
9 |
_parameters = parameters; |
93 |
9 |
} |
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
public EngineServiceLink(IRequestCycle cycle, String servletPath, String encoding, |
116 |
|
URLCodec codec, WebRequest request, QueryParameterMap parameters, boolean stateful) |
117 |
5 |
{ |
118 |
5 |
Defense.notNull(cycle, "cycle"); |
119 |
5 |
Defense.notNull(servletPath, "servletPath"); |
120 |
5 |
Defense.notNull(encoding, "encoding"); |
121 |
5 |
Defense.notNull(codec, "codec"); |
122 |
5 |
Defense.notNull(request, "request"); |
123 |
5 |
Defense.notNull(parameters, "parameters"); |
124 |
|
|
125 |
5 |
_cycle = cycle; |
126 |
5 |
_servletPath = servletPath; |
127 |
5 |
_encoding = encoding; |
128 |
5 |
_codec = codec; |
129 |
5 |
_request = request; |
130 |
5 |
_stateful = stateful; |
131 |
5 |
_parameters = parameters; |
132 |
5 |
} |
133 |
|
|
134 |
|
public String getURL() |
135 |
|
{ |
136 |
7 |
return getURL(null, true); |
137 |
|
} |
138 |
|
|
139 |
|
public String getURL(String anchor, boolean includeParameters) |
140 |
|
{ |
141 |
11 |
return constructURL(new StringBuffer(), anchor, includeParameters); |
142 |
|
} |
143 |
|
|
144 |
|
public String getAbsoluteURL() |
145 |
|
{ |
146 |
1 |
return getAbsoluteURL(null, null, 0, null, true); |
147 |
|
} |
148 |
|
|
149 |
|
public String getURL(String scheme, String server, int port, String anchor, |
150 |
|
boolean includeParameters) |
151 |
|
{ |
152 |
2 |
boolean useAbsolute = EngineUtils.needAbsoluteURL(scheme, server, port, _request); |
153 |
|
|
154 |
2 |
return useAbsolute ? getAbsoluteURL(scheme, server, port, anchor, includeParameters) |
155 |
|
: getURL(anchor, includeParameters); |
156 |
|
} |
157 |
|
|
158 |
|
public String getAbsoluteURL(String scheme, String server, int port, String anchor, |
159 |
|
boolean includeParameters) |
160 |
|
{ |
161 |
3 |
StringBuffer buffer = new StringBuffer(); |
162 |
|
|
163 |
3 |
int nport = port == 0 ? _request.getServerPort() : port; |
164 |
3 |
String nscheme = scheme == null ? _request.getScheme() : scheme; |
165 |
|
|
166 |
3 |
buffer.append(nscheme); |
167 |
3 |
buffer.append("://"); |
168 |
|
|
169 |
3 |
buffer.append(server == null ? _request.getServerName() : server); |
170 |
|
|
171 |
3 |
if (!(nscheme.equals("http") && nport == DEFAULT_HTTP_PORT) && !(nscheme.equals("https") && nport == DEFAULT_HTTPS_PORT)) |
172 |
|
{ |
173 |
3 |
buffer.append(':'); |
174 |
3 |
buffer.append(nport); |
175 |
|
} |
176 |
|
|
177 |
|
|
178 |
|
|
179 |
|
|
180 |
3 |
return constructURL(buffer, anchor, includeParameters); |
181 |
|
} |
182 |
|
|
183 |
|
private String constructURL(StringBuffer buffer, String anchor, boolean includeParameters) |
184 |
|
{ |
185 |
14 |
buffer.append(_servletPath); |
186 |
|
|
187 |
14 |
if (includeParameters) |
188 |
12 |
addParameters(buffer); |
189 |
|
|
190 |
14 |
if (anchor != null) |
191 |
|
{ |
192 |
4 |
buffer.append('#'); |
193 |
4 |
buffer.append(anchor); |
194 |
|
} |
195 |
|
|
196 |
14 |
String result = buffer.toString(); |
197 |
|
|
198 |
|
|
199 |
14 |
if (_cycle != null && _stateful) { |
200 |
|
|
201 |
1 |
result = _cycle.encodeURL(result); |
202 |
|
} |
203 |
|
|
204 |
14 |
return result; |
205 |
|
} |
206 |
|
|
207 |
|
private void addParameters(StringBuffer buffer) |
208 |
|
{ |
209 |
12 |
String[] names = getParameterNames(); |
210 |
|
|
211 |
12 |
String sep = "?"; |
212 |
|
|
213 |
35 |
for (int i = 0; i < names.length; i++) |
214 |
|
{ |
215 |
23 |
String name = names[i]; |
216 |
23 |
String[] values = getParameterValues(name); |
217 |
|
|
218 |
23 |
if (values == null) |
219 |
10 |
continue; |
220 |
|
|
221 |
27 |
for (int j = 0; j < values.length; j++) |
222 |
|
{ |
223 |
14 |
buffer.append(sep); |
224 |
14 |
buffer.append(name); |
225 |
14 |
buffer.append("="); |
226 |
14 |
buffer.append(encode(values[j])); |
227 |
|
|
228 |
14 |
sep = "&"; |
229 |
|
} |
230 |
|
|
231 |
|
} |
232 |
12 |
} |
233 |
|
|
234 |
|
private String encode(String value) |
235 |
|
{ |
236 |
|
try |
237 |
|
{ |
238 |
14 |
return _codec.encode(value, _encoding); |
239 |
|
} |
240 |
0 |
catch (UnsupportedEncodingException ex) |
241 |
|
{ |
242 |
0 |
throw new ApplicationRuntimeException(Tapestry.format("illegal-encoding", _encoding), |
243 |
|
ex); |
244 |
|
} |
245 |
|
} |
246 |
|
|
247 |
|
public String[] getParameterNames() |
248 |
|
{ |
249 |
13 |
return _parameters.getParameterNames(); |
250 |
|
} |
251 |
|
|
252 |
|
public String[] getParameterValues(String name) |
253 |
|
{ |
254 |
23 |
return _parameters.getParameterValues(name); |
255 |
|
} |
256 |
|
} |