001    /**
002     * Copyright (c) 2000-present 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.HashUtil;
024    import com.liferay.portal.kernel.util.LocaleUtil;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.kernel.xml.Element;
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<>();
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                            I18nData i18nData = getI18nData(request);
087    
088                            if ((i18nData == null) ||
089                                    !PortalUtil.isValidResourceId(i18nData.getPath())) {
090    
091                                    PortalUtil.sendError(
092                                            HttpServletResponse.SC_NOT_FOUND,
093                                            new NoSuchLayoutException(), request, response);
094                            }
095                            else {
096                                    request.setAttribute(
097                                            WebKeys.I18N_LANGUAGE_CODE, i18nData.getLanguageCode());
098                                    request.setAttribute(
099                                            WebKeys.I18N_LANGUAGE_ID, i18nData.getLanguageId());
100                                    request.setAttribute(WebKeys.I18N_PATH, i18nData.getI18nPath());
101    
102                                    Locale locale = LocaleUtil.fromLanguageId(
103                                            i18nData.getLanguageId());
104    
105                                    HttpSession session = request.getSession();
106    
107                                    session.setAttribute(Globals.LOCALE_KEY, locale);
108    
109                                    LanguageUtil.updateCookie(request, response, locale);
110    
111                                    ServletContext servletContext = getServletContext();
112    
113                                    RequestDispatcher requestDispatcher =
114                                            servletContext.getRequestDispatcher(i18nData.getPath());
115    
116                                    requestDispatcher.forward(request, response);
117                            }
118                    }
119                    catch (Exception e) {
120                            _log.error(e, e);
121    
122                            PortalUtil.sendError(
123                                    HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
124                                    response);
125                    }
126            }
127    
128            protected I18nData getI18nData(HttpServletRequest request) {
129                    String path = GetterUtil.getString(request.getPathInfo());
130    
131                    if (Validator.isNull(path)) {
132                            return null;
133                    }
134    
135                    String i18nLanguageId = request.getServletPath();
136    
137                    int pos = i18nLanguageId.lastIndexOf(CharPool.SLASH);
138    
139                    i18nLanguageId = i18nLanguageId.substring(pos + 1);
140    
141                    if (_log.isDebugEnabled()) {
142                            _log.debug("Language ID " + i18nLanguageId);
143                    }
144    
145                    if (Validator.isNull(i18nLanguageId)) {
146                            return null;
147                    }
148    
149                    String i18nPath = StringPool.SLASH + i18nLanguageId;
150    
151                    Locale locale = LocaleUtil.fromLanguageId(i18nLanguageId);
152    
153                    if (Validator.isNull(locale.getCountry())) {
154    
155                            // Locales must contain the country code
156    
157                            locale = LanguageUtil.getLocale(locale.getLanguage());
158    
159                            i18nLanguageId = LocaleUtil.toLanguageId(locale);
160                    }
161    
162                    if (!PropsValues.LOCALE_USE_DEFAULT_IF_NOT_AVAILABLE &&
163                            !LanguageUtil.isAvailableLocale(i18nLanguageId)) {
164    
165                                    return null;
166                    }
167    
168                    String redirect = path;
169    
170                    if (_log.isDebugEnabled()) {
171                            _log.debug("Redirect " + redirect);
172                    }
173    
174                    return new I18nData(
175                            i18nPath, locale.getLanguage(), i18nLanguageId, redirect);
176            }
177    
178            protected I18nData getI18nData(Locale locale) {
179                    String languageId = LocaleUtil.toLanguageId(locale);
180    
181                    return new I18nData(
182                            StringPool.SLASH + languageId, locale.getLanguage(), languageId,
183                            StringPool.SLASH);
184            }
185    
186            protected class I18nData {
187    
188                    public I18nData(
189                            String i18nPath, String languageCode, String languageId,
190                            String path) {
191    
192                            _i18nPath = i18nPath;
193                            _languageCode = languageCode;
194                            _languageId = languageId;
195                            _path = path;
196                    }
197    
198                    @Override
199                    public boolean equals(Object obj) {
200                            if (this == obj) {
201                                    return true;
202                            }
203    
204                            if (!(obj instanceof I18nData)) {
205                                    return false;
206                            }
207    
208                            I18nData i18nData = (I18nData)obj;
209    
210                            if (Validator.equals(getI18nPath(), i18nData.getI18nPath()) &&
211                                    Validator.equals(
212                                            getLanguageCode(), i18nData.getLanguageCode()) &&
213                                    Validator.equals(getLanguageId(), i18nData.getLanguageId()) &&
214                                    Validator.equals(getPath(), i18nData.getPath())) {
215    
216                                    return true;
217                            }
218    
219                            return false;
220                    }
221    
222                    public String getI18nPath() {
223                            return _i18nPath;
224                    }
225    
226                    public String getLanguageCode() {
227                            return _languageCode;
228                    }
229    
230                    public String getLanguageId() {
231                            return _languageId;
232                    }
233    
234                    public String getPath() {
235                            return _path;
236                    }
237    
238                    @Override
239                    public int hashCode() {
240                            int hash = HashUtil.hash(0, getI18nPath());
241    
242                            hash = HashUtil.hash(hash, getLanguageCode());
243                            hash = HashUtil.hash(hash, getLanguageId());
244    
245                            return HashUtil.hash(hash, getPath());
246                    }
247    
248                    private final String _i18nPath;
249                    private final String _languageCode;
250                    private final String _languageId;
251                    private final String _path;
252    
253            }
254    
255            private static final Log _log = LogFactoryUtil.getLog(I18nServlet.class);
256    
257            private static Set<String> _languageIds;
258    
259    }