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.filters.i18n;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.servlet.HttpMethods;
021    import com.liferay.portal.kernel.util.CookieKeys;
022    import com.liferay.portal.kernel.util.LocaleUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.model.Group;
027    import com.liferay.portal.model.LayoutSet;
028    import com.liferay.portal.model.User;
029    import com.liferay.portal.servlet.filters.BasePortalFilter;
030    import com.liferay.portal.util.PortalUtil;
031    import com.liferay.portal.util.PropsValues;
032    import com.liferay.portal.util.WebKeys;
033    
034    import java.util.Collections;
035    import java.util.HashSet;
036    import java.util.Locale;
037    import java.util.Set;
038    
039    import javax.servlet.FilterChain;
040    import javax.servlet.http.HttpServletRequest;
041    import javax.servlet.http.HttpServletResponse;
042    import javax.servlet.http.HttpSession;
043    
044    import org.apache.struts.Globals;
045    
046    /**
047     * @author Brian Wing Shun Chan
048     */
049    public class I18nFilter extends BasePortalFilter {
050    
051            public static final String SKIP_FILTER =
052                    I18nFilter.class.getName() + "SKIP_FILTER";
053    
054            public static Set<String> getLanguageIds() {
055                    return _languageIds;
056            }
057    
058            public static void setLanguageIds(Set<String> languageIds) {
059                    _languageIds = new HashSet<>();
060    
061                    for (String languageId : languageIds) {
062                            languageId = languageId.substring(1);
063    
064                            _languageIds.add(languageId);
065                    }
066    
067                    _languageIds = Collections.unmodifiableSet(_languageIds);
068            }
069    
070            @Override
071            public boolean isFilterEnabled(
072                    HttpServletRequest request, HttpServletResponse response) {
073    
074                    if (!isAlreadyFiltered(request) && !isForwardedByI18nServlet(request)) {
075                            return true;
076                    }
077                    else {
078                            return false;
079                    }
080            }
081    
082            protected String getRedirect(HttpServletRequest request) throws Exception {
083                    if (PropsValues.LOCALE_PREPEND_FRIENDLY_URL_STYLE == 0) {
084                            return null;
085                    }
086    
087                    String method = request.getMethod();
088    
089                    if (method.equals(HttpMethods.POST)) {
090                            return null;
091                    }
092    
093                    String contextPath = PortalUtil.getPathContext();
094    
095                    String requestURI = request.getRequestURI();
096    
097                    if (Validator.isNotNull(contextPath) &&
098                            requestURI.contains(contextPath)) {
099    
100                            requestURI = requestURI.substring(contextPath.length());
101                    }
102    
103                    requestURI = StringUtil.replace(
104                            requestURI, StringPool.DOUBLE_SLASH, StringPool.SLASH);
105    
106                    String i18nLanguageId = prependI18nLanguageId(
107                            request, PropsValues.LOCALE_PREPEND_FRIENDLY_URL_STYLE);
108    
109                    if (i18nLanguageId == null) {
110                            return null;
111                    }
112    
113                    Locale locale = LocaleUtil.fromLanguageId(i18nLanguageId);
114    
115                    if (!LanguageUtil.isAvailableLocale(locale)) {
116                            return null;
117                    }
118    
119                    String i18nPathLanguageId = PortalUtil.getI18nPathLanguageId(
120                            locale, i18nLanguageId);
121    
122                    String i18nPath = StringPool.SLASH.concat(i18nPathLanguageId);
123    
124                    if (requestURI.contains(i18nPath.concat(StringPool.SLASH))) {
125                            return null;
126                    }
127    
128                    String redirect = contextPath + i18nPath + requestURI;
129    
130                    LayoutSet layoutSet = (LayoutSet)request.getAttribute(
131                            WebKeys.VIRTUAL_HOST_LAYOUT_SET);
132    
133                    if ((layoutSet != null) &&
134                            requestURI.startsWith(
135                                    PropsValues.LAYOUT_FRIENDLY_URL_PUBLIC_SERVLET_MAPPING)) {
136    
137                            int[] groupFriendlyURLIndex = PortalUtil.getGroupFriendlyURLIndex(
138                                    requestURI);
139    
140                            if (groupFriendlyURLIndex != null) {
141                                    int x = groupFriendlyURLIndex[0];
142                                    int y = groupFriendlyURLIndex[1];
143    
144                                    String groupFriendlyURL = requestURI.substring(x, y);
145    
146                                    Group group = layoutSet.getGroup();
147    
148                                    if (groupFriendlyURL.equals(group.getFriendlyURL())) {
149                                            redirect = contextPath + i18nPath + requestURI.substring(y);
150                                    }
151                            }
152                    }
153    
154                    String queryString = request.getQueryString();
155    
156                    if (Validator.isNotNull(queryString)) {
157                            redirect += StringPool.QUESTION + request.getQueryString();
158                    }
159    
160                    return redirect;
161            }
162    
163            protected boolean isAlreadyFiltered(HttpServletRequest request) {
164                    if (request.getAttribute(SKIP_FILTER) != null) {
165                            return true;
166                    }
167                    else {
168                            return false;
169                    }
170            }
171    
172            protected boolean isForwardedByI18nServlet(HttpServletRequest request) {
173                    if ((request.getAttribute(WebKeys.I18N_LANGUAGE_ID) != null) ||
174                            (request.getAttribute(WebKeys.I18N_PATH) != null)) {
175    
176                            return true;
177                    }
178                    else {
179                            return false;
180                    }
181            }
182    
183            protected String prependI18nLanguageId(
184                    HttpServletRequest request, int prependFriendlyUrlStyle) {
185    
186                    String userLanguageId = null;
187    
188                    User user = (User)request.getAttribute(WebKeys.USER);
189    
190                    if (user != null) {
191                            userLanguageId = user.getLanguageId();
192                    }
193    
194                    String guestLanguageId = userLanguageId;
195    
196                    if (Validator.isNull(guestLanguageId)) {
197                            guestLanguageId = CookieKeys.getCookie(
198                                    request, CookieKeys.GUEST_LANGUAGE_ID, false);
199                    }
200    
201                    String defaultLanguageId = LocaleUtil.toLanguageId(
202                            LocaleUtil.getDefault());
203    
204                    if (Validator.isNull(guestLanguageId)) {
205                            guestLanguageId = defaultLanguageId;
206                    }
207    
208                    if (prependFriendlyUrlStyle == 1) {
209                            if (!defaultLanguageId.equals(guestLanguageId)) {
210                                    return guestLanguageId;
211                            }
212                            else {
213                                    return null;
214                            }
215                    }
216                    else if (prependFriendlyUrlStyle == 2) {
217                            return LocaleUtil.toLanguageId(PortalUtil.getLocale(request));
218                    }
219                    else if (prependFriendlyUrlStyle == 3) {
220                            if (user != null) {
221                                    HttpSession session = request.getSession();
222    
223                                    session.setAttribute(Globals.LOCALE_KEY, user.getLocale());
224                            }
225    
226                            return null;
227                    }
228    
229                    return null;
230            }
231    
232            @Override
233            protected void processFilter(
234                            HttpServletRequest request, HttpServletResponse response,
235                            FilterChain filterChain)
236                    throws Exception {
237    
238                    request.setAttribute(SKIP_FILTER, Boolean.TRUE);
239    
240                    String redirect = getRedirect(request);
241    
242                    if (redirect == null) {
243                            processFilter(I18nFilter.class, request, response, filterChain);
244    
245                            return;
246                    }
247    
248                    if (_log.isDebugEnabled()) {
249                            _log.debug("Redirect " + redirect);
250                    }
251    
252                    response.sendRedirect(redirect);
253            }
254    
255            private static final Log _log = LogFactoryUtil.getLog(I18nFilter.class);
256    
257            private static Set<String> _languageIds;
258    
259    }