Search in sources :

Example 36 with HttpStatus

use of org.springframework.http.HttpStatus in project motan by weibocom.

the class CommandController method addCommand.

/**
 * 向指定group添加指令
 *
 * @param group
 * @param clientCommand
 * @return
 */
@RequestMapping(value = "/{group}", method = RequestMethod.POST)
public ResponseEntity<Boolean> addCommand(@PathVariable("group") String group, @RequestBody ClientCommand clientCommand) {
    if (StringUtils.isEmpty(group) || clientCommand == null) {
        return new ResponseEntity<Boolean>(HttpStatus.BAD_REQUEST);
    }
    boolean result = commandService.addCommand(group, clientCommand);
    HttpStatus status;
    if (result) {
        status = HttpStatus.OK;
    } else {
        status = HttpStatus.NOT_MODIFIED;
    }
    return new ResponseEntity<Boolean>(result, status);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) HttpStatus(org.springframework.http.HttpStatus)

Example 37 with HttpStatus

use of org.springframework.http.HttpStatus in project spring-security by spring-projects.

the class BearerTokenServerAuthenticationEntryPoint method commence.

@Override
public Mono<Void> commence(ServerWebExchange exchange, AuthenticationException authException) {
    return Mono.defer(() -> {
        HttpStatus status = getStatus(authException);
        Map<String, String> parameters = createParameters(authException);
        String wwwAuthenticate = computeWWWAuthenticateHeaderValue(parameters);
        ServerHttpResponse response = exchange.getResponse();
        response.getHeaders().set(HttpHeaders.WWW_AUTHENTICATE, wwwAuthenticate);
        response.setStatusCode(status);
        return response.setComplete();
    });
}
Also used : HttpStatus(org.springframework.http.HttpStatus) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse)

Example 38 with HttpStatus

use of org.springframework.http.HttpStatus in project spring-framework by spring-projects.

the class ErrorHandlerIntegrationTests method emptyPathSegments.

// SPR-15560
@ParameterizedHttpServerTest
void emptyPathSegments(HttpServer httpServer) throws Exception {
    startServer(httpServer);
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.setErrorHandler(NO_OP_ERROR_HANDLER);
    URI url = new URI("http://localhost:" + port + "//");
    ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
    // Jetty 10+ rejects empty path segments, see https://github.com/eclipse/jetty.project/issues/6302,
    // but an application can apply CompactPathRule via RewriteHandler:
    // https://www.eclipse.org/jetty/documentation/jetty-11/programming_guide.php
    HttpStatus expectedStatus = (httpServer instanceof JettyHttpServer ? HttpStatus.BAD_REQUEST : HttpStatus.OK);
    assertThat(response.getStatusCode()).isEqualTo(expectedStatus);
}
Also used : HttpStatus(org.springframework.http.HttpStatus) RestTemplate(org.springframework.web.client.RestTemplate) URI(java.net.URI) JettyHttpServer(org.springframework.web.testfixture.http.server.reactive.bootstrap.JettyHttpServer)

Example 39 with HttpStatus

use of org.springframework.http.HttpStatus in project spring-framework by spring-projects.

the class ViewResolutionResultHandlerTests method handleReturnValueTypes.

@Test
public void handleReturnValueTypes() {
    Object returnValue;
    MethodParameter returnType;
    ViewResolver resolver = new TestViewResolver("account");
    returnType = on(Handler.class).resolveReturnType(View.class);
    returnValue = new TestView("account");
    testHandle("/path", returnType, returnValue, "account: {id=123}");
    returnType = on(Handler.class).resolveReturnType(Mono.class, View.class);
    returnValue = Mono.just(new TestView("account"));
    testHandle("/path", returnType, returnValue, "account: {id=123}");
    returnType = on(Handler.class).annotNotPresent(ModelAttribute.class).resolveReturnType(String.class);
    returnValue = "account";
    testHandle("/path", returnType, returnValue, "account: {id=123}", resolver);
    returnType = on(Handler.class).annotPresent(ModelAttribute.class).resolveReturnType(String.class);
    returnValue = "123";
    testHandle("/account", returnType, returnValue, "account: {id=123, myString=123}", resolver);
    returnType = on(Handler.class).resolveReturnType(Mono.class, String.class);
    returnValue = Mono.just("account");
    testHandle("/path", returnType, returnValue, "account: {id=123}", resolver);
    returnType = on(Handler.class).resolveReturnType(Model.class);
    returnValue = new ConcurrentModel().addAttribute("name", "Joe").addAttribute("ignore", null);
    testHandle("/account", returnType, returnValue, "account: {id=123, name=Joe}", resolver);
    // Work around  caching issue...
    ResolvableType.clearCache();
    returnType = on(Handler.class).annotNotPresent(ModelAttribute.class).resolveReturnType(Map.class);
    returnValue = Collections.singletonMap("name", "Joe");
    testHandle("/account", returnType, returnValue, "account: {id=123, name=Joe}", resolver);
    // Work around  caching issue...
    ResolvableType.clearCache();
    returnType = on(Handler.class).annotPresent(ModelAttribute.class).resolveReturnType(Map.class);
    returnValue = Collections.singletonMap("name", "Joe");
    testHandle("/account", returnType, returnValue, "account: {id=123, myMap={name=Joe}}", resolver);
    returnType = on(Handler.class).resolveReturnType(TestBean.class);
    returnValue = new TestBean("Joe");
    String responseBody = "account: {id=123, " + "org.springframework.validation.BindingResult.testBean=" + "org.springframework.validation.BeanPropertyBindingResult: 0 errors, " + "testBean=TestBean[name=Joe]}";
    testHandle("/account", returnType, returnValue, responseBody, resolver);
    returnType = on(Handler.class).annotPresent(ModelAttribute.class).resolveReturnType(Long.class);
    testHandle("/account", returnType, 99L, "account: {id=123, myLong=99}", resolver);
    returnType = on(Handler.class).resolveReturnType(Rendering.class);
    HttpStatus status = HttpStatus.UNPROCESSABLE_ENTITY;
    returnValue = Rendering.view("account").modelAttribute("a", "a1").status(status).header("h", "h1").build();
    String expected = "account: {a=a1, id=123}";
    ServerWebExchange exchange = testHandle("/path", returnType, returnValue, expected, resolver);
    assertThat(exchange.getResponse().getStatusCode()).isEqualTo(status);
    assertThat(exchange.getResponse().getHeaders().getFirst("h")).isEqualTo("h1");
}
Also used : MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) ConcurrentModel(org.springframework.ui.ConcurrentModel) HttpStatus(org.springframework.http.HttpStatus) Mono(reactor.core.publisher.Mono) ConcurrentModel(org.springframework.ui.ConcurrentModel) Model(org.springframework.ui.Model) MethodParameter(org.springframework.core.MethodParameter) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) Test(org.junit.jupiter.api.Test)

