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.util.NetUtils;
020:
021: import org.apache.avalon.framework.CascadingRuntimeException;
022:
023: import javax.portlet.PortletMode;
024: import javax.portlet.PortletModeException;
025: import javax.portlet.PortletPreferences;
026: import javax.portlet.WindowState;
027: import javax.portlet.WindowStateException;
028:
029: import java.io.IOException;
030: import java.util.HashMap;
031: import java.util.Iterator;
032: import java.util.Map;
033:
034: /**
035: * Implements the {@link org.apache.cocoon.environment.Response} interface for
036: * the JSR-168 (Portlet) environment.
037: *
038: * @author <a href="mailto:vadim.gritsenko@dc.gov">Vadim Gritsenko</a>
039: * @version CVS $Id: ActionResponse.java 433543 2006-08-22 06:22:54Z crossley $
040: */
041: public final class ActionResponse extends PortletResponse {
042:
043: private String uri;
044: private ActionRequest request;
045:
046: /**
047: * Creates a ActionResponse based on a real
048: * {@link ActionResponse} object
049: */
050: protected ActionResponse(javax.portlet.ActionResponse response,
051: PortletPreferences preferences, ActionRequest request,
052: String uri) {
053: super (response, preferences);
054: this .request = request;
055: this .uri = uri;
056: }
057:
058: // Response API methods
059:
060: /**
061: *
062: * @see PortletEnvironment#HEADER_PORTLET_MODE
063: * @see PortletEnvironment#HEADER_WINDOW_STATE
064: */
065: public void addHeader(String name, String value) {
066: if (PortletEnvironment.HEADER_PORTLET_MODE.equals(name)) {
067: try {
068: this .getActionResponse().setPortletMode(
069: new PortletMode(value));
070: } catch (PortletModeException e) {
071: throw new CascadingRuntimeException(
072: "Cant set portlet mode '" + value + "'", e);
073: }
074: } else if (PortletEnvironment.HEADER_WINDOW_STATE.equals(name)) {
075: try {
076: this .getActionResponse().setWindowState(
077: new WindowState(value));
078: } catch (WindowStateException e) {
079: throw new CascadingRuntimeException(
080: "Cant set window state '" + value + "'", e);
081: }
082: } else {
083: super .addHeader(name, value);
084: }
085: }
086:
087: /**
088: *
089: * @see PortletEnvironment#HEADER_PORTLET_MODE
090: * @see PortletEnvironment#HEADER_WINDOW_STATE
091: */
092: public void setHeader(String name, String value) {
093: if (PortletEnvironment.HEADER_PORTLET_MODE.equals(name)) {
094: try {
095: this .getActionResponse().setPortletMode(
096: new PortletMode(value));
097: } catch (PortletModeException e) {
098: throw new CascadingRuntimeException(
099: "Cant set portlet mode '" + value + "'", e);
100: }
101: } else if (PortletEnvironment.HEADER_WINDOW_STATE.equals(name)) {
102: try {
103: this .getActionResponse().setWindowState(
104: new WindowState(value));
105: } catch (WindowStateException e) {
106: throw new CascadingRuntimeException(
107: "Cant set window state '" + value + "'", e);
108: }
109: } else {
110: super .setHeader(name, value);
111: }
112: }
113:
114: /**
115: * Implements redirect.
116: *
117: * Redirects to self (starting with the question mark) are processed
118: * differently from other redirects: redirect parameters are parsed and
119: * set on response using {@link ActionResponse#setRenderParameter(String, String)}
120: * method, {@link ActionResponse#sendRedirect(String)} method is not called.
121: *
122: * @param location
123: * @throws IOException
124: */
125: public void sendRedirect(String location) throws IOException {
126: String servletPath = this .request.getServletPath();
127:
128: // Strip off parameters
129: Map parameters = new HashMap(7);
130: String absLoc = NetUtils.deparameterize(location, parameters);
131:
132: // Absolutize
133: if (absLoc.length() > 0) {
134: String base = NetUtils.getPath(uri);
135: absLoc = NetUtils.absolutize(base, absLoc);
136: absLoc = NetUtils.normalize(absLoc);
137: } else {
138: absLoc = uri;
139: }
140:
141: // Redirect within the portlet?
142: if (absLoc.startsWith(servletPath)) {
143: String pathInfo = absLoc.substring(servletPath.length());
144:
145: for (Iterator i = parameters.entrySet().iterator(); i
146: .hasNext();) {
147: Map.Entry me = (Map.Entry) i.next();
148: String name = (String) me.getKey();
149: String value = (String) me.getValue();
150: getActionResponse().setRenderParameter(name, value);
151: }
152: getActionResponse().setRenderParameter(
153: PortletEnvironment.PARAMETER_PATH_INFO, pathInfo);
154: } else {
155: getActionResponse().sendRedirect(location);
156: }
157:
158: /*
159: if (location.startsWith("?")) {
160: Map parameters = new HashMap(7);
161: NetUtils.deparameterize(location, parameters);
162: for (Iterator i = parameters.keySet().iterator(); i.hasNext();) {
163: String name = (String)i.next();
164: String value = (String)parameters.get(name);
165: getActionResponse().setRenderParameter(name, value);
166: }
167: } else {
168: getActionResponse().sendRedirect(location);
169: }
170: */
171: }
172:
173: // ActionResponse API methods
174:
175: /**
176: * Type cast portletResponse to {@link ActionResponse}
177: *
178: * @return type casted portletResponse
179: */
180: public javax.portlet.ActionResponse getActionResponse() {
181: return (javax.portlet.ActionResponse) getPortletResponse();
182: }
183:
184: public void setPortletMode(PortletMode mode)
185: throws PortletModeException {
186: getActionResponse().setPortletMode(mode);
187: }
188:
189: public void setRenderParameter(String key, String value) {
190: getActionResponse().setRenderParameter(key, value);
191: }
192:
193: public void setRenderParameter(String key, String[] values) {
194: getActionResponse().setRenderParameter(key, values);
195: }
196:
197: public void setRenderParameters(Map parameters) {
198: getActionResponse().setRenderParameters(parameters);
199: }
200:
201: public void setWindowState(WindowState state)
202: throws WindowStateException {
203: getActionResponse().setWindowState(state);
204: }
205:
206: // Portlet Environment API methods
207:
208: /**
209: * Action response is always committed (in a sense that you cannot reset() response)
210: */
211: boolean isCommitted() {
212: return true;
213: }
214: }
|