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 | 0 | { |
81 | 0 | Defense.notNull(servletPath, "servletPath"); |
82 | 0 | Defense.notNull(encoding, "encoding"); |
83 | 0 | Defense.notNull(codec, "codec"); |
84 | 0 | Defense.notNull(request, "request"); |
85 | 0 | Defense.notNull(parameters, "parameters"); |
86 | |
|
87 | 0 | _servletPath = servletPath; |
88 | 0 | _encoding = encoding; |
89 | 0 | _codec = codec; |
90 | 0 | _request = request; |
91 | 0 | _stateful = stateful; |
92 | 0 | _parameters = parameters; |
93 | 0 | } |
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 | 0 | { |
118 | 0 | Defense.notNull(cycle, "cycle"); |
119 | 0 | Defense.notNull(servletPath, "servletPath"); |
120 | 0 | Defense.notNull(encoding, "encoding"); |
121 | 0 | Defense.notNull(codec, "codec"); |
122 | 0 | Defense.notNull(request, "request"); |
123 | 0 | Defense.notNull(parameters, "parameters"); |
124 | |
|
125 | 0 | _cycle = cycle; |
126 | 0 | _servletPath = servletPath; |
127 | 0 | _encoding = encoding; |
128 | 0 | _codec = codec; |
129 | 0 | _request = request; |
130 | 0 | _stateful = stateful; |
131 | 0 | _parameters = parameters; |
132 | 0 | } |
133 | |
|
134 | |
public String getURL() |
135 | |
{ |
136 | 0 | return getURL(null, true); |
137 | |
} |
138 | |
|
139 | |
public String getURL(String anchor, boolean includeParameters) |
140 | |
{ |
141 | 0 | return constructURL(new StringBuffer(), anchor, includeParameters); |
142 | |
} |
143 | |
|
144 | |
public String getAbsoluteURL() |
145 | |
{ |
146 | 0 | 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 | 0 | boolean useAbsolute = EngineUtils.needAbsoluteURL(scheme, server, port, _request); |
153 | |
|
154 | 0 | 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 | 0 | StringBuffer buffer = new StringBuffer(); |
162 | |
|
163 | 0 | int nport = port == 0 ? _request.getServerPort() : port; |
164 | 0 | String nscheme = scheme == null ? _request.getScheme() : scheme; |
165 | |
|
166 | 0 | buffer.append(nscheme); |
167 | 0 | buffer.append("://"); |
168 | |
|
169 | 0 | buffer.append(server == null ? _request.getServerName() : server); |
170 | |
|
171 | 0 | if (!(nscheme.equals("http") && nport == DEFAULT_HTTP_PORT) && !(nscheme.equals("https") && nport == DEFAULT_HTTPS_PORT)) |
172 | |
{ |
173 | 0 | buffer.append(':'); |
174 | 0 | buffer.append(nport); |
175 | |
} |
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | 0 | return constructURL(buffer, anchor, includeParameters); |
181 | |
} |
182 | |
|
183 | |
private String constructURL(StringBuffer buffer, String anchor, boolean includeParameters) |
184 | |
{ |
185 | 0 | buffer.append(_servletPath); |
186 | |
|
187 | 0 | if (includeParameters) |
188 | 0 | addParameters(buffer); |
189 | |
|
190 | 0 | if (anchor != null) |
191 | |
{ |
192 | 0 | buffer.append('#'); |
193 | 0 | buffer.append(anchor); |
194 | |
} |
195 | |
|
196 | 0 | String result = buffer.toString(); |
197 | |
|
198 | |
|
199 | 0 | if (_cycle != null && _stateful) |
200 | |
{ |
201 | 0 | result = _cycle.encodeURL(result); |
202 | |
} |
203 | |
|
204 | 0 | return result; |
205 | |
} |
206 | |
|
207 | |
private void addParameters(StringBuffer buffer) |
208 | |
{ |
209 | 0 | String[] names = getParameterNames(); |
210 | |
|
211 | 0 | String sep = "?"; |
212 | |
|
213 | 0 | for (int i = 0; i < names.length; i++) |
214 | |
{ |
215 | 0 | String name = names[i]; |
216 | 0 | String[] values = getParameterValues(name); |
217 | |
|
218 | 0 | if (values == null) |
219 | 0 | continue; |
220 | |
|
221 | 0 | for (int j = 0; j < values.length; j++) |
222 | |
{ |
223 | 0 | buffer.append(sep); |
224 | 0 | buffer.append(name); |
225 | 0 | buffer.append("="); |
226 | 0 | buffer.append(encode(values[j])); |
227 | |
|
228 | 0 | sep = "&"; |
229 | |
} |
230 | |
|
231 | |
} |
232 | 0 | } |
233 | |
|
234 | |
private String encode(String value) |
235 | |
{ |
236 | |
try |
237 | |
{ |
238 | 0 | 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 | 0 | return _parameters.getParameterNames(); |
250 | |
} |
251 | |
|
252 | |
public String[] getParameterValues(String name) |
253 | |
{ |
254 | 0 | return _parameters.getParameterValues(name); |
255 | |
} |
256 | |
} |