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.LayoutFriendlyURLException;
018    import com.liferay.portal.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    
203                    Set<String> languageIds = I18nServlet.getLanguageIds();
204    
205                    for (String languageId : languageIds) {
206                            if (StringUtil.startsWith(friendlyURL, languageId)) {
207                                    pos = friendlyURL.indexOf(CharPool.SLASH, 1);
208    
209                                    if (((pos != -1) && (pos != languageId.length())) ||
210                                            ((pos == -1) &&
211                                             !StringUtil.equalsIgnoreCase(friendlyURL, languageId))) {
212    
213                                            continue;
214                                    }
215    
216                                    if (pos == -1) {
217                                            i18nLanguageId = languageId;
218                                            friendlyURL = StringPool.SLASH;
219                                    }
220                                    else {
221                                            i18nLanguageId = languageId.substring(0, pos);
222                                            friendlyURL = friendlyURL.substring(pos);
223                                    }
224    
225                                    break;
226                            }
227                    }
228    
229                    friendlyURL = StringUtil.replace(
230                            friendlyURL, PropsValues.WIDGET_SERVLET_MAPPING, StringPool.BLANK);
231    
232                    if (_log.isDebugEnabled()) {
233                            _log.debug("Friendly URL " + friendlyURL);
234                    }
235    
236                    if (!friendlyURL.equals(StringPool.SLASH) &&
237                            !isValidFriendlyURL(friendlyURL)) {
238    
239                            _log.debug("Friendly URL is not valid");
240    
241                            processFilter(
242                                    VirtualHostFilter.class.getName(), request, response,
243                                    filterChain);
244    
245                            return;
246                    }
247    
248                    LayoutSet layoutSet = (LayoutSet)request.getAttribute(
249                            WebKeys.VIRTUAL_HOST_LAYOUT_SET);
250    
251                    if (_log.isDebugEnabled()) {
252                            _log.debug("Layout set " + layoutSet);
253                    }
254    
255                    if (layoutSet == null) {
256                            processFilter(
257                                    VirtualHostFilter.class.getName(), request, response,
258                                    filterChain);
259    
260                            return;
261                    }
262    
263                    try {
264                            LastPath lastPath = new LastPath(
265                                    originalContextPath, friendlyURL, request.getParameterMap());
266    
267                            request.setAttribute(WebKeys.LAST_PATH, lastPath);
268    
269                            StringBundler forwardURL = new StringBundler(5);
270    
271                            if (i18nLanguageId != null) {
272                                    forwardURL.append(i18nLanguageId);
273                            }
274    
275                            if (originalFriendlyURL.startsWith(
276                                            PropsValues.WIDGET_SERVLET_MAPPING)) {
277    
278                                    forwardURL.append(PropsValues.WIDGET_SERVLET_MAPPING);
279    
280                                    friendlyURL = StringUtil.replaceFirst(
281                                            friendlyURL, PropsValues.WIDGET_SERVLET_MAPPING,
282                                            StringPool.BLANK);
283                            }
284    
285                            long plid = PortalUtil.getPlidFromFriendlyURL(
286                                    companyId, friendlyURL);
287    
288                            if (plid <= 0) {
289                                    Group group = GroupLocalServiceUtil.getGroup(
290                                            layoutSet.getGroupId());
291    
292                                    if (isDocumentFriendlyURL(
293                                                    request, group.getGroupId(), friendlyURL)) {
294    
295                                            processFilter(
296                                                    VirtualHostFilter.class.getName(), request, response,
297                                                    filterChain);
298    
299                                            return;
300                                    }
301    
302                                    if (group.isGuest() && friendlyURL.equals(StringPool.SLASH) &&
303                                            !layoutSet.isPrivateLayout()) {
304    
305                                            String homeURL = PortalUtil.getRelativeHomeURL(request);
306    
307                                            if (Validator.isNotNull(homeURL)) {
308                                                    friendlyURL = homeURL;
309                                            }
310                                    }
311                                    else {
312                                            if (layoutSet.isPrivateLayout()) {
313                                                    if (group.isUser()) {
314                                                            forwardURL.append(_PRIVATE_USER_SERVLET_MAPPING);
315                                                    }
316                                                    else {
317                                                            forwardURL.append(_PRIVATE_GROUP_SERVLET_MAPPING);
318                                                    }
319                                            }
320                                            else {
321                                                    forwardURL.append(_PUBLIC_GROUP_SERVLET_MAPPING);
322                                            }
323    
324                                            forwardURL.append(group.getFriendlyURL());
325                                    }
326                            }
327    
328                            forwardURL.append(friendlyURL);
329    
330                            if (_log.isDebugEnabled()) {
331                                    _log.debug("Forward to " + forwardURL);
332                            }
333    
334                            RequestDispatcher requestDispatcher =
335                                    _servletContext.getRequestDispatcher(forwardURL.toString());
336    
337                            requestDispatcher.forward(request, response);
338                    }
339                    catch (Exception e) {
340                            _log.error(e, e);
341    
342                            processFilter(
343                                    VirtualHostFilter.class.getName(), request, response,
344                                    filterChain);
345                    }
346            }
347    
348            private static final String _PATH_DOCUMENTS = "/documents/";
349    
350            private static final String _PATH_MODULE_SLASH =
351                    Portal.PATH_MODULE + StringPool.SLASH;
352    
353            private static final String _PRIVATE_GROUP_SERVLET_MAPPING =
354                    PropsValues.LAYOUT_FRIENDLY_URL_PRIVATE_GROUP_SERVLET_MAPPING;
355    
356            private static final String _PRIVATE_GROUP_SERVLET_MAPPING_SLASH =
357                    _PRIVATE_GROUP_SERVLET_MAPPING + StringPool.SLASH;
358    
359            private static final String _PRIVATE_USER_SERVLET_MAPPING =
360                    PropsValues.LAYOUT_FRIENDLY_URL_PRIVATE_USER_SERVLET_MAPPING;
361    
362            private static final String _PRIVATE_USER_SERVLET_MAPPING_SLASH =
363                    _PRIVATE_USER_SERVLET_MAPPING + StringPool.SLASH;
364    
365            private static final String _PUBLIC_GROUP_SERVLET_MAPPING =
366                    PropsValues.LAYOUT_FRIENDLY_URL_PUBLIC_SERVLET_MAPPING;
367    
368            private static final String _PUBLIC_GROUP_SERVLET_MAPPING_SLASH =
369                    _PUBLIC_GROUP_SERVLET_MAPPING + StringPool.SLASH;
370    
371            private static final Log _log = LogFactoryUtil.getLog(
372                    VirtualHostFilter.class);
373    
374            private ServletContext _servletContext;
375    
376    }