1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.servlet;
24  
25  import com.liferay.portal.NoSuchLayoutException;
26  import com.liferay.portal.kernel.language.LanguageUtil;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.LocaleUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.kernel.xml.Element;
32  import com.liferay.portal.util.PortalUtil;
33  import com.liferay.portal.util.WebKeys;
34  
35  import java.io.IOException;
36  
37  import java.util.HashSet;
38  import java.util.Iterator;
39  import java.util.Locale;
40  import java.util.Set;
41  
42  import javax.servlet.RequestDispatcher;
43  import javax.servlet.ServletContext;
44  import javax.servlet.ServletException;
45  import javax.servlet.http.HttpServlet;
46  import javax.servlet.http.HttpServletRequest;
47  import javax.servlet.http.HttpServletResponse;
48  
49  import org.apache.commons.logging.Log;
50  import org.apache.commons.logging.LogFactory;
51  
52  /**
53   * <a href="I18nServlet.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Brian Wing Shun Chan
56   *
57   */
58  public class I18nServlet extends HttpServlet {
59  
60      public static Set<String> getLanguageIds() {
61          return _languageIds;
62      }
63  
64      public static void setLanguageIds(Element root) {
65          Iterator<Element> itr = root.elements("servlet-mapping").iterator();
66  
67          while (itr.hasNext()) {
68              Element el = itr.next();
69  
70              String servletName = el.elementText("servlet-name");
71  
72              if (servletName.equals("I18nServlet")) {
73                  String urlPattern = el.elementText("url-pattern");
74  
75                  String languageId = urlPattern.substring(
76                      0, urlPattern.lastIndexOf(StringPool.SLASH));
77  
78                  _languageIds.add(languageId);
79              }
80          }
81      }
82  
83      public void service(
84              HttpServletRequest request, HttpServletResponse response)
85          throws IOException, ServletException {
86  
87          try {
88              String[] i18nData = getI18nData(request);
89  
90              if (i18nData == null) {
91                  PortalUtil.sendError(
92                      HttpServletResponse.SC_NOT_FOUND,
93                      new NoSuchLayoutException(), request, response);
94              }
95              else {
96                  String languageId = i18nData[0];
97                  String redirect = i18nData[1];
98  
99                  request.setAttribute(WebKeys.I18N_LANGUAGE_ID, languageId);
100 
101                 ServletContext servletContext = getServletContext();
102 
103                 RequestDispatcher requestDispatcher =
104                     servletContext.getRequestDispatcher(redirect);
105 
106                 requestDispatcher.forward(request, response);
107             }
108         }
109         catch (Exception e) {
110             _log.error(e, e);
111 
112             PortalUtil.sendError(
113                 HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
114                 response);
115         }
116     }
117 
118     protected String[] getI18nData(HttpServletRequest request) {
119         String path = GetterUtil.getString(request.getPathInfo());
120 
121         if (Validator.isNull(path)) {
122             return null;
123         }
124 
125         String languageId = request.getServletPath();
126 
127         int pos = languageId.lastIndexOf(StringPool.SLASH);
128 
129         languageId = languageId.substring(pos + 1);
130 
131         if (_log.isDebugEnabled()) {
132             _log.debug("Language ID " + languageId);
133         }
134 
135         if (Validator.isNull(languageId)) {
136             return null;
137         }
138 
139         Locale locale = LocaleUtil.fromLanguageId(languageId);
140 
141         if (Validator.isNull(locale.getCountry())) {
142 
143             // Locales must contain the country code
144 
145             locale = LanguageUtil.getLocale(locale.getLanguage());
146 
147             languageId = LocaleUtil.toLanguageId(locale);
148         }
149 
150         String redirect = path;
151 
152         if (_log.isDebugEnabled()) {
153             _log.debug("Redirect " + redirect);
154         }
155 
156         return new String[] {languageId, redirect};
157     }
158 
159     private static Log _log = LogFactory.getLog(I18nServlet.class);
160 
161     private static Set<String> _languageIds = new HashSet<String>();
162 
163 }