001    /**
002     * Copyright (c) 2000-2013 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.portal.kernel.servlet.ServletResponseUtil;
019    
020    import java.io.IOException;
021    
022    import java.util.Map;
023    import java.util.Set;
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, Set<Header>> headers) {
034    
035                    if (response.isCommitted()) {
036                            return;
037                    }
038    
039                    for (Map.Entry<String, Set<Header>> entry : headers.entrySet()) {
040                            String key = entry.getKey();
041    
042                            boolean first = true;
043    
044                            for (Header header : entry.getValue()) {
045                                    if (first) {
046                                            header.setToResponse(key, response);
047    
048                                            first = false;
049                                    }
050                                    else {
051                                            header.addToResponse(key, response);
052                                    }
053                            }
054                    }
055            }
056    
057            public static void write(
058                            HttpServletResponse response, CacheResponseData cacheResponseData)
059                    throws IOException {
060    
061                    setHeaders(response, cacheResponseData.getHeaders());
062    
063                    response.setContentType(cacheResponseData.getContentType());
064    
065                    ServletResponseUtil.write(response, cacheResponseData.getByteBuffer());
066            }
067    
068    }