Java Code Examples for org.springframework.http.HttpHeaders#setAllow()

The following examples show how to use org.springframework.http.HttpHeaders#setAllow() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RestTemplateTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void optionsForAllow() throws Exception {
	given(requestFactory.createRequest(new URI("http://example.com"), HttpMethod.OPTIONS)).willReturn(request);
	given(request.execute()).willReturn(response);
	given(errorHandler.hasError(response)).willReturn(false);
	HttpHeaders responseHeaders = new HttpHeaders();
	EnumSet<HttpMethod> expected = EnumSet.of(HttpMethod.GET, HttpMethod.POST);
	responseHeaders.setAllow(expected);
	given(response.getHeaders()).willReturn(responseHeaders);
	HttpStatus status = HttpStatus.OK;
	given(response.getStatusCode()).willReturn(status);
	given(response.getStatusText()).willReturn(status.getReasonPhrase());

	Set<HttpMethod> result = template.optionsForAllow("http://example.com");
	assertEquals("Invalid OPTIONS result", expected, result);

	verify(response).close();
}
 
Example 2
Source File: MethodNotAllowedAdviceTrait.java    From problem-spring-web with MIT License 6 votes vote down vote up
@API(status = INTERNAL)
@ExceptionHandler
default Mono<ResponseEntity<Problem>> handleRequestMethodNotSupportedException(
        final MethodNotAllowedException exception,
        final ServerWebExchange request) {

    final Set<HttpMethod> methods = exception.getSupportedMethods();

    if (methods.isEmpty()) {
        return create(Status.METHOD_NOT_ALLOWED, exception, request);
    }

    final HttpHeaders headers = new HttpHeaders();
    headers.setAllow(methods);

    return create(Status.METHOD_NOT_ALLOWED, exception, request, headers);
}
 
Example 3
Source File: MethodNotAllowedAdviceTrait.java    From problem-spring-web with MIT License 6 votes vote down vote up
@API(status = INTERNAL)
@ExceptionHandler
default ResponseEntity<Problem> handleRequestMethodNotSupportedException(
        final HttpRequestMethodNotSupportedException exception,
        final NativeWebRequest request) {

    @WTF
    @Facepalm("Nullable arrays... great work from Spring :/")
    @Nullable final String[] methods = exception.getSupportedMethods();

    if (methods == null || methods.length == 0) {
        return create(Status.METHOD_NOT_ALLOWED, exception, request);
    }

    final HttpHeaders headers = new HttpHeaders();
    headers.setAllow(requireNonNull(exception.getSupportedHttpMethods()));

    return create(Status.METHOD_NOT_ALLOWED, exception, request, headers);
}
 
Example 4
Source File: ResponseEntityResultHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void httpHeaders() throws Exception {
	HttpHeaders headers = new HttpHeaders();
	headers.setAllow(new LinkedHashSet<>(Arrays.asList(HttpMethod.GET, HttpMethod.POST, HttpMethod.OPTIONS)));
	MethodParameter returnType = on(TestController.class).resolveReturnType(entity(Void.class));
	HandlerResult result = handlerResult(headers, returnType);
	MockServerWebExchange exchange = MockServerWebExchange.from(get("/path"));
	this.resultHandler.handleResult(exchange, result).block(Duration.ofSeconds(5));

	assertEquals(HttpStatus.OK, exchange.getResponse().getStatusCode());
	assertEquals(1, exchange.getResponse().getHeaders().size());
	assertEquals("GET,POST,OPTIONS", exchange.getResponse().getHeaders().getFirst("Allow"));
	assertResponseBodyIsEmpty(exchange);
}
 
