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