Arjen Poutsma
14 years ago
19 changed files with 1044 additions and 559 deletions
@ -1,285 +0,0 @@
@@ -1,285 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2011 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.servlet.mvc.method.annotation; |
||||
|
||||
import java.util.Collections; |
||||
import java.util.LinkedHashSet; |
||||
import java.util.List; |
||||
import java.util.Set; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
|
||||
import org.springframework.http.MediaType; |
||||
import org.springframework.web.util.WebUtils; |
||||
|
||||
/** |
||||
* Factory for request condition objects. |
||||
* |
||||
* @author Arjen Poutsma |
||||
* @author Rossen Stoyanchev |
||||
*/ |
||||
public abstract class RequestConditionFactory { |
||||
|
||||
/** |
||||
* Parses the given parameters, and returns them as a set of request conditions. |
||||
* |
||||
* @param params the parameters |
||||
* @return the request conditions |
||||
* @see org.springframework.web.bind.annotation.RequestMapping#params() |
||||
*/ |
||||
public static Set<RequestCondition> parseParams(String... params) { |
||||
if (params == null) { |
||||
return Collections.emptySet(); |
||||
} |
||||
Set<RequestCondition> result = new LinkedHashSet<RequestCondition>(params.length); |
||||
for (String expression : params) { |
||||
result.add(new ParamNameValueCondition(expression)); |
||||
} |
||||
return result; |
||||
} |
||||
|
||||
/** |
||||
* Parses the given headers, and returns them as a set of request conditions. |
||||
* |
||||
* @param headers the headers |
||||
* @return the request conditions |
||||
* @see org.springframework.web.bind.annotation.RequestMapping#headers() |
||||
*/ |
||||
public static Set<RequestCondition> parseHeaders(String... headers) { |
||||
if (headers == null) { |
||||
return Collections.emptySet(); |
||||
} |
||||
Set<RequestCondition> result = new LinkedHashSet<RequestCondition>(headers.length); |
||||
for (String expression : headers) { |
||||
HeaderNameValueCondition header = new HeaderNameValueCondition(expression); |
||||
if (isMediaTypeHeader(header.name)) { |
||||
result.add(new MediaTypeHeaderNameValueCondition(expression)); |
||||
} |
||||
else { |
||||
result.add(header); |
||||
} |
||||
} |
||||
return result; |
||||
} |
||||
|
||||
private static boolean isMediaTypeHeader(String name) { |
||||
return "Accept".equalsIgnoreCase(name) || "Content-Type".equalsIgnoreCase(name); |
||||
} |
||||
|
||||
/** |
||||
* A condition that supports simple "name=value" style expressions as documented in |
||||
* <code>@RequestMapping.params()</code> and <code>@RequestMapping.headers()</code>. |
||||
*/ |
||||
private static abstract class AbstractNameValueCondition<T> implements RequestCondition { |
||||
|
||||
protected final String name; |
||||
|
||||
protected final T value; |
||||
|
||||
protected final boolean isNegated; |
||||
|
||||
protected AbstractNameValueCondition(String expression) { |
||||
int separator = expression.indexOf('='); |
||||
if (separator == -1) { |
||||
this.isNegated = expression.startsWith("!"); |
||||
this.name = isNegated ? expression.substring(1) : expression; |
||||
this.value = null; |
||||
} |
||||
else { |
||||
this.isNegated = (separator > 0) && (expression.charAt(separator - 1) == '!'); |
||||
this.name = isNegated ? expression.substring(0, separator - 1) : expression.substring(0, separator); |
||||
this.value = parseValue(expression.substring(separator + 1)); |
||||
} |
||||
} |
||||
|
||||
protected abstract T parseValue(String valueExpression); |
||||
|
||||
public final boolean match(HttpServletRequest request) { |
||||
boolean isMatch; |
||||
if (this.value != null) { |
||||
isMatch = matchValue(request); |
||||
} |
||||
else { |
||||
isMatch = matchName(request); |
||||
} |
||||
return isNegated ? !isMatch : isMatch; |
||||
} |
||||
|
||||
protected abstract boolean matchName(HttpServletRequest request); |
||||
|
||||
protected abstract boolean matchValue(HttpServletRequest request); |
||||
|
||||
@Override |
||||
public String toString() { |
||||
StringBuilder builder = new StringBuilder(); |
||||
if (value != null) { |
||||
builder.append(name); |
||||
if (isNegated) { |
||||
builder.append('!'); |
||||
} |
||||
builder.append('='); |
||||
builder.append(value); |
||||
} |
||||
else { |
||||
if (isNegated) { |
||||
builder.append('!'); |
||||
} |
||||
builder.append(name); |
||||
} |
||||
return builder.toString(); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public int hashCode() { |
||||
int result = name.hashCode(); |
||||
result = 31 * result + (value != null ? value.hashCode() : 0); |
||||
result = 31 * result + (isNegated ? 1 : 0); |
||||
return result; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Request parameter name-value condition. |
||||
*/ |
||||
private static class ParamNameValueCondition extends AbstractNameValueCondition<String> { |
||||
|
||||
private ParamNameValueCondition(String expression) { |
||||
super(expression); |
||||
} |
||||
|
||||
@Override |
||||
protected String parseValue(String valueExpression) { |
||||
return valueExpression; |
||||
} |
||||
|
||||
@Override |
||||
protected boolean matchName(HttpServletRequest request) { |
||||
return WebUtils.hasSubmitParameter(request, name); |
||||
} |
||||
|
||||
@Override |
||||
protected boolean matchValue(HttpServletRequest request) { |
||||
return value.equals(request.getParameter(name)); |
||||
} |
||||
|
||||
@Override |
||||
public boolean equals(Object obj) { |
||||
if (this == obj) { |
||||
return true; |
||||
} |
||||
if (obj != null && obj instanceof ParamNameValueCondition) { |
||||
ParamNameValueCondition other = (ParamNameValueCondition) obj; |
||||
return ((this.name.equals(other.name)) && |
||||
(this.value != null ? this.value.equals(other.value) : other.value == null) && |
||||
this.isNegated == other.isNegated); |
||||
} |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Request header name-value condition. |
||||
*/ |
||||
static class HeaderNameValueCondition extends AbstractNameValueCondition<String> { |
||||
|
||||
public HeaderNameValueCondition(String expression) { |
||||
super(expression); |
||||
} |
||||
|
||||
@Override |
||||
protected String parseValue(String valueExpression) { |
||||
return valueExpression; |
||||
} |
||||
|
||||
@Override |
||||
protected boolean matchName(HttpServletRequest request) { |
||||
return request.getHeader(name) != null; |
||||
} |
||||
|
||||
@Override |
||||
final protected boolean matchValue(HttpServletRequest request) { |
||||
return value.equals(request.getHeader(name)); |
||||
} |
||||
|
||||
@Override |
||||
public boolean equals(Object obj) { |
||||
if (this == obj) { |
||||
return true; |
||||
} |
||||
if (obj != null && obj instanceof HeaderNameValueCondition) { |
||||
HeaderNameValueCondition other = (HeaderNameValueCondition) obj; |
||||
return ((this.name.equalsIgnoreCase(other.name)) && |
||||
(this.value != null ? this.value.equals(other.value) : other.value == null) && |
||||
this.isNegated == other.isNegated); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
|
||||
} |
||||
|
||||
/** |
||||
* A RequestCondition that for headers that contain {@link org.springframework.http.MediaType MediaTypes}. |
||||
*/ |
||||
private static class MediaTypeHeaderNameValueCondition extends AbstractNameValueCondition<List<MediaType>> { |
||||
|
||||
public MediaTypeHeaderNameValueCondition(String expression) { |
||||
super(expression); |
||||
} |
||||
|
||||
@Override |
||||
protected List<MediaType> parseValue(String valueExpression) { |
||||
return Collections.unmodifiableList(MediaType.parseMediaTypes(valueExpression)); |
||||
} |
||||
|
||||
@Override |
||||
protected boolean matchName(HttpServletRequest request) { |
||||
return request.getHeader(name) != null; |
||||
} |
||||
|
||||
@Override |
||||
protected boolean matchValue(HttpServletRequest request) { |
||||
List<MediaType> requestMediaTypes = MediaType.parseMediaTypes(request.getHeader(name)); |
||||
|
||||
for (MediaType mediaType : this.value) { |
||||
for (MediaType requestMediaType : requestMediaTypes) { |
||||
if (mediaType.includes(requestMediaType)) { |
||||
return true; |
||||
} |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
@Override |
||||
public boolean equals(Object obj) { |
||||
if (this == obj) { |
||||
return true; |
||||
} |
||||
if (obj != null && obj instanceof MediaTypeHeaderNameValueCondition) { |
||||
MediaTypeHeaderNameValueCondition other = (MediaTypeHeaderNameValueCondition) obj; |
||||
return ((this.name.equalsIgnoreCase(other.name)) && |
||||
(this.value != null ? this.value.equals(other.value) : other.value == null) && |
||||
this.isNegated == other.isNegated); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
|
||||
} |
||||
} |
@ -0,0 +1,100 @@
@@ -0,0 +1,100 @@
|
||||
/* |
||||
* Copyright 2002-2011 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.servlet.mvc.method.condition; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
|
||||
/** |
||||
* A condition that supports simple "name=value" style expressions as documented in {@link |
||||
* org.springframework.web.bind.annotation.RequestMapping#params()} and {@link org.springframework.web.bind.annotation.RequestMapping#headers()}. |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @author Arjen Poutsma |
||||
* @since 3.1 |
||||
*/ |
||||
abstract class AbstractNameValueCondition<T> implements RequestCondition { |
||||
|
||||
protected final String name; |
||||
|
||||
protected final T value; |
||||
|
||||
protected final boolean isNegated; |
||||
|
||||
AbstractNameValueCondition(String expression) { |
||||
int separator = expression.indexOf('='); |
||||
if (separator == -1) { |
||||
this.isNegated = expression.startsWith("!"); |
||||
this.name = isNegated ? expression.substring(1) : expression; |
||||
this.value = null; |
||||
} |
||||
else { |
||||
this.isNegated = (separator > 0) && (expression.charAt(separator - 1) == '!'); |
||||
this.name = isNegated ? expression.substring(0, separator - 1) : expression.substring(0, separator); |
||||
this.value = parseValue(expression.substring(separator + 1)); |
||||
} |
||||
} |
||||
|
||||
protected abstract T parseValue(String valueExpression); |
||||
|
||||
public final boolean match(HttpServletRequest request) { |
||||
boolean isMatch; |
||||
if (this.value != null) { |
||||
isMatch = matchValue(request); |
||||
} |
||||
else { |
||||
isMatch = matchName(request); |
||||
} |
||||
return isNegated ? !isMatch : isMatch; |
||||
} |
||||
|
||||
protected abstract boolean matchName(HttpServletRequest request); |
||||
|
||||
protected abstract boolean matchValue(HttpServletRequest request); |
||||
|
||||
public int weight() { |
||||
return 1; |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public int hashCode() { |
||||
int result = name.hashCode(); |
||||
result = 31 * result + (value != null ? value.hashCode() : 0); |
||||
result = 31 * result + (isNegated ? 1 : 0); |
||||
return result; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
StringBuilder builder = new StringBuilder(); |
||||
if (value != null) { |
||||
builder.append(name); |
||||
if (isNegated) { |
||||
builder.append('!'); |
||||
} |
||||
builder.append('='); |
||||
builder.append(value); |
||||
} |
||||
else { |
||||
if (isNegated) { |
||||
builder.append('!'); |
||||
} |
||||
builder.append(name); |
||||
} |
||||
return builder.toString(); |
||||
} |
||||
} |
@ -0,0 +1,47 @@
@@ -0,0 +1,47 @@
|
||||
/* |
||||
* Copyright 2002-2011 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.servlet.mvc.method.condition; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
|
||||
import org.springframework.http.MediaType; |
||||
import org.springframework.util.StringUtils; |
||||
|
||||
/** |
||||
* @author Arjen Poutsma |
||||
*/ |
||||
class ConsumesRequestCondition implements RequestCondition { |
||||
|
||||
private final MediaType mediaType; |
||||
|
||||
ConsumesRequestCondition(String mediaType) { |
||||
this.mediaType = MediaType.parseMediaType(mediaType); |
||||
} |
||||
|
||||
public boolean match(HttpServletRequest request) { |
||||
String contentTypeString = request.getContentType(); |
||||
if (StringUtils.hasLength(contentTypeString)) { |
||||
MediaType contentType = MediaType.parseMediaType(contentTypeString); |
||||
return this.mediaType.includes(contentType); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
public int weight() { |
||||
return 1; |
||||
} |
||||
} |
@ -0,0 +1,64 @@
@@ -0,0 +1,64 @@
|
||||
/* |
||||
* Copyright 2002-2011 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.servlet.mvc.method.condition; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
|
||||
/** |
||||
* Request header name-value condition. |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @author Arjen Poutsma |
||||
* @see org.springframework.web.bind.annotation.RequestMapping#headers() |
||||
* @since 3.1 |
||||
*/ |
||||
class HeaderRequestCondition extends AbstractNameValueCondition<String> { |
||||
|
||||
public HeaderRequestCondition(String expression) { |
||||
super(expression); |
||||
} |
||||
|
||||
@Override |
||||
protected String parseValue(String valueExpression) { |
||||
return valueExpression; |
||||
} |
||||
|
||||
@Override |
||||
protected boolean matchName(HttpServletRequest request) { |
||||
return request.getHeader(name) != null; |
||||
} |
||||
|
||||
@Override |
||||
protected boolean matchValue(HttpServletRequest request) { |
||||
return value.equals(request.getHeader(name)); |
||||
} |
||||
|
||||
@Override |
||||
public boolean equals(Object obj) { |
||||
if (this == obj) { |
||||
return true; |
||||
} |
||||
if (obj != null && obj instanceof HeaderRequestCondition) { |
||||
HeaderRequestCondition other = (HeaderRequestCondition) obj; |
||||
return ((this.name.equalsIgnoreCase(other.name)) && |
||||
(this.value != null ? this.value.equals(other.value) : other.value == null) && |
||||
this.isNegated == other.isNegated); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,48 @@
@@ -0,0 +1,48 @@
|
||||
/* |
||||
* Copyright 2002-2011 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.servlet.mvc.method.condition; |
||||
|
||||
import java.util.List; |
||||
import javax.servlet.http.HttpServletRequest; |
||||
|
||||
/** |
||||
* {@link RequestCondition} implementation that represents a logical AND (i.e. &&). |
||||
* |
||||
* @author Arjen Poutsma |
||||
* @since 3.1 |
||||
*/ |
||||
class LogicalConjunctionRequestCondition extends RequestConditionComposite { |
||||
|
||||
LogicalConjunctionRequestCondition(List<RequestCondition> conditions) { |
||||
super(conditions); |
||||
} |
||||
|
||||
public boolean match(HttpServletRequest request) { |
||||
for (RequestCondition condition : conditions) { |
||||
if (!condition.match(request)) { |
||||
return false; |
||||
} |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
@Override |
||||
protected String getToStringInfix() { |
||||
return " && "; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,73 @@
@@ -0,0 +1,73 @@
|
||||
/* |
||||
* Copyright 2002-2011 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.servlet.mvc.method.condition; |
||||
|
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
import javax.servlet.http.HttpServletRequest; |
||||
|
||||
import org.springframework.http.MediaType; |
||||
|
||||
/** |
||||
* A RequestCondition that for headers that contain {@link org.springframework.http.MediaType MediaTypes}. |
||||
*/ |
||||
class MediaTypeHeaderRequestCondition extends AbstractNameValueCondition<List<MediaType>> { |
||||
|
||||
public MediaTypeHeaderRequestCondition(String expression) { |
||||
super(expression); |
||||
} |
||||
|
||||
@Override |
||||
protected List<MediaType> parseValue(String valueExpression) { |
||||
return Collections.unmodifiableList(MediaType.parseMediaTypes(valueExpression)); |
||||
} |
||||
|
||||
@Override |
||||
protected boolean matchName(HttpServletRequest request) { |
||||
return request.getHeader(name) != null; |
||||
} |
||||
|
||||
@Override |
||||
protected boolean matchValue(HttpServletRequest request) { |
||||
List<MediaType> requestMediaTypes = MediaType.parseMediaTypes(request.getHeader(name)); |
||||
|
||||
for (MediaType mediaType : this.value) { |
||||
for (MediaType requestMediaType : requestMediaTypes) { |
||||
if (mediaType.includes(requestMediaType)) { |
||||
return true; |
||||
} |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
@Override |
||||
public boolean equals(Object obj) { |
||||
if (this == obj) { |
||||
return true; |
||||
} |
||||
if (obj != null && obj instanceof MediaTypeHeaderRequestCondition) { |
||||
MediaTypeHeaderRequestCondition other = (MediaTypeHeaderRequestCondition) obj; |
||||
return ((this.name.equalsIgnoreCase(other.name)) && |
||||
(this.value != null ? this.value.equals(other.value) : other.value == null) && |
||||
this.isNegated == other.isNegated); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,66 @@
@@ -0,0 +1,66 @@
|
||||
/* |
||||
* Copyright 2002-2011 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.servlet.mvc.method.condition; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
|
||||
import org.springframework.web.util.WebUtils; |
||||
|
||||
/** |
||||
* Request parameter name-value condition. |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @author Arjen Poutsma |
||||
* @see org.springframework.web.bind.annotation.RequestMapping#params() |
||||
* @since 3.1 |
||||
*/ |
||||
class ParamRequestCondition extends AbstractNameValueCondition<String> { |
||||
|
||||
ParamRequestCondition(String expression) { |
||||
super(expression); |
||||
} |
||||
|
||||
@Override |
||||
protected String parseValue(String valueExpression) { |
||||
return valueExpression; |
||||
} |
||||
|
||||
@Override |
||||
protected boolean matchName(HttpServletRequest request) { |
||||
return WebUtils.hasSubmitParameter(request, name); |
||||
} |
||||
|
||||
@Override |
||||
protected boolean matchValue(HttpServletRequest request) { |
||||
return value.equals(request.getParameter(name)); |
||||
} |
||||
|
||||
@Override |
||||
public boolean equals(Object obj) { |
||||
if (this == obj) { |
||||
return true; |
||||
} |
||||
if (obj != null && obj instanceof ParamRequestCondition) { |
||||
ParamRequestCondition other = (ParamRequestCondition) obj; |
||||
return ((this.name.equals(other.name)) && |
||||
(this.value != null ? this.value.equals(other.value) : other.value == null) && |
||||
this.isNegated == other.isNegated); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,49 @@
@@ -0,0 +1,49 @@
|
||||
/* |
||||
* Copyright 2002-2011 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.servlet.mvc.method.condition; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
|
||||
/** |
||||
* Defines the contract for conditions that must be met before an incoming request matches a {@link |
||||
* org.springframework.web.servlet.mvc.method.annotation.RequestKey RequestKey}. |
||||
* |
||||
* <p>Implementations of this interface are created by the {@link RequestConditionFactory}. |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @author Arjen Poutsma |
||||
* @see RequestConditionFactory |
||||
* @since 3.1 |
||||
*/ |
||||
public interface RequestCondition { |
||||
|
||||
/** |
||||
* Indicates whether this condition matches against the given servlet request. |
||||
* |
||||
* @param request the request |
||||
* @return {@code true} if this condition matches the request; {@code false} otherwise |
||||
*/ |
||||
boolean match(HttpServletRequest request); |
||||
|
||||
/** |
||||
* Indicates the relative weight of this condition. More important conditions have a higher weight than ones that are |
||||
* less so. |
||||
* |
||||
* @return the weight of this condition |
||||
*/ |
||||
int weight(); |
||||
} |
@ -0,0 +1,77 @@
@@ -0,0 +1,77 @@
|
||||
/* |
||||
* Copyright 2002-2011 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.servlet.mvc.method.condition; |
||||
|
||||
import java.util.Iterator; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* Abstract base class for {@link RequestCondition} implementations that wrap other request conditions. |
||||
* |
||||
* @author Arjen Poutsma |
||||
* @since 3.1 |
||||
*/ |
||||
abstract class RequestConditionComposite implements RequestCondition { |
||||
|
||||
protected final List<RequestCondition> conditions; |
||||
|
||||
public RequestConditionComposite(List<RequestCondition> conditions) { |
||||
this.conditions = conditions; |
||||
} |
||||
|
||||
public int weight() { |
||||
int size = 0; |
||||
for (RequestCondition condition : conditions) { |
||||
size += condition.weight(); |
||||
} |
||||
return size; |
||||
} |
||||
|
||||
@Override |
||||
public boolean equals(Object o) { |
||||
if (this == o) { |
||||
return true; |
||||
} |
||||
if (o != null && getClass().equals(o.getClass())) { |
||||
RequestConditionComposite other = (RequestConditionComposite) o; |
||||
return this.conditions.equals(other.conditions); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
@Override |
||||
public int hashCode() { |
||||
return conditions.hashCode(); |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
StringBuilder builder = new StringBuilder("["); |
||||
String infix = getToStringInfix(); |
||||
for (Iterator<RequestCondition> iterator = conditions.iterator(); iterator.hasNext();) { |
||||
RequestCondition condition = iterator.next(); |
||||
builder.append(condition.toString()); |
||||
if (iterator.hasNext()) { |
||||
builder.append(infix); |
||||
} |
||||
} |
||||
builder.append("]"); |
||||
return builder.toString(); |
||||
} |
||||
|
||||
protected abstract String getToStringInfix(); |
||||
} |
@ -0,0 +1,175 @@
@@ -0,0 +1,175 @@
|
||||
/* |
||||
* Copyright 2002-2011 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.servlet.mvc.method.condition; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Arrays; |
||||
import java.util.Iterator; |
||||
import java.util.List; |
||||
import javax.servlet.http.HttpServletRequest; |
||||
|
||||
import org.springframework.util.ObjectUtils; |
||||
|
||||
/** |
||||
* Factory for {@link RequestCondition} objects. |
||||
* |
||||
* @author Arjen Poutsma |
||||
* @author Rossen Stoyanchev |
||||
* @since 3.1 |
||||
*/ |
||||
public abstract class RequestConditionFactory { |
||||
|
||||
private static final RequestCondition TRUE_CONDITION = new RequestCondition() { |
||||
public boolean match(HttpServletRequest request) { |
||||
return true; |
||||
} |
||||
|
||||
public int weight() { |
||||
return 0; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return "TRUE"; |
||||
} |
||||
}; |
||||
|
||||
private static final RequestCondition FALSE_CONDITION = new RequestCondition() { |
||||
public boolean match(HttpServletRequest request) { |
||||
return false; |
||||
} |
||||
|
||||
public int weight() { |
||||
return 0; |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public String toString() { |
||||
return "FALSE"; |
||||
} |
||||
}; |
||||
|
||||
public static RequestCondition trueCondition() { |
||||
return TRUE_CONDITION; |
||||
} |
||||
|
||||
public static RequestCondition falseCondition() { |
||||
return FALSE_CONDITION; |
||||
} |
||||
|
||||
/** |
||||
* Combines the given conditions into a logical AND, i.e. the returned condition will return {@code true} for {@link |
||||
* RequestCondition#match(HttpServletRequest)} if all of the given conditions do so. |
||||
* |
||||
* @param conditions the conditions |
||||
* @return a condition that represents a logical AND |
||||
*/ |
||||
public static RequestCondition and(RequestCondition... conditions) { |
||||
List<RequestCondition> filteredConditions = new ArrayList<RequestCondition>(Arrays.asList(conditions)); |
||||
for (Iterator<RequestCondition> iterator = filteredConditions.iterator(); iterator.hasNext();) { |
||||
RequestCondition condition = iterator.next(); |
||||
if (condition == TRUE_CONDITION) { |
||||
iterator.remove(); |
||||
} |
||||
} |
||||
return new LogicalConjunctionRequestCondition(filteredConditions); |
||||
} |
||||
|
||||
/** |
||||
* Combines the given conditions into a logical OR, i.e. the returned condition will return {@code true} for {@link |
||||
* RequestCondition#match(HttpServletRequest)} if any of the given conditions do so. |
||||
* |
||||
* @param conditions the conditions |
||||
* @return a condition that represents a logical OR |
||||
*/ |
||||
public static RequestCondition or(RequestCondition... conditions) { |
||||
List<RequestCondition> filteredConditions = new ArrayList<RequestCondition>(Arrays.asList(conditions)); |
||||
for (Iterator<RequestCondition> iterator = filteredConditions.iterator(); iterator.hasNext();) { |
||||
RequestCondition condition = iterator.next(); |
||||
if (condition == TRUE_CONDITION) { |
||||
return trueCondition(); |
||||
} |
||||
else if (condition == FALSE_CONDITION) { |
||||
iterator.remove(); |
||||
} |
||||
} |
||||
return new LogicalDisjunctionRequestCondition(filteredConditions); |
||||
} |
||||
|
||||
/** |
||||
* Parses the given parameters, and returns them as a single request conditions. |
||||
* |
||||
* @param params the parameters |
||||
* @return the request condition |
||||
* @see org.springframework.web.bind.annotation.RequestMapping#params() |
||||
*/ |
||||
public static RequestCondition parseParams(String... params) { |
||||
if (ObjectUtils.isEmpty(params)) { |
||||
return TRUE_CONDITION; |
||||
} |
||||
RequestCondition[] result = new RequestCondition[params.length]; |
||||
for (int i = 0; i < params.length; i++) { |
||||
result[i] = new ParamRequestCondition(params[i]); |
||||
} |
||||
return and(result); |
||||
} |
||||
|
||||
/** |
||||
* Parses the given headers, and returns them as a single request condition. |
||||
* |
||||
* @param headers the headers |
||||
* @return the request condition |
||||
* @see org.springframework.web.bind.annotation.RequestMapping#headers() |
||||
*/ |
||||
public static RequestCondition parseHeaders(String... headers) { |
||||
if (ObjectUtils.isEmpty(headers)) { |
||||
return TRUE_CONDITION; |
||||
} |
||||
RequestCondition[] result = new RequestCondition[headers.length]; |
||||
for (int i = 0; i < headers.length; i++) { |
||||
HeaderRequestCondition header = new HeaderRequestCondition(headers[i]); |
||||
if (isMediaTypeHeader(header.name)) { |
||||
result[i] = new MediaTypeHeaderRequestCondition(headers[i]); |
||||
} |
||||
else { |
||||
result[i] = header; |
||||
} |
||||
} |
||||
return and(result); |
||||
} |
||||
|
||||
private static boolean isMediaTypeHeader(String name) { |
||||
return "Accept".equalsIgnoreCase(name) || "Content-Type".equalsIgnoreCase(name); |
||||
} |
||||
|
||||
public static RequestCondition parseConsumes(String... consumes) { |
||||
if (ObjectUtils.isEmpty(consumes)) { |
||||
return TRUE_CONDITION; |
||||
} |
||||
RequestCondition[] result = new RequestCondition[consumes.length]; |
||||
for (int i = 0; i < consumes.length; i++) { |
||||
result[i] = new ConsumesRequestCondition(consumes[i]); |
||||
} |
||||
return or(result); |
||||
} |
||||
|
||||
//
|
||||
// Conditions
|
||||
//
|
||||
|
||||
} |
@ -1,155 +0,0 @@
@@ -1,155 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2011 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.servlet.mvc.method.annotation; |
||||
|
||||
import java.util.Set; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
import org.springframework.mock.web.MockHttpServletRequest; |
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestCondition; |
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestConditionFactory; |
||||
|
||||
import static org.junit.Assert.*; |
||||
|
||||
/** |
||||
* @author Arjen Poutsma |
||||
*/ |
||||
public class RequestConditionFactoryTests { |
||||
|
||||
@Test |
||||
public void paramEquals() { |
||||
assertEquals(getSingleParamCondition("foo"), getSingleParamCondition("foo")); |
||||
assertFalse(getSingleParamCondition("foo").equals(getSingleParamCondition("bar"))); |
||||
assertFalse(getSingleParamCondition("foo").equals(getSingleParamCondition("FOO"))); |
||||
assertEquals(getSingleParamCondition("foo=bar"), getSingleParamCondition("foo=bar")); |
||||
assertFalse(getSingleParamCondition("foo=bar").equals(getSingleParamCondition("FOO=bar"))); |
||||
} |
||||
|
||||
@Test |
||||
public void headerEquals() { |
||||
assertEquals(getSingleHeaderCondition("foo"), getSingleHeaderCondition("foo")); |
||||
assertEquals(getSingleHeaderCondition("foo"), getSingleHeaderCondition("FOO")); |
||||
assertFalse(getSingleHeaderCondition("foo").equals(getSingleHeaderCondition("bar"))); |
||||
assertEquals(getSingleHeaderCondition("foo=bar"), getSingleHeaderCondition("foo=bar")); |
||||
assertEquals(getSingleHeaderCondition("foo=bar"), getSingleHeaderCondition("FOO=bar")); |
||||
assertEquals(getSingleHeaderCondition("content-type=text/xml"), |
||||
getSingleHeaderCondition("Content-Type=TEXT/XML")); |
||||
} |
||||
|
||||
@Test |
||||
public void headerPresent() { |
||||
RequestCondition condition = getSingleHeaderCondition("accept"); |
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(); |
||||
request.addHeader("Accept", ""); |
||||
|
||||
assertTrue(condition.match(request)); |
||||
} |
||||
|
||||
@Test |
||||
public void headerPresentNoMatch() { |
||||
RequestCondition condition = getSingleHeaderCondition("foo"); |
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(); |
||||
request.addHeader("bar", ""); |
||||
|
||||
assertFalse(condition.match(request)); |
||||
} |
||||
|
||||
@Test |
||||
public void headerNotPresent() { |
||||
RequestCondition condition = getSingleHeaderCondition("!accept"); |
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(); |
||||
|
||||
assertTrue(condition.match(request)); |
||||
} |
||||
|
||||
@Test |
||||
public void headerValueMatch() { |
||||
RequestCondition condition = getSingleHeaderCondition("foo=bar"); |
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(); |
||||
request.addHeader("foo", "bar"); |
||||
|
||||
assertTrue(condition.match(request)); |
||||
} |
||||
|
||||
@Test |
||||
public void headerValueNoMatch() { |
||||
RequestCondition condition = getSingleHeaderCondition("foo=bar"); |
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(); |
||||
request.addHeader("foo", "bazz"); |
||||
|
||||
assertFalse(condition.match(request)); |
||||
} |
||||
|
||||
@Test |
||||
public void headerCaseSensitiveValueMatch() { |
||||
RequestCondition condition = getSingleHeaderCondition("foo=Bar"); |
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(); |
||||
request.addHeader("foo", "bar"); |
||||
|
||||
assertFalse(condition.match(request)); |
||||
} |
||||
|
||||
@Test |
||||
public void headerValueMatchNegated() { |
||||
RequestCondition condition = getSingleHeaderCondition("foo!=bar"); |
||||
MockHttpServletRequest request = new MockHttpServletRequest(); |
||||
request.addHeader("foo", "baz"); |
||||
|
||||
assertTrue(condition.match(request)); |
||||
} |
||||
|
||||
@Test |
||||
public void mediaTypeHeaderValueMatch() { |
||||
RequestCondition condition = getSingleHeaderCondition("accept=text/html"); |
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(); |
||||
request.addHeader("Accept", "text/html"); |
||||
|
||||
assertTrue(condition.match(request)); |
||||
} |
||||
|
||||
@Test |
||||
public void mediaTypeHeaderValueMatchNegated() { |
||||
RequestCondition condition = getSingleHeaderCondition("accept!=text/html"); |
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(); |
||||
request.addHeader("Accept", "application/html"); |
||||
|
||||
assertTrue(condition.match(request)); |
||||
} |
||||
|
||||
private RequestCondition getSingleHeaderCondition(String expression) { |
||||
Set<RequestCondition> conditions = RequestConditionFactory.parseHeaders(expression); |
||||
assertEquals(1, conditions.size()); |
||||
return conditions.iterator().next(); |
||||
} |
||||
|
||||
private RequestCondition getSingleParamCondition(String expression) { |
||||
Set<RequestCondition> conditions = RequestConditionFactory.parseParams(expression); |
||||
assertEquals(1, conditions.size()); |
||||
return conditions.iterator().next(); |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,212 @@
@@ -0,0 +1,212 @@
|
||||
/* |
||||
* Copyright 2002-2011 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.servlet.mvc.method.condition; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
import org.springframework.mock.web.MockHttpServletRequest; |
||||
|
||||
import static org.junit.Assert.*; |
||||
|
||||
/** |
||||
* @author Arjen Poutsma |
||||
*/ |
||||
public class RequestConditionFactoryTests { |
||||
|
||||
|
||||
@Test |
||||
public void andMatch() { |
||||
RequestCondition condition1 = RequestConditionFactory.trueCondition(); |
||||
RequestCondition condition2 = RequestConditionFactory.trueCondition(); |
||||
RequestCondition and = RequestConditionFactory.and(condition1, condition2); |
||||
assertTrue(and.match(new MockHttpServletRequest())); |
||||
} |
||||
|
||||
@Test |
||||
public void andNoMatch() { |
||||
RequestCondition condition1 = RequestConditionFactory.trueCondition(); |
||||
RequestCondition condition2 = RequestConditionFactory.falseCondition(); |
||||
RequestCondition and = RequestConditionFactory.and(condition1, condition2); |
||||
assertFalse(and.match(new MockHttpServletRequest())); |
||||
} |
||||
|
||||
@Test |
||||
public void orMatch() { |
||||
RequestCondition condition1 = RequestConditionFactory.trueCondition(); |
||||
RequestCondition condition2 = RequestConditionFactory.falseCondition(); |
||||
RequestCondition and = RequestConditionFactory.or(condition1, condition2); |
||||
assertTrue(and.match(new MockHttpServletRequest())); |
||||
} |
||||
|
||||
@Test |
||||
public void orNoMatch() { |
||||
RequestCondition condition1 = RequestConditionFactory.falseCondition(); |
||||
RequestCondition condition2 = RequestConditionFactory.falseCondition(); |
||||
RequestCondition and = RequestConditionFactory.and(condition1, condition2); |
||||
assertFalse(and.match(new MockHttpServletRequest())); |
||||
} |
||||
|
||||
@Test |
||||
public void paramEquals() { |
||||
assertEquals(RequestConditionFactory.parseParams("foo"), RequestConditionFactory.parseParams("foo")); |
||||
assertFalse(RequestConditionFactory.parseParams("foo").equals(RequestConditionFactory.parseParams("bar"))); |
||||
assertFalse(RequestConditionFactory.parseParams("foo").equals(RequestConditionFactory.parseParams("FOO"))); |
||||
assertEquals(RequestConditionFactory.parseParams("foo=bar"), RequestConditionFactory.parseParams("foo=bar")); |
||||
assertFalse( |
||||
RequestConditionFactory.parseParams("foo=bar").equals(RequestConditionFactory.parseParams("FOO=bar"))); |
||||
} |
||||
|
||||
@Test |
||||
public void headerEquals() { |
||||
assertEquals(RequestConditionFactory.parseHeaders("foo"), RequestConditionFactory.parseHeaders("foo")); |
||||
assertEquals(RequestConditionFactory.parseHeaders("foo"), RequestConditionFactory.parseHeaders("FOO")); |
||||
assertFalse(RequestConditionFactory.parseHeaders("foo").equals(RequestConditionFactory.parseHeaders("bar"))); |
||||
assertEquals(RequestConditionFactory.parseHeaders("foo=bar"), RequestConditionFactory.parseHeaders("foo=bar")); |
||||
assertEquals(RequestConditionFactory.parseHeaders("foo=bar"), RequestConditionFactory.parseHeaders("FOO=bar")); |
||||
assertEquals(RequestConditionFactory.parseHeaders("content-type=text/xml"), |
||||
RequestConditionFactory.parseHeaders("Content-Type=TEXT/XML")); |
||||
} |
||||
|
||||
@Test |
||||
public void headerPresent() { |
||||
RequestCondition condition = RequestConditionFactory.parseHeaders("accept"); |
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(); |
||||
request.addHeader("Accept", ""); |
||||
|
||||
assertTrue(condition.match(request)); |
||||
} |
||||
|
||||
@Test |
||||
public void headerPresentNoMatch() { |
||||
RequestCondition condition = RequestConditionFactory.parseHeaders("foo"); |
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(); |
||||
request.addHeader("bar", ""); |
||||
|
||||
assertFalse(condition.match(request)); |
||||
} |
||||
|
||||
@Test |
||||
public void headerNotPresent() { |
||||
RequestCondition condition = RequestConditionFactory.parseHeaders("!accept"); |
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(); |
||||
|
||||
assertTrue(condition.match(request)); |
||||
} |
||||
|
||||
@Test |
||||
public void headerValueMatch() { |
||||
RequestCondition condition = RequestConditionFactory.parseHeaders("foo=bar"); |
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(); |
||||
request.addHeader("foo", "bar"); |
||||
|
||||
assertTrue(condition.match(request)); |
||||
} |
||||
|
||||
@Test |
||||
public void headerValueNoMatch() { |
||||
RequestCondition condition = RequestConditionFactory.parseHeaders("foo=bar"); |
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(); |
||||
request.addHeader("foo", "bazz"); |
||||
|
||||
assertFalse(condition.match(request)); |
||||
} |
||||
|
||||
@Test |
||||
public void headerCaseSensitiveValueMatch() { |
||||
RequestCondition condition = RequestConditionFactory.parseHeaders("foo=Bar"); |
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(); |
||||
request.addHeader("foo", "bar"); |
||||
|
||||
assertFalse(condition.match(request)); |
||||
} |
||||
|
||||
@Test |
||||
public void headerValueMatchNegated() { |
||||
RequestCondition condition = RequestConditionFactory.parseHeaders("foo!=bar"); |
||||
MockHttpServletRequest request = new MockHttpServletRequest(); |
||||
request.addHeader("foo", "baz"); |
||||
|
||||
assertTrue(condition.match(request)); |
||||
} |
||||
|
||||
@Test |
||||
public void mediaTypeHeaderValueMatch() { |
||||
RequestCondition condition = RequestConditionFactory.parseHeaders("accept=text/html"); |
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(); |
||||
request.addHeader("Accept", "text/html"); |
||||
|
||||
assertTrue(condition.match(request)); |
||||
} |
||||
|
||||
@Test |
||||
public void mediaTypeHeaderValueMatchNegated() { |
||||
RequestCondition condition = RequestConditionFactory.parseHeaders("accept!=text/html"); |
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(); |
||||
request.addHeader("Accept", "application/html"); |
||||
|
||||
assertTrue(condition.match(request)); |
||||
} |
||||
|
||||
@Test |
||||
public void consumesMatch() { |
||||
RequestCondition condition = RequestConditionFactory.parseConsumes("text/plain"); |
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(); |
||||
request.setContentType("text/plain"); |
||||
|
||||
assertTrue(condition.match(request)); |
||||
} |
||||
|
||||
@Test |
||||
public void consumesWildcardMatch() { |
||||
RequestCondition condition = RequestConditionFactory.parseConsumes("text/*"); |
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(); |
||||
request.setContentType("text/plain"); |
||||
|
||||
assertTrue(condition.match(request)); |
||||
} |
||||
|
||||
@Test |
||||
public void consumesMultipleMatch() { |
||||
RequestCondition condition = RequestConditionFactory.parseConsumes("text/plain", "application/xml"); |
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(); |
||||
request.setContentType("text/plain"); |
||||
|
||||
assertTrue(condition.match(request)); |
||||
} |
||||
|
||||
@Test |
||||
public void consumesSingleNoMatch() { |
||||
RequestCondition condition = RequestConditionFactory.parseConsumes("text/plain"); |
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(); |
||||
request.setContentType("application/xml"); |
||||
|
||||
assertFalse(condition.match(request)); |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue