|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
EngineServiceLink.java | 100% | 98.2% | 100% | 98.9% |
|
1 |
// Copyright 2004, 2005 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
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.request.RequestContext;
|
|
26 |
import org.apache.tapestry.util.QueryParameterMap;
|
|
27 |
|
|
28 |
/**
|
|
29 |
* A EngineServiceLink represents a possible action within the client web browser; either clicking a
|
|
30 |
* link or submitting a form, which is constructed primarily from the servlet path, with some
|
|
31 |
* additional query parameters. A full URL for the EngineServiceLink can be generated, or the query
|
|
32 |
* parameters for the EngineServiceLink can be extracted (separately from the servlet path). The
|
|
33 |
* latter case is used when submitting constructing {@link org.apache.tapestry.form.Form forms}.
|
|
34 |
*
|
|
35 |
* @author Howard Lewis Ship
|
|
36 |
* @since 3.0
|
|
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 |
private final QueryParameterMap _parameters;
|
|
54 |
|
|
55 |
/**
|
|
56 |
* Creates a new EngineServiceLink.
|
|
57 |
*
|
|
58 |
* @param cycle
|
|
59 |
* The {@link IRequestCycle} the EngineServiceLink is to be created for.
|
|
60 |
* @param servletPath
|
|
61 |
* The path used to invoke the Tapestry servlet.
|
|
62 |
* @param codec
|
|
63 |
* A codec for converting strings into URL-safe formats.
|
|
64 |
* @param encoding
|
|
65 |
* The output encoding for the request.
|
|
66 |
* @param parameters
|
|
67 |
* The query parameters to be encoded into the url. Keys are strings, values are
|
|
68 |
* null, string or array of string. The map is retained, not copied.
|
|
69 |
* @param stateful
|
|
70 |
* if true, the service which generated the EngineServiceLink is stateful and expects
|
|
71 |
* that the final URL will be passed through {@link IRequestCycle#encodeURL(String)}.
|
|
72 |
*/
|
|
73 |
|
|
74 | 286 |
public EngineServiceLink(IRequestCycle cycle, String servletPath, String encoding,
|
75 |
URLCodec codec, Map parameters, boolean stateful)
|
|
76 |
{ |
|
77 | 286 |
Defense.notNull(cycle, "cycle");
|
78 | 286 |
Defense.notNull(servletPath, "servletPath");
|
79 | 286 |
Defense.notNull(encoding, "encoding");
|
80 | 286 |
Defense.notNull(codec, "codec");
|
81 | 286 |
Defense.notNull(parameters, "parameters");
|
82 |
|
|
83 | 286 |
_cycle = cycle; |
84 | 286 |
_servletPath = servletPath; |
85 | 286 |
_encoding = encoding; |
86 | 286 |
_codec = codec; |
87 | 286 |
_parameters = new QueryParameterMap(parameters);
|
88 | 286 |
_stateful = stateful; |
89 |
} |
|
90 |
|
|
91 | 96 |
public String getURL()
|
92 |
{ |
|
93 | 96 |
return getURL(null, true); |
94 |
} |
|
95 |
|
|
96 | 282 |
public String getURL(String anchor, boolean includeParameters) |
97 |
{ |
|
98 | 282 |
return constructURL(new StringBuffer(), anchor, includeParameters); |
99 |
} |
|
100 |
|
|
101 | 1 |
public String getAbsoluteURL()
|
102 |
{ |
|
103 | 1 |
return getAbsoluteURL(null, null, 0, null, true); |
104 |
} |
|
105 |
|
|
106 | 4 |
public String getAbsoluteURL(String scheme, String server, int port, String anchor, |
107 |
boolean includeParameters)
|
|
108 |
{ |
|
109 | 4 |
StringBuffer buffer = new StringBuffer();
|
110 | 4 |
RequestContext context = _cycle.getRequestContext(); |
111 |
|
|
112 | 4 |
if (scheme == null) |
113 | 2 |
scheme = context.getScheme(); |
114 |
|
|
115 | 4 |
buffer.append(scheme); |
116 | 4 |
buffer.append("://");
|
117 |
|
|
118 | 4 |
if (server == null) |
119 | 2 |
server = context.getServerName(); |
120 |
|
|
121 | 4 |
buffer.append(server); |
122 |
|
|
123 | 4 |
if (port == 0)
|
124 | 2 |
port = context.getServerPort(); |
125 |
|
|
126 | 4 |
if (!(scheme.equals("http") && port == DEFAULT_HTTP_PORT)) |
127 |
{ |
|
128 | 3 |
buffer.append(':'); |
129 | 3 |
buffer.append(port); |
130 |
} |
|
131 |
|
|
132 |
// Add the servlet path and the rest of the URL & query parameters.
|
|
133 |
// The servlet path starts with a leading slash.
|
|
134 |
|
|
135 | 4 |
return constructURL(buffer, anchor, includeParameters);
|
136 |
} |
|
137 |
|
|
138 | 286 |
private String constructURL(StringBuffer buffer, String anchor, boolean includeParameters) |
139 |
{ |
|
140 | 286 |
buffer.append(_servletPath); |
141 |
|
|
142 | 286 |
if (includeParameters)
|
143 | 230 |
addParameters(buffer); |
144 |
|
|
145 | 286 |
if (anchor != null) |
146 |
{ |
|
147 | 3 |
buffer.append('#'); |
148 | 3 |
buffer.append(anchor); |
149 |
} |
|
150 |
|
|
151 | 286 |
String result = buffer.toString(); |
152 |
|
|
153 | 286 |
if (_stateful)
|
154 | 187 |
result = _cycle.encodeURL(result); |
155 |
|
|
156 | 286 |
return result;
|
157 |
} |
|
158 |
|
|
159 | 230 |
private void addParameters(StringBuffer buffer) |
160 |
{ |
|
161 | 230 |
String[] names = getParameterNames(); |
162 |
|
|
163 | 230 |
String sep = "?";
|
164 |
|
|
165 | 230 |
for (int i = 0; i < names.length; i++) |
166 |
{ |
|
167 | 668 |
String name = names[i]; |
168 | 668 |
String[] values = getParameterValues(name); |
169 |
|
|
170 | 668 |
if (values == null) |
171 | 184 |
continue;
|
172 |
|
|
173 | 484 |
for (int j = 0; j < values.length; j++) |
174 |
{ |
|
175 | 501 |
buffer.append(sep); |
176 | 501 |
buffer.append(name); |
177 | 501 |
buffer.append("=");
|
178 | 501 |
buffer.append(encode(values[j])); |
179 |
|
|
180 | 501 |
sep = "&";
|
181 |
} |
|
182 |
|
|
183 |
} |
|
184 |
} |
|
185 |
|
|
186 | 501 |
private String encode(String value)
|
187 |
{ |
|
188 | 501 |
try
|
189 |
{ |
|
190 | 501 |
return _codec.encode(value, _encoding);
|
191 |
} |
|
192 |
catch (UnsupportedEncodingException ex)
|
|
193 |
{ |
|
194 | 0 |
throw new ApplicationRuntimeException(Tapestry.format("illegal-encoding", _encoding), |
195 |
ex); |
|
196 |
} |
|
197 |
} |
|
198 |
|
|
199 | 285 |
public String[] getParameterNames()
|
200 |
{ |
|
201 | 285 |
return _parameters.getParameterNames();
|
202 |
} |
|
203 |
|
|
204 | 992 |
public String[] getParameterValues(String name)
|
205 |
{ |
|
206 | 992 |
return _parameters.getParameterValues(name);
|
207 |
} |
|
208 |
} |
|