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 |
| private static final int DEFAULT_HTTPS_PORT = 443; |
43 |
| |
44 |
| private final IRequestCycle _cycle; |
45 |
| |
46 |
| private final String _servletPath; |
47 |
| |
48 |
| private final URLCodec _codec; |
49 |
| |
50 |
| private String _encoding; |
51 |
| |
52 |
| private boolean _stateful; |
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 |
| |
79 |
| |
80 |
398
| public EngineServiceLink(IRequestCycle cycle, String servletPath, String encoding,
|
81 |
| URLCodec codec, WebRequest request, Map parameters, boolean stateful) |
82 |
| { |
83 |
398
| Defense.notNull(cycle, "cycle");
|
84 |
398
| Defense.notNull(servletPath, "servletPath");
|
85 |
398
| Defense.notNull(encoding, "encoding");
|
86 |
398
| Defense.notNull(codec, "codec");
|
87 |
398
| Defense.notNull(request, "request");
|
88 |
398
| Defense.notNull(parameters, "parameters");
|
89 |
| |
90 |
398
| _cycle = cycle;
|
91 |
398
| _servletPath = servletPath;
|
92 |
398
| _encoding = encoding;
|
93 |
398
| _codec = codec;
|
94 |
398
| _request = request;
|
95 |
398
| _parameters = new QueryParameterMap(parameters);
|
96 |
398
| _stateful = stateful;
|
97 |
| } |
98 |
| |
99 |
160
| public String getURL()
|
100 |
| { |
101 |
160
| return getURL(null, true);
|
102 |
| } |
103 |
| |
104 |
384
| public String getURL(String anchor, boolean includeParameters)
|
105 |
| { |
106 |
384
| return constructURL(new StringBuffer(), anchor, includeParameters);
|
107 |
| } |
108 |
| |
109 |
2
| public String getAbsoluteURL()
|
110 |
| { |
111 |
2
| return getAbsoluteURL(null, null, 0, null, true);
|
112 |
| } |
113 |
| |
114 |
220
| public String getURL(String scheme, String server, int port, String anchor,
|
115 |
| boolean includeParameters) |
116 |
| { |
117 |
220
| boolean useAbsolute = EngineUtils.needAbsoluteURL(scheme, server, port, _request);
|
118 |
| |
119 |
220
| return useAbsolute ? getAbsoluteURL(scheme, server, port, anchor, includeParameters)
|
120 |
| : getURL(anchor, includeParameters); |
121 |
| } |
122 |
| |
123 |
6
| public String getAbsoluteURL(String scheme, String server, int port, String anchor,
|
124 |
| boolean includeParameters) |
125 |
| { |
126 |
6
| StringBuffer buffer = new StringBuffer();
|
127 |
| |
128 |
6
| if (scheme == null)
|
129 |
2
| scheme = _request.getScheme();
|
130 |
| |
131 |
6
| buffer.append(scheme);
|
132 |
6
| buffer.append("://");
|
133 |
| |
134 |
6
| if (server == null)
|
135 |
2
| server = _request.getServerName();
|
136 |
| |
137 |
6
| buffer.append(server);
|
138 |
| |
139 |
6
| if (port == 0)
|
140 |
2
| port = _request.getServerPort();
|
141 |
| |
142 |
6
| if (!(scheme.equals("http") && port == DEFAULT_HTTP_PORT))
|
143 |
| { |
144 |
6
| buffer.append(':');
|
145 |
6
| buffer.append(port);
|
146 |
| } |
147 |
| |
148 |
| |
149 |
| |
150 |
| |
151 |
6
| return constructURL(buffer, anchor, includeParameters);
|
152 |
| } |
153 |
| |
154 |
390
| private String constructURL(StringBuffer buffer, String anchor, boolean includeParameters)
|
155 |
| { |
156 |
390
| buffer.append(_servletPath);
|
157 |
| |
158 |
390
| if (includeParameters)
|
159 |
300
| addParameters(buffer);
|
160 |
| |
161 |
390
| if (anchor != null)
|
162 |
| { |
163 |
8
| buffer.append('#');
|
164 |
8
| buffer.append(anchor);
|
165 |
| } |
166 |
| |
167 |
390
| String result = buffer.toString();
|
168 |
| |
169 |
390
| result = _cycle.encodeURL(result);
|
170 |
| |
171 |
390
| return result;
|
172 |
| } |
173 |
| |
174 |
300
| private void addParameters(StringBuffer buffer)
|
175 |
| { |
176 |
300
| String[] names = getParameterNames();
|
177 |
| |
178 |
300
| String sep = "?";
|
179 |
| |
180 |
300
| for (int i = 0; i < names.length; i++)
|
181 |
| { |
182 |
902
| String name = names[i];
|
183 |
902
| String[] values = getParameterValues(name);
|
184 |
| |
185 |
902
| if (values == null)
|
186 |
146
| continue;
|
187 |
| |
188 |
756
| for (int j = 0; j < values.length; j++)
|
189 |
| { |
190 |
790
| buffer.append(sep);
|
191 |
790
| buffer.append(name);
|
192 |
790
| buffer.append("=");
|
193 |
790
| buffer.append(encode(values[j]));
|
194 |
| |
195 |
790
| sep = "&";
|
196 |
| } |
197 |
| |
198 |
| } |
199 |
| } |
200 |
| |
201 |
790
| private String encode(String value)
|
202 |
| { |
203 |
790
| try
|
204 |
| { |
205 |
790
| return _codec.encode(value, _encoding);
|
206 |
| } |
207 |
| catch (UnsupportedEncodingException ex) |
208 |
| { |
209 |
0
| throw new ApplicationRuntimeException(Tapestry.format("illegal-encoding", _encoding),
|
210 |
| ex); |
211 |
| } |
212 |
| } |
213 |
| |
214 |
396
| public String[] getParameterNames()
|
215 |
| { |
216 |
396
| return _parameters.getParameterNames();
|
217 |
| } |
218 |
| |
219 |
1466
| public String[] getParameterValues(String name)
|
220 |
| { |
221 |
1466
| return _parameters.getParameterValues(name);
|
222 |
| } |
223 |
| } |