1   /**
2    * Copyright (c) 2000-2009 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.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.kernel.util.GetterUtil;
30  import com.liferay.portal.kernel.util.LocaleUtil;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.kernel.util.Validator;
33  import com.liferay.portal.kernel.xml.Element;
34  import com.liferay.portal.util.PortalUtil;
35  import com.liferay.portal.util.WebKeys;
36  
37  import java.io.IOException;
38  
39  import java.util.HashSet;
40  import java.util.Iterator;
41  import java.util.Locale;
42  import java.util.Set;
43  
44  import javax.servlet.RequestDispatcher;
45  import javax.servlet.ServletContext;
46  import javax.servlet.ServletException;
47  import javax.servlet.http.HttpServlet;
48  import javax.servlet.http.HttpServletRequest;
49  import javax.servlet.http.HttpServletResponse;
50  
51  /**
52   * <a href="I18nServlet.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class I18nServlet extends HttpServlet {
58  
59      public static Set<String> getLanguageIds() {
60          return _languageIds;
61      }
62  
63      public static void setLanguageIds(Element root) {
64          Iterator<Element> itr = root.elements("servlet-mapping").iterator();
65  
66          while (itr.hasNext()) {
67              Element el = itr.next();
68  
69              String servletName = el.elementText("servlet-name");
70  
71              if (servletName.equals("I18nServlet")) {
72                  String urlPattern = el.elementText("url-pattern");
73  
74                  String languageId = urlPattern.substring(
75                      0, urlPattern.lastIndexOf(StringPool.SLASH));
76  
77                  _languageIds.add(languageId);
78              }
79          }
80      }
81  
82      public void service(
83              HttpServletRequest request, HttpServletResponse response)
84          throws IOException, ServletException {
85  
86          try {
87              String[] i18nData = getI18nData(request);
88  
89              if (i18nData == null) {
90                  PortalUtil.sendError(
91                      HttpServletResponse.SC_NOT_FOUND,
92                      new NoSuchLayoutException(), request, response);
93              }
94              else {
95                  String languageId = i18nData[0];
96                  String redirect = i18nData[1];
97  
98                  request.setAttribute(WebKeys.I18N_LANGUAGE_ID, languageId);
99  
100                 ServletContext servletContext = getServletContext();
101 
102                 RequestDispatcher requestDispatcher =
103                     servletContext.getRequestDispatcher(redirect);
104 
105                 requestDispatcher.forward(request, response);
106             }
107         }
108         catch (Exception e) {
109             _log.error(e, e);
110 
111             PortalUtil.sendError(
112                 HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
113                 response);
114         }
115     }
116 
117     protected String[] getI18nData(HttpServletRequest request) {
118         String path = GetterUtil.getString(request.getPathInfo());
119 
120         if (Validator.isNull(path)) {
121             return null;
122         }
123 
124         String languageId = request.getServletPath();
125 
126         int pos = languageId.lastIndexOf(StringPool.SLASH);
127 
128         languageId = languageId.substring(pos + 1);
129 
130         if (_log.isDebugEnabled()) {
131             _log.debug("Language ID " + languageId);
132         }
133 
134         if (Validator.isNull(languageId)) {
135             return null;
136         }
137 
138         Locale locale = LocaleUtil.fromLanguageId(languageId);
139 
140         if (Validator.isNull(locale.getCountry())) {
141 
142             // Locales must contain the country code
143 
144             locale = LanguageUtil.getLocale(locale.getLanguage());
145 
146             languageId = LocaleUtil.toLanguageId(locale);
147         }
148 
149         String redirect = path;
150 
151         if (_log.isDebugEnabled()) {
152             _log.debug("Redirect " + redirect);
153         }
154 
155         return new String[] {languageId, redirect};
156     }
157 
158     private static Log _log = LogFactoryUtil.getLog(I18nServlet.class);
159 
160     private static Set<String> _languageIds = new HashSet<String>();
161 
162 }