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.virtualhost;
016    
017    import com.liferay.portal.exception.LayoutFriendlyURLException;
018    import com.liferay.portal.exception.NoSuchLayoutException;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.struts.LastPath;
023    import com.liferay.portal.kernel.util.CharPool;
024    import com.liferay.portal.kernel.util.HttpUtil;
025    import com.liferay.portal.kernel.util.StringBundler;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.StringUtil;
028    import com.liferay.portal.kernel.util.Validator;
029    import com.liferay.portal.kernel.util.WebKeys;
030    import com.liferay.portal.model.Group;
031    import com.liferay.portal.model.LayoutSet;
032    import com.liferay.portal.model.impl.LayoutImpl;
033    import com.liferay.portal.service.GroupLocalServiceUtil;
034    import com.liferay.portal.service.LayoutLocalServiceUtil;
035    import com.liferay.portal.servlet.I18nServlet;
036    import com.liferay.portal.servlet.filters.BasePortalFilter;
037    import com.liferay.portal.util.Portal;
038    import com.liferay.portal.util.PortalInstances;
039    import com.liferay.portal.util.PortalUtil;
040    import com.liferay.portal.util.PropsValues;
041    import com.liferay.portal.webserver.WebServerServlet;
042    
043    import java.util.Set;
044    
045    import javax.servlet.FilterChain;
046    import javax.servlet.FilterConfig;
047    import javax.servlet.RequestDispatcher;
048    import javax.servlet.ServletContext;
049    import javax.servlet.http.HttpServletRequest;
050    import javax.servlet.http.HttpServletResponse;
051    
052    /**
053     * <p>
054     * This filter is used to provide virtual host functionality.
055     * </p>
056     *
057     * @author Joel Kozikowski
058     * @author Brian Wing Shun Chan
059     * @author Raymond Aug??
060     * @author Eduardo Lundgren
061     */
062    public class VirtualHostFilter extends BasePortalFilter {
063    
064            @Override
065            public void init(FilterConfig filterConfig) {
066                    super.init(filterConfig);
067    
068                    _servletContext = filterConfig.getServletContext();
069            }
070    
071            @Override
072            public boolean isFilterEnabled(
073                    HttpServletRequest request, HttpServletResponse response) {
074    
075                    StringBuffer requestURL = request.getRequestURL();
076    
077                    if (isValidRequestURL(requestURL)) {
078                            return true;
079                    }
080                    else {
081                            return false;
082                    }
083            }
084    
085            protected boolean isDocumentFriendlyURL(
086                            HttpServletRequest request, long groupId, String friendlyURL)
087                    throws PortalException {
088    
089                    if (friendlyURL.startsWith(_PATH_DOCUMENTS) &&
090                            WebServerServlet.hasFiles(request)) {
091    
092                            String path = HttpUtil.fixPath(request.getPathInfo());
093    
094                            String[] pathArray = StringUtil.split(path, CharPool.SLASH);
095    
096                            if (pathArray.length == 2) {
097                                    try {
098                                            LayoutLocalServiceUtil.getFriendlyURLLayout(
099                                                    groupId, false, friendlyURL);
100                                    }
101                                    catch (NoSuchLayoutException nsle) {
102                                            return true;
103                                    }
104                            }
105                            else {
106                                    return true;
107                            }
108                    }
109    
110                    return false;
111            }
112    
113            protected boolean isValidFriendlyURL(String friendlyURL) {
114                    friendlyURL = StringUtil.toLowerCase(friendlyURL);
115    
116                    if (PortalInstances.isVirtualHostsIgnorePath(friendlyURL) ||
117                            friendlyURL.startsWith(_PATH_MODULE_SLASH) ||
118                            friendlyURL.startsWith(_PRIVATE_GROUP_SERVLET_MAPPING_SLASH) ||
119                            friendlyURL.startsWith(_PRIVATE_USER_SERVLET_MAPPING_SLASH) ||
120                            friendlyURL.startsWith(_PUBLIC_GROUP_SERVLET_MAPPING_SLASH)) {
121    
122                            return false;
123                    }
124    
125                    if (LayoutImpl.hasFriendlyURLKeyword(friendlyURL)) {
126                            return false;
127                    }
128    
129                    int code = LayoutImpl.validateFriendlyURL(friendlyURL, false);
130    
131                    if ((code > -1) &&
132                            (code != LayoutFriendlyURLException.ENDS_WITH_SLASH)) {
133    
134                            return false;
135                    }
136    
137                    return true;
138            }
139    
140            protected boolean isValidRequestURL(StringBuffer requestURL) {
141                    if (requestURL == null) {
142                            return false;
143                    }
144    
145                    String url = requestURL.toString();
146    
147                    for (String extension : PropsValues.VIRTUAL_HOSTS_IGNORE_EXTENSIONS) {
148                            if (url.endsWith(extension)) {
149                                    return false;
150                            }
151                    }
152    
153                    return true;
154            }
155    
156            @Override
157            protected void processFilter(
158                            HttpServletRequest request, HttpServletResponse response,
159                            FilterChain filterChain)
160                    throws Exception {
161    
162                    long companyId = PortalInstances.getCompanyId(request);
163    
164                    String originalContextPath = PortalUtil.getPathContext();
165    
166                    String contextPath = originalContextPath;
167    
168                    String originalFriendlyURL = request.getRequestURI();
169    
170                    String friendlyURL = originalFriendlyURL;
171    
172                    friendlyURL = StringUtil.replace(
173                            friendlyURL, StringPool.DOUBLE_SLASH, StringPool.SLASH);
174    
175                    if (!friendlyURL.equals(StringPool.SLASH) &&
176                            Validator.isNotNull(contextPath)) {
177    
178                            String proxyPath = PortalUtil.getPathProxy();
179    
180                            if (Validator.isNotNull(proxyPath) &&
181                                    contextPath.startsWith(proxyPath)) {
182    
183                                    contextPath = contextPath.substring(proxyPath.length());
184                            }
185    
186                            if (friendlyURL.startsWith(contextPath) &&
187                                    StringUtil.startsWith(
188                                            friendlyURL.substring(contextPath.length()),
189                                            StringPool.SLASH)) {
190    
191                                    friendlyURL = friendlyURL.substring(contextPath.length());
192                            }
193                    }
194    
195                    int pos = friendlyURL.indexOf(StringPool.SEMICOLON);
196    
197                    if (pos != -1) {
198                            friendlyURL = friendlyURL.substring(0, pos);
199                    }
200    
201                    String i18nLanguageId = null;
202                    String i18nLanguageIdLowerCase = null;
203    
204                    Set<String> languageIds = I18nServlet.getLanguageIds();
205    
206                    for (String languageId : languageIds) {
207                            if (StringUtil.startsWith(friendlyURL, languageId)) {
208                                    pos = friendlyURL.indexOf(CharPool.SLASH, 1);
209    
210                                    if (((pos != -1) && (pos != languageId.length())) ||
211                                            ((pos == -1) &&
212                                             !StringUtil.equalsIgnoreCase(friendlyURL, languageId))) {
213    
214                                            continue;
215                                    }
216    
217                                    if (!friendlyURL.startsWith(languageId)) {
218                                            i18nLanguageIdLowerCase = StringUtil.toLowerCase(
219                                                    languageId);
220                                    }
221    
222                                    if (pos == -1) {
223                                            i18nLanguageId = languageId;
224                                            friendlyURL = StringPool.SLASH;
225                                    }
226                                    else {
227                                            i18nLanguageId = languageId.substring(0, pos);
228                                            friendlyURL = friendlyURL.substring(pos);
229                                    }
230    
231                                    break;
232                            }
233                    }
234    
235                    friendlyURL = StringUtil.replace(
236                            friendlyURL, PropsValues.WIDGET_SERVLET_MAPPING, StringPool.BLANK);
237    
238                    if (_log.isDebugEnabled()) {
239                            _log.debug("Friendly URL " + friendlyURL);
240                    }
241    
242                    if (!friendlyURL.equals(StringPool.SLASH) &&
243                            !isValidFriendlyURL(friendlyURL)) {
244    
245                            _log.debug("Friendly URL is not valid");
246    
247                            if (Validator.isNotNull(i18nLanguageIdLowerCase)) {
248                                    String forwardURL = StringUtil.replace(
249                                            originalFriendlyURL, i18nLanguageIdLowerCase,
250                                            i18nLanguageId);
251    
252                                    RequestDispatcher requestDispatcher =
253                                            _servletContext.getRequestDispatcher(forwardURL);
254    
255                                    requestDispatcher.forward(request, response);
256    
257                                    return;
258                            }
259                            else {
260                                    processFilter(
261                                            VirtualHostFilter.class.getName(), request, response,
262                                            filterChain);
263    
264                                    return;
265                            }
266                    }
267    
268                    LayoutSet layoutSet = (LayoutSet)request.getAttribute(
269                            WebKeys.VIRTUAL_HOST_LAYOUT_SET);
270    
271                    if (_log.isDebugEnabled()) {
272                            _log.debug("Layout set " + layoutSet);
273                    }
274    
275                    if (layoutSet == null) {
276                            processFilter(
277                                    VirtualHostFilter.class.getName(), request, response,
278                                    filterChain);
279    
280                            return;
281                    }
282    
283                    try {
284                            LastPath lastPath = new LastPath(
285                                    originalContextPath, friendlyURL,
286                                    HttpUtil.parameterMapToString(request.getParameterMap()));
287    
288                            request.setAttribute(WebKeys.LAST_PATH, lastPath);
289    
290                            StringBundler forwardURL = new StringBundler(5);
291    
292                            if (i18nLanguageId != null) {
293                                    forwardURL.append(i18nLanguageId);
294                            }
295    
296                            if (originalFriendlyURL.startsWith(
297                                            PropsValues.WIDGET_SERVLET_MAPPING)) {
298    
299                                    forwardURL.append(PropsValues.WIDGET_SERVLET_MAPPING);
300    
301                                    friendlyURL = StringUtil.replaceFirst(
302                                            friendlyURL, PropsValues.WIDGET_SERVLET_MAPPING,
303                                            StringPool.BLANK);
304                            }
305    
306                            long plid = PortalUtil.getPlidFromFriendlyURL(
307                                    companyId, friendlyURL);
308    
309                            if (plid <= 0) {
310                                    Group group = GroupLocalServiceUtil.getGroup(
311                                            layoutSet.getGroupId());
312    
313                                    if (isDocumentFriendlyURL(
314                                                    request, group.getGroupId(), friendlyURL)) {
315    
316                                            processFilter(
317                                                    VirtualHostFilter.class.getName(), request, response,
318                                                    filterChain);
319    
320                                            return;
321                                    }
322    
323                                    if (group.isGuest() && friendlyURL.equals(StringPool.SLASH) &&
324                                            !layoutSet.isPrivateLayout()) {
325    
326                                            String homeURL = PortalUtil.getRelativeHomeURL(request);
327    
328                                            if (Validator.isNotNull(homeURL)) {
329                                                    friendlyURL = homeURL;
330                                            }
331                                    }
332                                    else {
333                                            if (layoutSet.isPrivateLayout()) {
334                                                    if (group.isUser()) {
335                                                            forwardURL.append(_PRIVATE_USER_SERVLET_MAPPING);
336                                                    }
337                                                    else {
338                                                            forwardURL.append(_PRIVATE_GROUP_SERVLET_MAPPING);
339                                                    }
340                                            }
341                                            else {
342                                                    forwardURL.append(_PUBLIC_GROUP_SERVLET_MAPPING);
343                                            }
344    
345                                            forwardURL.append(group.getFriendlyURL());
346                                    }
347                            }
348    
349                            forwardURL.append(friendlyURL);
350    
351                            if (_log.isDebugEnabled()) {
352                                    _log.debug("Forward to " + forwardURL);
353                            }
354    
355                            RequestDispatcher requestDispatcher =
356                                    _servletContext.getRequestDispatcher(forwardURL.toString());
357    
358                            requestDispatcher.forward(request, response);
359                    }
360                    catch (Exception e) {
361                            _log.error(e, e);
362    
363                            processFilter(
364                                    VirtualHostFilter.class.getName(), request, response,
365                                    filterChain);
366                    }
367            }
368    
369            private static final String _PATH_DOCUMENTS = "/documents/";
370    
371            private static final String _PATH_MODULE_SLASH =
372                    Portal.PATH_MODULE + StringPool.SLASH;
373    
374            private static final String _PRIVATE_GROUP_SERVLET_MAPPING =
375                    PropsValues.LAYOUT_FRIENDLY_URL_PRIVATE_GROUP_SERVLET_MAPPING;
376    
377            private static final String _PRIVATE_GROUP_SERVLET_MAPPING_SLASH =
378                    _PRIVATE_GROUP_SERVLET_MAPPING + StringPool.SLASH;
379    
380            private static final String _PRIVATE_USER_SERVLET_MAPPING =
381                    PropsValues.LAYOUT_FRIENDLY_URL_PRIVATE_USER_SERVLET_MAPPING;
382    
383            private static final String _PRIVATE_USER_SERVLET_MAPPING_SLASH =
384                    _PRIVATE_USER_SERVLET_MAPPING + StringPool.SLASH;
385    
386            private static final String _PUBLIC_GROUP_SERVLET_MAPPING =
387                    PropsValues.LAYOUT_FRIENDLY_URL_PUBLIC_SERVLET_MAPPING;
388    
389            private static final String _PUBLIC_GROUP_SERVLET_MAPPING_SLASH =
390                    _PUBLIC_GROUP_SERVLET_MAPPING + StringPool.SLASH;
391    
392            private static final Log _log = LogFactoryUtil.getLog(
393                    VirtualHostFilter.class);
394    
395            private ServletContext _servletContext;
396    
397    }