View Javadoc
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  
22  
23  package org.apache.struts.webapp.validator;
24  
25  import java.util.Locale;
26  
27  import jakarta.servlet.http.HttpServletRequest;
28  import jakarta.servlet.http.HttpServletResponse;
29  import jakarta.servlet.http.HttpSession;
30  
31  import org.apache.commons.beanutils.PropertyUtils;
32  import org.apache.struts.Globals;
33  import org.apache.struts.action.Action;
34  import org.apache.struts.action.ActionForm;
35  import org.apache.struts.action.ActionForward;
36  import org.apache.struts.action.ActionMapping;
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  
40  
41  /**
42   * Implementation of <strong>Action</strong> that changes the user's
43   * {@link java.util.Locale} and forwards to a page, based on request level
44   * parameters that are set  (language, country, &amp; page).
45   *
46  */
47  public final class LocaleAction extends Action {
48      private static final long serialVersionUID = 8834021192169331239L;
49  
50      /**
51       * The {@code Log} instance for this class.
52       */
53      private final static Logger LOG =
54          LoggerFactory.getLogger(LocaleAction.class);
55  
56      /**
57       * <p>
58       * Change the user's {@link java.util.Locale} based on {@link ActionForm}
59       * properties.
60       * </p>
61       * <p>
62       * This <code>Action</code> looks for <code>language</code> and
63       * <code>country</code> properties on the given form, constructs an
64       * appropriate Locale object, and sets it as the Struts Locale for this
65       * user's session.
66       * Any <code>ActionForm</code>, including a
67       * {@link org.apache.struts.action.DynaActionForm}, may be used.
68       * </p>
69       * <p>
70       * If a <code>page</code> property is also provided, then after
71       * setting the Locale, control is forwarded to that URI path.
72       * Otherwise, control is forwarded to "success".
73       * </p>
74       *
75       * @param mapping The ActionMapping used to select this instance
76       * @param form The optional ActionForm bean for this request (if any)
77       * @param request The HTTP request we are processing
78       * @param response The HTTP response we are creating
79       *
80       * @return Action to forward to
81       * @exception java.lang.Exception if an input/output error or servlet exception occurs
82       */
83      public ActionForward execute(ActionMapping mapping,
84                   ActionForm form,
85                   HttpServletRequest request,
86                   HttpServletResponse response)
87      throws Exception {
88  
89      // Extract attributes we will need
90      HttpSession session = request.getSession();
91      Locale locale = getLocale(request);
92  
93      String language = null;
94      String country = null;
95      String page = null;
96  
97      try {
98              language = (String)
99                PropertyUtils.getSimpleProperty(form, "language");
100             country = (String)
101               PropertyUtils.getSimpleProperty(form, "country");
102             page = (String)
103               PropertyUtils.getSimpleProperty(form, "page");
104         } catch (Exception e) {
105            LOG.error(e.getMessage(), e);
106         }
107 
108         if ((language != null && language.length() > 0) &&
109             (country != null && country.length() > 0)) {
110            locale = new Locale(language, country);
111         } else if (language != null && language.length() > 0) {
112            locale = new Locale(language);
113     }
114 
115         session.setAttribute(Globals.LOCALE_KEY, locale);
116 
117         if (null==page || page.isEmpty()) return mapping.findForward("success");
118         else return new ActionForward(page);
119 
120     }
121 }