1 16 17 package org.springframework.mock.web.portlet; 18 19 import java.io.UnsupportedEncodingException ; 20 import java.net.URLEncoder ; 21 import java.util.Collections ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.Set ; 25 26 import javax.portlet.PortalContext; 27 import javax.portlet.PortletMode; 28 import javax.portlet.PortletModeException; 29 import javax.portlet.PortletSecurityException; 30 import javax.portlet.PortletURL; 31 import javax.portlet.WindowState; 32 import javax.portlet.WindowStateException; 33 34 import org.springframework.core.CollectionFactory; 35 import org.springframework.util.Assert; 36 import org.springframework.util.CollectionUtils; 37 38 45 public class MockPortletURL implements PortletURL { 46 47 public static final String URL_TYPE_RENDER = "render"; 48 49 public static final String URL_TYPE_ACTION = "action"; 50 51 private static final String ENCODING = "UTF-8"; 52 53 54 private final PortalContext portalContext; 55 56 private final String urlType; 57 58 private WindowState windowState; 59 60 private PortletMode portletMode; 61 62 private final Map parameters = CollectionFactory.createLinkedMapIfPossible(16); 63 64 private boolean secure = false; 65 66 67 75 public MockPortletURL(PortalContext portalContext, String urlType) { 76 Assert.notNull(portalContext, "PortalContext is required"); 77 this.portalContext = portalContext; 78 this.urlType = urlType; 79 } 80 81 82 86 public void setWindowState(WindowState windowState) throws WindowStateException { 87 if (!CollectionUtils.contains(this.portalContext.getSupportedWindowStates(), windowState)) { 88 throw new WindowStateException("WindowState not supported", windowState); 89 } 90 this.windowState = windowState; 91 } 92 93 public void setPortletMode(PortletMode portletMode) throws PortletModeException { 94 if (!CollectionUtils.contains(this.portalContext.getSupportedPortletModes(), portletMode)) { 95 throw new PortletModeException("PortletMode not supported", portletMode); 96 } 97 this.portletMode = portletMode; 98 } 99 100 public void setParameter(String key, String value) { 101 Assert.notNull(key, "Parameter key must be null"); 102 Assert.notNull(value, "Parameter value must not be null"); 103 this.parameters.put(key, new String [] {value}); 104 } 105 106 public void setParameter(String key, String [] values) { 107 Assert.notNull(key, "Parameter key must be null"); 108 Assert.notNull(values, "Parameter values must not be null"); 109 this.parameters.put(key, values); 110 } 111 112 public void setParameters(Map parameters) { 113 Assert.notNull(parameters, "Parameters Map must not be null"); 114 this.parameters.clear(); 115 for (Iterator it = parameters.entrySet().iterator(); it.hasNext();) { 116 Map.Entry entry = (Map.Entry ) it.next(); 117 Assert.isTrue(entry.getKey() instanceof String , "Key must be of type String"); 118 Assert.isTrue(entry.getValue() instanceof String [], "Value must be of type String[]"); 119 this.parameters.put(entry.getKey(), entry.getValue()); 120 } 121 } 122 123 public Set getParameterNames() { 124 return this.parameters.keySet(); 125 } 126 127 public String getParameter(String name) { 128 String [] arr = (String []) this.parameters.get(name); 129 return (arr != null && arr.length > 0 ? arr[0] : null); 130 } 131 132 public String [] getParameterValues(String name) { 133 return (String []) this.parameters.get(name); 134 } 135 136 public Map getParameterMap() { 137 return Collections.unmodifiableMap(this.parameters); 138 } 139 140 public void setSecure(boolean secure) throws PortletSecurityException { 141 this.secure = secure; 142 } 143 144 public boolean isSecure() { 145 return secure; 146 } 147 148 public String toString() { 149 StringBuffer query = new StringBuffer (); 150 query.append(encodeParameter("urlType", this.urlType)); 151 if (this.windowState != null) { 152 query.append(";" + encodeParameter("windowState", this.windowState.toString())); 153 } 154 if (this.portletMode != null) { 155 query.append(";" + encodeParameter("portletMode", this.portletMode.toString())); 156 } 157 for (Iterator it = this.parameters.entrySet().iterator(); it.hasNext();) { 158 Map.Entry entry = (Map.Entry ) it.next(); 159 String name = (String ) entry.getKey(); 160 String [] values = (String []) entry.getValue(); 161 query.append(";" + encodeParameter("param_" + name, values)); 162 } 163 return (this.secure ? "https:" : "http:") + 164 "//localhost/mockportlet?" + query.toString(); 165 } 166 167 168 private String encodeParameter(String name, String value) { 169 try { 170 return URLEncoder.encode(name, ENCODING) + "=" + 171 URLEncoder.encode(value, ENCODING); 172 } 173 catch (UnsupportedEncodingException ex) { 174 return null; 175 } 176 } 177 178 private String encodeParameter(String name, String [] values) { 179 try { 180 StringBuffer buf = new StringBuffer (); 181 for (int i = 0, n = values.length; i < n; i++) { 182 buf.append((i > 0 ? ";" : "") + 183 URLEncoder.encode(name, ENCODING) + "=" + 184 URLEncoder.encode(values[i], ENCODING)); 185 } 186 return buf.toString(); 187 } 188 catch (UnsupportedEncodingException ex) { 189 return null; 190 } 191 } 192 193 } 194 | Popular Tags |