1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 package org.objectledge.i18n;
30
31 import java.security.Principal;
32
33 import javax.servlet.http.Cookie;
34
35 import org.objectledge.authentication.AuthenticationContext;
36 import org.objectledge.context.Context;
37 import org.objectledge.pipeline.Valve;
38 import org.objectledge.utils.StringUtils;
39 import org.objectledge.web.HttpContext;
40
41 /***
42 * Base i18n processing valve with utility methods.
43 *
44 * @author <a href="mailto:dgajda@caltha.pl">Damian Gajda</a>
45 * @version $Id: AbstractI18nValve.java,v 1.3 2005/07/25 12:51:43 rafal Exp $
46 */
47 public abstract class AbstractI18nValve
48 implements Valve
49 {
50 /***
51 * Creates a base name of the cookie.
52 * @param context the request context.
53 * @return the basic part of cookie key.
54 */
55 protected String getCookieKeyBase(Context context)
56 {
57 AuthenticationContext authenticationContext =
58 AuthenticationContext.getAuthenticationContext(context);
59
60
61
62 String cookieKey = ".anonymous";
63 if(authenticationContext != null)
64 {
65 Principal principal = authenticationContext.getUserPrincipal();
66 if (principal != null && principal.getName() != null)
67 {
68 cookieKey = "." + StringUtils.cookieNameSafeString(principal.getName());
69 }
70 }
71 return cookieKey;
72 }
73
74 /***
75 * Gets the cookie from the HTTP request.
76 *
77 * @param httpContext the HTTPContext.
78 * @param cookieName the name of the cookie.
79 * @return the cookie object.
80 */
81 protected Cookie getCookie(HttpContext httpContext, String cookieName)
82 {
83 String value = null;
84 Cookie[] cookies = httpContext.getRequest().getCookies();
85 if (cookies != null)
86 {
87 for (int i = 0; i < cookies.length; i++)
88 {
89 if (cookies[i].getName().equals(cookieName))
90 {
91 return cookies[i];
92 }
93 }
94 }
95 return null;
96 }
97
98 /***
99 * Sets the cookie vaid for one year.
100 *
101 * @param httpContext the HttpContext.
102 * @param name name of the cookie.
103 * @param value value of the cookie.
104 */
105 protected void setCookie(HttpContext httpContext, String name, String value)
106 {
107 Cookie cookie = new Cookie(name, value);
108 cookie.setMaxAge(3600 * 24 * 365);
109 cookie.setPath(httpContext.getRequest().getContextPath() +
110 httpContext.getRequest().getServletPath());
111 httpContext.getResponse().addCookie(cookie);
112 }
113 }