KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > language > actions > SelectLanguageAction


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.language.actions;
21
22 import java.util.Locale JavaDoc;
23 import java.util.StringTokenizer JavaDoc;
24
25 import javax.servlet.http.Cookie JavaDoc;
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.struts.Globals;
32 import org.apache.struts.action.ActionForm;
33 import org.apache.struts.action.ActionForward;
34 import org.apache.struts.action.ActionMapping;
35
36 import com.sslexplorer.core.CoreException;
37 import com.sslexplorer.core.CoreUtil;
38 import com.sslexplorer.core.ErrorConstants;
39 import com.sslexplorer.core.actions.DefaultAction;
40 import com.sslexplorer.security.SessionInfo;
41 import com.sslexplorer.vfs.webdav.DAVUtilities;
42
43 /**
44  * Action to set the default per session locale.
45  *
46  * @author Brett Smith <brett@3sp.com>
47  */

48 public class SelectLanguageAction extends DefaultAction {
49
50     final static Log log = LogFactory.getLog(SelectLanguageAction.class);
51
52     /*
53      * (non-Javadoc)
54      *
55      * @see com.sslexplorer.core.actions.AuthenticatedAction#onExecute(org.apache.struts.action.ActionMapping,
56      * org.apache.struts.action.ActionForm,
57      * javax.servlet.http.HttpServletRequest,
58      * javax.servlet.http.HttpServletResponse)
59      */

60     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
61                     throws Exception JavaDoc {
62         String JavaDoc referer = DAVUtilities.encodePath(CoreUtil.getRequestReferer(request), false);
63         if(referer == null) {
64             throw new CoreException(ErrorConstants.ERR_MISSING_REQUEST_PARAMETER, ErrorConstants.CATEGORY_NAME, "referer");
65         }
66         String JavaDoc localeCode = request.getParameter("locale");
67         if(localeCode == null) {
68             throw new CoreException(ErrorConstants.ERR_MISSING_REQUEST_PARAMETER, ErrorConstants.CATEGORY_NAME, "locale");
69         }
70         
71         /* Tokenize the locale parameter so we only get the first line. This prevents
72          * a header injection exploit as the (not validated) locale gets added as
73          * a cookie.
74          */

75         StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(localeCode);
76         String JavaDoc locale = t.nextToken();
77         
78         // Parse the locale code
79
String JavaDoc country = "";
80         String JavaDoc variant = "";
81         String JavaDoc lang = locale;
82         int idx = locale.indexOf("_");
83         if(idx != -1) {
84             country = lang.substring(idx + 1);
85             lang = lang.substring(0, idx);
86         }
87         idx = country.indexOf('_');
88         if(idx != -1) {
89             variant = country.substring(idx + 1);
90             country = country.substring(0, idx);
91         }
92         
93         // Store the new locale in the session and set a persistant cookie
94
Locale JavaDoc l = new Locale JavaDoc(lang, country, variant);
95         request.getSession().setAttribute(Globals.LOCALE_KEY, l);
96         Cookie JavaDoc cookie = new Cookie JavaDoc(System.getProperty("sslexplorer.cookie", "SSLX_SSESHID") + "_LANG", locale.toString());
97         cookie.setMaxAge(60 * 60 * 24 * 7); // a week
98
cookie.setPath("/");
99         cookie.setSecure(true);
100         response.addCookie(cookie);
101         return referer == null ? mapping.findForward("home") : new ActionForward(referer, true);
102     }
103
104     /*
105      * (non-Javadoc)
106      *
107      * @see com.sslexplorer.core.actions.CoreAction#getNavigationContext(org.apache.struts.action.ActionMapping,
108      * org.apache.struts.action.ActionForm,
109      * javax.servlet.http.HttpServletRequest,
110      * javax.servlet.http.HttpServletResponse)
111      */

112     public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
113         return SessionInfo.ALL_CONTEXTS;
114     }
115
116 }
117
Popular Tags