View Javadoc

1   package org.appfuse.webapp.util;
2   
3   import org.apache.commons.logging.Log;
4   import org.apache.commons.logging.LogFactory;
5   
6   import javax.servlet.http.Cookie;
7   import javax.servlet.http.HttpServletRequest;
8   import javax.servlet.http.HttpServletResponse;
9   
10  /**
11   * Convenience class for setting and retrieving cookies.
12   */
13  public final class RequestUtil {
14      private static final Log log = LogFactory.getLog(RequestUtil.class);
15  
16      /**
17       * Checkstyle rule: utility classes should not have public constructor
18       */
19      private RequestUtil() {
20      }
21  
22      /**
23       * Convenience method to set a cookie
24       *
25       * @param response the current response
26       * @param name the name of the cookie
27       * @param value the value of the cookie
28       * @param path the path to set it on
29       */
30      public static void setCookie(HttpServletResponse response, String name,
31                                   String value, String path) {
32          if (log.isDebugEnabled()) {
33              log.debug("Setting cookie '" + name + "' on path '" + path + "'");
34          }
35  
36          Cookie cookie = new Cookie(name, value);
37          cookie.setSecure(false);
38          cookie.setPath(path);
39          cookie.setMaxAge(3600 * 24 * 30); // 30 days
40  
41          response.addCookie(cookie);
42      }
43  
44      /**
45       * Convenience method to get a cookie by name
46       *
47       * @param request the current request
48       * @param name the name of the cookie to find
49       *
50       * @return the cookie (if found), null if not found
51       */
52      public static Cookie getCookie(HttpServletRequest request, String name) {
53          Cookie[] cookies = request.getCookies();
54          Cookie returnCookie = null;
55  
56          if (cookies == null) {
57              return returnCookie;
58          }
59  
60          for (final Cookie thisCookie : cookies) {
61              if (thisCookie.getName().equals(name) && !"".equals(thisCookie.getValue())) {
62                  returnCookie = thisCookie;
63                  break;
64              }
65          }
66  
67          return returnCookie;
68      }
69  
70      /**
71       * Convenience method for deleting a cookie by name
72       *
73       * @param response the current web response
74       * @param cookie the cookie to delete
75       * @param path the path on which the cookie was set (i.e. /appfuse)
76       */
77      public static void deleteCookie(HttpServletResponse response,
78                                      Cookie cookie, String path) {
79          if (cookie != null) {
80              // Delete the cookie by setting its maximum age to zero
81              cookie.setMaxAge(0);
82              cookie.setPath(path);
83              response.addCookie(cookie);
84          }
85      }
86  
87      /**
88       * Convenience method to get the application's URL based on request
89       * variables.
90       * 
91       * @param request the current request
92       * @return URL to application
93       */
94      public static String getAppURL(HttpServletRequest request) {
95          if (request == null) return "";
96          
97          StringBuffer url = new StringBuffer();
98          int port = request.getServerPort();
99          if (port < 0) {
100             port = 80; // Work around java.net.URL bug
101         }
102         String scheme = request.getScheme();
103         url.append(scheme);
104         url.append("://");
105         url.append(request.getServerName());
106         if ((scheme.equals("http") && (port != 80)) || (scheme.equals("https") && (port != 443))) {
107             url.append(':');
108             url.append(port);
109         }
110         url.append(request.getContextPath());
111         return url.toString();
112     }
113 }