001: /*
002: * (C) Copyright 2000 - 2003 Nabh Information Systems, Inc.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019:
020: package com.nabhinc.portal.container;
021:
022: import java.io.IOException;
023: import java.util.HashMap;
024: import java.util.Iterator;
025: import java.util.Map;
026: import java.util.Set;
027:
028: import javax.portlet.ActionResponse;
029: import javax.portlet.PortletMode;
030: import javax.portlet.PortletModeException;
031: import javax.portlet.WindowState;
032: import javax.portlet.WindowStateException;
033: import javax.servlet.http.HttpServletResponse;
034:
035: /**
036: *
037: *
038: * @author Padmanabh dabke
039: * (c) 2003 Nabh Information Systems, Inc. All Rights Reserved.
040: */
041: public class ActionResponseImpl extends PortletResponseImpl implements
042: ActionResponse {
043:
044: // private PortletRequestImpl ariRenderRequest = null;
045: private boolean ariModeStateRenderParamSet = false;
046: private boolean ariRedirectSet = false;
047: private String ariRedirect = null;
048: private HashMap ariRenderParamMap = new HashMap();
049: private ActionRequestImpl ariActionRequest = null;
050:
051: public ActionResponseImpl(HttpServletResponse resp,
052: ActionRequestImpl actionRequest) {
053: super (resp);
054: ariActionRequest = actionRequest;
055: // ariRenderRequest = req;
056: }
057:
058: /* (non-Javadoc)
059: * @see javax.portlet.ActionResponse#setWindowState(javax.portlet.WindowState)
060: */
061: public void setWindowState(WindowState windowState)
062: throws WindowStateException {
063: if (ariRedirectSet)
064: throw new IllegalStateException(
065: "Window state cannot be set after setting redirect URL.");
066: ariActionRequest.setWindowState(windowState);
067: ariModeStateRenderParamSet = true;
068:
069: }
070:
071: /* (non-Javadoc)
072: * @see javax.portlet.ActionResponse#setPortletMode(javax.portlet.PortletMode)
073: */
074: public void setPortletMode(PortletMode portletMode)
075: throws PortletModeException {
076: if (ariRedirectSet)
077: throw new IllegalStateException(
078: "Portlet mode cannot be set after setting redirect URL.");
079: ariActionRequest.setPortletMode(portletMode);
080: ariModeStateRenderParamSet = true;
081:
082: }
083:
084: /* (non-Javadoc)
085: * @see javax.portlet.ActionResponse#sendRedirect(java.lang.String)
086: */
087: public void sendRedirect(String location) throws IOException {
088: if (ariModeStateRenderParamSet)
089: throw new IllegalStateException(
090: "Redirect URL cannot be set after setting portlet mode, window state, or render parameters.");
091: ariRedirect = location;
092: ariRedirectSet = true;
093:
094: }
095:
096: /* (non-Javadoc)
097: * @see javax.portlet.ActionResponse#setRenderParameters(java.util.Map)
098: */
099: @SuppressWarnings("unchecked")
100: public void setRenderParameters(Map paramMap) {
101: if (ariRedirectSet)
102: throw new IllegalStateException(
103: "Render parameters cannot be set after setting redirect URL.");
104: ariModeStateRenderParamSet = true;
105: if (paramMap == null) {
106: throw new IllegalArgumentException(
107: "Parameter map cannot be null.");
108: }
109: ariRenderParamMap.clear();
110: Set entries = paramMap.entrySet();
111: for (Iterator iter = entries.iterator(); iter.hasNext();) {
112: Map.Entry element = (Map.Entry) iter.next();
113: try {
114: String paramName = (String) element.getKey();
115: String[] paramValues = (String[]) element.getValue();
116: ariRenderParamMap.put(paramName, paramValues);
117: } catch (ClassCastException ex) {
118: throw new IllegalArgumentException(
119: "Parameter map keys must be strings and the values must be string arrays.");
120: }
121:
122: }
123: }
124:
125: /* (non-Javadoc)
126: * @see javax.portlet.ActionResponse#setRenderParameter(java.lang.String, java.lang.String)
127: */
128: @SuppressWarnings("unchecked")
129: public void setRenderParameter(String key, String value) {
130: if (ariRedirectSet)
131: throw new IllegalStateException(
132: "Render parameters cannot be set after setting redirect URL.");
133: if (key == null)
134: throw new IllegalArgumentException(
135: "Render parameter name cannot be null.");
136: if (value == null)
137: throw new IllegalArgumentException(
138: "Render parameter value cannot be null.");
139: ariModeStateRenderParamSet = true;
140: ariRenderParamMap.put(key, new String[] { value });
141:
142: }
143:
144: /* (non-Javadoc)
145: * @see javax.portlet.ActionResponse#setRenderParameter(java.lang.String, java.lang.String[])
146: */
147: @SuppressWarnings("unchecked")
148: public void setRenderParameter(String key, String[] values) {
149: if (ariRedirectSet)
150: throw new IllegalStateException(
151: "Render parameters cannot be set after setting redirect URL.");
152: if (key == null)
153: throw new IllegalArgumentException(
154: "Render parameter name cannot be null.");
155: if (values == null)
156: throw new IllegalArgumentException(
157: "Render parameter value cannot be null.");
158: ariModeStateRenderParamSet = true;
159: ariRenderParamMap.put(key, values);
160: }
161:
162: public void setHttpServletResponse(HttpServletResponse resp) {
163: super .setHttpServletResponse(resp);
164: ariRenderParamMap.clear();
165: ariModeStateRenderParamSet = false;
166: ariRedirectSet = false;
167: ariRedirect = null;
168: }
169:
170: public PortletMode getPortletMode() {
171: // return ariPortletMode == null ? ariRenderRequest.getPortletMode() : ariPortletMode;
172: return ariActionRequest.getPortletMode();
173: }
174:
175: public WindowState getWindowState() {
176: // return ariWindowState == null ? ariRenderRequest.getWindowState() : ariWindowState;
177: return ariActionRequest.getWindowState();
178: }
179:
180: public String getRedirect() {
181: return ariRedirect;
182: }
183:
184: public Map getParamMap() {
185: return ariRenderParamMap;
186: }
187:
188: }
|