Support HTTP HEAD

Issue: SPR-13130
This commit is contained in:
Rossen Stoyanchev
2016-01-24 19:35:45 -05:00
parent fb7dfc4569
commit d70ad765bf
8 changed files with 95 additions and 30 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2016 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.
@@ -36,6 +36,10 @@ import org.springframework.web.bind.annotation.RequestMethod;
*/
public final class RequestMethodsRequestCondition extends AbstractRequestCondition<RequestMethodsRequestCondition> {
private static final RequestMethodsRequestCondition HEAD_CONDITION =
new RequestMethodsRequestCondition(RequestMethod.HEAD);
private final Set<RequestMethod> methods;
@@ -98,17 +102,24 @@ public final class RequestMethodsRequestCondition extends AbstractRequestConditi
if (this.methods.isEmpty()) {
return this;
}
RequestMethod incomingRequestMethod = getRequestMethod(request);
if (incomingRequestMethod != null) {
RequestMethod requestMethod = getRequestMethod(request);
if (requestMethod != null) {
for (RequestMethod method : this.methods) {
if (method.equals(incomingRequestMethod)) {
if (method.equals(requestMethod)) {
return new RequestMethodsRequestCondition(method);
}
}
if (isHeadRequest(requestMethod) && getMethods().contains(RequestMethod.GET)) {
return HEAD_CONDITION;
}
}
return null;
}
private boolean isHeadRequest(RequestMethod requestMethod) {
return (requestMethod != null && RequestMethod.HEAD.equals(requestMethod));
}
private RequestMethod getRequestMethod(HttpServletRequest request) {
try {
return RequestMethod.valueOf(request.getMethod());

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@@ -170,7 +170,9 @@ public class HttpEntityMethodProcessor extends AbstractMessageConverterMethodPro
Object body = responseEntity.getBody();
if (responseEntity instanceof ResponseEntity) {
outputMessage.setStatusCode(((ResponseEntity<?>) responseEntity).getStatusCode());
if (HttpMethod.GET == inputMessage.getMethod() && isResourceNotModified(inputMessage, outputMessage)) {
HttpMethod method = inputMessage.getMethod();
boolean isGetOrHead = (HttpMethod.GET == method || HttpMethod.HEAD == method);
if (isGetOrHead && isResourceNotModified(inputMessage, outputMessage)) {
outputMessage.setStatusCode(HttpStatus.NOT_MODIFIED);
// Ensure headers are flushed, no body should be written.
outputMessage.flush();