001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.servlet.filters.cache;
016    
017    import com.liferay.portal.NoSuchLayoutException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.language.LanguageUtil;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
023    import com.liferay.portal.kernel.servlet.ByteBufferServletResponse;
024    import com.liferay.portal.kernel.servlet.HttpHeaders;
025    import com.liferay.portal.kernel.struts.LastPath;
026    import com.liferay.portal.kernel.util.CharPool;
027    import com.liferay.portal.kernel.util.GetterUtil;
028    import com.liferay.portal.kernel.util.Http;
029    import com.liferay.portal.kernel.util.HttpUtil;
030    import com.liferay.portal.kernel.util.JavaConstants;
031    import com.liferay.portal.kernel.util.ParamUtil;
032    import com.liferay.portal.kernel.util.StringBundler;
033    import com.liferay.portal.kernel.util.StringPool;
034    import com.liferay.portal.kernel.util.StringUtil;
035    import com.liferay.portal.kernel.util.UnicodeProperties;
036    import com.liferay.portal.kernel.util.Validator;
037    import com.liferay.portal.model.Group;
038    import com.liferay.portal.model.Layout;
039    import com.liferay.portal.model.LayoutTypePortletConstants;
040    import com.liferay.portal.model.Portlet;
041    import com.liferay.portal.model.PortletConstants;
042    import com.liferay.portal.service.GroupLocalServiceUtil;
043    import com.liferay.portal.service.LayoutLocalServiceUtil;
044    import com.liferay.portal.service.PortletLocalServiceUtil;
045    import com.liferay.portal.servlet.filters.BasePortalFilter;
046    import com.liferay.portal.util.PortalInstances;
047    import com.liferay.portal.util.PortalUtil;
048    import com.liferay.portal.util.PropsValues;
049    import com.liferay.portal.util.WebKeys;
050    import com.liferay.util.servlet.filters.CacheResponseData;
051    import com.liferay.util.servlet.filters.CacheResponseUtil;
052    
053    import javax.servlet.FilterChain;
054    import javax.servlet.FilterConfig;
055    import javax.servlet.http.HttpServletRequest;
056    import javax.servlet.http.HttpServletResponse;
057    import javax.servlet.http.HttpSession;
058    
059    /**
060     * @author Alexander Chow
061     * @author Javier de Ros
062     * @author Raymond Augé
063     */
064    public class CacheFilter extends BasePortalFilter {
065    
066            public static final String SKIP_FILTER = CacheFilter.class + "SKIP_FILTER";
067    
068            @Override
069            public void init(FilterConfig filterConfig) {
070                    super.init(filterConfig);
071    
072                    _pattern = GetterUtil.getInteger(
073                            filterConfig.getInitParameter("pattern"));
074    
075                    if ((_pattern != _PATTERN_FRIENDLY) &&
076                            (_pattern != _PATTERN_LAYOUT) &&
077                            (_pattern != _PATTERN_RESOURCE)) {
078    
079                            _log.error("Cache pattern is invalid");
080                    }
081            }
082    
083            @Override
084            public boolean isFilterEnabled(
085                    HttpServletRequest request, HttpServletResponse response) {
086    
087                    if (isCacheableRequest(request) && !isInclude(request) &&
088                            !isAlreadyFiltered(request)) {
089    
090                            return true;
091                    }
092                    else {
093                            return false;
094                    }
095            }
096    
097            protected String getCacheKey(HttpServletRequest request) {
098                    StringBundler sb = new StringBundler(13);
099    
100                    // Url
101    
102                    sb.append(HttpUtil.getProtocol(request));
103                    sb.append(Http.PROTOCOL_DELIMITER);
104                    sb.append(request.getContextPath());
105                    sb.append(request.getServletPath());
106                    sb.append(request.getPathInfo());
107                    sb.append(StringPool.QUESTION);
108    
109                    String queryString = request.getQueryString();
110    
111                    if (queryString == null) {
112                            queryString = (String)request.getAttribute(
113                                    JavaConstants.JAVAX_SERVLET_FORWARD_QUERY_STRING);
114    
115                            if (queryString == null) {
116                                    String url = PortalUtil.getCurrentCompleteURL(request);
117    
118                                    int pos = url.indexOf(StringPool.QUESTION);
119    
120                                    if (pos > -1) {
121                                            queryString = url.substring(pos + 1);
122                                    }
123                            }
124                    }
125    
126                    if (queryString != null) {
127                            sb.append(queryString);
128                    }
129    
130                    // Language
131    
132                    sb.append(StringPool.POUND);
133    
134                    String languageId = (String)request.getAttribute(
135                            WebKeys.I18N_LANGUAGE_ID);
136    
137                    if (Validator.isNull(languageId)) {
138                            languageId = LanguageUtil.getLanguageId(request);
139                    }
140    
141                    sb.append(languageId);
142    
143                    // User agent
144    
145                    String userAgent = GetterUtil.getString(
146                            request.getHeader(HttpHeaders.USER_AGENT));
147    
148                    sb.append(StringPool.POUND);
149                    sb.append(userAgent.toLowerCase().hashCode());
150    
151                    // Gzip compression
152    
153                    sb.append(StringPool.POUND);
154                    sb.append(BrowserSnifferUtil.acceptsGzip(request));
155    
156                    return sb.toString().trim().toUpperCase();
157            }
158    
159            protected long getPlid(
160                    long companyId, String pathInfo, String servletPath, long defaultPlid) {
161    
162                    if (_pattern == _PATTERN_LAYOUT) {
163                            return defaultPlid;
164                    }
165    
166                    if (Validator.isNull(pathInfo) ||
167                            !pathInfo.startsWith(StringPool.SLASH)) {
168    
169                            return 0;
170                    }
171    
172                    // Group friendly URL
173    
174                    String friendlyURL = null;
175    
176                    int pos = pathInfo.indexOf(CharPool.SLASH, 1);
177    
178                    if (pos != -1) {
179                            friendlyURL = pathInfo.substring(0, pos);
180                    }
181                    else if (pathInfo.length() > 1) {
182                            friendlyURL = pathInfo;
183                    }
184    
185                    if (Validator.isNull(friendlyURL)) {
186                            return 0;
187                    }
188    
189                    long groupId = 0;
190                    boolean privateLayout = false;
191    
192                    try {
193                            Group group = GroupLocalServiceUtil.getFriendlyURLGroup(
194                                    companyId, friendlyURL);
195    
196                            groupId = group.getGroupId();
197    
198                            if (servletPath.startsWith(
199                                            PropsValues.
200                                                    LAYOUT_FRIENDLY_URL_PRIVATE_GROUP_SERVLET_MAPPING) ||
201                                    servletPath.startsWith(
202                                            PropsValues.
203                                                    LAYOUT_FRIENDLY_URL_PRIVATE_USER_SERVLET_MAPPING)) {
204    
205                                    privateLayout = true;
206                            }
207                            else if (servletPath.startsWith(
208                                                    PropsValues.
209                                                            LAYOUT_FRIENDLY_URL_PUBLIC_SERVLET_MAPPING)) {
210    
211                                    privateLayout = false;
212                            }
213                    }
214                    catch (NoSuchLayoutException nsle) {
215                            if (_log.isWarnEnabled()) {
216                                    _log.warn(nsle);
217                            }
218                    }
219                    catch (Exception e) {
220                            if (_log.isWarnEnabled()) {
221                                    _log.error(e);
222                            }
223    
224                            return 0;
225                    }
226    
227                    // Layout friendly URL
228    
229                    friendlyURL = null;
230    
231                    if ((pos != -1) && ((pos + 1) != pathInfo.length())) {
232                            friendlyURL = pathInfo.substring(pos);
233                    }
234    
235                    if (Validator.isNull(friendlyURL)) {
236                            try {
237                                    long plid = LayoutLocalServiceUtil.getDefaultPlid(
238                                            groupId, privateLayout);
239    
240                                    return plid;
241                            }
242                            catch (Exception e) {
243                                    _log.warn(e);
244    
245                                    return 0;
246                            }
247                    }
248                    else if (friendlyURL.endsWith(StringPool.FORWARD_SLASH)) {
249                            friendlyURL = friendlyURL.substring(0, friendlyURL.length() - 1);
250                    }
251    
252                    // If there is no layout path take the first from the group or user
253    
254                    try {
255                            Layout layout = LayoutLocalServiceUtil.getFriendlyURLLayout(
256                                    groupId, privateLayout, friendlyURL);
257    
258                            return layout.getPlid();
259                    }
260                    catch (NoSuchLayoutException nsle) {
261                            _log.warn(nsle);
262    
263                            return 0;
264                    }
265                    catch (Exception e) {
266                            _log.error(e);
267    
268                            return 0;
269                    }
270            }
271    
272            protected boolean isAlreadyFiltered(HttpServletRequest request) {
273                    if (request.getAttribute(SKIP_FILTER) != null) {
274                            return true;
275                    }
276                    else {
277                            return false;
278                    }
279            }
280    
281            protected boolean isCacheableColumn(long companyId, String columnSettings)
282                    throws SystemException {
283    
284                    String[] portletIds = StringUtil.split(columnSettings);
285    
286                    for (String portletId : portletIds) {
287                            portletId = PortletConstants.getRootPortletId(portletId);
288    
289                            Portlet portlet = PortletLocalServiceUtil.getPortletById(
290                                    companyId, portletId);
291    
292                            if (!portlet.isLayoutCacheable()) {
293                                    return false;
294                            }
295                    }
296    
297                    return true;
298            }
299    
300            protected boolean isCacheableData(
301                    long companyId, HttpServletRequest request) {
302    
303                    try {
304                            if (_pattern == _PATTERN_RESOURCE) {
305                                    return true;
306                            }
307    
308                            long plid = getPlid(
309                                    companyId, request.getPathInfo(), request.getServletPath(),
310                                    ParamUtil.getLong(request, "p_l_id"));
311    
312                            if (plid <= 0) {
313                                    return false;
314                            }
315    
316                            Layout layout = LayoutLocalServiceUtil.getLayout(plid);
317    
318                            if (!layout.isTypePortlet()) {
319                                    return false;
320                            }
321    
322                            UnicodeProperties properties = layout.getTypeSettingsProperties();
323    
324                            for (int i = 0; i < 10; i++) {
325                                    String columnId = "column-" + i;
326    
327                                    String settings = properties.getProperty(
328                                            columnId, StringPool.BLANK);
329    
330                                    if (!isCacheableColumn(companyId, settings)) {
331                                            return false;
332                                    }
333                            }
334    
335                            String columnIdsString = properties.get(
336                                    LayoutTypePortletConstants.NESTED_COLUMN_IDS);
337    
338                            if (columnIdsString != null) {
339                                    String[] columnIds = StringUtil.split(columnIdsString);
340    
341                                    for (String columnId : columnIds) {
342                                            String settings = properties.getProperty(
343                                                    columnId, StringPool.BLANK);
344    
345                                            if (!isCacheableColumn(companyId, settings)) {
346                                                    return false;
347                                            }
348                                    }
349                            }
350    
351                            return true;
352                    }
353                    catch (Exception e) {
354                            return false;
355                    }
356            }
357    
358            protected boolean isCacheableRequest(HttpServletRequest request) {
359                    String portletId = ParamUtil.getString(request, "p_p_id");
360    
361                    if (Validator.isNotNull(portletId)) {
362                            return false;
363                    }
364    
365                    if ((_pattern == _PATTERN_FRIENDLY) || (_pattern == _PATTERN_LAYOUT)) {
366                            long userId = PortalUtil.getUserId(request);
367                            String remoteUser = request.getRemoteUser();
368    
369                            if ((userId > 0) || Validator.isNotNull(remoteUser)) {
370                                    return false;
371                            }
372                    }
373    
374                    if (_pattern == _PATTERN_LAYOUT) {
375                            String plid = ParamUtil.getString(request, "p_l_id");
376    
377                            if (Validator.isNull(plid)) {
378                                    return false;
379                            }
380                    }
381    
382                    return true;
383            }
384    
385            protected boolean isCacheableResponse(
386                    ByteBufferServletResponse byteBufferResponse) {
387    
388                    if ((byteBufferResponse.getStatus() == HttpServletResponse.SC_OK) &&
389                            (byteBufferResponse.getBufferSize() <
390                                    PropsValues.CACHE_CONTENT_THRESHOLD_SIZE)) {
391    
392                            return true;
393                    }
394                    else {
395                            return false;
396                    }
397            }
398    
399            protected boolean isInclude(HttpServletRequest request) {
400                    String uri = (String)request.getAttribute(
401                            JavaConstants.JAVAX_SERVLET_INCLUDE_REQUEST_URI);
402    
403                    if (uri == null) {
404                            return false;
405                    }
406                    else {
407                            return true;
408                    }
409            }
410    
411            @Override
412            protected void processFilter(
413                            HttpServletRequest request, HttpServletResponse response,
414                            FilterChain filterChain)
415                    throws Exception {
416    
417                    request.setAttribute(SKIP_FILTER, Boolean.TRUE);
418    
419                    String key = getCacheKey(request);
420    
421                    long companyId = PortalInstances.getCompanyId(request);
422    
423                    CacheResponseData cacheResponseData = CacheUtil.getCacheResponseData(
424                            companyId, key);
425    
426                    if (cacheResponseData == null) {
427                            if (!isCacheableData(companyId, request)) {
428                                    if (_log.isDebugEnabled()) {
429                                            _log.debug("Request is not cacheable " + key);
430                                    }
431    
432                                    processFilter(
433                                            CacheFilter.class, request, response, filterChain);
434    
435                                    return;
436                            }
437    
438                            if (_log.isInfoEnabled()) {
439                                    _log.info("Caching request " + key);
440                            }
441    
442                            ByteBufferServletResponse byteBufferResponse =
443                                    new ByteBufferServletResponse(response);
444    
445                            processFilter(
446                                    CacheFilter.class, request, byteBufferResponse, filterChain);
447    
448                            cacheResponseData = new CacheResponseData(byteBufferResponse);
449    
450                            LastPath lastPath = (LastPath)request.getAttribute(
451                                    WebKeys.LAST_PATH);
452    
453                            if (lastPath != null) {
454                                    cacheResponseData.setAttribute(WebKeys.LAST_PATH, lastPath);
455                            }
456    
457                            // Cache the result if and only if there is a result and the request
458                            // is cacheable. We have to test the cacheability of a request
459                            // twice because the user could have been authenticated after the
460                            // initial test.
461    
462                            String cacheControl = GetterUtil.getString(
463                                    byteBufferResponse.getHeader(HttpHeaders.CACHE_CONTROL));
464    
465                            if ((byteBufferResponse.getStatus() == HttpServletResponse.SC_OK) &&
466                                    !cacheControl.contains(HttpHeaders.PRAGMA_NO_CACHE_VALUE) &&
467                                    isCacheableRequest(request) &&
468                                    isCacheableResponse(byteBufferResponse)) {
469    
470                                    CacheUtil.putCacheResponseData(
471                                            companyId, key, cacheResponseData);
472                            }
473                    }
474                    else {
475                            LastPath lastPath = (LastPath)cacheResponseData.getAttribute(
476                                    WebKeys.LAST_PATH);
477    
478                            if (lastPath != null) {
479                                    HttpSession session = request.getSession();
480    
481                                    session.setAttribute(WebKeys.LAST_PATH, lastPath);
482                            }
483                    }
484    
485                    CacheResponseUtil.write(response, cacheResponseData);
486            }
487    
488            private static final int _PATTERN_FRIENDLY = 0;
489    
490            private static final int _PATTERN_LAYOUT = 1;
491    
492            private static final int _PATTERN_RESOURCE = 2;
493    
494            private static Log _log = LogFactoryUtil.getLog(CacheFilter.class);
495    
496            private int _pattern;
497    
498    }