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.actions;
022:
023: import org.apache.commons.beanutils.PropertyUtils;
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.apache.struts.Globals;
027: import org.apache.struts.action.ActionForm;
028: import org.apache.struts.action.ActionForward;
029: import org.apache.struts.action.ActionMapping;
030:
031: import javax.servlet.http.HttpServletRequest;
032: import javax.servlet.http.HttpServletResponse;
033: import javax.servlet.http.HttpSession;
034:
035: import java.util.Locale;
036:
037: /**
038: * Implementation of <strong>Action</strong> that changes the user's {@link
039: * java.util.Locale} and forwards to a page, based on request level parameters
040: * that are set (language, country, & page).
041: */
042: public final class LocaleAction extends BaseAction {
043: /**
044: * Commons Logging instance.
045: */
046: private Log log = LogFactory.getFactory().getInstance(
047: this .getClass().getName());
048:
049: /**
050: * <p> Change the user's {@link java.util.Locale} based on {@link
051: * ActionForm} properties. </p> <p> This <code>Action</code> looks for
052: * <code>language</code> and <code>country</code> properties on the given
053: * form, constructs an appropriate Locale object, and sets it as the
054: * Struts Locale for this user's session. Any <code>ActionForm</code>,
055: * including a {@link org.apache.struts.action.DynaActionForm}, may be
056: * used. </p> <p> If a <code>page</code> property is also provided, then
057: * after setting the Locale, control is forwarded to that URI path.
058: * Otherwise, control is forwarded to "success". </p>
059: *
060: * @param mapping The ActionMapping used to select this instance
061: * @param form The optional ActionForm bean for this request (if any)
062: * @param request The HTTP request we are processing
063: * @param response The HTTP response we are creating
064: * @return Action to forward to
065: * @throws Exception if an input/output error or servlet exception occurs
066: */
067: public ActionForward execute(ActionMapping mapping,
068: ActionForm form, HttpServletRequest request,
069: HttpServletResponse response) throws Exception {
070: // Extract attributes we will need
071: HttpSession session = request.getSession();
072: Locale locale = getLocale(request);
073:
074: String language = null;
075: String country = null;
076: String page = null;
077:
078: try {
079: language = (String) PropertyUtils.getSimpleProperty(form,
080: "language");
081: country = (String) PropertyUtils.getSimpleProperty(form,
082: "country");
083: page = (String) PropertyUtils.getSimpleProperty(form,
084: "page");
085: } catch (Exception e) {
086: log.error(e.getMessage(), e);
087: }
088:
089: boolean isLanguage = ((language != null) && (language.length() > 0));
090: boolean isCountry = ((country != null) && (country.length() > 0));
091:
092: if ((isLanguage) && (isCountry)) {
093: locale = new java.util.Locale(language, country);
094: } else if (isLanguage) {
095: locale = new java.util.Locale(language, "");
096: }
097:
098: session.setAttribute(Globals.LOCALE_KEY, locale);
099:
100: if (null == page) {
101: return mapping.findForward("success");
102: } else {
103: return new ActionForward(page);
104: }
105: }
106: }
|