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