1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.util.servlet.filters;
16  
17  import com.liferay.util.servlet.Header;
18  import com.liferay.util.servlet.ServletResponseUtil;
19  
20  import java.io.IOException;
21  
22  import java.util.List;
23  import java.util.Map;
24  
25  import javax.servlet.http.HttpServletResponse;
26  
27  /**
28   * <a href="CacheResponseUtil.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Alexander Chow
31   */
32  public class CacheResponseUtil {
33  
34      public static void addHeaders(
35          HttpServletResponse response, Map<String, List<Header>> headers) {
36  
37          for (Map.Entry<String, List<Header>> entry : headers.entrySet()) {
38              String headerKey = entry.getKey();
39              List<Header> headerValues = entry.getValue();
40  
41              for (Header header : headerValues) {
42                  int type = header.getType();
43  
44                  if (type == Header.DATE_TYPE) {
45                      response.addDateHeader(headerKey, header.getDateValue());
46                  }
47                  else if (type == Header.INTEGER_TYPE) {
48                      response.addIntHeader(headerKey, header.getIntValue());
49                  }
50                  else if (type == Header.STRING_TYPE) {
51                      response.addHeader(headerKey, header.getStringValue());
52                  }
53              }
54          }
55      }
56  
57      public static void write(
58              HttpServletResponse response, CacheResponseData cacheResponseData)
59          throws IOException {
60  
61          addHeaders(response, cacheResponseData.getHeaders());
62  
63          response.setContentType(cacheResponseData.getContentType());
64  
65          ServletResponseUtil.write(
66              response, cacheResponseData.getContent(),
67              cacheResponseData.getContentLength());
68      }
69  
70  }