View Javadoc

1   // 
2   // Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. 
3   // All rights reserved. 
4   // 
5   // Redistribution and use in source and binary forms, with or without modification,  
6   // are permitted provided that the following conditions are met: 
7   //  
8   // * Redistributions of source code must retain the above copyright notice,  
9   //   this list of conditions and the following disclaimer. 
10  // * Redistributions in binary form must reproduce the above copyright notice,  
11  //   this list of conditions and the following disclaimer in the documentation  
12  //   and/or other materials provided with the distribution. 
13  // * Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.  
14  //   nor the names of its contributors may be used to endorse or promote products  
15  //   derived from this software without specific prior written permission. 
16  // 
17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  
18  // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  
19  // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
20  // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,  
21  // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  
22  // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
23  // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
24  // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  
25  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  
26  // POSSIBILITY OF SUCH DAMAGE. 
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          // set up cookie keys - neccessary for browsers with multiple
61          // users on a single user system - for instance Win95/98
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 }