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 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
39
40
41
42 public final class LocaleAction extends BaseAction {
43 private static final long serialVersionUID = 7038993006910281561L;
44
45
46
47
48 private final static Logger LOG =
49 LoggerFactory.getLogger(LocaleAction.class);
50
51
52
53
54
55
56
57
58
59 private boolean isBlank(String string) {
60 return ((string == null) || (string.trim().length() == 0));
61 }
62
63
64
65
66
67
68 private static final String LANGUAGE = "language";
69
70
71
72
73
74
75 private static final String COUNTRY = "country";
76
77
78
79
80
81
82 private static final String PAGE = "page";
83
84
85
86
87
88
89 private static final String FORWARD = "forward";
90
91
92
93
94
95
96 private static final String LOCALE_LOG =
97 "LocaleAction: Missing page or forward parameter";
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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 }