Example 40 with HttpStatus

use of org.springframework.http.HttpStatus in project spring-framework by spring-projects.

the class RouterFunctionBuilderTests method route.

@Test
public void route() {
    RouterFunction<ServerResponse> route = RouterFunctions.route().GET("/foo", request -> ServerResponse.ok().build()).POST("/", RequestPredicates.contentType(MediaType.TEXT_PLAIN), request -> ServerResponse.noContent().build()).route(HEAD("/foo"), request -> ServerResponse.accepted().build()).build();
    MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://example.com/foo").build();
    ServerRequest getRequest = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
    Mono<Integer> responseMono = route.route(getRequest).flatMap(handlerFunction -> handlerFunction.handle(getRequest)).map(ServerResponse::statusCode).map(HttpStatus::value);
    StepVerifier.create(responseMono).expectNext(200).verifyComplete();
    mockRequest = MockServerHttpRequest.head("https://example.com/foo").build();
    ServerRequest headRequest = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
    responseMono = route.route(headRequest).flatMap(handlerFunction -> handlerFunction.handle(headRequest)).map(ServerResponse::statusCode).map(HttpStatus::value);
    StepVerifier.create(responseMono).expectNext(202).verifyComplete();
    mockRequest = MockServerHttpRequest.post("https://example.com/").contentType(MediaType.TEXT_PLAIN).build();
    ServerRequest barRequest = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
    responseMono = route.route(barRequest).flatMap(handlerFunction -> handlerFunction.handle(barRequest)).map(ServerResponse::statusCode).map(HttpStatus::value);
    StepVerifier.create(responseMono).expectNext(204).verifyComplete();
    mockRequest = MockServerHttpRequest.post("https://example.com/").build();
    ServerRequest invalidRequest = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
    responseMono = route.route(invalidRequest).flatMap(handlerFunction -> handlerFunction.handle(invalidRequest)).map(ServerResponse::statusCode).map(HttpStatus::value);
    StepVerifier.create(responseMono).verifyComplete();
}
Also used : StepVerifier(reactor.test.StepVerifier) MediaType(org.springframework.http.MediaType) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ClassPathResource(org.springframework.core.io.ClassPathResource) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) HEAD(org.springframework.web.reactive.function.server.RequestPredicates.HEAD) Test(org.junit.jupiter.api.Test) HttpStatus(org.springframework.http.HttpStatus) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Collections(java.util.Collections) Resource(org.springframework.core.io.Resource) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) HttpStatus(org.springframework.http.HttpStatus) Test(org.junit.jupiter.api.Test)

Aggregations

HttpStatus (org.springframework.http.HttpStatus)161 ResponseEntity (org.springframework.http.ResponseEntity)39 HttpHeaders (org.springframework.http.HttpHeaders)35 Test (org.junit.jupiter.api.Test)26 MediaType (org.springframework.http.MediaType)17 Mono (reactor.core.publisher.Mono)17 IOException (java.io.IOException)16 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)14 Collections (java.util.Collections)13 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)13 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)13 URI (java.net.URI)12 List (java.util.List)11 Optional (java.util.Optional)11 Test (org.junit.Test)11 Map (java.util.Map)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 ClassPathResource (org.springframework.core.io.ClassPathResource)9 Resource (org.springframework.core.io.Resource)9 HashMap (java.util.HashMap)8