1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.servlet;
21  
22  import com.liferay.portal.NoSuchLayoutException;
23  import com.liferay.portal.kernel.language.LanguageUtil;
24  import com.liferay.portal.kernel.log.Log;
25  import com.liferay.portal.kernel.log.LogFactoryUtil;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.LocaleUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.kernel.xml.Element;
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  /**
49   * <a href="I18nServlet.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Brian Wing Shun Chan
52   *
53   */
54  public class I18nServlet extends HttpServlet {
55  
56      public static Set<String> getLanguageIds() {
57          return _languageIds;
58      }
59  
60      public static void setLanguageIds(Element root) {
61          Iterator<Element> itr = root.elements("servlet-mapping").iterator();
62  
63          while (itr.hasNext()) {
64              Element el = itr.next();
65  
66              String servletName = el.elementText("servlet-name");
67  
68              if (servletName.equals("I18n Servlet")) {
69                  String urlPattern = el.elementText("url-pattern");
70  
71                  String languageId = urlPattern.substring(
72                      0, urlPattern.lastIndexOf(StringPool.SLASH));
73  
74                  _languageIds.add(languageId);
75              }
76          }
77      }
78  
79      public void service(
80              HttpServletRequest request, HttpServletResponse response)
81          throws IOException, ServletException {
82  
83          try {
84              String[] i18nData = getI18nData(request);
85  
86              if (i18nData == null) {
87                  PortalUtil.sendError(
88                      HttpServletResponse.SC_NOT_FOUND,
89                      new NoSuchLayoutException(), request, response);
90              }
91              else {
92                  String languageId = i18nData[0];
93                  String redirect = i18nData[1];
94  
95                  request.setAttribute(WebKeys.I18N_LANGUAGE_ID, languageId);
96  
97                  ServletContext servletContext = getServletContext();
98  
99                  RequestDispatcher requestDispatcher =
100                     servletContext.getRequestDispatcher(redirect);
101 
102                 requestDispatcher.forward(request, response);
103             }
104         }
105         catch (Exception e) {
106             _log.error(e, e);
107 
108             PortalUtil.sendError(
109                 HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
110                 response);
111         }
112     }
113 
114     protected String[] getI18nData(HttpServletRequest request) {
115         String path = GetterUtil.getString(request.getPathInfo());
116 
117         if (Validator.isNull(path)) {
118             return null;
119         }
120 
121         String languageId = request.getServletPath();
122 
123         int pos = languageId.lastIndexOf(StringPool.SLASH);
124 
125         languageId = languageId.substring(pos + 1);
126 
127         if (_log.isDebugEnabled()) {
128             _log.debug("Language ID " + languageId);
129         }
130 
131         if (Validator.isNull(languageId)) {
132             return null;
133         }
134 
135         Locale locale = LocaleUtil.fromLanguageId(languageId);
136 
137         if (Validator.isNull(locale.getCountry())) {
138 
139             // Locales must contain the country code
140 
141             locale = LanguageUtil.getLocale(locale.getLanguage());
142 
143             languageId = LocaleUtil.toLanguageId(locale);
144         }
145 
146         String redirect = path;
147 
148         if (_log.isDebugEnabled()) {
149             _log.debug("Redirect " + redirect);
150         }
151 
152         return new String[] {languageId, redirect};
153     }
154 
155     private static Log _log = LogFactoryUtil.getLog(I18nServlet.class);
156 
157     private static Set<String> _languageIds = new HashSet<String>();
158 
159 }