KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > mock > web > portlet > MockActionResponse


1 /*
2  * Copyright 2002-2006 the original author or authors.
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
17 package org.springframework.mock.web.portlet;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.portlet.ActionResponse;
25 import javax.portlet.PortalContext;
26 import javax.portlet.PortletMode;
27 import javax.portlet.PortletModeException;
28 import javax.portlet.WindowState;
29 import javax.portlet.WindowStateException;
30
31 import org.springframework.core.CollectionFactory;
32 import org.springframework.util.Assert;
33 import org.springframework.util.CollectionUtils;
34
35 /**
36  * Mock implementation of the {@link javax.portlet.ActionResponse} interface.
37  *
38  * @author John A. Lewis
39  * @author Juergen Hoeller
40  * @since 2.0
41  */

42 public class MockActionResponse extends MockPortletResponse implements ActionResponse {
43
44     private WindowState windowState;
45
46     private PortletMode portletMode;
47
48     private String JavaDoc redirectedUrl;
49
50     private final Map JavaDoc renderParameters = CollectionFactory.createLinkedMapIfPossible(16);
51
52
53     /**
54      * Create a new MockActionResponse with a default {@link MockPortalContext}.
55      * @see MockPortalContext
56      */

57     public MockActionResponse() {
58         super();
59     }
60
61     /**
62      * Create a new MockActionResponse.
63      * @param portalContext the PortalContext defining the supported
64      * PortletModes and WindowStates
65      */

66     public MockActionResponse(PortalContext portalContext) {
67         super(portalContext);
68     }
69
70
71     public void setWindowState(WindowState windowState) throws WindowStateException {
72         if (this.redirectedUrl != null) {
73             throw new IllegalStateException JavaDoc("Cannot set WindowState after sendRedirect has been called");
74         }
75         if (!CollectionUtils.contains(getPortalContext().getSupportedWindowStates(), windowState)) {
76             throw new WindowStateException("WindowState not supported", windowState);
77         }
78         this.windowState = windowState;
79     }
80
81     public WindowState getWindowState() {
82         return windowState;
83     }
84
85     public void setPortletMode(PortletMode portletMode) throws PortletModeException {
86         if (this.redirectedUrl != null) {
87             throw new IllegalStateException JavaDoc("Cannot set PortletMode after sendRedirect has been called");
88         }
89         if (!CollectionUtils.contains(getPortalContext().getSupportedPortletModes(), portletMode)) {
90             throw new PortletModeException("PortletMode not supported", portletMode);
91         }
92         this.portletMode = portletMode;
93     }
94
95     public PortletMode getPortletMode() {
96         return portletMode;
97     }
98
99     public void sendRedirect(String JavaDoc url) throws IOException JavaDoc {
100         if (this.windowState != null || this.portletMode != null || !this.renderParameters.isEmpty()) {
101             throw new IllegalStateException JavaDoc(
102                     "Cannot call sendRedirect after windowState, portletMode, or renderParameters have been set");
103         }
104         Assert.notNull(url, "Redirect URL must not be null");
105         this.redirectedUrl = url;
106     }
107
108     public String JavaDoc getRedirectedUrl() {
109         return redirectedUrl;
110     }
111
112     public void setRenderParameters(Map JavaDoc parameters) {
113         if (this.redirectedUrl != null) {
114             throw new IllegalStateException JavaDoc("Cannot set render parameters after sendRedirect has been called");
115         }
116         Assert.notNull(parameters, "Parameters Map must not be null");
117         this.renderParameters.clear();
118         for (Iterator JavaDoc it = parameters.entrySet().iterator(); it.hasNext();) {
119             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
120             Assert.isTrue(entry.getKey() instanceof String JavaDoc, "Key must be of type String");
121             Assert.isTrue(entry.getValue() instanceof String JavaDoc[], "Value must be of type String[]");
122             this.renderParameters.put(entry.getKey(), entry.getValue());
123         }
124     }
125
126     public void setRenderParameter(String JavaDoc key, String JavaDoc value) {
127         if (this.redirectedUrl != null) {
128             throw new IllegalStateException JavaDoc("Cannot set render parameters after sendRedirect has been called");
129         }
130         Assert.notNull(key, "Parameter key must not be null");
131         Assert.notNull(value, "Parameter value must not be null");
132         this.renderParameters.put(key, new String JavaDoc[] {value});
133     }
134
135     public String JavaDoc getRenderParameter(String JavaDoc name) {
136         String JavaDoc[] arr = (String JavaDoc[]) this.renderParameters.get(name);
137         return (arr != null && arr.length > 0 ? arr[0] : null);
138     }
139
140     public void setRenderParameter(String JavaDoc key, String JavaDoc[] values) {
141         if (this.redirectedUrl != null) {
142             throw new IllegalStateException JavaDoc("Cannot set render parameters after sendRedirect has been called");
143         }
144         Assert.notNull(key, "Parameter key must not be null");
145         Assert.notNull(values, "Parameter values must not be null");
146         this.renderParameters.put(key, values);
147     }
148
149     public String JavaDoc[] getRenderParameterValues(String JavaDoc key) {
150         Assert.notNull(key, "Parameter key must not be null");
151         return (String JavaDoc[]) this.renderParameters.get(key);
152     }
153
154     public Iterator JavaDoc getRenderParameterNames() {
155         return this.renderParameters.keySet().iterator();
156     }
157
158     public Map JavaDoc getRenderParameterMap() {
159         return Collections.unmodifiableMap(this.renderParameters);
160     }
161
162
163 }
164
Popular Tags