1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.servlet;
16  
17  import com.liferay.portal.NoSuchLayoutException;
18  import com.liferay.portal.kernel.language.LanguageUtil;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.kernel.util.CharPool;
22  import com.liferay.portal.kernel.util.GetterUtil;
23  import com.liferay.portal.kernel.util.LocaleUtil;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.kernel.util.Validator;
26  import com.liferay.portal.kernel.xml.Element;
27  import com.liferay.portal.util.PortalUtil;
28  import com.liferay.portal.util.WebKeys;
29  
30  import java.io.IOException;
31  
32  import java.util.Collections;
33  import java.util.HashSet;
34  import java.util.Iterator;
35  import java.util.Locale;
36  import java.util.Set;
37  
38  import javax.servlet.RequestDispatcher;
39  import javax.servlet.ServletContext;
40  import javax.servlet.ServletException;
41  import javax.servlet.http.HttpServlet;
42  import javax.servlet.http.HttpServletRequest;
43  import javax.servlet.http.HttpServletResponse;
44  
45  /**
46   * <a href="I18nServlet.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   */
50  public class I18nServlet extends HttpServlet {
51  
52      public static Set<String> getLanguageIds() {
53          return _languageIds;
54      }
55  
56      public static void setLanguageIds(Element root) {
57          Iterator<Element> itr = root.elements("servlet-mapping").iterator();
58  
59          while (itr.hasNext()) {
60              Element el = itr.next();
61  
62              String servletName = el.elementText("servlet-name");
63  
64              if (servletName.equals("I18n Servlet")) {
65                  String urlPattern = el.elementText("url-pattern");
66  
67                  String languageId = urlPattern.substring(
68                      0, urlPattern.lastIndexOf(CharPool.SLASH));
69  
70                  _languageIds.add(languageId);
71              }
72          }
73  
74          _languageIds = Collections.unmodifiableSet(_languageIds);
75      }
76  
77      public void service(
78              HttpServletRequest request, HttpServletResponse response)
79          throws IOException, ServletException {
80  
81          try {
82              String[] i18nData = getI18nData(request);
83  
84              if ((i18nData == null) ||
85                  !PortalUtil.isValidResourceId(i18nData[2])) {
86  
87                  PortalUtil.sendError(
88                      HttpServletResponse.SC_NOT_FOUND,
89                      new NoSuchLayoutException(), request, response);
90              }
91              else {
92                  String i18nLanguageId = i18nData[0];
93                  String i18nPath = i18nData[1];
94                  String redirect = i18nData[2];
95  
96                  request.setAttribute(WebKeys.I18N_LANGUAGE_ID, i18nLanguageId);
97                  request.setAttribute(WebKeys.I18N_PATH, i18nPath);
98  
99                  ServletContext servletContext = getServletContext();
100 
101                 RequestDispatcher requestDispatcher =
102                     servletContext.getRequestDispatcher(redirect);
103 
104                 requestDispatcher.forward(request, response);
105             }
106         }
107         catch (Exception e) {
108             _log.error(e, e);
109 
110             PortalUtil.sendError(
111                 HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
112                 response);
113         }
114     }
115 
116     protected String[] getI18nData(HttpServletRequest request) {
117         String path = GetterUtil.getString(request.getPathInfo());
118 
119         if (Validator.isNull(path)) {
120             return null;
121         }
122 
123         String i18nLanguageId = request.getServletPath();
124 
125         int pos = i18nLanguageId.lastIndexOf(CharPool.SLASH);
126 
127         i18nLanguageId = i18nLanguageId.substring(pos + 1);
128 
129         if (_log.isDebugEnabled()) {
130             _log.debug("Language ID " + i18nLanguageId);
131         }
132 
133         if (Validator.isNull(i18nLanguageId)) {
134             return null;
135         }
136 
137         String i18nPath = StringPool.SLASH + i18nLanguageId;
138 
139         Locale locale = LocaleUtil.fromLanguageId(i18nLanguageId);
140 
141         if (Validator.isNull(locale.getCountry())) {
142 
143             // Locales must contain the country code
144 
145             locale = LanguageUtil.getLocale(locale.getLanguage());
146 
147             i18nLanguageId = 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[] {i18nLanguageId, i18nPath, redirect};
157     }
158 
159     private static Log _log = LogFactoryUtil.getLog(I18nServlet.class);
160 
161     private static Set<String> _languageIds = new HashSet<String>();
162 
163 }