001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.mock.web.portlet;
018:
019: import java.io.IOException;
020: import java.util.Collections;
021: import java.util.Iterator;
022: import java.util.Map;
023:
024: import javax.portlet.ActionResponse;
025: import javax.portlet.PortalContext;
026: import javax.portlet.PortletMode;
027: import javax.portlet.PortletModeException;
028: import javax.portlet.WindowState;
029: import javax.portlet.WindowStateException;
030:
031: import org.springframework.core.CollectionFactory;
032: import org.springframework.util.Assert;
033: import org.springframework.util.CollectionUtils;
034:
035: /**
036: * Mock implementation of the {@link javax.portlet.ActionResponse} interface.
037: *
038: * @author John A. Lewis
039: * @author Juergen Hoeller
040: * @since 2.0
041: */
042: public class MockActionResponse extends MockPortletResponse implements
043: ActionResponse {
044:
045: private WindowState windowState;
046:
047: private PortletMode portletMode;
048:
049: private String redirectedUrl;
050:
051: private final Map renderParameters = CollectionFactory
052: .createLinkedMapIfPossible(16);
053:
054: /**
055: * Create a new MockActionResponse with a default {@link MockPortalContext}.
056: * @see MockPortalContext
057: */
058: public MockActionResponse() {
059: super ();
060: }
061:
062: /**
063: * Create a new MockActionResponse.
064: * @param portalContext the PortalContext defining the supported
065: * PortletModes and WindowStates
066: */
067: public MockActionResponse(PortalContext portalContext) {
068: super (portalContext);
069: }
070:
071: public void setWindowState(WindowState windowState)
072: throws WindowStateException {
073: if (this .redirectedUrl != null) {
074: throw new IllegalStateException(
075: "Cannot set WindowState after sendRedirect has been called");
076: }
077: if (!CollectionUtils.contains(getPortalContext()
078: .getSupportedWindowStates(), windowState)) {
079: throw new WindowStateException("WindowState not supported",
080: windowState);
081: }
082: this .windowState = windowState;
083: }
084:
085: public WindowState getWindowState() {
086: return windowState;
087: }
088:
089: public void setPortletMode(PortletMode portletMode)
090: throws PortletModeException {
091: if (this .redirectedUrl != null) {
092: throw new IllegalStateException(
093: "Cannot set PortletMode after sendRedirect has been called");
094: }
095: if (!CollectionUtils.contains(getPortalContext()
096: .getSupportedPortletModes(), portletMode)) {
097: throw new PortletModeException("PortletMode not supported",
098: portletMode);
099: }
100: this .portletMode = portletMode;
101: }
102:
103: public PortletMode getPortletMode() {
104: return portletMode;
105: }
106:
107: public void sendRedirect(String url) throws IOException {
108: if (this .windowState != null || this .portletMode != null
109: || !this .renderParameters.isEmpty()) {
110: throw new IllegalStateException(
111: "Cannot call sendRedirect after windowState, portletMode, or renderParameters have been set");
112: }
113: Assert.notNull(url, "Redirect URL must not be null");
114: this .redirectedUrl = url;
115: }
116:
117: public String getRedirectedUrl() {
118: return redirectedUrl;
119: }
120:
121: public void setRenderParameters(Map parameters) {
122: if (this .redirectedUrl != null) {
123: throw new IllegalStateException(
124: "Cannot set render parameters after sendRedirect has been called");
125: }
126: Assert.notNull(parameters, "Parameters Map must not be null");
127: this .renderParameters.clear();
128: for (Iterator it = parameters.entrySet().iterator(); it
129: .hasNext();) {
130: Map.Entry entry = (Map.Entry) it.next();
131: Assert.isTrue(entry.getKey() instanceof String,
132: "Key must be of type String");
133: Assert.isTrue(entry.getValue() instanceof String[],
134: "Value must be of type String[]");
135: this .renderParameters.put(entry.getKey(), entry.getValue());
136: }
137: }
138:
139: public void setRenderParameter(String key, String value) {
140: if (this .redirectedUrl != null) {
141: throw new IllegalStateException(
142: "Cannot set render parameters after sendRedirect has been called");
143: }
144: Assert.notNull(key, "Parameter key must not be null");
145: Assert.notNull(value, "Parameter value must not be null");
146: this .renderParameters.put(key, new String[] { value });
147: }
148:
149: public String getRenderParameter(String name) {
150: String[] arr = (String[]) this .renderParameters.get(name);
151: return (arr != null && arr.length > 0 ? arr[0] : null);
152: }
153:
154: public void setRenderParameter(String key, String[] values) {
155: if (this .redirectedUrl != null) {
156: throw new IllegalStateException(
157: "Cannot set render parameters after sendRedirect has been called");
158: }
159: Assert.notNull(key, "Parameter key must not be null");
160: Assert.notNull(values, "Parameter values must not be null");
161: this .renderParameters.put(key, values);
162: }
163:
164: public String[] getRenderParameterValues(String key) {
165: Assert.notNull(key, "Parameter key must not be null");
166: return (String[]) this .renderParameters.get(key);
167: }
168:
169: public Iterator getRenderParameterNames() {
170: return this .renderParameters.keySet().iterator();
171: }
172:
173: public Map getRenderParameterMap() {
174: return Collections.unmodifiableMap(this.renderParameters);
175: }
176:
177: }
|