KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > webapp > example > LocaleAction


1 /*
2  * $Id: LocaleAction.java 157397 2005-03-14 06:20:47Z martinc $
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.webapp.example;
19
20 import java.util.Locale JavaDoc;
21
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24 import javax.servlet.http.HttpSession JavaDoc;
25
26 import org.apache.struts.Globals;
27 import org.apache.struts.action.ActionForm;
28 import org.apache.struts.action.ActionForward;
29 import org.apache.struts.action.ActionMapping;
30
31
32 /**
33  * <p>Change user's Struts {@link java.util.Locale}.</p>
34  */

35 public final class LocaleAction extends BaseAction {
36
37     /**
38      * <p>Return true if parameter is null or trims to empty.</p>
39      * @param string The string to text; may be null
40      * @return true if parameter is null or empty
41      */

42     private boolean isBlank(String JavaDoc string) {
43         return ((string==null) || (string.trim().length()==0));
44     }
45
46     /**
47      * <p>Parameter for {@link java.util.Locale} language property.
48      * ["language"]</p>
49      */

50     private static final String JavaDoc LANGUAGE = "language" ;
51
52     /**
53      * <p>Parameter for {@link java.util.Locale} country property.
54      * ["country"]</p>
55      */

56     private static final String JavaDoc COUNTRY = "country";
57
58     /**
59      * <p>Parameter for response page URI. ["page"]</p>
60      */

61     private static final String JavaDoc PAGE = "page";
62
63     /**
64      * <p>Parameter for response forward name.
65      * ["forward"]</p>
66      */

67     private static final String JavaDoc FORWARD = "forward";
68
69     /**
70      * <p>Logging message if LocaleAction is missing a target parameter.</p>
71      */

72     private static final String JavaDoc LOCALE_LOG = "LocaleAction: Missing page or forward parameter";
73
74     /**
75      * <p>
76      * Change the user's Struts {@link java.util.Locale} based on request
77      * parameters for "language", "country".
78      * After setting the Locale, control is forwarded to an URI path
79      * indicated by a "page" parameter, or a forward indicated by a
80      * "forward" parameter, or to a forward given as the mappings
81      * "parameter" property.
82      * The response location must be specified one of these ways.
83      * </p>
84      * @param mapping The ActionMapping used to select this instance
85      * @param form The optional ActionForm bean for this request (if any)
86      * @param request The HTTP request we are processing
87      * @param response The HTTP response we are creating
88      *
89      * @return An ActionForward indicate the resources that will render the response
90      * @exception Exception if an input/output error or servlet exception occurs
91      */

92     public ActionForward execute(ActionMapping mapping,
93                  ActionForm form,
94                  HttpServletRequest JavaDoc request,
95                  HttpServletResponse JavaDoc response)
96     throws Exception JavaDoc {
97
98         String JavaDoc language = request.getParameter(LANGUAGE);
99         String JavaDoc country = request.getParameter(COUNTRY);
100
101         Locale JavaDoc locale = getLocale(request);
102
103         if ((!isBlank(language)) && (!isBlank(country))) {
104             locale = new Locale JavaDoc(language, country);
105         }
106         else if (!isBlank(language)) {
107             locale = new Locale JavaDoc(language, "");
108         }
109
110         HttpSession JavaDoc session = request.getSession();
111         session.setAttribute(Globals.LOCALE_KEY, locale);
112
113         String JavaDoc target = request.getParameter(PAGE);
114         if (!isBlank(target)) return new ActionForward(target);
115
116         target = request.getParameter(FORWARD);
117         if (isBlank(target)) target = mapping.getParameter();
118         if (isBlank(target)) {
119             log.warn(LOCALE_LOG);
120             return null;
121         }
122         return mapping.findForward(target);
123     }
124 }
125
Popular Tags