下面列出了org.springframework.http.HttpMethod#HEAD 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void handleRequest(HttpServerExchange exchange) {
UndertowServerHttpRequest request = null;
try {
request = new UndertowServerHttpRequest(exchange, getDataBufferFactory());
}
catch (URISyntaxException ex) {
if (logger.isWarnEnabled()) {
logger.debug("Failed to get request URI: " + ex.getMessage());
}
exchange.setStatusCode(400);
return;
}
ServerHttpResponse response = new UndertowServerHttpResponse(exchange, getDataBufferFactory(), request);
if (request.getMethod() == HttpMethod.HEAD) {
response = new HttpHeadResponseDecorator(response);
}
HandlerResultSubscriber resultSubscriber = new HandlerResultSubscriber(exchange, request);
this.httpHandler.handle(request, response).subscribe(resultSubscriber);
}
@Override
public void handleRequest(HttpServerExchange exchange) {
UndertowServerHttpRequest request = null;
try {
request = new UndertowServerHttpRequest(exchange, getDataBufferFactory());
}
catch (URISyntaxException ex) {
if (logger.isWarnEnabled()) {
logger.debug("Failed to get request URI: " + ex.getMessage());
}
exchange.setStatusCode(400);
return;
}
ServerHttpResponse response = new UndertowServerHttpResponse(exchange, getDataBufferFactory(), request);
if (request.getMethod() == HttpMethod.HEAD) {
response = new HttpHeadResponseDecorator(response);
}
HandlerResultSubscriber resultSubscriber = new HandlerResultSubscriber(exchange, request);
this.httpHandler.handle(request, response).subscribe(resultSubscriber);
}
/**
* Gets the HTTP method indicating the operation to perform on the resource identified in the client's HTTP request.
* This method converts GemFire's HttpMethod enumerated value from the Link into a corresponding Spring HttpMethod
* enumerated value.
* <p/>
* @return a Spring HttpMethod enumerated value indicating the operation to perform on the resource identified in the
* client's HTTP request.
* @see com.gemstone.gemfire.management.internal.web.http.HttpMethod
* @see com.gemstone.gemfire.management.internal.web.domain.Link#getMethod()
* @see org.springframework.http.HttpMethod
* @see org.springframework.http.HttpRequest#getMethod()
*/
@Override
public HttpMethod getMethod() {
switch (getLink().getMethod()) {
case DELETE:
return HttpMethod.DELETE;
case HEAD:
return HttpMethod.HEAD;
case OPTIONS:
return HttpMethod.OPTIONS;
case POST:
return HttpMethod.POST;
case PUT:
return HttpMethod.PUT;
case TRACE:
return HttpMethod.TRACE;
case GET:
default:
return HttpMethod.GET;
}
}
/**
* Gets the HTTP method indicating the operation to perform on the resource identified in the client's HTTP request.
* This method converts GemFire's HttpMethod enumerated value from the Link into a corresponding Spring HttpMethod
* enumerated value.
* <p/>
* @return a Spring HttpMethod enumerated value indicating the operation to perform on the resource identified in the
* client's HTTP request.
* @see com.gemstone.gemfire.management.internal.web.http.HttpMethod
* @see com.gemstone.gemfire.management.internal.web.domain.Link#getMethod()
* @see org.springframework.http.HttpMethod
* @see org.springframework.http.HttpRequest#getMethod()
*/
@Override
public HttpMethod getMethod() {
switch (getLink().getMethod()) {
case DELETE:
return HttpMethod.DELETE;
case HEAD:
return HttpMethod.HEAD;
case OPTIONS:
return HttpMethod.OPTIONS;
case POST:
return HttpMethod.POST;
case PUT:
return HttpMethod.PUT;
case TRACE:
return HttpMethod.TRACE;
case GET:
default:
return HttpMethod.GET;
}
}
@Nullable
private RequestMethodsRequestCondition matchRequestMethod(String httpMethodValue) {
HttpMethod httpMethod = HttpMethod.resolve(httpMethodValue);
if (httpMethod != null) {
for (RequestMethod method : getMethods()) {
if (httpMethod.matches(method.name())) {
return requestMethodConditionCache.get(method.name());
}
}
if (httpMethod == HttpMethod.HEAD && getMethods().contains(RequestMethod.GET)) {
return requestMethodConditionCache.get(HttpMethod.GET.name());
}
}
return null;
}
private boolean isResourceNotModified(ServletServerHttpRequest request, ServletServerHttpResponse response) {
ServletWebRequest servletWebRequest =
new ServletWebRequest(request.getServletRequest(), response.getServletResponse());
HttpHeaders responseHeaders = response.getHeaders();
String etag = responseHeaders.getETag();
long lastModifiedTimestamp = responseHeaders.getLastModified();
if (request.getMethod() == HttpMethod.GET || request.getMethod() == HttpMethod.HEAD) {
responseHeaders.remove(HttpHeaders.ETAG);
responseHeaders.remove(HttpHeaders.LAST_MODIFIED);
}
return servletWebRequest.checkNotModified(etag, lastModifiedTimestamp);
}
@Nullable
private RequestMethodsRequestCondition matchRequestMethod(@Nullable HttpMethod httpMethod) {
if (httpMethod != null) {
for (RequestMethod method : getMethods()) {
if (httpMethod.matches(method.name())) {
return new RequestMethodsRequestCondition(method);
}
}
if (httpMethod == HttpMethod.HEAD && getMethods().contains(RequestMethod.GET)) {
return GET_CONDITION;
}
}
return null;
}
@Test
public void givenFooService_whenCallOptionsForAllow_thenReceiveValueOfAllowHeader() {
final Set<HttpMethod> optionsForAllow = restTemplate.optionsForAllow(fooResourceUrl);
final HttpMethod[] supportedMethods = { HttpMethod.GET, HttpMethod.POST, HttpMethod.HEAD };
assertTrue(optionsForAllow.containsAll(Arrays.asList(supportedMethods)));
}
private boolean isResourceNotModified(ServletServerHttpRequest request, ServletServerHttpResponse response) {
ServletWebRequest servletWebRequest =
new ServletWebRequest(request.getServletRequest(), response.getServletResponse());
HttpHeaders responseHeaders = response.getHeaders();
String etag = responseHeaders.getETag();
long lastModifiedTimestamp = responseHeaders.getLastModified();
if (request.getMethod() == HttpMethod.GET || request.getMethod() == HttpMethod.HEAD) {
responseHeaders.remove(HttpHeaders.ETAG);
responseHeaders.remove(HttpHeaders.LAST_MODIFIED);
}
return servletWebRequest.checkNotModified(etag, lastModifiedTimestamp);
}
private RequestMethodsRequestCondition matchRequestMethod(String httpMethodValue) {
HttpMethod httpMethod = HttpMethod.resolve(httpMethodValue);
if (httpMethod != null) {
for (RequestMethod method : getMethods()) {
if (httpMethod.matches(method.name())) {
return new RequestMethodsRequestCondition(method);
}
}
if (httpMethod == HttpMethod.HEAD && getMethods().contains(RequestMethod.GET)) {
return GET_CONDITION;
}
}
return null;
}
private boolean isResourceNotModified(ServletServerHttpRequest inputMessage, ServletServerHttpResponse outputMessage) {
ServletWebRequest servletWebRequest =
new ServletWebRequest(inputMessage.getServletRequest(), outputMessage.getServletResponse());
HttpHeaders responseHeaders = outputMessage.getHeaders();
String etag = responseHeaders.getETag();
long lastModifiedTimestamp = responseHeaders.getLastModified();
if (inputMessage.getMethod() == HttpMethod.GET || inputMessage.getMethod() == HttpMethod.HEAD) {
responseHeaders.remove(HttpHeaders.ETAG);
responseHeaders.remove(HttpHeaders.LAST_MODIFIED);
}
return servletWebRequest.checkNotModified(etag, lastModifiedTimestamp);
}
@Test
public void givenFooService_whenCallOptionsForAllow_thenReceiveValueOfAllowHeader() {
final Set<HttpMethod> optionsForAllow = restTemplate.optionsForAllow(fooResourceUrl);
final HttpMethod[] supportedMethods = { HttpMethod.GET, HttpMethod.POST, HttpMethod.HEAD };
assertTrue(optionsForAllow.containsAll(Arrays.asList(supportedMethods)));
}
private ServerHttpResponse prepareResponse(ServerHttpResponse response, ServerHttpRequest request) {
return (request.getMethod() == HttpMethod.HEAD ? new HttpHeadResponseDecorator(response) : response);
}
private ServerHttpResponse prepareResponse(ServerHttpResponse response, ServerHttpRequest request) {
return (request.getMethod() == HttpMethod.HEAD ? new HttpHeadResponseDecorator(response) : response);
}
/**
* Create a {@link MockHttpServletRequestBuilder} for a HEAD request.
* @param urlTemplate a URL template; the resulting URL will be encoded
* @param uriVars zero or more URI variables
* @since 4.1
*/
public static MockHttpServletRequestBuilder head(String urlTemplate, Object... uriVars) {
return new MockHttpServletRequestBuilder(HttpMethod.HEAD, urlTemplate, uriVars);
}
/**
* Create a {@link MockHttpServletRequestBuilder} for a HEAD request.
* @param uri the URL
* @since 4.1
*/
public static MockHttpServletRequestBuilder head(URI uri) {
return new MockHttpServletRequestBuilder(HttpMethod.HEAD, uri);
}
/**
* Create a {@link MockHttpServletRequestBuilder} for a HEAD request.
* @param urlTemplate a URL template; the resulting URL will be encoded
* @param uriVars zero or more URI variables
* @since 4.1
*/
public static MockHttpServletRequestBuilder head(String urlTemplate, Object... uriVars) {
return new MockHttpServletRequestBuilder(HttpMethod.HEAD, urlTemplate, uriVars);
}
/**
* Create a {@link MockHttpServletRequestBuilder} for a HEAD request.
* @param uri the URL
* @since 4.1
*/
public static MockHttpServletRequestBuilder head(URI uri) {
return new MockHttpServletRequestBuilder(HttpMethod.HEAD, uri);
}
/**
* Create a {@link MockHttpServletRequestBuilder} for a HEAD request.
* @param urlTemplate a URL template; the resulting URL will be encoded
* @param urlVariables zero or more URL variables
* @since 4.1
*/
public static MockHttpServletRequestBuilder head(String urlTemplate, Object... urlVariables) {
return new MockHttpServletRequestBuilder(HttpMethod.HEAD, urlTemplate, urlVariables);
}
/**
* Create a {@link MockHttpServletRequestBuilder} for a HEAD request.
* @param uri the URL
* @since 4.1
*/
public static MockHttpServletRequestBuilder head(URI uri) {
return new MockHttpServletRequestBuilder(HttpMethod.HEAD, uri);
}