1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class ResourceResponseImpl
45      extends MimeResponseImpl implements ResourceResponse {
46  
47      public void addDateHeader(String name, long date) {
48          _response.addDateHeader(name, date);
49      }
50  
51      public void addHeader(String name, String value) {
52          _response.addHeader(name, value);
53      }
54  
55      public void addIntHeader(String name, int value) {
56          _response.addIntHeader(name, value);
57      }
58  
59      public void addProperty(Cookie cookie) {
60          _response.addCookie(cookie);
61      }
62  
63      public PortletURL createActionURL() {
64          return super.createActionURL();
65      }
66  
67      public PortletURLImpl createPortletURLImpl(
68          String portletName, String lifecycle) {
69  
70          ResourceRequest resourceRequest = (ResourceRequest)getPortletRequest();
71  
72          String cacheability = resourceRequest.getCacheability();
73  
74          if (cacheability.equals(ResourceURL.PAGE)) {
75          }
76          else if (lifecycle.equals(PortletRequest.ACTION_PHASE)) {
77              throw new IllegalStateException(
78                  "Unable to create an action URL from a resource response " +
79                      "when the cacheability is not set to PAGE");
80          }
81          else if (lifecycle.equals(PortletRequest.RENDER_PHASE)) {
82              throw new IllegalStateException(
83                  "Unable to create a render URL from a resource response when " +
84                      "the cacheability is not set to PAGE");
85          }
86  
87          return super.createPortletURLImpl(portletName, lifecycle);
88      }
89  
90      public PortletURL createRenderURL() {
91          return super.createRenderURL();
92      }
93  
94      public ResourceURL createResourceURL() {
95          return super.createResourceURL();
96      }
97  
98      public String getLifecycle() {
99          return PortletRequest.RESOURCE_PHASE;
100     }
101 
102     public void setCharacterEncoding(String charset) {
103         _response.setCharacterEncoding(charset);
104     }
105 
106     public void setLocale(Locale locale) {
107         _response.setLocale(locale);
108     }
109 
110     public void setContentLength(int length) {
111         _response.setContentLength(length);
112     }
113 
114     public void setDateHeader(String name, long date) {
115         _response.setDateHeader(name, date);
116     }
117 
118     public void setHeader(String name, String value) {
119         _response.setHeader(name, value);
120     }
121 
122     public void setIntHeader(String name, int value) {
123         _response.setIntHeader(name, value);
124     }
125 
126     protected ResourceResponseImpl() {
127         if (_log.isDebugEnabled()) {
128             _log.debug("Creating new instance " + hashCode());
129         }
130     }
131 
132     protected void init(
133         PortletRequestImpl portletRequestImpl, HttpServletResponse response,
134         String portletName, long companyId, long plid) {
135 
136         super.init(portletRequestImpl, response, portletName, companyId, plid);
137 
138         _response = response;
139     }
140 
141     protected void recycle() {
142         if (_log.isDebugEnabled()) {
143             _log.debug("Recycling instance " + hashCode());
144         }
145 
146         super.recycle();
147 
148         _response = null;
149     }
150 
151     private static Log _log = LogFactoryUtil.getLog(ResourceResponseImpl.class);
152 
153     private HttpServletResponse _response;
154 
155 }