001: /*
002: * $Id: LocaleAction.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.apps.mailreader.actions;
022:
023: import org.apache.struts.Globals;
024: import org.apache.struts.action.ActionForm;
025: import org.apache.struts.action.ActionForward;
026: import org.apache.struts.action.ActionMapping;
027:
028: import javax.servlet.http.HttpServletRequest;
029: import javax.servlet.http.HttpServletResponse;
030: import javax.servlet.http.HttpSession;
031: import java.util.Locale;
032:
033: /**
034: * <p>
035: * Change user's Struts {@link java.util.Locale}.
036: * </p>
037: */
038: public final class LocaleAction extends BaseAction {
039:
040: /**
041: * <p>
042: * Return true if parameter is null or trims to empty.
043: * </p>
044: *
045: * @param string The string to text; may be null
046: * @return true if parameter is null or empty
047: */
048: private boolean isBlank(String string) {
049: return ((string == null) || (string.trim().length() == 0));
050: }
051:
052: /**
053: * <p>
054: * Parameter for {@link java.util.Locale} language property. ["language"]
055: * </p>
056: */
057: private static final String LANGUAGE = "language";
058:
059: /**
060: * <p>
061: * Parameter for {@link java.util.Locale} country property. ["country"]
062: * </p>
063: */
064: private static final String COUNTRY = "country";
065:
066: /**
067: * <p>
068: * Parameter for response page URI. ["page"]
069: * </p>
070: */
071: private static final String PAGE = "page";
072:
073: /**
074: * <p>
075: * Parameter for response forward name. ["forward"]
076: * </p>
077: */
078: private static final String FORWARD = "forward";
079:
080: /**
081: * <p>
082: * Logging message if LocaleAction is missing a target parameter.
083: * </p>
084: */
085: private static final String LOCALE_LOG = "LocaleAction: Missing page or forward parameter";
086:
087: /**
088: * <p>
089: * Change the user's Struts {@link java.util.Locale} based on request
090: * parameters for "language", "country".
091: * After setting the Locale, control is forwarded to an URI path
092: * indicated by a "page" parameter, or a forward indicated by a
093: * "forward" parameter, or to a forward given as the mappings
094: * "parameter" property.
095: * The response location must be specified one of these ways.
096: * </p>
097: *
098: * @param mapping The ActionMapping used to select this instance
099: * @param form The optional ActionForm bean for this request (if any)
100: * @param request The HTTP request we are processing
101: * @param response The HTTP response we are creating
102: * @return An ActionForward indicate the resources that will render the
103: * response
104: * @throws Exception if an input/output error or servlet exception occurs
105: */
106: public ActionForward execute(ActionMapping mapping,
107: ActionForm form, HttpServletRequest request,
108: HttpServletResponse response) throws Exception {
109:
110: String language = request.getParameter(LANGUAGE);
111: String country = request.getParameter(COUNTRY);
112:
113: Locale locale = getLocale(request);
114:
115: if ((!isBlank(language)) && (!isBlank(country))) {
116: locale = new Locale(language, country);
117: } else if (!isBlank(language)) {
118: locale = new Locale(language, "");
119: }
120:
121: HttpSession session = request.getSession();
122: session.setAttribute(Globals.LOCALE_KEY, locale);
123:
124: String target = request.getParameter(PAGE);
125: if (!isBlank(target)) {
126: return new ActionForward(target);
127: }
128:
129: target = request.getParameter(FORWARD);
130: if (isBlank(target)) {
131: target = mapping.getParameter();
132: }
133: if (isBlank(target)) {
134: log.warn(LOCALE_LOG);
135: return null;
136: }
137: return mapping.findForward(target);
138: }
139: }
|