1   /**
2    * Copyright (c) 2000-2008 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 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  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
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          super.addDateHeader(name, date);
50  
51          _response.addDateHeader(name, date);
52      }
53  
54      public void addHeader(String name, String value) {
55          super.addHeader(name, value);
56  
57          _response.addHeader(name, value);
58      }
59  
60      public void addIntHeader(String name, int value) {
61          super.addIntHeader(name, value);
62  
63          _response.addIntHeader(name, value);
64      }
65  
66      public void addProperty(Cookie cookie) {
67          super.addProperty(cookie);
68  
69          _response.addCookie(cookie);
70      }
71  
72      public PortletURL createActionURL() {
73          return super.createActionURL();
74      }
75  
76      public PortletURLImpl createPortletURLImpl(
77          String portletName, String lifecycle) {
78  
79          ResourceRequest resourceRequest = (ResourceRequest)getPortletRequest();
80  
81          String cacheability = resourceRequest.getCacheability();
82  
83          if (cacheability.equals(ResourceURL.PAGE)) {
84          }
85          else if (lifecycle.equals(PortletRequest.ACTION_PHASE)) {
86              throw new IllegalStateException(
87                  "Unable to create an action URL from a resource response " +
88                      "when the cacheability is not set to PAGE");
89          }
90          else if (lifecycle.equals(PortletRequest.RENDER_PHASE)) {
91              throw new IllegalStateException(
92                  "Unable to create a render URL from a resource response when " +
93                      "the cacheability is not set to PAGE");
94          }
95  
96          return super.createPortletURLImpl(portletName, lifecycle);
97      }
98  
99      public PortletURL createRenderURL() {
100         return super.createRenderURL();
101     }
102 
103     public ResourceURL createResourceURL() {
104         return super.createResourceURL();
105     }
106 
107     public String getLifecycle() {
108         return PortletRequest.RESOURCE_PHASE;
109     }
110 
111     public void setCharacterEncoding(String charset) {
112         _response.setCharacterEncoding(charset);
113     }
114 
115     public void setLocale(Locale locale) {
116         _response.setLocale(locale);
117     }
118 
119     public void setContentLength(int length) {
120         _response.setContentLength(length);
121     }
122 
123     public void setDateHeader(String name, long date) {
124         super.setDateHeader(name, date);
125 
126         _response.setDateHeader(name, date);
127     }
128 
129     public void setHeader(String name, String value) {
130         super.setHeader(name, value);
131 
132         _response.setHeader(name, value);
133     }
134 
135     public void setIntHeader(String name, int value) {
136         super.setIntHeader(name, value);
137 
138         _response.setIntHeader(name, value);
139     }
140 
141     protected ResourceResponseImpl() {
142         if (_log.isDebugEnabled()) {
143             _log.debug("Creating new instance " + hashCode());
144         }
145     }
146 
147     protected void init(
148         PortletRequestImpl portletRequestImpl, HttpServletResponse response,
149         String portletName, long companyId, long plid) {
150 
151         super.init(portletRequestImpl, response, portletName, companyId, plid);
152 
153         _response = response;
154     }
155 
156     protected void recycle() {
157         if (_log.isDebugEnabled()) {
158             _log.debug("Recycling instance " + hashCode());
159         }
160 
161         super.recycle();
162 
163         _response = null;
164     }
165 
166     private static Log _log = LogFactory.getLog(ResourceResponseImpl.class);
167 
168     private HttpServletResponse _response;
169 
170 }