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.model.Group;
030    import com.liferay.portal.model.LayoutSet;
031    import com.liferay.portal.model.impl.LayoutImpl;
032    import com.liferay.portal.service.GroupLocalServiceUtil;
033    import com.liferay.portal.service.LayoutLocalServiceUtil;
034    import com.liferay.portal.servlet.I18nServlet;
035    import com.liferay.portal.servlet.filters.BasePortalFilter;
036    import com.liferay.portal.util.Portal;
037    import com.liferay.portal.util.PortalInstances;
038    import com.liferay.portal.util.PortalUtil;
039    import com.liferay.portal.util.PropsValues;
040    import com.liferay.portal.util.WebKeys;
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 contextPath = PortalUtil.getPathContext();
165    
166                    String originalFriendlyURL = request.getRequestURI();
167    
168                    String friendlyURL = originalFriendlyURL;
169    
170                    if (Validator.isNotNull(contextPath) &&
171                            friendlyURL.contains(contextPath)) {
172    
173                            friendlyURL = friendlyURL.substring(contextPath.length());
174                    }
175    
176                    int pos = friendlyURL.indexOf(StringPool.SEMICOLON);
177    
178                    if (pos != -1) {
179                            friendlyURL = friendlyURL.substring(0, pos);
180                    }
181    
182                    friendlyURL = StringUtil.replace(
183                            friendlyURL, StringPool.DOUBLE_SLASH, StringPool.SLASH);
184    
185                    String i18nLanguageId = null;
186    
187                    Set<String> languageIds = I18nServlet.getLanguageIds();
188    
189                    for (String languageId : languageIds) {
190                            if (StringUtil.startsWith(friendlyURL, languageId)) {
191                                    pos = friendlyURL.indexOf(CharPool.SLASH, 1);
192    
193                                    if (((pos != -1) && (pos != languageId.length())) ||
194                                            ((pos == -1) &&
195                                             !StringUtil.equalsIgnoreCase(friendlyURL, languageId))) {
196    
197                                            continue;
198                                    }
199    
200                                    if (pos == -1) {
201                                            i18nLanguageId = languageId;
202                                            friendlyURL = StringPool.SLASH;
203                                    }
204                                    else {
205                                            i18nLanguageId = languageId.substring(0, pos);
206                                            friendlyURL = friendlyURL.substring(pos);
207                                    }
208    
209                                    break;
210                            }
211                    }
212    
213                    friendlyURL = StringUtil.replace(
214                            friendlyURL, PropsValues.WIDGET_SERVLET_MAPPING, StringPool.BLANK);
215    
216                    if (_log.isDebugEnabled()) {
217                            _log.debug("Friendly URL " + friendlyURL);
218                    }
219    
220                    if (!friendlyURL.equals(StringPool.SLASH) &&
221                            !isValidFriendlyURL(friendlyURL)) {
222    
223                            _log.debug("Friendly URL is not valid");
224    
225                            processFilter(
226                                    VirtualHostFilter.class, request, response, filterChain);
227    
228                            return;
229                    }
230    
231                    LayoutSet layoutSet = (LayoutSet)request.getAttribute(
232                            WebKeys.VIRTUAL_HOST_LAYOUT_SET);
233    
234                    if (_log.isDebugEnabled()) {
235                            _log.debug("Layout set " + layoutSet);
236                    }
237    
238                    if (layoutSet == null) {
239                            processFilter(
240                                    VirtualHostFilter.class, request, response, filterChain);
241    
242                            return;
243                    }
244    
245                    try {
246                            LastPath lastPath = new LastPath(
247                                    contextPath, friendlyURL, request.getParameterMap());
248    
249                            request.setAttribute(WebKeys.LAST_PATH, lastPath);
250    
251                            StringBundler forwardURL = new StringBundler(5);
252    
253                            if (i18nLanguageId != null) {
254                                    forwardURL.append(i18nLanguageId);
255                            }
256    
257                            if (originalFriendlyURL.startsWith(
258                                            PropsValues.WIDGET_SERVLET_MAPPING)) {
259    
260                                    forwardURL.append(PropsValues.WIDGET_SERVLET_MAPPING);
261    
262                                    friendlyURL = StringUtil.replaceFirst(
263                                            friendlyURL, PropsValues.WIDGET_SERVLET_MAPPING,
264                                            StringPool.BLANK);
265                            }
266    
267                            long plid = PortalUtil.getPlidFromFriendlyURL(
268                                    companyId, friendlyURL);
269    
270                            if (plid <= 0) {
271                                    Group group = GroupLocalServiceUtil.getGroup(
272                                            layoutSet.getGroupId());
273    
274                                    if (isDocumentFriendlyURL(
275                                                    request, group.getGroupId(), friendlyURL)) {
276    
277                                            processFilter(
278                                                    VirtualHostFilter.class, request, response,
279                                                    filterChain);
280    
281                                            return;
282                                    }
283    
284                                    if (group.isGuest() && friendlyURL.equals(StringPool.SLASH) &&
285                                            !layoutSet.isPrivateLayout()) {
286    
287                                            String homeURL = PortalUtil.getRelativeHomeURL(request);
288    
289                                            if (Validator.isNotNull(homeURL)) {
290                                                    friendlyURL = homeURL;
291                                            }
292                                    }
293                                    else {
294                                            if (layoutSet.isPrivateLayout()) {
295                                                    if (group.isUser()) {
296                                                            forwardURL.append(_PRIVATE_USER_SERVLET_MAPPING);
297                                                    }
298                                                    else {
299                                                            forwardURL.append(_PRIVATE_GROUP_SERVLET_MAPPING);
300                                                    }
301                                            }
302                                            else {
303                                                    forwardURL.append(_PUBLIC_GROUP_SERVLET_MAPPING);
304                                            }
305    
306                                            forwardURL.append(group.getFriendlyURL());
307                                    }
308                            }
309    
310                            forwardURL.append(friendlyURL);
311    
312                            if (_log.isDebugEnabled()) {
313                                    _log.debug("Forward to " + forwardURL);
314                            }
315    
316                            RequestDispatcher requestDispatcher =
317                                    _servletContext.getRequestDispatcher(forwardURL.toString());
318    
319                            requestDispatcher.forward(request, response);
320                    }
321                    catch (Exception e) {
322                            _log.error(e, e);
323    
324                            processFilter(
325                                    VirtualHostFilter.class, request, response, filterChain);
326                    }
327            }
328    
329            private static final String _PATH_DOCUMENTS = "/documents/";
330    
331            private static final String _PATH_MODULE_SLASH =
332                    Portal.PATH_MODULE + StringPool.SLASH;
333    
334            private static final String _PRIVATE_GROUP_SERVLET_MAPPING =
335                    PropsValues.LAYOUT_FRIENDLY_URL_PRIVATE_GROUP_SERVLET_MAPPING;
336    
337            private static final String _PRIVATE_GROUP_SERVLET_MAPPING_SLASH =
338                    _PRIVATE_GROUP_SERVLET_MAPPING + StringPool.SLASH;
339    
340            private static final String _PRIVATE_USER_SERVLET_MAPPING =
341                    PropsValues.LAYOUT_FRIENDLY_URL_PRIVATE_USER_SERVLET_MAPPING;
342    
343            private static final String _PRIVATE_USER_SERVLET_MAPPING_SLASH =
344                    _PRIVATE_USER_SERVLET_MAPPING + StringPool.SLASH;
345    
346            private static final String _PUBLIC_GROUP_SERVLET_MAPPING =
347                    PropsValues.LAYOUT_FRIENDLY_URL_PUBLIC_SERVLET_MAPPING;
348    
349            private static final String _PUBLIC_GROUP_SERVLET_MAPPING_SLASH =
350                    _PUBLIC_GROUP_SERVLET_MAPPING + StringPool.SLASH;
351    
352            private static final Log _log = LogFactoryUtil.getLog(
353                    VirtualHostFilter.class);
354    
355            private ServletContext _servletContext;
356    
357    }