001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.servlet;
016    
017    import com.liferay.portal.NoSuchLayoutException;
018    import com.liferay.portal.kernel.language.LanguageUtil;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.util.CharPool;
022    import com.liferay.portal.kernel.util.LocaleUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.kernel.xml.Element;
026    import com.liferay.portal.util.PortalUtil;
027    import com.liferay.portal.util.WebKeys;
028    
029    import java.io.IOException;
030    
031    import java.util.Collections;
032    import java.util.HashSet;
033    import java.util.List;
034    import java.util.Locale;
035    import java.util.Set;
036    
037    import javax.servlet.RequestDispatcher;
038    import javax.servlet.ServletContext;
039    import javax.servlet.ServletException;
040    import javax.servlet.http.HttpServlet;
041    import javax.servlet.http.HttpServletRequest;
042    import javax.servlet.http.HttpServletResponse;
043    
044    /**
045     * @author Brian Wing Shun Chan
046     */
047    public class I18nServlet extends HttpServlet {
048    
049            public static Set<String> getLanguageIds() {
050                    return _languageIds;
051            }
052    
053            public static void setLanguageIds(Element root) {
054                    List<Element> rootElements = root.elements("servlet-mapping");
055    
056                    for (Element element : rootElements) {
057                            String servletName = element.elementText("servlet-name");
058    
059                            if (servletName.equals("I18n Servlet")) {
060                                    String urlPattern = element.elementText("url-pattern");
061    
062                                    String languageId = urlPattern.substring(
063                                            0, urlPattern.lastIndexOf(CharPool.SLASH));
064    
065                                    _languageIds.add(languageId);
066                            }
067                    }
068    
069                    _languageIds = Collections.unmodifiableSet(_languageIds);
070            }
071    
072            @Override
073            public void service(
074                            HttpServletRequest request, HttpServletResponse response)
075                    throws IOException, ServletException {
076    
077                    try {
078                            String[] i18nData = getI18nData(request);
079    
080                            if ((i18nData == null) ||
081                                    !PortalUtil.isValidResourceId(i18nData[2])) {
082    
083                                    PortalUtil.sendError(
084                                            HttpServletResponse.SC_NOT_FOUND,
085                                            new NoSuchLayoutException(), request, response);
086                            }
087                            else {
088                                    String i18nLanguageId = i18nData[0];
089                                    String i18nPath = i18nData[1];
090                                    String redirect = i18nData[2];
091    
092                                    request.setAttribute(WebKeys.I18N_LANGUAGE_ID, i18nLanguageId);
093                                    request.setAttribute(WebKeys.I18N_PATH, i18nPath);
094    
095                                    ServletContext servletContext = getServletContext();
096    
097                                    RequestDispatcher requestDispatcher =
098                                            servletContext.getRequestDispatcher(redirect);
099    
100                                    requestDispatcher.forward(request, response);
101                            }
102                    }
103                    catch (Exception e) {
104                            _log.error(e, e);
105    
106                            PortalUtil.sendError(
107                                    HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
108                                    response);
109                    }
110            }
111    
112            protected String[] getI18nData(HttpServletRequest request) {
113                    String path = request.getRequestURI();
114    
115                    String contextPath = request.getContextPath();
116                    String servletPath = request.getServletPath();
117    
118                    path = path.substring(contextPath.length() + servletPath.length());
119    
120                    if (Validator.isNull(path)) {
121                            return null;
122                    }
123    
124                    String i18nLanguageId = servletPath;
125    
126                    int pos = i18nLanguageId.lastIndexOf(CharPool.SLASH);
127    
128                    i18nLanguageId = i18nLanguageId.substring(pos + 1);
129    
130                    if (_log.isDebugEnabled()) {
131                            _log.debug("Language ID " + i18nLanguageId);
132                    }
133    
134                    if (Validator.isNull(i18nLanguageId)) {
135                            return null;
136                    }
137    
138                    String i18nPath = StringPool.SLASH + i18nLanguageId;
139    
140                    Locale locale = LocaleUtil.fromLanguageId(i18nLanguageId);
141    
142                    if (Validator.isNull(locale.getCountry())) {
143    
144                            // Locales must contain the country code
145    
146                            locale = LanguageUtil.getLocale(locale.getLanguage());
147    
148                            i18nLanguageId = 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[] {i18nLanguageId, i18nPath, redirect};
158            }
159    
160            private static Log _log = LogFactoryUtil.getLog(I18nServlet.class);
161    
162            private static Set<String> _languageIds = new HashSet<String>();
163    
164    }