1 package net.ramapuram.thomas.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
12
13 public final class RequestUtil {
14 private static final Log log = LogFactory.getLog(RequestUtil.class);
15
16
17
18
19 private RequestUtil() {
20 }
21
22
23
24
25
26
27
28
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);
40
41 response.addCookie(cookie);
42 }
43
44
45
46
47
48
49
50
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
72
73
74
75
76
77 public static void deleteCookie(HttpServletResponse response,
78 Cookie cookie, String path) {
79 if (cookie != null) {
80
81 cookie.setMaxAge(0);
82 cookie.setPath(path);
83 response.addCookie(cookie);
84 }
85 }
86
87
88
89
90
91
92
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;
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 }