1 /* 2 * $Id$ 3 * 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 package org.apache.struts.extras.actions; 22 23 import java.util.Locale; 24 25 import jakarta.servlet.http.HttpServletRequest; 26 import jakarta.servlet.http.HttpServletResponse; 27 import jakarta.servlet.http.HttpSession; 28 29 import org.apache.commons.beanutils.PropertyUtils; 30 import org.apache.struts.Globals; 31 import org.apache.struts.action.ActionForm; 32 import org.apache.struts.action.ActionForward; 33 import org.apache.struts.action.ActionMapping; 34 import org.slf4j.Logger; 35 import org.slf4j.LoggerFactory; 36 37 /** 38 * Implementation of <strong>Action</strong> that changes the user's {@link 39 * java.util.Locale} and forwards to a page, based on request level parameters 40 * that are set (language, country, & page). 41 */ 42 public final class LocaleAction extends BaseAction { 43 private static final long serialVersionUID = 9133617067776601112L; 44 45 /** 46 * The {@code Log} instance for this class. 47 */ 48 private transient final Logger log = 49 LoggerFactory.getLogger(this.getClass().getName()); 50 51 /** 52 * <p> Change the user's {@link java.util.Locale} based on {@link 53 * ActionForm} properties. </p> <p> This <code>Action</code> looks for 54 * <code>language</code> and <code>country</code> properties on the given 55 * form, constructs an appropriate Locale object, and sets it as the 56 * Struts Locale for this user's session. Any <code>ActionForm</code>, 57 * including a {@link org.apache.struts.action.DynaActionForm}, may be 58 * used. </p> <p> If a <code>page</code> property is also provided, then 59 * after setting the Locale, control is forwarded to that URI path. 60 * Otherwise, control is forwarded to "success". </p> 61 * 62 * @param mapping The ActionMapping used to select this instance 63 * @param form The optional ActionForm bean for this request (if any) 64 * @param request The HTTP request we are processing 65 * @param response The HTTP response we are creating 66 * @return Action to forward to 67 * @throws Exception if an input/output error or servlet exception occurs 68 */ 69 public ActionForward execute(ActionMapping mapping, ActionForm form, 70 HttpServletRequest request, HttpServletResponse response) 71 throws Exception { 72 // Extract attributes we will need 73 HttpSession session = request.getSession(); 74 Locale locale = getLocale(request); 75 76 String language = null; 77 String country = null; 78 String page = null; 79 80 try { 81 language = 82 (String) PropertyUtils.getSimpleProperty(form, "language"); 83 country = (String) PropertyUtils.getSimpleProperty(form, "country"); 84 page = (String) PropertyUtils.getSimpleProperty(form, "page"); 85 } catch (Exception e) { 86 log.error(e.getMessage(), e); 87 } 88 89 boolean isLanguage = ((language != null) && (language.length() > 0)); 90 boolean isCountry = ((country != null) && (country.length() > 0)); 91 92 if ((isLanguage) && (isCountry)) { 93 locale = new Locale(language, country); 94 } else if (isLanguage) { 95 locale = new Locale(language); 96 } 97 98 session.setAttribute(Globals.LOCALE_KEY, locale); 99 100 if (null == page) { 101 return mapping.findForward("success"); 102 } else { 103 return new ActionForward(page); 104 } 105 } 106 }