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.Iterator;
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                    Iterator<Element> itr = root.elements("servlet-mapping").iterator();
055    
056                    while (itr.hasNext()) {
057                            Element el = itr.next();
058    
059                            String servletName = el.elementText("servlet-name");
060    
061                            if (servletName.equals("I18n Servlet")) {
062                                    String urlPattern = el.elementText("url-pattern");
063    
064                                    String languageId = urlPattern.substring(
065                                            0, urlPattern.lastIndexOf(CharPool.SLASH));
066    
067                                    _languageIds.add(languageId);
068                            }
069                    }
070    
071                    _languageIds = Collections.unmodifiableSet(_languageIds);
072            }
073    
074            @Override
075            public void service(
076                            HttpServletRequest request, HttpServletResponse response)
077                    throws IOException, ServletException {
078    
079                    try {
080                            String[] i18nData = getI18nData(request);
081    
082                            if ((i18nData == null) ||
083                                    !PortalUtil.isValidResourceId(i18nData[2])) {
084    
085                                    PortalUtil.sendError(
086                                            HttpServletResponse.SC_NOT_FOUND,
087                                            new NoSuchLayoutException(), request, response);
088                            }
089                            else {
090                                    String i18nLanguageId = i18nData[0];
091                                    String i18nPath = i18nData[1];
092                                    String redirect = i18nData[2];
093    
094                                    request.setAttribute(WebKeys.I18N_LANGUAGE_ID, i18nLanguageId);
095                                    request.setAttribute(WebKeys.I18N_PATH, i18nPath);
096    
097                                    ServletContext servletContext = getServletContext();
098    
099                                    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 = request.getRequestURI();
116    
117                    String contextPath = request.getContextPath();
118                    String servletPath = request.getServletPath();
119    
120                    path = path.substring(contextPath.length() + servletPath.length());
121    
122                    if (Validator.isNull(path)) {
123                            return null;
124                    }
125    
126                    String i18nLanguageId = servletPath;
127    
128                    int pos = i18nLanguageId.lastIndexOf(CharPool.SLASH);
129    
130                    i18nLanguageId = i18nLanguageId.substring(pos + 1);
131    
132                    if (_log.isDebugEnabled()) {
133                            _log.debug("Language ID " + i18nLanguageId);
134                    }
135    
136                    if (Validator.isNull(i18nLanguageId)) {
137                            return null;
138                    }
139    
140                    String i18nPath = StringPool.SLASH + i18nLanguageId;
141    
142                    Locale locale = LocaleUtil.fromLanguageId(i18nLanguageId);
143    
144                    if (Validator.isNull(locale.getCountry())) {
145    
146                            // Locales must contain the country code
147    
148                            locale = LanguageUtil.getLocale(locale.getLanguage());
149    
150                            i18nLanguageId = LocaleUtil.toLanguageId(locale);
151                    }
152    
153                    String redirect = path;
154    
155                    if (_log.isDebugEnabled()) {
156                            _log.debug("Redirect " + redirect);
157                    }
158    
159                    return new String[] {i18nLanguageId, i18nPath, redirect};
160            }
161    
162            private static Log _log = LogFactoryUtil.getLog(I18nServlet.class);
163    
164            private static Set<String> _languageIds = new HashSet<String>();
165    
166    }