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.Collections;
40  import java.util.HashSet;
41  import java.util.Iterator;
42  import java.util.Locale;
43  import java.util.Set;
44  
45  import javax.servlet.RequestDispatcher;
46  import javax.servlet.ServletContext;
47  import javax.servlet.ServletException;
48  import javax.servlet.http.HttpServlet;
49  import javax.servlet.http.HttpServletRequest;
50  import javax.servlet.http.HttpServletResponse;
51  
52  /**
53   * <a href="I18nServlet.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Brian Wing Shun Chan
56   */
57  public class I18nServlet extends HttpServlet {
58  
59      public static Set<String> getLanguageIds() {
60          return _languageIds;
61      }
62  
63      public static void setLanguageIds(Element root) {
64          Iterator<Element> itr = root.elements("servlet-mapping").iterator();
65  
66          while (itr.hasNext()) {
67              Element el = itr.next();
68  
69              String servletName = el.elementText("servlet-name");
70  
71              if (servletName.equals("I18n Servlet")) {
72                  String urlPattern = el.elementText("url-pattern");
73  
74                  String languageId = urlPattern.substring(
75                      0, urlPattern.lastIndexOf(StringPool.SLASH));
76  
77                  _languageIds.add(languageId);
78              }
79          }
80  
81          _languageIds = Collections.unmodifiableSet(_languageIds);
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 i18nLanguageId = i18nData[0];
98                  String i18nPath = i18nData[1];
99                  String redirect = i18nData[2];
100 
101                 request.setAttribute(WebKeys.I18N_LANGUAGE_ID, i18nLanguageId);
102                 request.setAttribute(WebKeys.I18N_PATH, i18nPath);
103 
104                 ServletContext servletContext = getServletContext();
105 
106                 RequestDispatcher requestDispatcher =
107                     servletContext.getRequestDispatcher(redirect);
108 
109                 requestDispatcher.forward(request, response);
110             }
111         }
112         catch (Exception e) {
113             _log.error(e, e);
114 
115             PortalUtil.sendError(
116                 HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
117                 response);
118         }
119     }
120 
121     protected String[] getI18nData(HttpServletRequest request) {
122         String path = GetterUtil.getString(request.getPathInfo());
123 
124         if (Validator.isNull(path)) {
125             return null;
126         }
127 
128         String i18nLanguageId = request.getServletPath();
129 
130         int pos = i18nLanguageId.lastIndexOf(StringPool.SLASH);
131 
132         i18nLanguageId = i18nLanguageId.substring(pos + 1);
133 
134         if (_log.isDebugEnabled()) {
135             _log.debug("Language ID " + i18nLanguageId);
136         }
137 
138         if (Validator.isNull(i18nLanguageId)) {
139             return null;
140         }
141 
142         String i18nPath = StringPool.SLASH + i18nLanguageId;
143 
144         Locale locale = LocaleUtil.fromLanguageId(i18nLanguageId);
145 
146         if (Validator.isNull(locale.getCountry())) {
147 
148             // Locales must contain the country code
149 
150             locale = LanguageUtil.getLocale(locale.getLanguage());
151 
152             i18nLanguageId = LocaleUtil.toLanguageId(locale);
153         }
154 
155         String redirect = path;
156 
157         if (_log.isDebugEnabled()) {
158             _log.debug("Redirect " + redirect);
159         }
160 
161         return new String[] {i18nLanguageId, i18nPath, redirect};
162     }
163 
164     private static Log _log = LogFactoryUtil.getLog(I18nServlet.class);
165 
166     private static Set<String> _languageIds = new HashSet<String>();
167 
168 }