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("I18n Servlet")) {
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 i18nLanguageId = i18nData[0];
96                  String i18nPath = i18nData[1];
97                  String redirect = i18nData[2];
98  
99                  request.setAttribute(WebKeys.I18N_LANGUAGE_ID, i18nLanguageId);
100                 request.setAttribute(WebKeys.I18N_PATH, i18nPath);
101 
102                 ServletContext servletContext = getServletContext();
103 
104                 RequestDispatcher requestDispatcher =
105                     servletContext.getRequestDispatcher(redirect);
106 
107                 requestDispatcher.forward(request, response);
108             }
109         }
110         catch (Exception e) {
111             _log.error(e, e);
112 
113             PortalUtil.sendError(
114                 HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
115                 response);
116         }
117     }
118 
119     protected String[] getI18nData(HttpServletRequest request) {
120         String path = GetterUtil.getString(request.getPathInfo());
121 
122         if (Validator.isNull(path)) {
123             return null;
124         }
125 
126         String i18nLanguageId = request.getServletPath();
127 
128         int pos = i18nLanguageId.lastIndexOf(StringPool.SLASH);
129 
130         i18nLanguageId = i18nLanguageId.substring(pos + 1);
131 
132         if (_log.isDebugEnabled()) {
133             _log.debug("Language ID " + i18nLanguageId);
134         }
135 
136         if (Validator.isNull(i18nLanguageId)) {
137             return null;
138         }
139 
140         String i18nPath = StringPool.SLASH + i18nLanguageId;
141 
142         Locale locale = LocaleUtil.fromLanguageId(i18nLanguageId);
143 
144         if (Validator.isNull(locale.getCountry())) {
145 
146             // Locales must contain the country code
147 
148             locale = LanguageUtil.getLocale(locale.getLanguage());
149 
150             i18nLanguageId = LocaleUtil.toLanguageId(locale);
151         }
152 
153         String redirect = path;
154 
155         if (_log.isDebugEnabled()) {
156             _log.debug("Redirect " + redirect);
157         }
158 
159         return new String[] {i18nLanguageId, i18nPath, redirect};
160     }
161 
162     private static Log _log = LogFactoryUtil.getLog(I18nServlet.class);
163 
164     private static Set<String> _languageIds = new HashSet<String>();
165 
166 }