List all unsatisfied request param groups
Issue: SPR-12854
This commit is contained in:
parent
0b8554f94a
commit
b6449baaa6
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -16,9 +16,13 @@
|
|||
|
||||
package org.springframework.web.bind;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
|
@ -34,7 +38,7 @@ import org.springframework.util.StringUtils;
|
|||
@SuppressWarnings("serial")
|
||||
public class UnsatisfiedServletRequestParameterException extends ServletRequestBindingException {
|
||||
|
||||
private final String[] paramConditions;
|
||||
private final List<String[]> paramConditions;
|
||||
|
||||
private final Map<String, String[]> actualParams;
|
||||
|
||||
|
@ -46,6 +50,21 @@ public class UnsatisfiedServletRequestParameterException extends ServletRequestB
|
|||
*/
|
||||
public UnsatisfiedServletRequestParameterException(String[] paramConditions, Map<String, String[]> actualParams) {
|
||||
super("");
|
||||
this.paramConditions = Arrays.<String[]>asList(paramConditions);
|
||||
this.actualParams = actualParams;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new UnsatisfiedServletRequestParameterException.
|
||||
* @param paramConditions all sets of parameter conditions that have been violated
|
||||
* @param actualParams the actual parameter Map associated with the ServletRequest
|
||||
* @since 4.2
|
||||
*/
|
||||
public UnsatisfiedServletRequestParameterException(List<String[]> paramConditions,
|
||||
Map<String, String[]> actualParams) {
|
||||
|
||||
super("");
|
||||
Assert.isTrue(!CollectionUtils.isEmpty(paramConditions));
|
||||
this.paramConditions = paramConditions;
|
||||
this.actualParams = actualParams;
|
||||
}
|
||||
|
@ -53,8 +72,20 @@ public class UnsatisfiedServletRequestParameterException extends ServletRequestB
|
|||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return "Parameter conditions \"" + StringUtils.arrayToDelimitedString(this.paramConditions, ", ") +
|
||||
"\" not met for actual request parameters: " + requestParameterMapToString(this.actualParams);
|
||||
StringBuilder sb = new StringBuilder("Parameter conditions ");
|
||||
int i = 0;
|
||||
for (String[] conditions : this.paramConditions) {
|
||||
if (i > 0) {
|
||||
sb.append(" OR ");
|
||||
}
|
||||
sb.append("\"");
|
||||
sb.append(StringUtils.arrayToDelimitedString(conditions, ", "));
|
||||
sb.append("\"");
|
||||
i++;
|
||||
}
|
||||
sb.append(" not met for actual request parameters: ");
|
||||
sb.append(requestParameterMapToString(this.actualParams));
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static String requestParameterMapToString(Map<String, String[]> actualParams) {
|
||||
|
@ -70,10 +101,20 @@ public class UnsatisfiedServletRequestParameterException extends ServletRequestB
|
|||
}
|
||||
|
||||
/**
|
||||
* Return the parameter conditions that have been violated.
|
||||
* Return the parameter conditions that have been violated or the first group
|
||||
* in case of multiple groups.
|
||||
* @see org.springframework.web.bind.annotation.RequestMapping#params()
|
||||
*/
|
||||
public final String[] getParamConditions() {
|
||||
return this.paramConditions.get(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all parameter condition groups that have been violated.
|
||||
* @see org.springframework.web.bind.annotation.RequestMapping#params()
|
||||
* @since 4.2
|
||||
*/
|
||||
public final List<String[]> getParamConditionGroups() {
|
||||
return this.paramConditions;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -22,6 +22,7 @@ import java.util.Comparator;
|
|||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
@ -205,7 +206,7 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe
|
|||
|
||||
Set<MediaType> consumableMediaTypes;
|
||||
Set<MediaType> producibleMediaTypes;
|
||||
Set<String> paramConditions;
|
||||
List<String[]> paramConditions;
|
||||
|
||||
if (patternAndMethodMatches.isEmpty()) {
|
||||
consumableMediaTypes = getConsumableMediaTypes(request, patternMatches);
|
||||
|
@ -234,8 +235,7 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe
|
|||
throw new HttpMediaTypeNotAcceptableException(new ArrayList<MediaType>(producibleMediaTypes));
|
||||
}
|
||||
else if (!CollectionUtils.isEmpty(paramConditions)) {
|
||||
String[] params = paramConditions.toArray(new String[paramConditions.size()]);
|
||||
throw new UnsatisfiedServletRequestParameterException(params, request.getParameterMap());
|
||||
throw new UnsatisfiedServletRequestParameterException(paramConditions, request.getParameterMap());
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
|
@ -262,18 +262,21 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe
|
|||
return result;
|
||||
}
|
||||
|
||||
private Set<String> getRequestParams(HttpServletRequest request, Set<RequestMappingInfo> partialMatches) {
|
||||
private List<String[]> getRequestParams(HttpServletRequest request, Set<RequestMappingInfo> partialMatches) {
|
||||
List<String[]> result = new ArrayList<String[]>();
|
||||
for (RequestMappingInfo partialMatch : partialMatches) {
|
||||
ParamsRequestCondition condition = partialMatch.getParamsCondition();
|
||||
if (!CollectionUtils.isEmpty(condition.getExpressions()) && (condition.getMatchingCondition(request) == null)) {
|
||||
Set<String> expressions = new HashSet<String>();
|
||||
for (NameValueExpression<String> expr : condition.getExpressions()) {
|
||||
expressions.add(expr.toString());
|
||||
Set<NameValueExpression<String>> expressions = condition.getExpressions();
|
||||
if (!CollectionUtils.isEmpty(expressions) && condition.getMatchingCondition(request) == null) {
|
||||
int i = 0;
|
||||
String[] array = new String[expressions.size()];
|
||||
for (NameValueExpression<String> expression : expressions) {
|
||||
array[i++] = expression.toString();
|
||||
}
|
||||
return expressions;
|
||||
result.add(array);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -16,12 +16,17 @@
|
|||
|
||||
package org.springframework.web.servlet.mvc.method;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.junit.Before;
|
||||
|
@ -54,8 +59,6 @@ import org.springframework.web.servlet.mvc.condition.ProducesRequestCondition;
|
|||
import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;
|
||||
import org.springframework.web.util.UrlPathHelper;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Test fixture with {@link RequestMappingInfoHandlerMapping}.
|
||||
*
|
||||
|
@ -211,6 +214,8 @@ public class RequestMappingInfoHandlerMappingTests {
|
|||
}
|
||||
}
|
||||
|
||||
// SPR-12854
|
||||
|
||||
@Test
|
||||
public void testUnsatisfiedServletRequestParameterException() throws Exception {
|
||||
try {
|
||||
|
@ -219,8 +224,10 @@ public class RequestMappingInfoHandlerMappingTests {
|
|||
fail("UnsatisfiedServletRequestParameterException expected");
|
||||
}
|
||||
catch (UnsatisfiedServletRequestParameterException ex) {
|
||||
assertArrayEquals("Invalid request parameter conditions",
|
||||
new String[] { "foo=bar" }, ex.getParamConditions());
|
||||
List<String[]> groups = ex.getParamConditionGroups();
|
||||
assertEquals(2, groups.size());
|
||||
assertThat(Arrays.asList("foo=bar", "bar=baz"),
|
||||
containsInAnyOrder(groups.get(0)[0], groups.get(1)[0]));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -408,6 +415,7 @@ public class RequestMappingInfoHandlerMappingTests {
|
|||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@Controller
|
||||
private static class TestController {
|
||||
|
||||
|
@ -441,6 +449,11 @@ public class RequestMappingInfoHandlerMappingTests {
|
|||
return "";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/params", params="bar=baz")
|
||||
public String param2() {
|
||||
return "";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/content", produces="application/xml")
|
||||
public String xmlContent() {
|
||||
return "";
|
||||
|
@ -452,6 +465,7 @@ public class RequestMappingInfoHandlerMappingTests {
|
|||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@Controller
|
||||
private static class UserController {
|
||||
|
||||
|
|
Loading…
Reference in New Issue