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