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.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<String>();
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                            int[] groupFriendlyURLIndex = PortalUtil.getGroupFriendlyURLIndex(
135                                    requestURI);
136    
137                            if (groupFriendlyURLIndex != null) {
138                                    int x = groupFriendlyURLIndex[0];
139                                    int y = groupFriendlyURLIndex[1];
140    
141                                    String groupFriendlyURL = requestURI.substring(x, y);
142    
143                                    Group group = layoutSet.getGroup();
144    
145                                    if (groupFriendlyURL.equals(group.getFriendlyURL())) {
146                                            redirect = contextPath + i18nPath + requestURI.substring(y);
147                                    }
148                            }
149                    }
150    
151                    String queryString = request.getQueryString();
152    
153                    if (Validator.isNotNull(queryString)) {
154                            redirect += StringPool.QUESTION + request.getQueryString();
155                    }
156    
157                    return redirect;
158            }
159    
160            protected boolean isAlreadyFiltered(HttpServletRequest request) {
161                    if (request.getAttribute(SKIP_FILTER) != null) {
162                            return true;
163                    }
164                    else {
165                            return false;
166                    }
167            }
168    
169            protected boolean isForwardedByI18nServlet(HttpServletRequest request) {
170                    if ((request.getAttribute(WebKeys.I18N_LANGUAGE_ID) != null) ||
171                            (request.getAttribute(WebKeys.I18N_PATH) != null)) {
172    
173                            return true;
174                    }
175                    else {
176                            return false;
177                    }
178            }
179    
180            protected String prependI18nLanguageId(
181                    HttpServletRequest request, int prependFriendlyUrlStyle) {
182    
183                    String userLanguageId = null;
184    
185                    User user = (User)request.getAttribute(WebKeys.USER);
186    
187                    if (user != null) {
188                            userLanguageId = user.getLanguageId();
189                    }
190    
191                    String guestLanguageId = userLanguageId;
192    
193                    if (Validator.isNull(guestLanguageId)) {
194                            guestLanguageId = CookieKeys.getCookie(
195                                    request, CookieKeys.GUEST_LANGUAGE_ID, false);
196                    }
197    
198                    String defaultLanguageId = LocaleUtil.toLanguageId(
199                            LocaleUtil.getDefault());
200    
201                    if (Validator.isNull(guestLanguageId)) {
202                            guestLanguageId = defaultLanguageId;
203                    }
204    
205                    if ((prependFriendlyUrlStyle == 1) ||
206                            ((prependFriendlyUrlStyle == 3) &&
207                             Validator.isNull(userLanguageId))) {
208    
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 (Validator.isNotNull(userLanguageId)) {
221                                    HttpSession session = request.getSession();
222    
223                                    Locale locale = (Locale)session.getAttribute(
224                                            Globals.LOCALE_KEY);
225    
226                                    if (!userLanguageId.equals(LocaleUtil.toLanguageId(locale))) {
227                                            PortalUtil.addUserLocaleOptionsMessage(request);
228    
229                                            return LocaleUtil.toLanguageId(locale);
230                                    }
231                                    else {
232                                            return null;
233                                    }
234                            }
235                    }
236    
237                    return null;
238            }
239    
240            @Override
241            protected void processFilter(
242                            HttpServletRequest request, HttpServletResponse response,
243                            FilterChain filterChain)
244                    throws Exception {
245    
246                    request.setAttribute(SKIP_FILTER, Boolean.TRUE);
247    
248                    String redirect = getRedirect(request);
249    
250                    if (redirect == null) {
251                            processFilter(I18nFilter.class, request, response, filterChain);
252    
253                            return;
254                    }
255    
256                    if (_log.isDebugEnabled()) {
257                            _log.debug("Redirect " + redirect);
258                    }
259    
260                    response.sendRedirect(redirect);
261            }
262    
263            private static Log _log = LogFactoryUtil.getLog(I18nFilter.class);
264    
265            private static Set<String> _languageIds;
266    
267    }