1 19 20 package com.sslexplorer.language.actions; 21 22 import java.util.Locale ; 23 import java.util.StringTokenizer ; 24 25 import javax.servlet.http.Cookie ; 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 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 48 public class SelectLanguageAction extends DefaultAction { 49 50 final static Log log = LogFactory.getLog(SelectLanguageAction.class); 51 52 60 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 61 throws Exception { 62 String 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 localeCode = request.getParameter("locale"); 67 if(localeCode == null) { 68 throw new CoreException(ErrorConstants.ERR_MISSING_REQUEST_PARAMETER, ErrorConstants.CATEGORY_NAME, "locale"); 69 } 70 71 75 StringTokenizer t = new StringTokenizer (localeCode); 76 String locale = t.nextToken(); 77 78 String country = ""; 80 String variant = ""; 81 String 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 Locale l = new Locale (lang, country, variant); 95 request.getSession().setAttribute(Globals.LOCALE_KEY, l); 96 Cookie cookie = new Cookie (System.getProperty("sslexplorer.cookie", "SSLX_SSESHID") + "_LANG", locale.toString()); 97 cookie.setMaxAge(60 * 60 * 24 * 7); cookie.setPath("/"); 99 cookie.setSecure(true); 100 response.addCookie(cookie); 101 return referer == null ? mapping.findForward("home") : new ActionForward(referer, true); 102 } 103 104 112 public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { 113 return SessionInfo.ALL_CONTEXTS; 114 } 115 116 } 117 | Popular Tags |