1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  
28  import java.util.Locale;
29  
30  import javax.portlet.PortletRequest;
31  import javax.portlet.PortletURL;
32  import javax.portlet.ResourceRequest;
33  import javax.portlet.ResourceResponse;
34  import javax.portlet.ResourceURL;
35  
36  import javax.servlet.http.Cookie;
37  import javax.servlet.http.HttpServletResponse;
38  
39  /**
40   * <a href="ResourceResponseImpl.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class ResourceResponseImpl
46      extends MimeResponseImpl implements ResourceResponse {
47  
48      public void addDateHeader(String name, long date) {
49          _response.addDateHeader(name, date);
50      }
51  
52      public void addHeader(String name, String value) {
53          _response.addHeader(name, value);
54      }
55  
56      public void addIntHeader(String name, int value) {
57          _response.addIntHeader(name, value);
58      }
59  
60      public void addProperty(Cookie cookie) {
61          _response.addCookie(cookie);
62      }
63  
64      public PortletURL createActionURL() {
65          return super.createActionURL();
66      }
67  
68      public PortletURLImpl createPortletURLImpl(
69          String portletName, String lifecycle) {
70  
71          ResourceRequest resourceRequest = (ResourceRequest)getPortletRequest();
72  
73          String cacheability = resourceRequest.getCacheability();
74  
75          if (cacheability.equals(ResourceURL.PAGE)) {
76          }
77          else if (lifecycle.equals(PortletRequest.ACTION_PHASE)) {
78              throw new IllegalStateException(
79                  "Unable to create an action URL from a resource response " +
80                      "when the cacheability is not set to PAGE");
81          }
82          else if (lifecycle.equals(PortletRequest.RENDER_PHASE)) {
83              throw new IllegalStateException(
84                  "Unable to create a render URL from a resource response when " +
85                      "the cacheability is not set to PAGE");
86          }
87  
88          return super.createPortletURLImpl(portletName, lifecycle);
89      }
90  
91      public PortletURL createRenderURL() {
92          return super.createRenderURL();
93      }
94  
95      public ResourceURL createResourceURL() {
96          return super.createResourceURL();
97      }
98  
99      public String getLifecycle() {
100         return PortletRequest.RESOURCE_PHASE;
101     }
102 
103     public void setCharacterEncoding(String charset) {
104         _response.setCharacterEncoding(charset);
105     }
106 
107     public void setLocale(Locale locale) {
108         _response.setLocale(locale);
109     }
110 
111     public void setContentLength(int length) {
112         _response.setContentLength(length);
113     }
114 
115     public void setDateHeader(String name, long date) {
116         _response.setDateHeader(name, date);
117     }
118 
119     public void setHeader(String name, String value) {
120         _response.setHeader(name, value);
121     }
122 
123     public void setIntHeader(String name, int value) {
124         _response.setIntHeader(name, value);
125     }
126 
127     protected ResourceResponseImpl() {
128         if (_log.isDebugEnabled()) {
129             _log.debug("Creating new instance " + hashCode());
130         }
131     }
132 
133     protected void init(
134         PortletRequestImpl portletRequestImpl, HttpServletResponse response,
135         String portletName, long companyId, long plid) {
136 
137         super.init(portletRequestImpl, response, portletName, companyId, plid);
138 
139         _response = response;
140     }
141 
142     protected void recycle() {
143         if (_log.isDebugEnabled()) {
144             _log.debug("Recycling instance " + hashCode());
145         }
146 
147         super.recycle();
148 
149         _response = null;
150     }
151 
152     private static Log _log = LogFactoryUtil.getLog(ResourceResponseImpl.class);
153 
154     private HttpServletResponse _response;
155 
156 }