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.exception.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.util.WebKeys;
028    import com.liferay.portal.kernel.xml.Element;
029    import com.liferay.portal.util.PortalUtil;
030    import com.liferay.portal.util.PropsValues;
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                    String i18nLanguageCode = locale.getLanguage();
154    
155                    if (Validator.isNull(locale.getCountry())) {
156    
157                            // Locales must contain the country code
158    
159                            locale = LanguageUtil.getLocale(i18nLanguageCode);
160    
161                            if (locale == null) {
162                                    i18nLanguageId = null;
163                                    i18nLanguageCode = null;
164                            }
165                            else {
166                                    i18nLanguageId = LocaleUtil.toLanguageId(locale);
167                            }
168                    }
169    
170                    if (!PropsValues.LOCALE_USE_DEFAULT_IF_NOT_AVAILABLE &&
171                            !LanguageUtil.isAvailableLocale(i18nLanguageId)) {
172    
173                            return null;
174                    }
175    
176                    String redirect = path;
177    
178                    if (_log.isDebugEnabled()) {
179                            _log.debug("Redirect " + redirect);
180                    }
181    
182                    return new I18nData(
183                            i18nPath, i18nLanguageCode, i18nLanguageId, redirect);
184            }
185    
186            protected I18nData getI18nData(Locale locale) {
187                    String languageId = LocaleUtil.toLanguageId(locale);
188    
189                    return new I18nData(
190                            StringPool.SLASH + languageId, locale.getLanguage(), languageId,
191                            StringPool.SLASH);
192            }
193    
194            protected class I18nData {
195    
196                    public I18nData(
197                            String i18nPath, String languageCode, String languageId,
198                            String path) {
199    
200                            _i18nPath = i18nPath;
201                            _languageCode = languageCode;
202                            _languageId = languageId;
203                            _path = path;
204                    }
205    
206                    @Override
207                    public boolean equals(Object obj) {
208                            if (this == obj) {
209                                    return true;
210                            }
211    
212                            if (!(obj instanceof I18nData)) {
213                                    return false;
214                            }
215    
216                            I18nData i18nData = (I18nData)obj;
217    
218                            if (Validator.equals(getI18nPath(), i18nData.getI18nPath()) &&
219                                    Validator.equals(
220                                            getLanguageCode(), i18nData.getLanguageCode()) &&
221                                    Validator.equals(getLanguageId(), i18nData.getLanguageId()) &&
222                                    Validator.equals(getPath(), i18nData.getPath())) {
223    
224                                    return true;
225                            }
226    
227                            return false;
228                    }
229    
230                    public String getI18nPath() {
231                            return _i18nPath;
232                    }
233    
234                    public String getLanguageCode() {
235                            return _languageCode;
236                    }
237    
238                    public String getLanguageId() {
239                            return _languageId;
240                    }
241    
242                    public String getPath() {
243                            return _path;
244                    }
245    
246                    @Override
247                    public int hashCode() {
248                            int hash = HashUtil.hash(0, getI18nPath());
249    
250                            hash = HashUtil.hash(hash, getLanguageCode());
251                            hash = HashUtil.hash(hash, getLanguageId());
252    
253                            return HashUtil.hash(hash, getPath());
254                    }
255    
256                    private final String _i18nPath;
257                    private final String _languageCode;
258                    private final String _languageId;
259                    private final String _path;
260    
261            }
262    
263            private static final Log _log = LogFactoryUtil.getLog(I18nServlet.class);
264    
265            private static Set<String> _languageIds;
266    
267    }