Search in sources :

Example 71 with HttpStatus

use of org.springframework.http.HttpStatus in project metacat by Netflix.

the class MetacatErrorController method error.

/**
 * Mapping for error handling.
 * @param request http request
 * @return error response
 */
@RequestMapping
@ResponseBody
public ResponseEntity<Map<String, Object>> error(final HttpServletRequest request) {
    final Map<String, Object> body = getErrorAttributes(request, getErrorAttributeOptions(request));
    final HttpStatus status = getStatus(request);
    return new ResponseEntity<>(body, status);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) HttpStatus(org.springframework.http.HttpStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 72 with HttpStatus

use of org.springframework.http.HttpStatus in project open-kilda by telstra.

the class HealthCheckController method getHealthCheck.

/**
 * Gets the health-check status.
 *
 * @return health-check model entity
 */
@ApiOperation(value = "Gets health-check status", response = HealthCheck.class)
@GetMapping(value = "/health-check")
public ResponseEntity<HealthCheck> getHealthCheck() {
    logger.debug("getHealthCheck");
    HealthCheck healthCheck = healthCheckService.getHealthCheck();
    HttpStatus status = healthCheck.hasNonOperational() ? HttpStatus.SERVICE_UNAVAILABLE : HttpStatus.OK;
    return new ResponseEntity<>(healthCheck, new HttpHeaders(), status);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) HttpStatus(org.springframework.http.HttpStatus) HealthCheck(org.openkilda.messaging.model.HealthCheck) GetMapping(org.springframework.web.bind.annotation.GetMapping) ApiOperation(io.swagger.annotations.ApiOperation)

Example 73 with HttpStatus

use of org.springframework.http.HttpStatus in project open-kilda by telstra.

the class NorthboundExceptionHandler method handleMessageException.

/**
 * Handles NorthboundException exception.
 *
 * @param exception the NorthboundException instance
 * @param request   the WebRequest caused exception
 * @return the ResponseEntity object instance
 */
@ExceptionHandler(MessageException.class)
protected ResponseEntity<Object> handleMessageException(MessageException exception, WebRequest request) {
    HttpStatus status;
    switch(exception.getErrorType()) {
        case NOT_FOUND:
            status = HttpStatus.NOT_FOUND;
            break;
        case DATA_INVALID:
            status = HttpStatus.BAD_REQUEST;
            break;
        case PARAMETERS_INVALID:
            status = HttpStatus.BAD_REQUEST;
            break;
        case REQUEST_INVALID:
            status = HttpStatus.BAD_REQUEST;
            break;
        case OPERATION_TIMED_OUT:
            status = HttpStatus.REQUEST_TIMEOUT;
            break;
        case ALREADY_EXISTS:
            status = HttpStatus.CONFLICT;
            break;
        case AUTH_FAILED:
            status = HttpStatus.UNAUTHORIZED;
            break;
        case UNPROCESSABLE_REQUEST:
            status = HttpStatus.UNPROCESSABLE_ENTITY;
            break;
        case INTERNAL_ERROR:
            status = HttpStatus.INTERNAL_SERVER_ERROR;
            break;
        case NOT_PERMITTED:
            status = HttpStatus.FORBIDDEN;
            break;
        case NOT_IMPLEMENTED:
            status = HttpStatus.NOT_IMPLEMENTED;
            break;
        default:
            status = HttpStatus.INTERNAL_SERVER_ERROR;
            break;
    }
    MessageError error = new MessageError(exception.getCorrelationId(), exception.getTimestamp(), exception.getErrorType().toString(), exception.getMessage(), exception.getErrorDescription());
    try (MDCCloseable mdcCloseable = MDC.putCloseable(CORRELATION_ID, exception.getCorrelationId())) {
        logger.warn(format("Error %s caught.", error), exception);
    }
    return super.handleExceptionInternal(exception, error, new HttpHeaders(), status, request);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpStatus(org.springframework.http.HttpStatus) MessageError(org.openkilda.messaging.error.MessageError) MDCCloseable(org.slf4j.MDC.MDCCloseable) ResponseEntityExceptionHandler(org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 74 with HttpStatus

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

the class AuthorizationCodeGrantTests method testTokenAcquisitionWithCorrectContext.

@Test
public void testTokenAcquisitionWithCorrectContext() throws Exception {
    ResponseEntity<String> page = serverRunning.getForString("/tonr2/login.jsp");
    String cookie = page.getHeaders().getFirst("Set-Cookie");
    HttpHeaders headers = new HttpHeaders();
    headers.set("Cookie", cookie);
    Matcher matcher = Pattern.compile("(?s).*name=\"_csrf\".*?value=\"([^\"]+).*").matcher(page.getBody());
    MultiValueMap<String, String> form;
    form = new LinkedMultiValueMap<String, String>();
    form.add("username", "marissa");
    form.add("password", "wombat");
    if (matcher.matches()) {
        form.add("_csrf", matcher.group(1));
    }
    ResponseEntity<Void> response = serverRunning.postForStatus("/tonr2/login", headers, form);
    cookie = response.getHeaders().getFirst("Set-Cookie");
    headers = new HttpHeaders();
    headers.set("Cookie", cookie);
    // headers.setAccept(Collections.singletonList(MediaType.ALL));
    headers.setAccept(MediaType.parseMediaTypes("image/png,image/*;q=0.8,*/*;q=0.5"));
    String location = serverRunning.getForRedirect("/tonr2/sparklr/photos/1", headers);
    location = authenticateAndApprove(location);
    assertTrue("Redirect location should be to the original photo URL: " + location, location.contains("photos/1"));
    HttpStatus status = serverRunning.getStatusCode(location, headers);
    assertEquals(HttpStatus.OK, status);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) Matcher(java.util.regex.Matcher) HttpStatus(org.springframework.http.HttpStatus) Test(org.junit.Test)

Example 75 with HttpStatus

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

the class OAuth2RestTemplateTests method open.

@Before
public void open() throws Exception {
    resource = new BaseOAuth2ProtectedResourceDetails();
    // Facebook and older specs:
    resource.setTokenName("bearer_token");
    restTemplate = new OAuth2RestTemplate(resource);
    restTemplate.setAccessTokenProvider(accessTokenProvider);
    request = Mockito.mock(ClientHttpRequest.class);
    headers = new HttpHeaders();
    Mockito.when(request.getHeaders()).thenReturn(headers);
    ClientHttpResponse response = Mockito.mock(ClientHttpResponse.class);
    HttpStatus statusCode = HttpStatus.OK;
    Mockito.when(response.getStatusCode()).thenReturn(statusCode);
    Mockito.when(request.execute()).thenReturn(response);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpStatus(org.springframework.http.HttpStatus) BaseOAuth2ProtectedResourceDetails(org.springframework.security.oauth2.client.resource.BaseOAuth2ProtectedResourceDetails) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) Before(org.junit.Before)

Aggregations

HttpStatus (org.springframework.http.HttpStatus)165 ResponseEntity (org.springframework.http.ResponseEntity)43 HttpHeaders (org.springframework.http.HttpHeaders)38 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)15 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