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