View Javadoc
1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  package com.gisgraphy.webapp.util;
24  
25  import javax.servlet.http.Cookie;
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  /**
33   * Convenience class for setting and retrieving cookies.
34   */
35  public final class RequestUtil {
36      private static final Log log = LogFactory.getLog(RequestUtil.class);
37  
38      /**
39       * Checkstyle rule: utility classes should not have public constructor
40       */
41      private RequestUtil() {
42      }
43  
44      /**
45       * Convenience method to set a cookie
46       * 
47       * @param response
48       *                the current response
49       * @param name
50       *                the name of the cookie
51       * @param value
52       *                the value of the cookie
53       * @param path
54       *                the path to set it on
55       */
56      public static void setCookie(HttpServletResponse response, String name,
57  	    String value, String path) {
58  	if (log.isDebugEnabled()) {
59  	    log.debug("Setting cookie '" + name + "' on path '" + path + "'");
60  	}
61  
62  	Cookie cookie = new Cookie(name, value);
63  	cookie.setSecure(false);
64  	cookie.setPath(path);
65  	cookie.setMaxAge(3600 * 24 * 30); // 30 days
66  
67  	response.addCookie(cookie);
68      }
69  
70      /**
71       * Convenience method to get a cookie by name
72       * 
73       * @param request
74       *                the current request
75       * @param name
76       *                the name of the cookie to find
77       * @return the cookie (if found), null if not found
78       */
79      public static Cookie getCookie(HttpServletRequest request, String name) {
80  	Cookie[] cookies = request.getCookies();
81  	Cookie returnCookie = null;
82  
83  	if (cookies == null) {
84  	    return returnCookie;
85  	}
86  
87  	for (Cookie thisCookie : cookies) {
88  	    if (thisCookie.getName().equals(name)) {
89  		// cookies with no value do me no good!
90  		if (!thisCookie.getValue().equals("")) {
91  		    returnCookie = thisCookie;
92  
93  		    break;
94  		}
95  	    }
96  	}
97  
98  	return returnCookie;
99      }
100 
101     /**
102      * Convenience method for deleting a cookie by name
103      * 
104      * @param response
105      *                the current web response
106      * @param cookie
107      *                the cookie to delete
108      * @param path
109      *                the path on which the cookie was set (i.e. /appfuse)
110      */
111     public static void deleteCookie(HttpServletResponse response,
112 	    Cookie cookie, String path) {
113 	if (cookie != null) {
114 	    // Delete the cookie by setting its maximum age to zero
115 	    cookie.setMaxAge(0);
116 	    cookie.setPath(path);
117 	    response.addCookie(cookie);
118 	}
119     }
120 
121     /**
122      * Convenience method to get the application's URL based on request
123      * variables.
124      * 
125      * @param request
126      *                the current request
127      * @return URL to application
128      */
129     public static String getAppURL(HttpServletRequest request) {
130 	StringBuffer url = new StringBuffer();
131 	int port = request.getServerPort();
132 	if (port < 0) {
133 	    port = 80; // Work around java.net.URL bug
134 	}
135 	String scheme = request.getScheme();
136 	url.append(scheme);
137 	url.append("://");
138 	url.append(request.getServerName());
139 	if ((scheme.equals("http") && (port != 80))
140 		|| (scheme.equals("https") && (port != 443))) {
141 	    url.append(':');
142 	    url.append(port);
143 	}
144 	url.append(request.getContextPath());
145 	return url.toString();
146     }
147 }