KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jpublish > util > URLUtilities


1 /*--
2  Copyright (C) 2001-2003 Aetrion LLC.
3  All rights reserved.
4
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions
7  are met:
8
9  1. Redistributions of source code must retain the above copyright
10     notice, this list of conditions, and the following disclaimer.
11
12  2. Redistributions in binary form must reproduce the above copyright
13     notice, this list of conditions, and the disclaimer that follows
14     these conditions in the documentation and/or other materials
15     provided with the distribution.
16  3. The name "JPublish" must not be used to endorse or promote products
17     derived from this software without prior written permission. For
18     written permission, please contact info@aetrion.com.
19
20  4. Products derived from this software may not be called "JPublish", nor
21     may "JPublish" appear in their name, without prior written permission
22     from Aetrion LLC (info@aetrion.com).
23
24  In addition, the authors of this software request (but do not require)
25  that you include in the end-user documentation provided with the
26  redistribution and/or in the software itself an acknowledgement equivalent
27  to the following:
28      "This product includes software developed by
29       Aetrion LLC (http://www.aetrion.com/)."
30  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
31  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
33  DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
34  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
36  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
39  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40  POSSIBILITY OF SUCH DAMAGE.
41  For more information on JPublish, please see <http://www.jpublish.org/>.
42
43  */

44 package org.jpublish.util;
45
46 import java.net.URLEncoder JavaDoc;
47
48 import javax.servlet.http.HttpServletRequest JavaDoc;
49 import javax.servlet.http.HttpServletResponse JavaDoc;
50
51 import org.apache.commons.logging.Log;
52 import org.apache.commons.logging.LogFactory;
53
54 /**
55  * Utility class for building URLs. All URLs are passed through the HttpServletResponse.encodeURL() method before being
56  * returned to ensure that session IDs are properly attached to URLs.
57  *
58  * @author Anthony Eden
59  */

60 public class URLUtilities {
61
62     /**
63      * The URL path separator.
64      */

65     public final static String JavaDoc URL_PATH_SEPARATOR = "/";
66
67     private static Log log = LogFactory.getLog(URLUtilities.class);
68
69     private HttpServletRequest JavaDoc request;
70     private HttpServletResponse JavaDoc response;
71
72     /**
73      * Construct a new URLUtilities class which can use the given request and response objects to build URLs.
74      *
75      * @param request The request object
76      * @param response The response object
77      */

78     public URLUtilities(HttpServletRequest JavaDoc request,
79             HttpServletResponse JavaDoc response) {
80         this.request = request;
81         this.response = response;
82     }
83
84     /**
85      * Build an HTTP URL relative to the application context using the given path.
86      *
87      * @param path The path
88      * @return Description of the Return Value
89      */

90     public String JavaDoc buildStandard(String JavaDoc path) {
91         return buildStandard(path, 0);
92     }
93
94     /**
95      * Build an HTTP URL relative to the application context using the given path.
96      *
97      * @param path The path
98      * @return Description of the Return Value
99      */

100     public String JavaDoc standard(String JavaDoc path) {
101         return buildStandard(path, 0);
102     }
103
104     /**
105      * Build an HTTP URL relative to the application context using the given path. This version of the
106      * <code>buildStandard</code> method allows you to specify the port number. A port number of 0 will cause the port
107      * argument to be ignored.
108      *
109      * @param path The path
110      * @param port The port
111      * @return Description of the Return Value
112      */

113     public String JavaDoc buildStandard(String JavaDoc path, int port) {
114         return build(path, "http", port);
115     }
116
117     /**
118      * Build an HTTP URL relative to the application context using the given path. This version of the
119      * <code>buildStandard</code> method allows you to specify the port number. A port number of 0 will cause the port
120      * argument to be ignored.
121      *
122      * @param path The path
123      * @param port The port
124      * @return Description of the Return Value
125      */

126     public String JavaDoc standard(String JavaDoc path, int port) {
127         return build(path, "http", port);
128     }
129
130     /**
131      * Build an HTTPS (Secure Socket Layer) method relative to the application context using the given path.
132      *
133      * @param path The path
134      * @return Description of the Return Value
135      */

136     public String JavaDoc buildSecure(String JavaDoc path) {
137         return buildSecure(path, 0);
138     }
139
140     /**
141      * Build an HTTPS (Secure Socket Layer) method relative to the application context using the given path.
142      *
143      * @param path The path
144      * @return Description of the Return Value
145      */

146     public String JavaDoc secure(String JavaDoc path) {
147         return buildSecure(path, 0);
148     }
149
150     /**
151      * Build an HTTPS (Secure Socket Layer) method relative to the application context using the given path. This
152      * version of the <code>buildSecure</code> method allows you to specify the port number. A port number of 0 will
153      * cause the port argument to be ignored.
154      *
155      * @param path The path
156      * @param port The port
157      * @return Description of the Return Value
158      */

159     public String JavaDoc buildSecure(String JavaDoc path, int port) {
160         return build(path, "https", port);
161     }
162
163     /**
164      * Build an HTTPS (Secure Socket Layer) method relative to the application context using the given path. This
165      * version of the <code>buildSecure</code> method allows you to specify the port number. A port number of 0 will
166      * cause the port argument to be ignored.
167      *
168      * @param path The path
169      * @param port The port
170      * @return Description of the Return Value
171      */

172     public String JavaDoc secure(String JavaDoc path, int port) {
173         return build(path, "https", port);
174     }
175
176     /**
177      * Build a URL using the given path, protocol and port. The path will be relative to the current context.
178      *
179      * @param path The path
180      * @param protocol (i.e. http or https)
181      * @param port The port (0 to ignore the port argument)
182      * @return The URL as a String
183      */

184     protected String JavaDoc build(String JavaDoc path, String JavaDoc protocol, int port) {
185         String JavaDoc serverName = request.getServerName();
186         String JavaDoc contextPath = request.getContextPath();
187
188         if (log.isDebugEnabled()) {
189             log.debug("Server name: " + serverName);
190             log.debug("Context path: " + contextPath);
191         }
192
193         if (!contextPath.endsWith(URL_PATH_SEPARATOR)) {
194             contextPath = contextPath + URL_PATH_SEPARATOR;
195         }
196
197         if (path.startsWith(URL_PATH_SEPARATOR)) {
198             path = path.substring(1);
199         }
200
201         String JavaDoc requestPath = contextPath + path;
202         if (log.isDebugEnabled()) {
203             log.debug("Request path: " + requestPath);
204         }
205
206         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
207         buffer.append(protocol);
208         buffer.append("://");
209         buffer.append(serverName);
210
211         if (port > 0) {
212             buffer.append(":");
213             buffer.append(port);
214         }
215
216         if (!requestPath.startsWith(URL_PATH_SEPARATOR)) {
217             buffer.append(URL_PATH_SEPARATOR);
218         }
219
220         buffer.append(requestPath);
221
222         if (log.isDebugEnabled()) {
223             log.debug("URL: '" + buffer + "'");
224         }
225
226         return response.encodeURL(buffer.toString());
227     }
228
229     /**
230      * Percent-encode the given String. This method delegates to the URLEncoder.encode() method.
231      *
232      * @param s The String to encode
233      * @return The encoded String
234      * @see java.net.URLEncoder
235      */

236     public String JavaDoc encode(String JavaDoc s) {
237         return URLEncoder.encode(s);
238     }
239
240 }
241
242
Popular Tags