Example 5
Source File: ResponseEntityExceptionHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Customize the response for HttpRequestMethodNotSupportedException.
 * <p>This method logs a warning, sets the "Allow" header, and delegates to
 * {@link #handleExceptionInternal}.
 * @param ex the exception
 * @param headers the headers to be written to the response
 * @param status the selected response status
 * @param request the current request
 * @return a {@code ResponseEntity} instance
 */
protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(
		HttpRequestMethodNotSupportedException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {

	pageNotFoundLogger.warn(ex.getMessage());

	Set<HttpMethod> supportedMethods = ex.getSupportedHttpMethods();
	if (!CollectionUtils.isEmpty(supportedMethods)) {
		headers.setAllow(supportedMethods);
	}
	return handleExceptionInternal(ex, null, headers, status, request);
}
 
Example 6
Source File: RestTemplateTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void optionsForAllow() throws Exception {
	mockSentRequest(OPTIONS, "https://example.com");
	mockResponseStatus(HttpStatus.OK);
	HttpHeaders responseHeaders = new HttpHeaders();
	EnumSet<HttpMethod> expected = EnumSet.of(GET, POST);
	responseHeaders.setAllow(expected);
	given(response.getHeaders()).willReturn(responseHeaders);

	Set<HttpMethod> result = template.optionsForAllow("https://example.com");
	assertEquals("Invalid OPTIONS result", expected, result);

	verify(response).close();
}
 
Example 7
Source File: ResponseEntityResultHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void httpHeaders() throws Exception {
	HttpHeaders headers = new HttpHeaders();
	headers.setAllow(new LinkedHashSet<>(Arrays.asList(HttpMethod.GET, HttpMethod.POST, HttpMethod.OPTIONS)));
	MethodParameter returnType = on(TestController.class).resolveReturnType(entity(Void.class));
	HandlerResult result = handlerResult(headers, returnType);
	MockServerWebExchange exchange = MockServerWebExchange.from(get("/path"));
	this.resultHandler.handleResult(exchange, result).block(Duration.ofSeconds(5));

	assertEquals(HttpStatus.OK, exchange.getResponse().getStatusCode());
	assertEquals(1, exchange.getResponse().getHeaders().size());
	assertEquals("GET,POST,OPTIONS", exchange.getResponse().getHeaders().getFirst("Allow"));
	assertResponseBodyIsEmpty(exchange);
}
 
Example 8
Source File: ResponseEntityExceptionHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Customize the response for HttpRequestMethodNotSupportedException.
 * <p>This method logs a warning, sets the "Allow" header, and delegates to
 * {@link #handleExceptionInternal}.
 * @param ex the exception
 * @param headers the headers to be written to the response
 * @param status the selected response status
 * @param request the current request
 * @return a {@code ResponseEntity} instance
 */
protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(
		HttpRequestMethodNotSupportedException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {

	pageNotFoundLogger.warn(ex.getMessage());

	Set<HttpMethod> supportedMethods = ex.getSupportedHttpMethods();
	if (!CollectionUtils.isEmpty(supportedMethods)) {
		headers.setAllow(supportedMethods);
	}
	return handleExceptionInternal(ex, null, headers, status, request);
}
 
Example 9
Source File: RestTemplateTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void optionsForAllow() throws Exception {
	mockSentRequest(OPTIONS, "http://example.com");
	mockResponseStatus(HttpStatus.OK);
	HttpHeaders responseHeaders = new HttpHeaders();
	EnumSet<HttpMethod> expected = EnumSet.of(GET, POST);
	responseHeaders.setAllow(expected);
	given(response.getHeaders()).willReturn(responseHeaders);

	Set<HttpMethod> result = template.optionsForAllow("http://example.com");
	assertEquals("Invalid OPTIONS result", expected, result);

	verify(response).close();
}
 
Example 10
Source File: ResponseEntityExceptionHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Customize the response for HttpRequestMethodNotSupportedException.
 * <p>This method logs a warning, sets the "Allow" header, and delegates to
 * {@link #handleExceptionInternal}.
 * @param ex the exception
 * @param headers the headers to be written to the response
 * @param status the selected response status
 * @param request the current request
 * @return a {@code ResponseEntity} instance
 */
protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex,
		HttpHeaders headers, HttpStatus status, WebRequest request) {

	pageNotFoundLogger.warn(ex.getMessage());

	Set<HttpMethod> supportedMethods = ex.getSupportedHttpMethods();
	if (!CollectionUtils.isEmpty(supportedMethods)) {
		headers.setAllow(supportedMethods);
	}
	return handleExceptionInternal(ex, null, headers, status, request);
}
 
Example 11
Source File: ResponseEntityExceptionHandler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Customize the response for HttpRequestMethodNotSupportedException.
 * <p>This method logs a warning, sets the "Allow" header, and delegates to
 * {@link #handleExceptionInternal}.
 * @param ex the exception
 * @param headers the headers to be written to the response
 * @param status the selected response status
 * @param request the current request
 * @return a {@code ResponseEntity} instance
 */
protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex,
		HttpHeaders headers, HttpStatus status, WebRequest request) {

	pageNotFoundLogger.warn(ex.getMessage());

	Set<HttpMethod> supportedMethods = ex.getSupportedHttpMethods();
	if (!supportedMethods.isEmpty()) {
		headers.setAllow(supportedMethods);
	}

	return handleExceptionInternal(ex, null, headers, status, request);
}
 
Example 12
Source File: SampleController.java    From spring-tutorials with Apache License 2.0 5 votes vote down vote up
@RequestMapping(value = "/api", method = RequestMethod.OPTIONS)
public HttpEntity<String> simpleOptions() {
    HttpHeaders headers = new HttpHeaders();

    Set<HttpMethod> allowedOps = new HashSet<>();
    allowedOps.add(HttpMethod.GET);
    allowedOps.add(HttpMethod.POST);
    allowedOps.add(HttpMethod.PUT);
    allowedOps.add(HttpMethod.DELETE);
    allowedOps.add(HttpMethod.PATCH);
    allowedOps.add(HttpMethod.OPTIONS);
    headers.setAllow(allowedOps);
    return new HttpEntity<>("Hello World", headers);
}
 
Example 13
Source File: HttpRequestMethodNotSupportedExceptionHandler.java    From spring-rest-exception-handler with Apache License 2.0 5 votes vote down vote up
@Override
protected HttpHeaders createHeaders(HttpRequestMethodNotSupportedException ex, HttpServletRequest req) {

    HttpHeaders headers = super.createHeaders(ex, req);

    if (!isEmpty(ex.getSupportedMethods())) {
        headers.setAllow(ex.getSupportedHttpMethods());
    }
    return headers;
}