View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.portlets.localeselector;
18  
19  import java.io.IOException;
20  import java.util.Locale;
21  import java.util.prefs.Preferences;
22  
23  import javax.portlet.ActionRequest;
24  import javax.portlet.ActionResponse;
25  import javax.portlet.GenericPortlet;
26  import javax.portlet.PortletConfig;
27  import javax.portlet.PortletContext;
28  import javax.portlet.PortletException;
29  import javax.portlet.PortletRequestDispatcher;
30  import javax.portlet.PortletSession;
31  import javax.portlet.RenderRequest;
32  import javax.portlet.RenderResponse;
33  
34  import org.apache.jetspeed.CommonPortletServices;
35  import org.apache.jetspeed.PortalReservedParameters;
36  import org.apache.jetspeed.request.RequestContext;
37  import org.apache.jetspeed.security.SecurityException;
38  import org.apache.jetspeed.security.User;
39  import org.apache.jetspeed.security.UserManager;
40  import org.apache.jetspeed.util.JetspeedLocale;
41  
42  /***
43   * This is the portlet to select user's preferred locale.
44   * 
45   * @author <a href="mailto:shinsuke@yahoo.co.jp">Shinsuke Sugaya</a>
46   * @version $Id: LocaleSelectorPortlet.java 348264 2005-11-22 22:06:45Z taylor $
47   */
48  public class LocaleSelectorPortlet extends GenericPortlet
49  {
50      public static final String PREFERED_LOCALE_SESSION_KEY = "org.apache.jetspeed.prefered.locale";
51  
52      private UserManager userManager;
53      
54      /* (non-Javadoc)
55       * @see javax.portlet.Portlet#init(javax.portlet.PortletConfig)
56       */
57      public void init(PortletConfig config) throws PortletException
58      {
59          super.init(config);
60          userManager = (UserManager)getPortletContext().getAttribute(CommonPortletServices.CPS_USER_MANAGER_COMPONENT);
61          if (null == userManager)
62          {
63              throw new PortletException("Failed to find the User Manager on portlet initialization");
64          }
65      }
66  
67      /* (non-Javadoc)
68       * @see javax.portlet.GenericPortlet#doView(javax.portlet.RenderRequest, javax.portlet.RenderResponse)
69       */
70      protected void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException
71      {
72          PortletContext context = getPortletContext();
73  
74          Locale locale = request.getLocale();
75          if (locale == null)
76          {
77              locale = Locale.getDefault();
78          }
79          request.setAttribute("currentLocale", locale.toString());
80  
81          PortletRequestDispatcher rd = context.getRequestDispatcher("/WEB-INF/view/locale-list.jsp");
82          rd.include(request, response);
83      }
84  
85      /* (non-Javadoc)
86       * @see javax.portlet.Portlet#processAction(javax.portlet.ActionRequest, javax.portlet.ActionResponse)
87       */
88      public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException
89      {
90  
91          PortletSession session = request.getPortletSession();
92          String language = request.getParameter(PREFERED_LOCALE_SESSION_KEY);
93  
94          if (language != null)
95          {
96              String[] localeArray = language.split("[-|_]");
97              String country = "";
98              String variant = "";
99              for (int i = 0; i < localeArray.length; i++)
100             {
101                 if (i == 0)
102                 {
103                     language = localeArray[i];
104                 }
105                 else if (i == 1)
106                 {
107                     country = localeArray[i];
108                 }
109                 else if (i == 2)
110                 {
111                     variant = localeArray[i];
112                 }
113             }
114 
115             Locale preferedLocale = new Locale(language, country, variant);
116 
117             if (request.getRemoteUser() != null)
118             {
119                 // Set the prefered locale to user's perferences(persistent storage) if not anon user
120                 try
121                 {
122                     User user = userManager.getUser(request.getRemoteUser());
123                     // TODO if preferred lang or locale is defined in PLT.D, it's better to use it
124                     Preferences prefs = user.getPreferences();
125                     prefs.put(PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE, JetspeedLocale
126                             .convertLocaleToString(preferedLocale));
127                 }
128                 catch (SecurityException e)
129                 {
130                     // TODO Auto-generated catch block
131                     e.printStackTrace();
132                 }
133             }
134 
135             session.setAttribute(PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE, preferedLocale,
136                     PortletSession.APPLICATION_SCOPE);
137             RequestContext requestContext = (RequestContext) request
138                     .getAttribute(PortalReservedParameters.REQUEST_CONTEXT_ATTRIBUTE);
139             requestContext.setLocale(preferedLocale);
140             requestContext.setSessionAttribute(PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE, preferedLocale);
141         }
142 
143         return;
144     }
145 
146 }