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