KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > environment > portlet > PortletResponse


1 /*
2  * Copyright 1999-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 package org.apache.cocoon.environment.portlet;
17
18 import org.apache.cocoon.environment.Cookie;
19 import org.apache.cocoon.environment.Response;
20
21 import org.apache.avalon.framework.CascadingRuntimeException;
22
23 import javax.portlet.PortletPreferences;
24 import javax.portlet.ReadOnlyException;
25
26 import java.io.IOException JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Locale JavaDoc;
30 import java.util.Map JavaDoc;
31
32 /**
33  * Implements the {@link Response} interface for the JSR-168 (Portlet) environment.
34  *
35  * @author <a HREF="mailto:alex.rudnev@dc.gov">Alex Rudnev</a>
36  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
37  * @version CVS $Id: PortletResponse.java 30932 2004-07-29 17:35:38Z vgritsenko $
38  */

39 public abstract class PortletResponse implements Response {
40
41     /** The real PortletResponse object */
42     private final javax.portlet.PortletResponse response;
43
44     private final PortletPreferences preferences;
45
46     /**
47      * Stores property names set on the response.
48      * Portlet response does not have containsProperty() method.
49      */

50     private Map JavaDoc properties = new HashMap JavaDoc(5);
51
52
53     /**
54      * Creates a PortletResponse based on a real PortletResponse object
55      */

56     protected PortletResponse(javax.portlet.PortletResponse response,
57                               PortletPreferences preferences) {
58         this.response = response;
59         this.preferences = preferences;
60     }
61
62
63     public boolean containsHeader(String JavaDoc name) {
64         return properties.containsKey(name);
65     }
66
67     public void setHeader(String JavaDoc name, String JavaDoc value) {
68         properties.put(name, name);
69         response.setProperty(name, value);
70     }
71
72     public void setIntHeader(String JavaDoc name, int value) {
73         setHeader(name, "" + value);
74     }
75
76     public void setDateHeader(String JavaDoc name, long date) {
77         setHeader(name, "" + date);
78     }
79
80     public void addHeader(String JavaDoc name, String JavaDoc value) {
81         properties.put(name, name);
82         response.addProperty(name, value);
83     }
84
85     public void addIntHeader(String JavaDoc name, int value) {
86         addHeader(name, "" + value);
87     }
88
89     public void addDateHeader(String JavaDoc name, long date) {
90         addHeader(name, "" + date);
91     }
92
93
94     public String JavaDoc getCharacterEncoding() {
95         return null;
96     }
97
98     public Cookie createCookie(String JavaDoc name, String JavaDoc value) {
99         return new PortletCookie(name, value);
100     }
101
102     public void addCookie(Cookie cookie) {
103         try {
104             this.preferences.setValue(cookie.getName(), cookie.getValue());
105 // TODO: When is good time to persist changes?
106
this.preferences.store();
107         } catch (ReadOnlyException e) {
108             throw new CascadingRuntimeException("Cannot set read-only preference '" + cookie.getName() + "'", e);
109         } catch (Exception JavaDoc e) {
110             throw new CascadingRuntimeException("Cannot set preference '" + cookie.getName() + "'", e);
111         }
112     }
113
114     public void setLocale(Locale JavaDoc locale) {
115     }
116
117     public Locale JavaDoc getLocale() {
118         return null;
119     }
120
121     public String JavaDoc encodeURL(String JavaDoc url) {
122 // TODO: Why this check?
123
if (url != null && url.indexOf(";jsessionid=") != -1) {
124             return url;
125         }
126         return this.response.encodeURL(url);
127     }
128
129
130     // Portlet API related methods
131

132     /**
133      * Provides access to the underlying response object
134      * @return portlet API response object
135      */

136     public javax.portlet.PortletResponse getPortletResponse() {
137         return response;
138     }
139
140     public void addProperty(String JavaDoc key, String JavaDoc value) {
141         getPortletResponse().addProperty(key, value);
142     }
143
144     public void setProperty(String JavaDoc key, String JavaDoc value) {
145         getPortletResponse().setProperty(key, value);
146     }
147
148
149     // Portlet Environment Methods
150

151     OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
152         throw new IllegalStateException JavaDoc("Operation 'getOutputStream' is not supported by '" + getClass().getName() + "'");
153     }
154
155     void setContentType(String JavaDoc type) {
156         throw new IllegalStateException JavaDoc("Operation 'setContentType' is not supported by '" + getClass().getName() + "'");
157     }
158
159     void sendRedirect(String JavaDoc location) throws IOException JavaDoc {
160         throw new IllegalStateException JavaDoc("Operation 'sendRedirect' is not supported by '" + getClass().getName() + "'");
161     }
162
163     boolean isCommitted() {
164         throw new IllegalStateException JavaDoc("Operation 'isCommitted' is not supported by '" + getClass().getName() + "'");
165     }
166
167     void reset() {
168         throw new IllegalStateException JavaDoc("Operation 'reset' is not supported by '" + getClass().getName() + "'");
169     }
170 }
171
Popular Tags