1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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  }