001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.environment.portlet;
018:
019: import org.apache.cocoon.environment.Cookie;
020: import org.apache.cocoon.environment.Response;
021:
022: import org.apache.avalon.framework.CascadingRuntimeException;
023:
024: import javax.portlet.PortletPreferences;
025: import javax.portlet.ReadOnlyException;
026:
027: import java.io.IOException;
028: import java.io.OutputStream;
029: import java.util.HashMap;
030: import java.util.Locale;
031: import java.util.Map;
032:
033: /**
034: * Implements the {@link Response} interface for the JSR-168 (Portlet) environment.
035: *
036: * @author <a href="mailto:alex.rudnev@dc.gov">Alex Rudnev</a>
037: * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
038: * @version CVS $Id: PortletResponse.java 433543 2006-08-22 06:22:54Z crossley $
039: */
040: public abstract class PortletResponse implements Response {
041:
042: /** The real PortletResponse object */
043: private final javax.portlet.PortletResponse response;
044:
045: private final PortletPreferences preferences;
046:
047: /**
048: * Stores property names set on the response.
049: * Portlet response does not have containsProperty() method.
050: */
051: private Map properties = new HashMap(5);
052:
053: /**
054: * Creates a PortletResponse based on a real PortletResponse object
055: */
056: protected PortletResponse(javax.portlet.PortletResponse response,
057: PortletPreferences preferences) {
058: this .response = response;
059: this .preferences = preferences;
060: }
061:
062: public boolean containsHeader(String name) {
063: return properties.containsKey(name);
064: }
065:
066: public void setHeader(String name, String value) {
067: properties.put(name, name);
068: response.setProperty(name, value);
069: }
070:
071: public void setIntHeader(String name, int value) {
072: setHeader(name, "" + value);
073: }
074:
075: public void setDateHeader(String name, long date) {
076: setHeader(name, "" + date);
077: }
078:
079: public void addHeader(String name, String value) {
080: properties.put(name, name);
081: response.addProperty(name, value);
082: }
083:
084: public void addIntHeader(String name, int value) {
085: addHeader(name, "" + value);
086: }
087:
088: public void addDateHeader(String name, long date) {
089: addHeader(name, "" + date);
090: }
091:
092: public String getCharacterEncoding() {
093: return null;
094: }
095:
096: public Cookie createCookie(String name, String value) {
097: return new PortletCookie(name, value);
098: }
099:
100: public void addCookie(Cookie cookie) {
101: try {
102: this .preferences.setValue(cookie.getName(), cookie
103: .getValue());
104: // TODO: When is good time to persist changes?
105: this .preferences.store();
106: } catch (ReadOnlyException e) {
107: throw new CascadingRuntimeException(
108: "Cannot set read-only preference '"
109: + cookie.getName() + "'", e);
110: } catch (Exception e) {
111: throw new CascadingRuntimeException(
112: "Cannot set preference '" + cookie.getName() + "'",
113: e);
114: }
115: }
116:
117: public void setLocale(Locale locale) {
118: }
119:
120: public Locale getLocale() {
121: return null;
122: }
123:
124: public String encodeURL(String url) {
125: // TODO: Why this check?
126: if (url != null && url.indexOf(";jsessionid=") != -1) {
127: return url;
128: }
129: return this .response.encodeURL(url);
130: }
131:
132: // Portlet API related methods
133:
134: /**
135: * Provides access to the underlying response object
136: * @return portlet API response object
137: */
138: public javax.portlet.PortletResponse getPortletResponse() {
139: return response;
140: }
141:
142: public void addProperty(String key, String value) {
143: getPortletResponse().addProperty(key, value);
144: }
145:
146: public void setProperty(String key, String value) {
147: getPortletResponse().setProperty(key, value);
148: }
149:
150: // Portlet Environment Methods
151:
152: OutputStream getOutputStream() throws IOException {
153: throw new IllegalStateException(
154: "Operation 'getOutputStream' is not supported by '"
155: + getClass().getName() + "'");
156: }
157:
158: void setContentType(String type) {
159: throw new IllegalStateException(
160: "Operation 'setContentType' is not supported by '"
161: + getClass().getName() + "'");
162: }
163:
164: void sendRedirect(String location) throws IOException {
165: throw new IllegalStateException(
166: "Operation 'sendRedirect' is not supported by '"
167: + getClass().getName() + "'");
168: }
169:
170: boolean isCommitted() {
171: throw new IllegalStateException(
172: "Operation 'isCommitted' is not supported by '"
173: + getClass().getName() + "'");
174: }
175:
176: void reset() {
177: throw new IllegalStateException(
178: "Operation 'reset' is not supported by '"
179: + getClass().getName() + "'");
180: }
181: }
|