View Javadoc

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