@Override public EntityResponse.Builder<T> allow(Set<HttpMethod> allowedMethods) { this.headers.setAllow(allowedMethods); return this; }
@Override public ServerResponse.BodyBuilder allow(Set<HttpMethod> allowedMethods) { this.headers.setAllow(allowedMethods); return this; }
@Override public ServerResponse.BodyBuilder allow(HttpMethod... allowedMethods) { this.headers.setAllow(new LinkedHashSet<>(Arrays.asList(allowedMethods))); return this; }
@Override public EntityResponse.Builder<T> allow(HttpMethod... allowedMethods) { this.headers.setAllow(new LinkedHashSet<>(Arrays.asList(allowedMethods))); return this; }
@Override public BodyBuilder allow(HttpMethod... allowedMethods) { this.headers.setAllow(new LinkedHashSet<>(Arrays.asList(allowedMethods))); return this; }
public HttpOptionsHandler(Set<String> declaredMethods) { this.headers.setAllow(initAllowedHttpMethods(declaredMethods)); }
public HttpOptionsHandler(Set<HttpMethod> declaredMethods) { this.headers.setAllow(initAllowedHttpMethods(declaredMethods)); }
@Override public BodyBuilder allow(HttpMethod... allowedMethods) { this.headers.setAllow(new LinkedHashSet<>(Arrays.asList(allowedMethods))); return this; }
protected void sendMethodNotAllowed(ServerHttpResponse response, HttpMethod... httpMethods) { logger.warn("Sending Method Not Allowed (405)"); response.setStatusCode(HttpStatus.METHOD_NOT_ALLOWED); response.getHeaders().setAllow(new LinkedHashSet<>(Arrays.asList(httpMethods))); }
public HttpOptionsHandler(Set<String> declaredMethods) { this.headers.setAllow(initAllowedHttpMethods(declaredMethods)); }
/** * 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); }
/** * 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); }
@Test // SPR-11917 public void getAllowEmptySet() { headers.setAllow(Collections.emptySet()); assertThat(headers.getAllow(), Matchers.emptyCollectionOf(HttpMethod.class)); }
@Test public void allow() { EnumSet<HttpMethod> methods = EnumSet.of(HttpMethod.GET, HttpMethod.POST); headers.setAllow(methods); assertEquals("Invalid Allow header", methods, headers.getAllow()); assertEquals("Invalid Allow header", "GET,POST", headers.getFirst("Allow")); }
if (HttpMethod.GET != request.getMethod()) { response.setStatusCode(HttpStatus.METHOD_NOT_ALLOWED); response.getHeaders().setAllow(Collections.singleton(HttpMethod.GET)); if (logger.isErrorEnabled()) { logger.error("Handshake failed due to unexpected HTTP method: " + request.getMethod());
@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(); }
@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); }
/** * List the OPTIONS for this controller. * * @return */ @RequestMapping(value = PROFILE_ROOT_MAPPING, method = RequestMethod.OPTIONS) public HttpEntity<?> profileOptions() { HttpHeaders headers = new HttpHeaders(); headers.setAllow(Collections.singleton(HttpMethod.GET)); return new ResponseEntity<Object>(headers, HttpStatus.OK); }
/** * <code>OPTIONS /</code>. * * @return * @since 2.2 */ @RequestMapping(value = { "/", "" }, method = RequestMethod.OPTIONS) public HttpEntity<?> optionsForRepositories() { HttpHeaders headers = new HttpHeaders(); headers.setAllow(Collections.singleton(HttpMethod.GET)); return new ResponseEntity<Object>(headers, HttpStatus.OK); }
/** * Send {@code 405 Method Not Allowed} and include the supported {@link org.springframework.http.HttpMethod}s in the * {@code Allow} header. * * @param o_O the exception to handle. * @return */ @ExceptionHandler ResponseEntity<Void> handle(HttpRequestMethodNotSupportedException o_O) { HttpHeaders headers = new HttpHeaders(); headers.setAllow(o_O.getSupportedHttpMethods()); return response(HttpStatus.METHOD_NOT_ALLOWED, headers); }