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.util.GetterUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.util.PortalUtil;
30  import com.liferay.portal.util.WebKeys;
31  
32  import java.io.IOException;
33  
34  import javax.servlet.RequestDispatcher;
35  import javax.servlet.ServletContext;
36  import javax.servlet.ServletException;
37  import javax.servlet.http.HttpServlet;
38  import javax.servlet.http.HttpServletRequest;
39  import javax.servlet.http.HttpServletResponse;
40  
41  import org.apache.commons.logging.Log;
42  import org.apache.commons.logging.LogFactory;
43  
44  /**
45   * <a href="I18nServlet.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   *
49   */
50  public class I18nServlet extends HttpServlet {
51  
52      public void service(
53              HttpServletRequest request, HttpServletResponse response)
54          throws IOException, ServletException {
55  
56          try {
57              String[] i18nData = getI18nData(request);
58  
59              if (i18nData == null) {
60                  PortalUtil.sendError(
61                      HttpServletResponse.SC_NOT_FOUND,
62                      new NoSuchLayoutException(), request, response);
63              }
64              else {
65                  String languageId = i18nData[0];
66                  String redirect = i18nData[1];
67  
68                  request.setAttribute(WebKeys.I18N_LANGUAGE_ID, languageId);
69  
70                  ServletContext servletContext = getServletContext();
71  
72                  RequestDispatcher requestDispatcher =
73                      servletContext.getRequestDispatcher(redirect);
74  
75                  requestDispatcher.forward(request, response);
76              }
77          }
78          catch (Exception e) {
79              _log.error(e, e);
80  
81              PortalUtil.sendError(
82                  HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
83                  response);
84          }
85      }
86  
87      protected String[] getI18nData(HttpServletRequest request) {
88          String path = GetterUtil.getString(request.getPathInfo());
89  
90          if (Validator.isNull(path)) {
91              return null;
92          }
93  
94          String languageId = request.getServletPath();
95  
96          int pos = languageId.lastIndexOf(StringPool.SLASH);
97  
98          languageId = languageId.substring(pos + 1);
99  
100         if (_log.isDebugEnabled()) {
101             _log.debug("Language ID " + languageId);
102         }
103 
104         if (Validator.isNull(languageId)) {
105             return null;
106         }
107 
108         String redirect = path;
109 
110         if (_log.isDebugEnabled()) {
111             _log.debug("Redirect " + redirect);
112         }
113 
114         return new String[] {languageId, redirect};
115     }
116 
117     private static Log _log = LogFactory.getLog(I18nServlet.class);
118 
119 }