1 16 17 package org.springframework.mock.web.portlet; 18 19 import java.io.IOException ; 20 import java.util.Collections ; 21 import java.util.Iterator ; 22 import java.util.Map ; 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 42 public class MockActionResponse extends MockPortletResponse implements ActionResponse { 43 44 private WindowState windowState; 45 46 private PortletMode portletMode; 47 48 private String redirectedUrl; 49 50 private final Map renderParameters = CollectionFactory.createLinkedMapIfPossible(16); 51 52 53 57 public MockActionResponse() { 58 super(); 59 } 60 61 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 ("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 ("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 url) throws IOException { 100 if (this.windowState != null || this.portletMode != null || !this.renderParameters.isEmpty()) { 101 throw new IllegalStateException ( 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 getRedirectedUrl() { 109 return redirectedUrl; 110 } 111 112 public void setRenderParameters(Map parameters) { 113 if (this.redirectedUrl != null) { 114 throw new IllegalStateException ("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 it = parameters.entrySet().iterator(); it.hasNext();) { 119 Map.Entry entry = (Map.Entry ) it.next(); 120 Assert.isTrue(entry.getKey() instanceof String , "Key must be of type String"); 121 Assert.isTrue(entry.getValue() instanceof String [], "Value must be of type String[]"); 122 this.renderParameters.put(entry.getKey(), entry.getValue()); 123 } 124 } 125 126 public void setRenderParameter(String key, String value) { 127 if (this.redirectedUrl != null) { 128 throw new IllegalStateException ("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 [] {value}); 133 } 134 135 public String getRenderParameter(String name) { 136 String [] arr = (String []) this.renderParameters.get(name); 137 return (arr != null && arr.length > 0 ? arr[0] : null); 138 } 139 140 public void setRenderParameter(String key, String [] values) { 141 if (this.redirectedUrl != null) { 142 throw new IllegalStateException ("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 [] getRenderParameterValues(String key) { 150 Assert.notNull(key, "Parameter key must not be null"); 151 return (String []) this.renderParameters.get(key); 152 } 153 154 public Iterator getRenderParameterNames() { 155 return this.renderParameters.keySet().iterator(); 156 } 157 158 public Map getRenderParameterMap() { 159 return Collections.unmodifiableMap(this.renderParameters); 160 } 161 162 163 } 164 | Popular Tags |