1 /*
2 * $Id$
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21 package org.apache.struts.apps.mailreader.actions;
22
23 import java.util.Locale;
24
25 import jakarta.servlet.http.HttpServletRequest;
26 import jakarta.servlet.http.HttpServletResponse;
27 import jakarta.servlet.http.HttpSession;
28
29 import org.apache.struts.Globals;
30 import org.apache.struts.action.ActionForm;
31 import org.apache.struts.action.ActionForward;
32 import org.apache.struts.action.ActionMapping;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36
37 /**
38 * <p>
39 * Change user's Struts {@link java.util.Locale}.
40 * </p>
41 */
42 public final class LocaleAction extends BaseAction {
43 private static final long serialVersionUID = 7038993006910281561L;
44
45 /**
46 * The {@code Log} instance for this class.
47 */
48 private final static Logger LOG =
49 LoggerFactory.getLogger(LocaleAction.class);
50
51 /**
52 * <p>
53 * Return true if parameter is null or trims to empty.
54 * </p>
55 *
56 * @param string The string to text; may be null
57 * @return true if parameter is null or empty
58 */
59 private boolean isBlank(String string) {
60 return ((string == null) || (string.trim().length() == 0));
61 }
62
63 /**
64 * <p>
65 * Parameter for {@link java.util.Locale} language property. ["language"]
66 * </p>
67 */
68 private static final String LANGUAGE = "language";
69
70 /**
71 * <p>
72 * Parameter for {@link java.util.Locale} country property. ["country"]
73 * </p>
74 */
75 private static final String COUNTRY = "country";
76
77 /**
78 * <p>
79 * Parameter for response page URI. ["page"]
80 * </p>
81 */
82 private static final String PAGE = "page";
83
84 /**
85 * <p>
86 * Parameter for response forward name. ["forward"]
87 * </p>
88 */
89 private static final String FORWARD = "forward";
90
91 /**
92 * <p>
93 * Logging message if LocaleAction is missing a target parameter.
94 * </p>
95 */
96 private static final String LOCALE_LOG =
97 "LocaleAction: Missing page or forward parameter";
98
99 /**
100 * <p>
101 * Change the user's Struts {@link java.util.Locale} based on request
102 * parameters for "language", "country".
103 * After setting the Locale, control is forwarded to an URI path
104 * indicated by a "page" parameter, or a forward indicated by a
105 * "forward" parameter, or to a forward given as the mappings
106 * "parameter" property.
107 * The response location must be specified one of these ways.
108 * </p>
109 *
110 * @param mapping The ActionMapping used to select this instance
111 * @param form The optional ActionForm bean for this request (if any)
112 * @param request The HTTP request we are processing
113 * @param response The HTTP response we are creating
114 * @return An ActionForward indicate the resources that will render the
115 * response
116 * @throws Exception if an input/output error or servlet exception occurs
117 */
118 public ActionForward execute(ActionMapping mapping,
119 ActionForm form,
120 HttpServletRequest request,
121 HttpServletResponse response)
122 throws Exception {
123
124 String language = request.getParameter(LANGUAGE);
125 String country = request.getParameter(COUNTRY);
126
127 Locale locale = getLocale(request);
128
129 if ((!isBlank(language)) && (!isBlank(country))) {
130 locale = new Locale(language, country);
131 } else if (!isBlank(language)) {
132 locale = new Locale(language);
133 }
134
135 HttpSession session = request.getSession();
136 session.setAttribute(Globals.LOCALE_KEY, locale);
137
138 String target = request.getParameter(PAGE);
139 if (!isBlank(target)) {
140 return new ActionForward(target);
141 }
142
143 target = request.getParameter(FORWARD);
144 if (isBlank(target)) {
145 target = mapping.getParameter();
146 }
147 if (isBlank(target)) {
148 LOG.warn(LOCALE_LOG);
149 return null;
150 }
151 return mapping.findForward(target);
152 }
153 }