Search in sources :

Example 11 with HttpStatus

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

the class HealthCheckController method getHealthCheck.

/**
 * Gets the health-check status.
 *
 * @param correlationId request correlation id
 * @return health-check model entity
 */
@ApiOperation(value = "Gets health-check status", response = HealthCheck.class)
@ApiResponses(value = { @ApiResponse(code = 200, response = HealthCheck.class, message = "Operation is successful"), @ApiResponse(code = 401, response = MessageError.class, message = "Unauthorized"), @ApiResponse(code = 403, response = MessageError.class, message = "Forbidden"), @ApiResponse(code = 404, response = MessageError.class, message = "Not found"), @ApiResponse(code = 503, response = MessageError.class, message = "Service unavailable") })
@RequestMapping(value = "/health-check", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<HealthCheck> getHealthCheck(@RequestHeader(value = CORRELATION_ID, defaultValue = DEFAULT_CORRELATION_ID) String correlationId) {
    logger.debug("getHealthCheck");
    HealthCheck healthCheck = healthCheckService.getHealthCheck(correlationId);
    HttpStatus status = healthCheck.hasNonOperational() ? HttpStatus.GATEWAY_TIMEOUT : 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) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 12 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:
        case PARAMETERS_INVALID:
            status = HttpStatus.BAD_REQUEST;
            break;
        case ALREADY_EXISTS:
            status = HttpStatus.CONFLICT;
            break;
        case AUTH_FAILED:
            status = HttpStatus.UNAUTHORIZED;
            break;
        case OPERATION_TIMED_OUT:
        case INTERNAL_ERROR:
        default:
            status = HttpStatus.INTERNAL_SERVER_ERROR;
            break;
    }
    MessageError error = new MessageError(request.getHeader(CORRELATION_ID), exception.getTimestamp(), exception.getErrorType().toString(), exception.getMessage(), exception.getErrorDescription());
    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) ResponseEntityExceptionHandler(org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 13 with HttpStatus

use of org.springframework.http.HttpStatus in project dhis2-core by dhis2.

the class SmsGateway method send.

public HttpStatus send(String urlTemplate, HttpEntity<?> request, Class<?> klass) {
    ResponseEntity<?> response;
    HttpStatus statusCode = null;
    try {
        response = restTemplate.exchange(urlTemplate, HttpMethod.POST, request, klass);
        statusCode = response.getStatusCode();
    } catch (HttpClientErrorException ex) {
        log.error("Client error", ex);
        statusCode = ex.getStatusCode();
    } catch (HttpServerErrorException ex) {
        log.error("Server error", ex);
        statusCode = ex.getStatusCode();
    } catch (Exception ex) {
        log.error("Error", ex);
        statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
    }
    log.info("Response status code: " + statusCode);
    return statusCode;
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) HttpStatus(org.springframework.http.HttpStatus) HttpServerErrorException(org.springframework.web.client.HttpServerErrorException) HttpServerErrorException(org.springframework.web.client.HttpServerErrorException) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException)

Example 14 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, isIncludeStackTrace(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 15 with HttpStatus

use of org.springframework.http.HttpStatus in project ORCID-Source by ORCID.

the class OauthGenericCallsController method obtainOauth2TokenPost.

@RequestMapping(value = "/oauth/token", consumes = MediaType.APPLICATION_FORM_URLENCODED, produces = MediaType.APPLICATION_JSON)
public ResponseEntity<?> obtainOauth2TokenPost(HttpServletRequest request) {
    String authorization = request.getHeader("Authorization");
    Enumeration<String> paramNames = request.getParameterNames();
    MultivaluedMap<String, String> formParams = new MultivaluedMapImpl();
    while (paramNames.hasMoreElements()) {
        String paramName = paramNames.nextElement();
        formParams.add(paramName, request.getParameter(paramName));
    }
    try {
        Response response = orcidClientCredentialEndPointDelegator.obtainOauth2Token(authorization, formParams);
        return ResponseEntity.status(response.getStatus()).body(response.getEntity());
    } catch (Exception e) {
        OAuthError error = OAuthErrorUtils.getOAuthError(e);
        HttpStatus status = HttpStatus.valueOf(error.getResponseStatus().getStatusCode());
        return ResponseEntity.status(status).body(error);
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(javax.ws.rs.core.Response) OAuthError(org.orcid.core.oauth.OAuthError) HttpStatus(org.springframework.http.HttpStatus) MultivaluedMapImpl(com.sun.jersey.core.util.MultivaluedMapImpl) InvalidRequestException(org.springframework.security.oauth2.common.exceptions.InvalidRequestException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

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