001    /**
002     * Copyright (c) 2000-2013 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.GetterUtil;
023    import com.liferay.portal.kernel.util.LocaleUtil;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.kernel.xml.Element;
027    import com.liferay.portal.model.User;
028    import com.liferay.portal.util.PortalUtil;
029    import com.liferay.portal.util.PropsValues;
030    import com.liferay.portal.util.WebKeys;
031    
032    import java.io.IOException;
033    
034    import java.util.Collections;
035    import java.util.HashSet;
036    import java.util.List;
037    import java.util.Locale;
038    import java.util.Set;
039    
040    import javax.servlet.RequestDispatcher;
041    import javax.servlet.ServletContext;
042    import javax.servlet.ServletException;
043    import javax.servlet.http.HttpServlet;
044    import javax.servlet.http.HttpServletRequest;
045    import javax.servlet.http.HttpServletResponse;
046    import javax.servlet.http.HttpSession;
047    
048    import org.apache.struts.Globals;
049    
050    /**
051     * @author Brian Wing Shun Chan
052     */
053    public class I18nServlet extends HttpServlet {
054    
055            public static Set<String> getLanguageIds() {
056                    return _languageIds;
057            }
058    
059            public static void setLanguageIds(Element root) {
060                    _languageIds = new HashSet<String>();
061    
062                    List<Element> rootElements = root.elements("servlet-mapping");
063    
064                    for (Element element : rootElements) {
065                            String servletName = element.elementText("servlet-name");
066    
067                            if (servletName.equals("I18n Servlet")) {
068                                    String urlPattern = element.elementText("url-pattern");
069    
070                                    String languageId = urlPattern.substring(
071                                            0, urlPattern.lastIndexOf(CharPool.SLASH));
072    
073                                    _languageIds.add(languageId);
074                            }
075                    }
076    
077                    _languageIds = Collections.unmodifiableSet(_languageIds);
078            }
079    
080            @Override
081            public void service(
082                            HttpServletRequest request, HttpServletResponse response)
083                    throws IOException, ServletException {
084    
085                    try {
086                            String[] i18nData = getI18nData(request);
087    
088                            if ((i18nData == null) ||
089                                    !PortalUtil.isValidResourceId(i18nData[2])) {
090    
091                                    PortalUtil.sendError(
092                                            HttpServletResponse.SC_NOT_FOUND,
093                                            new NoSuchLayoutException(), request, response);
094                            }
095                            else {
096                                    String i18nLanguageId = i18nData[0];
097                                    String i18nPath = i18nData[1];
098                                    String redirect = i18nData[2];
099    
100                                    request.setAttribute(WebKeys.I18N_LANGUAGE_ID, i18nLanguageId);
101                                    request.setAttribute(WebKeys.I18N_PATH, i18nPath);
102    
103                                    Locale locale = LocaleUtil.fromLanguageId(i18nLanguageId);
104    
105                                    HttpSession session = request.getSession();
106    
107                                    session.setAttribute(Globals.LOCALE_KEY, locale);
108    
109                                    LanguageUtil.updateCookie(request, response, locale);
110    
111                                    if (PropsValues.LOCALE_PREPEND_FRIENDLY_URL_STYLE == 3) {
112                                            User user = (User)request.getAttribute(WebKeys.USER);
113    
114                                            if ((user != null) && (user.getLocale() != locale)) {
115                                                    PortalUtil.addUserLocaleOptionsMessage(request);
116                                            }
117                                    }
118    
119                                    ServletContext servletContext = getServletContext();
120    
121                                    RequestDispatcher requestDispatcher =
122                                            servletContext.getRequestDispatcher(redirect);
123    
124                                    requestDispatcher.forward(request, response);
125                            }
126                    }
127                    catch (Exception e) {
128                            _log.error(e, e);
129    
130                            PortalUtil.sendError(
131                                    HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
132                                    response);
133                    }
134            }
135    
136            protected String[] getI18nData(HttpServletRequest request) {
137                    String path = GetterUtil.getString(request.getPathInfo());
138    
139                    if (Validator.isNull(path)) {
140                            return null;
141                    }
142    
143                    String i18nLanguageId = request.getServletPath();
144    
145                    int pos = i18nLanguageId.lastIndexOf(CharPool.SLASH);
146    
147                    i18nLanguageId = i18nLanguageId.substring(pos + 1);
148    
149                    if (_log.isDebugEnabled()) {
150                            _log.debug("Language ID " + i18nLanguageId);
151                    }
152    
153                    if (Validator.isNull(i18nLanguageId)) {
154                            return null;
155                    }
156    
157                    String i18nPath = StringPool.SLASH + i18nLanguageId;
158    
159                    Locale locale = LocaleUtil.fromLanguageId(i18nLanguageId);
160    
161                    if (Validator.isNull(locale.getCountry())) {
162    
163                            // Locales must contain the country code
164    
165                            locale = LanguageUtil.getLocale(locale.getLanguage());
166    
167                            i18nLanguageId = LocaleUtil.toLanguageId(locale);
168                    }
169    
170                    String redirect = path;
171    
172                    if (_log.isDebugEnabled()) {
173                            _log.debug("Redirect " + redirect);
174                    }
175    
176                    return new String[] {i18nLanguageId, i18nPath, redirect};
177            }
178    
179            private static Log _log = LogFactoryUtil.getLog(I18nServlet.class);
180    
181            private static Set<String> _languageIds;
182    
183    }