1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class I18nServlet extends HttpServlet {
57  
58      public static Set<String> getLanguageIds() {
59          return _languageIds;
60      }
61  
62      public static void setLanguageIds(Element root) {
63          Iterator<Element> itr = root.elements("servlet-mapping").iterator();
64  
65          while (itr.hasNext()) {
66              Element el = itr.next();
67  
68              String servletName = el.elementText("servlet-name");
69  
70              if (servletName.equals("I18n Servlet")) {
71                  String urlPattern = el.elementText("url-pattern");
72  
73                  String languageId = urlPattern.substring(
74                      0, urlPattern.lastIndexOf(StringPool.SLASH));
75  
76                  _languageIds.add(languageId);
77              }
78          }
79      }
80  
81      public void service(
82              HttpServletRequest request, HttpServletResponse response)
83          throws IOException, ServletException {
84  
85          try {
86              String[] i18nData = getI18nData(request);
87  
88              if (i18nData == null) {
89                  PortalUtil.sendError(
90                      HttpServletResponse.SC_NOT_FOUND,
91                      new NoSuchLayoutException(), request, response);
92              }
93              else {
94                  String i18nLanguageId = i18nData[0];
95                  String i18nPath = i18nData[1];
96                  String redirect = i18nData[2];
97  
98                  request.setAttribute(WebKeys.I18N_LANGUAGE_ID, i18nLanguageId);
99                  request.setAttribute(WebKeys.I18N_PATH, i18nPath);
100 
101                 ServletContext servletContext = getServletContext();
102 
103                 RequestDispatcher requestDispatcher =
104                     servletContext.getRequestDispatcher(redirect);
105 
106                 requestDispatcher.forward(request, response);
107             }
108         }
109         catch (Exception e) {
110             _log.error(e, e);
111 
112             PortalUtil.sendError(
113                 HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
114                 response);
115         }
116     }
117 
118     protected String[] getI18nData(HttpServletRequest request) {
119         String path = GetterUtil.getString(request.getPathInfo());
120 
121         if (Validator.isNull(path)) {
122             return null;
123         }
124 
125         String i18nLanguageId = request.getServletPath();
126 
127         int pos = i18nLanguageId.lastIndexOf(StringPool.SLASH);
128 
129         i18nLanguageId = i18nLanguageId.substring(pos + 1);
130 
131         if (_log.isDebugEnabled()) {
132             _log.debug("Language ID " + i18nLanguageId);
133         }
134 
135         if (Validator.isNull(i18nLanguageId)) {
136             return null;
137         }
138 
139         String i18nPath = StringPool.SLASH + i18nLanguageId;
140 
141         Locale locale = LocaleUtil.fromLanguageId(i18nLanguageId);
142 
143         if (Validator.isNull(locale.getCountry())) {
144 
145             // Locales must contain the country code
146 
147             locale = LanguageUtil.getLocale(locale.getLanguage());
148 
149             i18nLanguageId = LocaleUtil.toLanguageId(locale);
150         }
151 
152         String redirect = path;
153 
154         if (_log.isDebugEnabled()) {
155             _log.debug("Redirect " + redirect);
156         }
157 
158         return new String[] {i18nLanguageId, i18nPath, redirect};
159     }
160 
161     private static Log _log = LogFactoryUtil.getLog(I18nServlet.class);
162 
163     private static Set<String> _languageIds = new HashSet<String>();
164 
165 }