1 16 package org.springframework.webflow.executor.support; 17 18 import java.util.Iterator ; 19 import java.util.Map ; 20 21 import org.springframework.core.style.StylerUtils; 22 import org.springframework.util.StringUtils; 23 import org.springframework.webflow.context.ExternalContext; 24 import org.springframework.webflow.core.collection.ParameterMap; 25 import org.springframework.webflow.execution.FlowExecutionContext; 26 import org.springframework.webflow.execution.support.ExternalRedirect; 27 import org.springframework.webflow.execution.support.FlowDefinitionRedirect; 28 import org.springframework.webflow.executor.FlowExecutor; 29 30 38 public class RequestParameterFlowExecutorArgumentHandler extends FlowExecutorArgumentHandler { 39 40 48 private static final String PARAMETER_VALUE_DELIMITER = "_"; 49 50 55 private String parameterValueDelimiter = PARAMETER_VALUE_DELIMITER; 56 57 61 public String getParameterValueDelimiter() { 62 return parameterValueDelimiter; 63 } 64 65 69 public void setParameterValueDelimiter(String parameterValueDelimiter) { 70 this.parameterValueDelimiter = parameterValueDelimiter; 71 } 72 73 public boolean isFlowIdPresent(ExternalContext context) { 74 return context.getRequestParameterMap().contains(getFlowIdArgumentName()); 75 } 76 77 public String extractFlowId(ExternalContext context) throws FlowExecutorArgumentExtractionException { 78 String flowId = context.getRequestParameterMap().get(getFlowIdArgumentName()); 79 flowId = applyDefaultFlowId(flowId); 80 if (!StringUtils.hasText(flowId)) { 81 throw new FlowExecutorArgumentExtractionException( 82 "Unable to extract the flow definition id parameter: make sure the client provides the '" 83 + getFlowIdArgumentName() 84 + "' parameter as input or set the 'defaultFlowId' property; " 85 + "the parameters provided in this request are: " 86 + StylerUtils.style(context.getRequestParameterMap())); 87 } 88 return flowId; 89 } 90 91 public boolean isFlowExecutionKeyPresent(ExternalContext context) { 92 return context.getRequestParameterMap().contains(getFlowExecutionKeyArgumentName()); 93 } 94 95 public String extractFlowExecutionKey(ExternalContext context) throws FlowExecutorArgumentExtractionException { 96 String encodedKey = context.getRequestParameterMap().get(getFlowExecutionKeyArgumentName()); 97 if (!StringUtils.hasText(encodedKey)) { 98 throw new FlowExecutorArgumentExtractionException( 99 "Unable to extract the flow execution key parameter: make sure the client provides the '" 100 + getFlowExecutionKeyArgumentName() 101 + "' parameter as input; the parameters provided in this request are: " 102 + StylerUtils.style(context.getRequestParameterMap())); 103 } 104 return encodedKey; 105 } 106 107 public boolean isEventIdPresent(ExternalContext context) { 108 return StringUtils.hasText(findParameter(getEventIdArgumentName(), context.getRequestParameterMap())); 109 } 110 111 public String extractEventId(ExternalContext context) throws FlowExecutorArgumentExtractionException { 112 String eventId = findParameter(getEventIdArgumentName(), context.getRequestParameterMap()); 113 if (!StringUtils.hasText(eventId)) { 114 throw new FlowExecutorArgumentExtractionException( 115 "Unable to extract the event id parameter: make sure the client provides the '" 116 + getEventIdArgumentName() + "' parameter as input along with the '" 117 + getFlowExecutionKeyArgumentName() 118 + "' parameter; the parameters provided in this request are: " 119 + StylerUtils.style(context.getRequestParameterMap())); 120 } 121 return eventId; 122 } 123 124 149 protected String findParameter(String logicalParameterName, ParameterMap parameters) { 150 String value = parameters.get(logicalParameterName); 152 if (value != null) { 153 return value; 154 } 155 String prefix = logicalParameterName + getParameterValueDelimiter(); 157 Iterator paramNames = parameters.asMap().keySet().iterator(); 158 while (paramNames.hasNext()) { 159 String paramName = (String )paramNames.next(); 160 if (paramName.startsWith(prefix)) { 161 String strValue = paramName.substring(prefix.length()); 162 if (strValue.endsWith(".x") || strValue.endsWith(".y")) { 165 strValue = strValue.substring(0, strValue.length() - 2); 166 } 167 return strValue; 168 } 169 } 170 return null; 172 } 173 174 public String createFlowDefinitionUrl(FlowDefinitionRedirect flowDefinitionRedirect, ExternalContext context) { 175 StringBuffer url = new StringBuffer (); 176 appendFlowExecutorPath(url, context); 177 url.append('?'); 178 appendQueryParameter(url, getFlowIdArgumentName(), flowDefinitionRedirect.getFlowDefinitionId()); 179 if (!flowDefinitionRedirect.getExecutionInput().isEmpty()) { 180 url.append('&'); 181 } 182 appendQueryParameters(url, flowDefinitionRedirect.getExecutionInput()); 183 return url.toString(); 184 } 185 186 public String createFlowExecutionUrl(String flowExecutionKey, FlowExecutionContext flowExecution, 187 ExternalContext context) { 188 StringBuffer url = new StringBuffer (); 189 appendFlowExecutorPath(url, context); 190 url.append('?'); 191 appendQueryParameter(url, getFlowExecutionKeyArgumentName(), flowExecutionKey); 192 return url.toString(); 193 } 194 195 public String createExternalUrl(ExternalRedirect redirect, String flowExecutionKey, ExternalContext context) { 196 StringBuffer externalUrl = new StringBuffer (); 197 externalUrl.append(makeRedirectUrlContextRelativeIfNecessary(redirect.getUrl(), context)); 198 if (flowExecutionKey != null) { 199 boolean first = redirect.getUrl().indexOf('?') < 0; 200 if (first) { 201 externalUrl.append('?'); 202 } 203 else { 204 externalUrl.append('&'); 205 } 206 appendQueryParameter(externalUrl, getFlowExecutionKeyArgumentName(), flowExecutionKey); 207 } 208 return externalUrl.toString(); 209 } 210 211 213 219 protected void appendFlowExecutorPath(StringBuffer url, ExternalContext context) { 220 url.append(context.getContextPath()); 221 url.append(context.getDispatcherPath()); 222 if (context.getRequestPathInfo() != null) { 223 url.append(context.getRequestPathInfo()); 224 } 225 } 226 227 233 protected void appendQueryParameters(StringBuffer url, Map parameters) { 234 Iterator entries = parameters.entrySet().iterator(); 235 while (entries.hasNext()) { 236 Map.Entry entry = (Map.Entry )entries.next(); 237 appendQueryParameter(url, entry.getKey(), entry.getValue()); 238 if (entries.hasNext()) { 239 url.append('&'); 240 } 241 } 242 } 243 244 250 protected void appendQueryParameter(StringBuffer url, Object key, Object value) { 251 String encodedKey = encodeValue(key); 252 String encodedValue = encodeValue(value); 253 url.append(encodedKey).append('=').append(encodedValue); 254 } 255 } | Popular Tags |