|
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 |
597
| public EngineServiceLink(IRequestCycle cycle, String servletPath, String encoding,
|
|
81 |
| URLCodec codec, WebRequest request, Map parameters, boolean stateful) |
|
82 |
| { |
|
83 |
597
| Defense.notNull(cycle, "cycle");
|
|
84 |
597
| Defense.notNull(servletPath, "servletPath");
|
|
85 |
597
| Defense.notNull(encoding, "encoding");
|
|
86 |
597
| Defense.notNull(codec, "codec");
|
|
87 |
597
| Defense.notNull(request, "request");
|
|
88 |
597
| Defense.notNull(parameters, "parameters");
|
|
89 |
| |
|
90 |
597
| _cycle = cycle;
|
|
91 |
597
| _servletPath = servletPath;
|
|
92 |
597
| _encoding = encoding;
|
|
93 |
597
| _codec = codec;
|
|
94 |
597
| _request = request;
|
|
95 |
597
| _parameters = new QueryParameterMap(parameters);
|
|
96 |
597
| _stateful = stateful;
|
|
97 |
| } |
|
98 |
| |
|
99 |
240
| public String getURL()
|
|
100 |
| { |
|
101 |
240
| return getURL(null, true);
|
|
102 |
| } |
|
103 |
| |
|
104 |
576
| public String getURL(String anchor, boolean includeParameters)
|
|
105 |
| { |
|
106 |
576
| return constructURL(new StringBuffer(), anchor, includeParameters);
|
|
107 |
| } |
|
108 |
| |
|
109 |
3
| public String getAbsoluteURL()
|
|
110 |
| { |
|
111 |
3
| return getAbsoluteURL(null, null, 0, null, true);
|
|
112 |
| } |
|
113 |
| |
|
114 |
330
| public String getURL(String scheme, String server, int port, String anchor,
|
|
115 |
| boolean includeParameters) |
|
116 |
| { |
|
117 |
330
| boolean useAbsolute = EngineUtils.needAbsoluteURL(scheme, server, port, _request);
|
|
118 |
| |
|
119 |
330
| return useAbsolute ? getAbsoluteURL(scheme, server, port, anchor, includeParameters)
|
|
120 |
| : getURL(anchor, includeParameters); |
|
121 |
| } |
|
122 |
| |
|
123 |
9
| public String getAbsoluteURL(String scheme, String server, int port, String anchor,
|
|
124 |
| boolean includeParameters) |
|
125 |
| { |
|
126 |
9
| StringBuffer buffer = new StringBuffer();
|
|
127 |
| |
|
128 |
9
| if (scheme == null)
|
|
129 |
3
| scheme = _request.getScheme();
|
|
130 |
| |
|
131 |
9
| buffer.append(scheme);
|
|
132 |
9
| buffer.append("://");
|
|
133 |
| |
|
134 |
9
| if (server == null)
|
|
135 |
3
| server = _request.getServerName();
|
|
136 |
| |
|
137 |
9
| buffer.append(server);
|
|
138 |
| |
|
139 |
9
| if (port == 0)
|
|
140 |
3
| port = _request.getServerPort();
|
|
141 |
| |
|
142 |
9
| if (!(scheme.equals("http") && port == DEFAULT_HTTP_PORT))
|
|
143 |
| { |
|
144 |
9
| buffer.append(':');
|
|
145 |
9
| buffer.append(port);
|
|
146 |
| } |
|
147 |
| |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
9
| return constructURL(buffer, anchor, includeParameters);
|
|
152 |
| } |
|
153 |
| |
|
154 |
585
| private String constructURL(StringBuffer buffer, String anchor, boolean includeParameters)
|
|
155 |
| { |
|
156 |
585
| buffer.append(_servletPath);
|
|
157 |
| |
|
158 |
585
| if (includeParameters)
|
|
159 |
450
| addParameters(buffer);
|
|
160 |
| |
|
161 |
585
| if (anchor != null)
|
|
162 |
| { |
|
163 |
12
| buffer.append('#');
|
|
164 |
12
| buffer.append(anchor);
|
|
165 |
| } |
|
166 |
| |
|
167 |
585
| String result = buffer.toString();
|
|
168 |
| |
|
169 |
585
| result = _cycle.encodeURL(result);
|
|
170 |
| |
|
171 |
585
| return result;
|
|
172 |
| } |
|
173 |
| |
|
174 |
450
| private void addParameters(StringBuffer buffer)
|
|
175 |
| { |
|
176 |
450
| String[] names = getParameterNames();
|
|
177 |
| |
|
178 |
450
| String sep = "?";
|
|
179 |
| |
|
180 |
450
| for (int i = 0; i < names.length; i++)
|
|
181 |
| { |
|
182 |
1353
| String name = names[i];
|
|
183 |
1353
| String[] values = getParameterValues(name);
|
|
184 |
| |
|
185 |
1353
| if (values == null)
|
|
186 |
219
| continue;
|
|
187 |
| |
|
188 |
1134
| for (int j = 0; j < values.length; j++)
|
|
189 |
| { |
|
190 |
1185
| buffer.append(sep);
|
|
191 |
1185
| buffer.append(name);
|
|
192 |
1185
| buffer.append("=");
|
|
193 |
1185
| buffer.append(encode(values[j]));
|
|
194 |
| |
|
195 |
1185
| sep = "&";
|
|
196 |
| } |
|
197 |
| |
|
198 |
| } |
|
199 |
| } |
|
200 |
| |
|
201 |
1185
| private String encode(String value)
|
|
202 |
| { |
|
203 |
1185
| try
|
|
204 |
| { |
|
205 |
1185
| 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 |
594
| public String[] getParameterNames()
|
|
215 |
| { |
|
216 |
594
| return _parameters.getParameterNames();
|
|
217 |
| } |
|
218 |
| |
|
219 |
2199
| public String[] getParameterValues(String name)
|
|
220 |
| { |
|
221 |
2199
| return _parameters.getParameterValues(name);
|
|
222 |
| } |
|
223 |
| } |