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