KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > pluto > core > impl > PortletResponseImpl


1 /*
2  * Copyright 2003,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17
18  */

19
20 package org.apache.pluto.core.impl;
21
22 import java.io.IOException JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24 import java.io.UnsupportedEncodingException JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import javax.portlet.PortletResponse;
29 import javax.servlet.ServletOutputStream JavaDoc;
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31
32 import org.apache.pluto.core.InternalPortletResponse;
33 import org.apache.pluto.om.window.PortletWindow;
34 import org.apache.pluto.services.information.InformationProviderAccess;
35 import org.apache.pluto.services.information.ResourceURLProvider;
36 import org.apache.pluto.services.property.PropertyManager;
37 import org.apache.pluto.util.PrintWriterServletOutputStream;
38
39 public abstract class PortletResponseImpl extends javax.servlet.http.HttpServletResponseWrapper JavaDoc
40 implements InternalPortletResponse, PortletResponse
41 {
42
43     PortletWindow portletWindow;
44
45     /**
46      * this variable holds the servlet request of the target/portlet's
47      * web module
48      */

49     private javax.servlet.http.HttpServletRequest JavaDoc webModuleServletRequest;
50
51     private boolean usingWriter;
52     private boolean usingStream;
53
54     private ServletOutputStream JavaDoc wrappedWriter;
55     private Map JavaDoc properties;
56
57     /**
58      * true if we are in an include call
59      */

60     private boolean included;
61
62     public PortletResponseImpl(PortletWindow portletWindow,
63                                javax.servlet.http.HttpServletRequest JavaDoc servletRequest,
64                                javax.servlet.http.HttpServletResponse JavaDoc servletResponse)
65     {
66         super(servletResponse);
67
68         this.webModuleServletRequest = servletRequest;
69         this.portletWindow = portletWindow;
70     }
71
72     // javax.portlet.PortletResponse --------------------------------------------------------------
73
public void addProperty(String JavaDoc key, String JavaDoc value)
74     {
75         if (key == null)
76         {
77             throw new IllegalArgumentException JavaDoc("Property key == null");
78         }
79
80         Map JavaDoc props = getProperties();
81         
82         String JavaDoc[] oldValues = (String JavaDoc[]) props.get(key);
83         String JavaDoc[] newValues = null;
84         if (oldValues == null)
85         {
86             newValues = new String JavaDoc[]{value};
87         }
88         else
89         {
90             int len = oldValues.length;
91             newValues = new String JavaDoc[len+1];
92             System.arraycopy(oldValues, 0, newValues, 0, len);
93             newValues[len] = value;
94         }
95         props.put(key, newValues);
96
97         PropertyManager.setResponseProperties(portletWindow, this.getHttpServletRequest(), _getHttpServletResponse(), props);
98
99     }
100     
101     public void setProperty(String JavaDoc key, String JavaDoc value)
102     {
103         if (key == null)
104         {
105             throw new IllegalArgumentException JavaDoc("Property key == null");
106         }
107
108         Map JavaDoc props = getProperties();
109         
110         String JavaDoc[] newValues = new String JavaDoc[]{value};
111         props.put(key, newValues);
112
113         PropertyManager.setResponseProperties(portletWindow, this.getHttpServletRequest(), _getHttpServletResponse(), props);
114     }
115     
116     public String JavaDoc encodeURL(String JavaDoc path)
117     {
118         if (path.indexOf("://") == -1 && !path.startsWith("/"))
119         {
120             throw new IllegalArgumentException JavaDoc("only absolute URLs or full path URIs are allowed");
121         }
122         ResourceURLProvider provider = InformationProviderAccess.getDynamicProvider(getHttpServletRequest()).getResourceURLProvider(portletWindow);
123         if (path.indexOf("://") != -1)
124         {
125             provider.setAbsoluteURL(path);
126         }
127         else
128         {
129             provider.setFullPath(path);
130         }
131         return this._getHttpServletResponse().encodeURL(provider.toString());
132     }
133     // --------------------------------------------------------------------------------------------
134

135     // org.apache.pluto.core.InternalPortletResponse implementation -------------------------------
136
public void lateInit(javax.servlet.http.HttpServletRequest JavaDoc webModuleServletRequest,
137                          javax.servlet.http.HttpServletResponse JavaDoc webModuleServletResponse)
138     {
139         this.webModuleServletRequest = webModuleServletRequest;
140         this.setResponse(webModuleServletResponse);
141     }
142     // --------------------------------------------------------------------------------------------
143

144     // internal methods ---------------------------------------------------------------------------
145
protected javax.servlet.http.HttpServletResponse JavaDoc _getHttpServletResponse()
146     {
147         return(javax.servlet.http.HttpServletResponse JavaDoc) super.getResponse();
148     }
149
150     protected javax.servlet.http.HttpServletRequest JavaDoc getHttpServletRequest()
151     {
152         return webModuleServletRequest;
153     }
154
155     private Map JavaDoc getProperties() {
156         if (properties == null)
157             properties = new HashMap JavaDoc();
158         return properties;
159     }
160     // --------------------------------------------------------------------------------------------
161

162     // additional methods -------------------------------------------------------------------------
163
// servlet-only implementation
164
// (inherited from HttpServletResponseWrapper)
165
public void addCookie(javax.servlet.http.Cookie JavaDoc cookie)
166     {
167         this._getHttpServletResponse().addCookie(cookie);
168     }
169
170     public boolean containsHeader(String JavaDoc name)
171     {
172         return this._getHttpServletResponse().containsHeader(name);
173     }
174
175     /**
176      * @deprecated use encodeRedirectURL instead
177      */

178     public String JavaDoc encodeRedirectUrl(String JavaDoc url)
179     {
180         return included ? null : this._getHttpServletResponse().encodeRedirectUrl(url);
181     }
182
183     public String JavaDoc encodeRedirectURL(String JavaDoc url)
184     {
185         return included ? null : this._getHttpServletResponse().encodeRedirectURL(url);
186     }
187
188     public void sendRedirect(String JavaDoc location) throws java.io.IOException JavaDoc
189     {
190         this._getHttpServletResponse().sendRedirect(location);
191     }
192
193     public void setDateHeader(String JavaDoc name, long date)
194     {
195         this._getHttpServletResponse().setDateHeader(name,date);
196     }
197
198     public void sendError(int sc, String JavaDoc msg) throws java.io.IOException JavaDoc
199     {
200         this._getHttpServletResponse().sendError(sc,msg);
201     }
202
203     public void sendError(int sc) throws java.io.IOException JavaDoc
204     {
205         this._getHttpServletResponse().sendError(sc);
206     }
207
208     public void addHeader(String JavaDoc name, String JavaDoc value)
209     {
210         this._getHttpServletResponse().addHeader(name,value);
211     }
212
213     public void setIntHeader(String JavaDoc name, int value)
214     {
215         this._getHttpServletResponse().setIntHeader(name,value);
216     }
217
218     public void addDateHeader(String JavaDoc name, long date)
219     {
220         this._getHttpServletResponse().addDateHeader(name,date);
221     }
222
223     public void setHeader(String JavaDoc name, String JavaDoc value)
224     {
225         this._getHttpServletResponse().setHeader(name,value);
226     }
227
228     public void setStatus(int sc)
229     {
230         this._getHttpServletResponse().setStatus(sc);
231     }
232
233     /**
234      * @deprecated As of version 2.1, due to ambiguous meaning
235      * of the message parameter. To set a status code use
236      * setStatus(int), to send an error with a description
237      * use sendError(int, String). Sets the status code and
238      * message for this response.
239      */

240     public void setStatus(int sc, String JavaDoc sm)
241     {
242         this._getHttpServletResponse().setStatus(sc,sm);
243     }
244
245     public void addIntHeader(String JavaDoc name, int value)
246     {
247         this._getHttpServletResponse().addIntHeader(name,value);
248     }
249
250     public void setContentLength(int len)
251     {
252         this._getHttpServletResponse().setContentLength(len);
253     }
254
255     public String JavaDoc encodeUrl(String JavaDoc url)
256     {
257         return this.encodeURL(url);
258     }
259
260     public void setLocale(java.util.Locale JavaDoc loc)
261     {
262         this._getHttpServletResponse().setLocale(loc);
263     }
264
265     public ServletOutputStream JavaDoc getOutputStream() throws IllegalStateException JavaDoc, IOException JavaDoc {
266         if (usingWriter)
267         {
268             throw new IllegalStateException JavaDoc("getPortletOutputStream can't be used after getWriter was invoked");
269         }
270
271         if (wrappedWriter == null)
272         {
273             wrappedWriter = new PrintWriterServletOutputStream(
274                 _getHttpServletResponse().getWriter(),
275                 _getHttpServletResponse().getCharacterEncoding()
276             );
277         }
278
279         usingStream = true;
280
281         return wrappedWriter;
282     }
283
284     public PrintWriter JavaDoc getWriter() throws UnsupportedEncodingException JavaDoc, IllegalStateException JavaDoc, IOException JavaDoc {
285         if (usingStream)
286         {
287             throw new IllegalStateException JavaDoc("getWriter can't be used after getOutputStream was invoked");
288         }
289
290         usingWriter = true;
291
292         return _getHttpServletResponse().getWriter();
293     }
294
295     // other
296
public PortletWindow getInternalPortletWindow()
297     {
298         return portletWindow;
299     }
300     
301     // internal
302

303     HttpServletRequest JavaDoc getHttpDServletRequest()
304     {
305         return webModuleServletRequest;
306     }
307
308    public void setIncluded(boolean included)
309    {
310        this.included = included;
311    }
312
313    public boolean isIncluded()
314    {
315        return included;
316    }
317 }
318
Popular Tags