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.kernel.servlet;
016    
017    import com.liferay.portal.kernel.util.GetterUtil;
018    import com.liferay.portal.kernel.util.ObjectValuePair;
019    
020    import javax.servlet.RequestDispatcher;
021    import javax.servlet.http.HttpServletRequest;
022    import javax.servlet.http.HttpServletRequestWrapper;
023    import javax.servlet.http.HttpServletResponse;
024    
025    /**
026     * @author Shuyang Zhou
027     */
028    public class RequestDispatcherUtil {
029    
030            public static ObjectValuePair<String, Long> getContentAndLastModifiedTime(
031                            RequestDispatcher requestDispatcher, HttpServletRequest request,
032                            HttpServletResponse response)
033                    throws Exception {
034    
035                    BufferCacheServletResponse bufferCacheServletResponse =
036                            new BufferCacheServletResponse(response);
037    
038                    requestDispatcher.include(
039                            new HttpServletRequestWrapper(request) {
040    
041                                    @Override
042                                    public String getMethod() {
043                                            return HttpMethods.GET;
044                                    }
045    
046                            }, bufferCacheServletResponse);
047    
048                    return new ObjectValuePair<>(
049                            bufferCacheServletResponse.getString(),
050                            GetterUtil.getLong(
051                                    bufferCacheServletResponse.getHeader(HttpHeaders.LAST_MODIFIED),
052                                    -1));
053            }
054    
055            public static long getLastModifiedTime(
056                            RequestDispatcher requestDispatcher, HttpServletRequest request,
057                            HttpServletResponse response)
058                    throws Exception {
059    
060                    MetaInfoCacheServletResponse metaInfoCacheServletResponse =
061                            new MetaInfoCacheServletResponse(response);
062    
063                    requestDispatcher.include(
064                            new HttpServletRequestWrapper(request) {
065    
066                                    @Override
067                                    public String getMethod() {
068                                            return HttpMethods.HEAD;
069                                    }
070    
071                            }, metaInfoCacheServletResponse);
072    
073                    return GetterUtil.getLong(
074                            metaInfoCacheServletResponse.getHeader(HttpHeaders.LAST_MODIFIED),
075                            -1);
076            }
077    
078    }