001    /**
002     * Copyright (c) 2000-2010 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.util.servlet.filters;
016    
017    import com.liferay.portal.kernel.servlet.Header;
018    import com.liferay.util.servlet.ServletResponseUtil;
019    
020    import java.io.IOException;
021    
022    import java.util.List;
023    import java.util.Map;
024    
025    import javax.servlet.http.HttpServletResponse;
026    
027    /**
028     * @author Alexander Chow
029     */
030    public class CacheResponseUtil {
031    
032            public static void setHeaders(
033                    HttpServletResponse response, Map<String, List<Header>> headers) {
034    
035                    for (Map.Entry<String, List<Header>> entry : headers.entrySet()) {
036                            String headerKey = entry.getKey();
037                            List<Header> headerValues = entry.getValue();
038    
039                            for (Header header : headerValues) {
040                                    int type = header.getType();
041    
042                                    if (type == Header.DATE_TYPE) {
043                                            response.setDateHeader(headerKey, header.getDateValue());
044                                    }
045                                    else if (type == Header.INTEGER_TYPE) {
046                                            response.setIntHeader(headerKey, header.getIntValue());
047                                    }
048                                    else if (type == Header.STRING_TYPE) {
049                                            response.setHeader(headerKey, header.getStringValue());
050                                    }
051                            }
052                    }
053            }
054    
055            public static void write(
056                            HttpServletResponse response, CacheResponseData cacheResponseData)
057                    throws IOException {
058    
059                    setHeaders(response, cacheResponseData.getHeaders());
060    
061                    response.setContentType(cacheResponseData.getContentType());
062    
063                    ServletResponseUtil.write(
064                            response, cacheResponseData.getContent(),
065                            cacheResponseData.getContentLength());
066            }
067    
068    }