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