/ftr-gwt-library-tags/src/main/java/eu/future/earth/gwt/tags/RequestUtils.java

http://ftr-gwt-library.googlecode.com/ · Java · 366 lines · 123 code · 44 blank · 199 comment · 34 complexity · ef973c8640953e07ce8e9506c8d95d57 MD5 · raw file

  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. */
  21. package eu.future.earth.gwt.tags;
  22. import java.net.MalformedURLException;
  23. import java.net.URL;
  24. import java.util.Locale;
  25. import javax.servlet.http.HttpServletRequest;
  26. import javax.servlet.http.HttpSession;
  27. /**
  28. * <p>
  29. * General purpose utility methods related to processing a servlet request in the Struts controller framework.
  30. * </p>
  31. *
  32. * @version $Rev: 471754 $ $Date: 2006-11-06 08:55:09 -0600 (Mon, 06 Nov 2006) $
  33. */
  34. @SuppressWarnings({
  35. "rawtypes"
  36. })
  37. public class RequestUtils {
  38. // ------------------------------------------------------- Static Variables
  39. // --------------------------------------------------------- Public Methods
  40. public static String getParameter(HttpServletRequest request, String name) {
  41. String result = request.getParameter(name);
  42. if (result != null && result.trim().length() > 0) {
  43. return result;
  44. }
  45. return null;
  46. }
  47. public static int getIntParameter(HttpServletRequest request, String name) throws NumberFormatException {
  48. String result = request.getParameter(name);
  49. if (result != null && result.trim().length() > 0) {
  50. return Integer.parseInt(result);
  51. }
  52. return -1;
  53. }
  54. public static long getLongParameter(HttpServletRequest request, String name) throws NumberFormatException {
  55. String result = request.getParameter(name);
  56. if (result != null && result.trim().length() > 0) {
  57. return Long.parseLong(result);
  58. }
  59. return -1;
  60. }
  61. public static boolean getBooleanParameter(HttpServletRequest request, String name) {
  62. String result = request.getParameter(name);
  63. if (result != null && result.trim().length() > 0) {
  64. return Boolean.parseBoolean(result);
  65. }
  66. return false;
  67. }
  68. /**
  69. * <p>
  70. * Create and return an absolute URL for the specified context-relative path, based on the server and context information in the specified request.
  71. * </p>
  72. *
  73. * @param request
  74. * The servlet request we are processing
  75. * @param path
  76. * The context-relative path (must start with '/')
  77. * @return absolute URL based on context-relative path
  78. * @throws MalformedURLException
  79. * if we cannot create an absolute URL
  80. */
  81. public static URL absoluteURL(HttpServletRequest request, String path) throws MalformedURLException {
  82. return (new URL(serverURL(request), request.getContextPath() + path));
  83. }
  84. /**
  85. * <p>
  86. * Return the <code>Class</code> object for the specified fully qualified class name, from this web application's class loader.
  87. * </p>
  88. *
  89. * @param className
  90. * Fully qualified class name to be loaded
  91. * @return Class object
  92. * @throws ClassNotFoundException
  93. * if the class cannot be found
  94. */
  95. public static Class applicationClass(String className) throws ClassNotFoundException {
  96. return applicationClass(className, null);
  97. }
  98. /**
  99. * <p>
  100. * Return the <code>Class</code> object for the specified fully qualified class name, from this web application's class loader.
  101. * </p>
  102. *
  103. * @param className
  104. * Fully qualified class name to be loaded
  105. * @param classLoader
  106. * The desired classloader to use
  107. * @return Class object
  108. * @throws ClassNotFoundException
  109. * if the class cannot be found
  110. */
  111. public static Class applicationClass(String className, ClassLoader classLoader) throws ClassNotFoundException {
  112. if (classLoader == null) {
  113. // Look up the class loader to be used
  114. classLoader = Thread.currentThread().getContextClassLoader();
  115. if (classLoader == null) {
  116. classLoader = RequestUtils.class.getClassLoader();
  117. }
  118. }
  119. // Attempt to load the specified class
  120. return (classLoader.loadClass(className));
  121. }
  122. /**
  123. * <p>
  124. * 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.
  125. * </p>
  126. *
  127. * @param className
  128. * Fully qualified class name to use
  129. * @return new instance of class
  130. * @throws ClassNotFoundException
  131. * if the class cannot be found
  132. * @throws IllegalAccessException
  133. * if the class or its constructor is not accessible
  134. * @throws InstantiationException
  135. * if this class represents an abstract class, an interface, an array class, a primitive type, or void
  136. * @throws InstantiationException
  137. * if this class has no zero-arguments constructor
  138. */
  139. public static Object applicationInstance(String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
  140. return applicationInstance(className, null);
  141. }
  142. /**
  143. * <p>
  144. * 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.
  145. * </p>
  146. *
  147. * @param className
  148. * Fully qualified class name to use
  149. * @param classLoader
  150. * The desired classloader to use
  151. * @return new instance of class
  152. * @throws ClassNotFoundException
  153. * if the class cannot be found
  154. * @throws IllegalAccessException
  155. * if the class or its constructor is not accessible
  156. * @throws InstantiationException
  157. * if this class represents an abstract class, an interface, an array class, a primitive type, or void
  158. * @throws InstantiationException
  159. * if this class has no zero-arguments constructor
  160. */
  161. public static Object applicationInstance(String className, ClassLoader classLoader) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
  162. return (applicationClass(className, classLoader).newInstance());
  163. }
  164. /**
  165. * <p>
  166. * Look up and return current user locale, based on the specified parameters.
  167. * </p>
  168. *
  169. * @param request
  170. * The request used to lookup the Locale
  171. * @param locale
  172. * 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.
  173. * @return current user locale
  174. * @since Struts 1.2
  175. */
  176. public static Locale getUserLocale(HttpServletRequest request, String locale) {
  177. Locale userLocale = null;
  178. HttpSession session = request.getSession(false);
  179. if (locale == null) {
  180. locale = Globals.LOCALE_KEY;
  181. }
  182. // Only check session if sessions are enabled
  183. if (session != null) {
  184. userLocale = (Locale) session.getAttribute(locale);
  185. }
  186. if (userLocale == null) {
  187. // Returns Locale based on Accept-Language header or the server default
  188. userLocale = request.getLocale();
  189. }
  190. return userLocale;
  191. }
  192. /**
  193. * <p>
  194. * 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.
  195. * </p>
  196. *
  197. * @param url
  198. * URL to render in a printable representation
  199. * @return printable representation of a URL
  200. */
  201. public static String printableURL(URL url) {
  202. if (url.getHost() != null) {
  203. return (url.toString());
  204. }
  205. String file = url.getFile();
  206. String ref = url.getRef();
  207. if (ref == null) {
  208. return (file);
  209. } else {
  210. StringBuffer sb = new StringBuffer(file);
  211. sb.append('#');
  212. sb.append(ref);
  213. return (sb.toString());
  214. }
  215. }
  216. /**
  217. * <p>
  218. * Return the URL representing the current request. This is equivalent to <code>HttpServletRequest.getRequestURL</code> in Servlet 2.3.
  219. * </p>
  220. *
  221. * @param request
  222. * The servlet request we are processing
  223. * @return URL representing the current request
  224. * @throws MalformedURLException
  225. * if a URL cannot be created
  226. */
  227. public static URL requestURL(HttpServletRequest request) throws MalformedURLException {
  228. StringBuffer url = requestToServerUriStringBuffer(request);
  229. return (new URL(url.toString()));
  230. }
  231. /**
  232. * <p>
  233. * 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.
  234. * </p>
  235. *
  236. * @param request
  237. * The servlet request we are processing
  238. * @return URL representing the scheme, server, and port number of the current request
  239. * @throws MalformedURLException
  240. * if a URL cannot be created
  241. */
  242. public static URL serverURL(HttpServletRequest request) throws MalformedURLException {
  243. StringBuffer url = requestToServerStringBuffer(request);
  244. return (new URL(url.toString()));
  245. }
  246. /**
  247. * <p>
  248. * 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.
  249. * </p>
  250. *
  251. * @param request
  252. * The servlet request we are processing
  253. * @return URL representing the scheme, server, and port number of the current request
  254. * @since Struts 1.2.0
  255. */
  256. public static StringBuffer requestToServerUriStringBuffer(HttpServletRequest request) {
  257. StringBuffer serverUri = createServerUriStringBuffer(request.getScheme(), request.getServerName(), request.getServerPort(), request.getRequestURI());
  258. return serverUri;
  259. }
  260. /**
  261. * <p>
  262. * 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.
  263. * </p>
  264. *
  265. * @param request
  266. * The servlet request we are processing
  267. * @return URL representing the scheme, server, and port number of the current request
  268. * @since Struts 1.2.0
  269. */
  270. public static StringBuffer requestToServerStringBuffer(HttpServletRequest request) {
  271. return createServerStringBuffer(request.getScheme(), request.getServerName(), request.getServerPort());
  272. }
  273. /**
  274. * <p>
  275. * Return <code>StringBuffer</code> representing the scheme, server, and port number of the current request.
  276. * </p>
  277. *
  278. * @param scheme
  279. * The scheme name to use
  280. * @param server
  281. * The server name to use
  282. * @param port
  283. * The port value to use
  284. * @return StringBuffer in the form scheme: server: port
  285. * @since Struts 1.2.0
  286. */
  287. public static StringBuffer createServerStringBuffer(String scheme, String server, int port) {
  288. StringBuffer url = new StringBuffer();
  289. if (port < 0) {
  290. port = 80; // Work around java.net.URL bug
  291. }
  292. url.append(scheme);
  293. url.append("://");
  294. url.append(server);
  295. if ((scheme.equals("http") && (port != 80)) || (scheme.equals("https") && (port != 443))) {
  296. url.append(':');
  297. url.append(port);
  298. }
  299. return url;
  300. }
  301. /**
  302. * <p>
  303. * Return <code>StringBuffer</code> representing the scheme, server, and port number of the current request.
  304. * </p>
  305. *
  306. * @param scheme
  307. * The scheme name to use
  308. * @param server
  309. * The server name to use
  310. * @param port
  311. * The port value to use
  312. * @param uri
  313. * The uri value to use
  314. * @return StringBuffer in the form scheme: server: port
  315. * @since Struts 1.2.0
  316. */
  317. public static StringBuffer createServerUriStringBuffer(String scheme, String server, int port, String uri) {
  318. StringBuffer serverUri = createServerStringBuffer(scheme, server, port);
  319. serverUri.append(uri);
  320. return serverUri;
  321. }
  322. }