View Javadoc

1   /*
2    * $Id: LocaleAction.java 421489 2006-07-13 03:45:27Z wsmoak $
3    *
4    * Copyright 2000-2004 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.apps.mailreader.actions;
19  
20  import org.apache.struts.Globals;
21  import org.apache.struts.action.ActionForm;
22  import org.apache.struts.action.ActionForward;
23  import org.apache.struts.action.ActionMapping;
24  
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  import javax.servlet.http.HttpSession;
28  import java.util.Locale;
29  
30  
31  /***
32   * <p>
33   * Change user's Struts {@link java.util.Locale}.
34   * </p>
35   */
36  public final class LocaleAction extends BaseAction {
37  
38      /***
39       * <p>
40       * Return true if parameter is null or trims to empty.
41       * </p>
42       *
43       * @param string The string to text; may be  null
44       * @return true if parameter is null or empty
45       */
46      private boolean isBlank(String string) {
47          return ((string == null) || (string.trim().length() == 0));
48      }
49  
50      /***
51       * <p>
52       * Parameter for {@link java.util.Locale} language property. ["language"]
53       * </p>
54       */
55      private static final String LANGUAGE = "language";
56  
57      /***
58       * <p>
59       * Parameter for {@link java.util.Locale} country property. ["country"]
60       * </p>
61       */
62      private static final String COUNTRY = "country";
63  
64      /***
65       * <p>
66       * Parameter for response page URI. ["page"]
67       * </p>
68       */
69      private static final String PAGE = "page";
70  
71      /***
72       * <p>
73       * Parameter for response forward name. ["forward"]
74       * </p>
75       */
76      private static final String FORWARD = "forward";
77  
78      /***
79       * <p>
80       * Logging message if LocaleAction is missing a target parameter.
81       * </p>
82       */
83      private static final String LOCALE_LOG =
84              "LocaleAction: Missing page or forward parameter";
85  
86      /***
87       * <p>
88       * Change the user's Struts {@link java.util.Locale} based on request
89       * parameters for "language", "country".
90       * After setting the Locale, control is forwarded to an URI path
91       * indicated by a "page" parameter, or a forward indicated by a
92       * "forward" parameter, or to a forward given as the mappings
93       * "parameter" property.
94       * The response location must be specified one of these ways.
95       * </p>
96       *
97       * @param mapping  The ActionMapping used to select this instance
98       * @param form     The optional ActionForm bean for this request (if any)
99       * @param request  The HTTP request we are processing
100      * @param response The HTTP response we are creating
101      * @return An ActionForward indicate the resources that will render the
102      *         response
103      * @throws Exception if an input/output error or servlet exception occurs
104      */
105     public ActionForward execute(ActionMapping mapping,
106                                  ActionForm form,
107                                  HttpServletRequest request,
108                                  HttpServletResponse response)
109             throws Exception {
110 
111         String language = request.getParameter(LANGUAGE);
112         String country = request.getParameter(COUNTRY);
113 
114         Locale locale = getLocale(request);
115 
116         if ((!isBlank(language)) && (!isBlank(country))) {
117             locale = new Locale(language, country);
118         } else if (!isBlank(language)) {
119             locale = new Locale(language, "");
120         }
121 
122         HttpSession session = request.getSession();
123         session.setAttribute(Globals.LOCALE_KEY, locale);
124 
125         String target = request.getParameter(PAGE);
126         if (!isBlank(target)) {
127             return new ActionForward(target);
128         }
129 
130         target = request.getParameter(FORWARD);
131         if (isBlank(target)) {
132             target = mapping.getParameter();
133         }
134         if (isBlank(target)) {
135             log.warn(LOCALE_LOG);
136             return null;
137         }
138         return mapping.findForward(target);
139     }
140 }