KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > engine > EngineServiceLink


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 JavaDoc;
18 import java.util.Map JavaDoc;
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  * 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 JavaDoc _servletPath;
46
47     private final URLCodec _codec;
48
49     private String JavaDoc _encoding;
50
51     private boolean _stateful;
52
53     /** @since 4.0 */
54     private final QueryParameterMap _parameters;
55
56     /** @since 4.0 */
57
58     private final WebRequest _request;
59
60     /**
61      * Creates a new EngineServiceLink.
62      *
63      * @param cycle
64      * The {@link IRequestCycle}  the EngineServiceLink is to be created for.
65      * @param servletPath
66      * The path used to invoke the Tapestry servlet.
67      * @param codec
68      * A codec for converting strings into URL-safe formats.
69      * @param encoding
70      * The output encoding for the request.
71      * @param parameters
72      * The query parameters to be encoded into the url. Keys are strings, values are
73      * null, string or array of string. The map is retained, not copied.
74      * @param stateful
75      * if true, the service which generated the EngineServiceLink is stateful and expects
76      * that the final URL will be passed through {@link IRequestCycle#encodeURL(String)}.
77      */

78
79     public EngineServiceLink(IRequestCycle cycle, String JavaDoc servletPath, String JavaDoc encoding,
80             URLCodec codec, WebRequest request, Map JavaDoc parameters, boolean stateful)
81     {
82         Defense.notNull(cycle, "cycle");
83         Defense.notNull(servletPath, "servletPath");
84         Defense.notNull(encoding, "encoding");
85         Defense.notNull(codec, "codec");
86         Defense.notNull(request, "request");
87         Defense.notNull(parameters, "parameters");
88
89         _cycle = cycle;
90         _servletPath = servletPath;
91         _encoding = encoding;
92         _codec = codec;
93         _request = request;
94         _parameters = new QueryParameterMap(parameters);
95         _stateful = stateful;
96     }
97
98     public String JavaDoc getURL()
99     {
100         return getURL(null, true);
101     }
102
103     public String JavaDoc getURL(String JavaDoc anchor, boolean includeParameters)
104     {
105         return constructURL(new StringBuffer JavaDoc(), anchor, includeParameters);
106     }
107
108     public String JavaDoc getAbsoluteURL()
109     {
110         return getAbsoluteURL(null, null, 0, null, true);
111     }
112
113     public String JavaDoc getAbsoluteURL(String JavaDoc scheme, String JavaDoc server, int port, String JavaDoc anchor,
114             boolean includeParameters)
115     {
116         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
117
118         if (scheme == null)
119             scheme = _request.getScheme();
120
121         buffer.append(scheme);
122         buffer.append("://");
123
124         if (server == null)
125             server = _request.getServerName();
126
127         buffer.append(server);
128
129         if (port == 0)
130             port = _request.getServerPort();
131
132         if (!(scheme.equals("http") && port == DEFAULT_HTTP_PORT))
133         {
134             buffer.append(':');
135             buffer.append(port);
136         }
137
138         // Add the servlet path and the rest of the URL & query parameters.
139
// The servlet path starts with a leading slash.
140

141         return constructURL(buffer, anchor, includeParameters);
142     }
143
144     private String JavaDoc constructURL(StringBuffer JavaDoc buffer, String JavaDoc anchor, boolean includeParameters)
145     {
146         buffer.append(_servletPath);
147
148         if (includeParameters)
149             addParameters(buffer);
150
151         if (anchor != null)
152         {
153             buffer.append('#');
154             buffer.append(anchor);
155         }
156
157         String JavaDoc result = buffer.toString();
158
159         if (_stateful)
160             result = _cycle.encodeURL(result);
161
162         return result;
163     }
164
165     private void addParameters(StringBuffer JavaDoc buffer)
166     {
167         String JavaDoc[] names = getParameterNames();
168
169         String JavaDoc sep = "?";
170
171         for (int i = 0; i < names.length; i++)
172         {
173             String JavaDoc name = names[i];
174             String JavaDoc[] values = getParameterValues(name);
175
176             if (values == null)
177                 continue;
178
179             for (int j = 0; j < values.length; j++)
180             {
181                 buffer.append(sep);
182                 buffer.append(name);
183                 buffer.append("=");
184                 buffer.append(encode(values[j]));
185
186                 sep = "&";
187             }
188
189         }
190     }
191
192     private String JavaDoc encode(String JavaDoc value)
193     {
194         try
195         {
196             return _codec.encode(value, _encoding);
197         }
198         catch (UnsupportedEncodingException JavaDoc ex)
199         {
200             throw new ApplicationRuntimeException(Tapestry.format("illegal-encoding", _encoding),
201                     ex);
202         }
203     }
204
205     public String JavaDoc[] getParameterNames()
206     {
207         return _parameters.getParameterNames();
208     }
209
210     public String JavaDoc[] getParameterValues(String JavaDoc name)
211     {
212         return _parameters.getParameterValues(name);
213     }
214 }
Popular Tags