View Javadoc

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