/ftr-gwt-library-tags/src/main/java/eu/future/earth/gwt/tags/RequestUtils.java
Java | 366 lines | 123 code | 44 blank | 199 comment | 34 complexity | ef973c8640953e07ce8e9506c8d95d57 MD5 | raw file
Possible License(s): Apache-2.0
1/* 2 * $Id: RequestUtils.java 471754 2006-11-06 14:55:09Z husted $ 3 * 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21package eu.future.earth.gwt.tags; 22 23import java.net.MalformedURLException; 24import java.net.URL; 25import java.util.Locale; 26 27import javax.servlet.http.HttpServletRequest; 28import javax.servlet.http.HttpSession; 29 30/** 31 * <p> 32 * General purpose utility methods related to processing a servlet request in the Struts controller framework. 33 * </p> 34 * 35 * @version $Rev: 471754 $ $Date: 2006-11-06 08:55:09 -0600 (Mon, 06 Nov 2006) $ 36 */ 37@SuppressWarnings({ 38 "rawtypes" 39}) 40public class RequestUtils { 41 // ------------------------------------------------------- Static Variables 42 43 44 // --------------------------------------------------------- Public Methods 45 46 public static String getParameter(HttpServletRequest request, String name) { 47 String result = request.getParameter(name); 48 if (result != null && result.trim().length() > 0) { 49 return result; 50 } 51 return null; 52 } 53 54 public static int getIntParameter(HttpServletRequest request, String name) throws NumberFormatException { 55 String result = request.getParameter(name); 56 if (result != null && result.trim().length() > 0) { 57 return Integer.parseInt(result); 58 } 59 return -1; 60 } 61 62 63 public static long getLongParameter(HttpServletRequest request, String name) throws NumberFormatException { 64 String result = request.getParameter(name); 65 if (result != null && result.trim().length() > 0) { 66 return Long.parseLong(result); 67 } 68 return -1; 69 } 70 71 public static boolean getBooleanParameter(HttpServletRequest request, String name) { 72 String result = request.getParameter(name); 73 if (result != null && result.trim().length() > 0) { 74 return Boolean.parseBoolean(result); 75 } 76 return false; 77 } 78 79 /** 80 * <p> 81 * Create and return an absolute URL for the specified context-relative path, based on the server and context information in the specified request. 82 * </p> 83 * 84 * @param request 85 * The servlet request we are processing 86 * @param path 87 * The context-relative path (must start with '/') 88 * @return absolute URL based on context-relative path 89 * @throws MalformedURLException 90 * if we cannot create an absolute URL 91 */ 92 public static URL absoluteURL(HttpServletRequest request, String path) throws MalformedURLException { 93 return (new URL(serverURL(request), request.getContextPath() + path)); 94 } 95 96 /** 97 * <p> 98 * Return the <code>Class</code> object for the specified fully qualified class name, from this web application's class loader. 99 * </p> 100 * 101 * @param className 102 * Fully qualified class name to be loaded 103 * @return Class object 104 * @throws ClassNotFoundException 105 * if the class cannot be found 106 */ 107 public static Class applicationClass(String className) throws ClassNotFoundException { 108 return applicationClass(className, null); 109 } 110 111 /** 112 * <p> 113 * Return the <code>Class</code> object for the specified fully qualified class name, from this web application's class loader. 114 * </p> 115 * 116 * @param className 117 * Fully qualified class name to be loaded 118 * @param classLoader 119 * The desired classloader to use 120 * @return Class object 121 * @throws ClassNotFoundException 122 * if the class cannot be found 123 */ 124 125 public static Class applicationClass(String className, ClassLoader classLoader) throws ClassNotFoundException { 126 if (classLoader == null) { 127 // Look up the class loader to be used 128 classLoader = Thread.currentThread().getContextClassLoader(); 129 130 if (classLoader == null) { 131 classLoader = RequestUtils.class.getClassLoader(); 132 } 133 } 134 135 // Attempt to load the specified class 136 return (classLoader.loadClass(className)); 137 } 138 139 /** 140 * <p> 141 * Return a new instance of the specified fully qualified class name, after loading the class from this web application's class loader. The specified class <strong>MUST</strong> have a public zero-arguments constructor. 142 * </p> 143 * 144 * @param className 145 * Fully qualified class name to use 146 * @return new instance of class 147 * @throws ClassNotFoundException 148 * if the class cannot be found 149 * @throws IllegalAccessException 150 * if the class or its constructor is not accessible 151 * @throws InstantiationException 152 * if this class represents an abstract class, an interface, an array class, a primitive type, or void 153 * @throws InstantiationException 154 * if this class has no zero-arguments constructor 155 */ 156 public static Object applicationInstance(String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException { 157 return applicationInstance(className, null); 158 } 159 160 /** 161 * <p> 162 * Return a new instance of the specified fully qualified class name, after loading the class from this web application's class loader. The specified class <strong>MUST</strong> have a public zero-arguments constructor. 163 * </p> 164 * 165 * @param className 166 * Fully qualified class name to use 167 * @param classLoader 168 * The desired classloader to use 169 * @return new instance of class 170 * @throws ClassNotFoundException 171 * if the class cannot be found 172 * @throws IllegalAccessException 173 * if the class or its constructor is not accessible 174 * @throws InstantiationException 175 * if this class represents an abstract class, an interface, an array class, a primitive type, or void 176 * @throws InstantiationException 177 * if this class has no zero-arguments constructor 178 */ 179 public static Object applicationInstance(String className, ClassLoader classLoader) throws ClassNotFoundException, IllegalAccessException, InstantiationException { 180 return (applicationClass(className, classLoader).newInstance()); 181 } 182 183 /** 184 * <p> 185 * Look up and return current user locale, based on the specified parameters. 186 * </p> 187 * 188 * @param request 189 * The request used to lookup the Locale 190 * @param locale 191 * Name of the session attribute for our user's Locale. If this is <code>null</code>, the default locale key is used for the lookup. 192 * @return current user locale 193 * @since Struts 1.2 194 */ 195 public static Locale getUserLocale(HttpServletRequest request, String locale) { 196 Locale userLocale = null; 197 HttpSession session = request.getSession(false); 198 199 if (locale == null) { 200 locale = Globals.LOCALE_KEY; 201 } 202 203 // Only check session if sessions are enabled 204 if (session != null) { 205 userLocale = (Locale) session.getAttribute(locale); 206 } 207 208 if (userLocale == null) { 209 // Returns Locale based on Accept-Language header or the server default 210 userLocale = request.getLocale(); 211 } 212 213 return userLocale; 214 } 215 216 /** 217 * <p> 218 * Compute the printable representation of a URL, leaving off the scheme/host/port part if no host is specified. This will typically be the case for URLs that were originally created from relative or context-relative URIs. 219 * </p> 220 * 221 * @param url 222 * URL to render in a printable representation 223 * @return printable representation of a URL 224 */ 225 public static String printableURL(URL url) { 226 if (url.getHost() != null) { 227 return (url.toString()); 228 } 229 230 String file = url.getFile(); 231 String ref = url.getRef(); 232 233 if (ref == null) { 234 return (file); 235 } else { 236 StringBuffer sb = new StringBuffer(file); 237 238 sb.append('#'); 239 sb.append(ref); 240 241 return (sb.toString()); 242 } 243 } 244 245 /** 246 * <p> 247 * Return the URL representing the current request. This is equivalent to <code>HttpServletRequest.getRequestURL</code> in Servlet 2.3. 248 * </p> 249 * 250 * @param request 251 * The servlet request we are processing 252 * @return URL representing the current request 253 * @throws MalformedURLException 254 * if a URL cannot be created 255 */ 256 public static URL requestURL(HttpServletRequest request) throws MalformedURLException { 257 StringBuffer url = requestToServerUriStringBuffer(request); 258 259 return (new URL(url.toString())); 260 } 261 262 /** 263 * <p> 264 * Return the URL representing the scheme, server, and port number of the current request. Server-relative URLs can be created by simply appending the server-relative path (starting with '/') to this. 265 * </p> 266 * 267 * @param request 268 * The servlet request we are processing 269 * @return URL representing the scheme, server, and port number of the current request 270 * @throws MalformedURLException 271 * if a URL cannot be created 272 */ 273 public static URL serverURL(HttpServletRequest request) throws MalformedURLException { 274 StringBuffer url = requestToServerStringBuffer(request); 275 276 return (new URL(url.toString())); 277 } 278 279 /** 280 * <p> 281 * Return the string representing the scheme, server, and port number of the current request. Server-relative URLs can be created by simply appending the server-relative path (starting with '/') to this. 282 * </p> 283 * 284 * @param request 285 * The servlet request we are processing 286 * @return URL representing the scheme, server, and port number of the current request 287 * @since Struts 1.2.0 288 */ 289 public static StringBuffer requestToServerUriStringBuffer(HttpServletRequest request) { 290 StringBuffer serverUri = createServerUriStringBuffer(request.getScheme(), request.getServerName(), request.getServerPort(), request.getRequestURI()); 291 292 return serverUri; 293 } 294 295 /** 296 * <p> 297 * Return <code>StringBuffer</code> representing the scheme, server, and port number of the current request. Server-relative URLs can be created by simply appending the server-relative path (starting with '/') to this. 298 * </p> 299 * 300 * @param request 301 * The servlet request we are processing 302 * @return URL representing the scheme, server, and port number of the current request 303 * @since Struts 1.2.0 304 */ 305 public static StringBuffer requestToServerStringBuffer(HttpServletRequest request) { 306 return createServerStringBuffer(request.getScheme(), request.getServerName(), request.getServerPort()); 307 } 308 309 /** 310 * <p> 311 * Return <code>StringBuffer</code> representing the scheme, server, and port number of the current request. 312 * </p> 313 * 314 * @param scheme 315 * The scheme name to use 316 * @param server 317 * The server name to use 318 * @param port 319 * The port value to use 320 * @return StringBuffer in the form scheme: server: port 321 * @since Struts 1.2.0 322 */ 323 public static StringBuffer createServerStringBuffer(String scheme, String server, int port) { 324 StringBuffer url = new StringBuffer(); 325 326 if (port < 0) { 327 port = 80; // Work around java.net.URL bug 328 } 329 330 url.append(scheme); 331 url.append("://"); 332 url.append(server); 333 334 if ((scheme.equals("http") && (port != 80)) || (scheme.equals("https") && (port != 443))) { 335 url.append(':'); 336 url.append(port); 337 } 338 339 return url; 340 } 341 342 /** 343 * <p> 344 * Return <code>StringBuffer</code> representing the scheme, server, and port number of the current request. 345 * </p> 346 * 347 * @param scheme 348 * The scheme name to use 349 * @param server 350 * The server name to use 351 * @param port 352 * The port value to use 353 * @param uri 354 * The uri value to use 355 * @return StringBuffer in the form scheme: server: port 356 * @since Struts 1.2.0 357 */ 358 public static StringBuffer createServerUriStringBuffer(String scheme, String server, int port, String uri) { 359 StringBuffer serverUri = createServerStringBuffer(scheme, server, port); 360 361 serverUri.append(uri); 362 363 return serverUri; 364 } 365 366}