KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > misc > HTTPUtil


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.core.misc;
66
67 import com.jcorporate.expresso.core.jsdkapi.GenericSession;
68 import com.jcorporate.expresso.kernel.util.FastStringBuffer;
69
70 import javax.servlet.ServletException JavaDoc;
71 import javax.servlet.http.HttpServletRequest JavaDoc;
72 import javax.servlet.http.HttpServletResponse JavaDoc;
73
74
75 /**
76  * This utility class includes static methods for manipulating URLs.
77  * Specifically, this class is used to set the expresso.continueURL session
78  * object, to retrieve the continueURL object, to qualify Expresso next
79  * parameters, etc.
80  * <p/>
81  * Creation date: (9/14/00 10:28:34 PM)
82  *
83  * @author Adam Rossi, PlatinumSolutions
84  */

85 public class HTTPUtil {
86     /**
87      * HTTPUtil constructor comment.
88      */

89     public HTTPUtil() {
90         super();
91     } /* HTTPUtil() */
92
93     /**
94      * Return the name of the URL to "continue" to from this servlet,
95      *
96      * @param request
97      * @return String
98      */

99     public static String JavaDoc getContinueURL(HttpServletRequest JavaDoc request)
100             throws ServletException JavaDoc {
101         return GenericSession.getAttributeString(request,
102                 "expresso.continueURL");
103     } /* getContinueURL(HttpServletRequest) */
104
105
106     /**
107      * Creation date: (9/26/00 12:21:19 PM)
108      *
109      * @param req javax.servlet.http.HttpServletRequest
110      * @return java.lang.String
111      */

112     public static String JavaDoc getNextURI(HttpServletRequest JavaDoc req) {
113         String JavaDoc uri = req.getRequestURI();
114
115         return uri;
116     } /* getNextURI(HttpServletRequest) */
117
118     /**
119      * Override the normal "next" parameter to go somewhere else
120      *
121      * @return String
122      */

123     /**
124      * We always want expresso.continueURL objects to be fully qualified.
125      * If we detect that the URL is not fully qualified, we attempt to make
126      * it fully qualified.
127      * <p/>
128      * Creation date: (9/16/00 3:22:30 PM)
129      * author: Adam Rossi, PlatinumSolutions
130      *
131      * @param url java.lang.String
132      * @param req
133      * @return java.lang.String
134      */

135     public static String JavaDoc qualifyURLIfNeeded(String JavaDoc url, HttpServletRequest JavaDoc req) {
136         if ("".equals(url) || url == null) {
137             return null;
138         }
139         if (url.startsWith("http") || url.startsWith("HTTP")) {
140
141             //this URL is already fully qualified (hopefully)
142
return url;
143         }
144
145         // We need to qualify the URL
146
FastStringBuffer sb = new FastStringBuffer(128);
147         sb.append(req.getScheme());
148         sb.append("://");
149         sb.append(req.getServerName());
150
151         //to avoid problems with no port specified <--> port 80 translation
152
if (req.getServerPort() != 80) {
153             sb.append(":");
154             sb.append(req.getServerPort());
155         }
156         if (!('/' == url.charAt(0))) {
157             sb.append("/");
158         }
159
160         sb.append(url);
161
162         return sb.toString();
163     } /* qualifyURLIfNeeded(String, HttpServletRequest) */
164
165     /**
166      * The "back" URL is used at the bottom of servlets to offer the user a quick
167      * way to go "back" to the page they started from e.g. before they requested
168      * the current servlet. This method allows us to set a specific "back" URL
169      *
170      * @param newURL
171      * @param request
172      */

173     public static void setBackURL(String JavaDoc newURL, HttpServletRequest JavaDoc request)
174             throws ServletException JavaDoc {
175         newURL = StringUtil.notNull(newURL);
176
177         if (newURL.equals("")) {
178             GenericSession.removeAttribute(request, "expresso.backURL");
179         }
180
181         // We want all URL's in the session to be fully qualified and encoded
182
newURL = qualifyURLIfNeeded(newURL, request);
183         GenericSession.setAttribute(request, "expresso.backURL", newURL);
184     } /* setBackURL(String, HttpServletRequest) */
185
186
187     /**
188      * The "back" URL is used at the bottom of servlets to offer the user a quick way
189      * to go "back" to the page they started from e.g. before they requested the
190      * current servlet. This method sets the "back" URL to be the value of the "back"
191      * parameter given to the current servlet
192      *
193      * @param request
194      */

195     public static void setBackURL(HttpServletRequest JavaDoc request)
196             throws ServletException JavaDoc {
197         setBackURL(request.getParameter("back"), request);
198     } /* setBackURL(HttpServletRequest) */
199
200
201     /**
202      * Return the "back" URL, usually displayed at the bottom of the servlet page
203      * to provide the user a way to go back to the page they called the servlet from
204      *
205      * @param request
206      * @return
207      */

208     public static String JavaDoc getBackURL(HttpServletRequest JavaDoc request)
209             throws ServletException JavaDoc {
210         return GenericSession.getAttributeString(request, "expresso.backURL");
211     } /* getBackURL(HttpServletRequest) */
212
213
214     /**
215      * @param newURL
216      * @param request
217      */

218     public static void setContinueURL(String JavaDoc newURL,
219                                       HttpServletRequest JavaDoc request)
220             throws ServletException JavaDoc {
221         if (!(newURL.equals("")) || newURL == null) {
222             GenericSession.removeAttribute(request, "expresso.continueURL");
223         }
224
225         // We want all URL's in the session to be fully qualified and encoded
226
newURL = qualifyURLIfNeeded(newURL, request);
227         GenericSession.removeAttribute(request, "expresso.continueURL");
228         GenericSession.setAttribute(request, "expresso.continueURL", newURL);
229     } /* setContinueURL(String, HttpServletRequest) */
230
231
232     /**
233      * Override the normal "next" parameter to go somewhere else
234      *
235      * @param newURL
236      * @param request
237      * @param response
238      */

239     public static void setContinueURL(String JavaDoc newURL,
240                                       HttpServletRequest JavaDoc request,
241                                       HttpServletResponse JavaDoc response)
242             throws ServletException JavaDoc {
243         if (!"".equals(newURL) || newURL == null) {
244             GenericSession.removeAttribute(request, "expresso.continueURL");
245         }
246
247         // We want all URL's in the session to be fully qualified and encoded
248
newURL = qualifyURLIfNeeded(newURL, request);
249         GenericSession.removeAttribute(request, "expresso.continueURL");
250         GenericSession.setAttribute(request, "expresso.continueURL",
251                 response.encodeURL(newURL));
252     } /* setContinueURL(String, HttpServletRequest, HttpServletResponse) */
253
254
255     /**
256      * We use this method to set the current location as the continueURL.
257      * Useful in JSP pages when we want to set the continueURL if we get an error.
258      * <p/>
259      * Creation date: (9/14/00 11:10:11 PM)
260      * author: Adam Rossi, PlatinumSolutions
261      *
262      * @param req javax.servlet.http.HttpServletRequest
263      */

264     public static void setContinueURL(HttpServletRequest JavaDoc req)
265             throws ServletException JavaDoc {
266         String JavaDoc qstring = StringUtil.notNull(req.getQueryString());
267
268         StringBuffer JavaDoc urlsb = req.getRequestURL();//HttpUtils.getRequestURL(req);
269

270         if (!qstring.equals("")) {
271             urlsb.append("?");
272             urlsb.append(qstring);
273         }
274
275         setContinueURL(urlsb.toString(), req);
276     } /* setContinueURL(HttpServletRequest) */
277
278
279     /**
280      * We use this method to set the current location as the continueURL.
281      * Useful in JSP pages when we want to set the continueURL if we get an error.
282      * <p/>
283      * Creation date: (9/14/00 11:10:11 PM)
284      * author: Adam Rossi, PlatinumSolutions
285      *
286      * @param res
287      * @param req javax.servlet.http.HttpServletRequest
288      */

289     public static void setContinueURL(HttpServletRequest JavaDoc req,
290                                       HttpServletResponse JavaDoc res)
291             throws ServletException JavaDoc {
292         String JavaDoc qstring = StringUtil.notNull(req.getQueryString());
293         StringBuffer JavaDoc urlsb = req.getRequestURL();//HttpUtils.getRequestURL(req);
294

295         if (!(qstring.equals(""))) {
296             urlsb.append("?");
297             urlsb.append(qstring);
298         }
299
300         setContinueURL(urlsb.toString(), req, res);
301     } /* setContinueURL(HttpServletRequest, HttpServletResponse) */
302
303
304     /**
305      * set the continue URL from a query string "next" parameter
306      * Creation date: (12/17/00 2:24:49 PM)
307      *
308      * @param req javax.servlet.http.HttpServletRequest
309      * @throws javax.servlet.ServletException The exception description.
310      */

311     public static void setContinueURLFromQueryString(HttpServletRequest JavaDoc req)
312             throws ServletException JavaDoc {
313         String JavaDoc nextURL = req.getParameter("next");
314
315         if (nextURL != null && !"".equals(nextURL)) {
316             setContinueURL(nextURL, req);
317         }
318     }
319 } /* HttpUtil */
320
321 /* HttpUtil */
Popular Tags