1 package org.appfuse.webapp.util; 2 3 import javax.servlet.http.Cookie ; 4 import javax.servlet.http.HttpServletRequest ; 5 import javax.servlet.http.HttpServletResponse ; 6 7 import org.apache.commons.logging.Log; 8 import org.apache.commons.logging.LogFactory; 9 10 11 14 public class RequestUtil { 15 private transient static Log log = LogFactory.getLog(RequestUtil.class); 16 17 25 public static void setCookie(HttpServletResponse response, String name, 26 String value, String path) { 27 if (log.isDebugEnabled()) { 28 log.debug("Setting cookie '" + name + "' on path '" + path + "'"); 29 } 30 31 Cookie cookie = new Cookie (name, value); 32 cookie.setSecure(false); 33 cookie.setPath(path); 34 cookie.setMaxAge(3600 * 24 * 30); 36 response.addCookie(cookie); 37 } 38 39 47 public static Cookie getCookie(HttpServletRequest request, String name) { 48 Cookie [] cookies = request.getCookies(); 49 Cookie returnCookie = null; 50 51 if (cookies == null) { 52 return returnCookie; 53 } 54 55 for (int i = 0; i < cookies.length; i++) { 56 Cookie thisCookie = cookies[i]; 57 58 if (thisCookie.getName().equals(name)) { 59 if (!thisCookie.getValue().equals("")) { 61 returnCookie = thisCookie; 62 63 break; 64 } 65 } 66 } 67 68 return returnCookie; 69 } 70 71 78 public static void deleteCookie(HttpServletResponse response, 79 Cookie cookie, String path) { 80 if (cookie != null) { 81 cookie.setMaxAge(0); 83 cookie.setPath(path); 84 response.addCookie(cookie); 85 } 86 } 87 88 92 public static String getAppURL(HttpServletRequest request) { 93 StringBuffer url = new StringBuffer (); 94 int port = request.getServerPort(); 95 if (port < 0) { 96 port = 80; } 98 String scheme = request.getScheme(); 99 url.append(scheme); 100 url.append("://"); 101 url.append(request.getServerName()); 102 if ((scheme.equals("http") && (port != 80)) || (scheme.equals("https") && (port != 443))) { 103 url.append(':'); 104 url.append(port); 105 } 106 url.append(request.getContextPath()); 107 return url.toString(); 108 } 109 } 110 | Popular Tags |