Search in sources :

Example 16 with HttpStatus

use of org.springframework.http.HttpStatus in project judge by zjnu-acm.

the class GlobalExceptionHandler method messageExceptionHandler.

@ExceptionHandler(MessageException.class)
public ModelAndView messageExceptionHandler(MessageException ex, Locale locale) {
    String message = ex.getMessage();
    HttpStatus status = ex.getHttpStatus();
    message = messageSource.getMessage(message, null, message, locale);
    return new ModelAndView("message", Collections.singletonMap("message", message), status);
}
Also used : HttpStatus(org.springframework.http.HttpStatus) ModelAndView(org.springframework.web.servlet.ModelAndView) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 17 with HttpStatus

use of org.springframework.http.HttpStatus in project judge by zjnu-acm.

the class GlobalExceptionHandler method handler.

@ExceptionHandler(BusinessException.class)
public ModelAndView handler(BusinessException businessException, Locale locale) {
    BusinessCode code = businessException.getCode();
    HttpStatus status = code.getStatus();
    String message = code.getMessage();
    message = messageSource.getMessage(message, businessException.getParams(), message, locale);
    return new ModelAndView("message", Collections.singletonMap("message", message), status);
}
Also used : HttpStatus(org.springframework.http.HttpStatus) ModelAndView(org.springframework.web.servlet.ModelAndView) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 18 with HttpStatus

use of org.springframework.http.HttpStatus in project data-prep by Talend.

the class CommandHelper method toStreaming.

public static ResponseEntity<StreamingResponseBody> toStreaming(final GenericCommand<InputStream> command) {
    final Observable<InputStream> stream = command.toObservable();
    return stream.map(is -> {
        // Content for the response entity
        final StreamingResponseBody body = outputStream -> {
            try {
                IOUtils.copyLarge(is, outputStream);
                outputStream.flush();
            } catch (IOException e) {
                try {
                    is.close();
                } catch (IOException closingException) {
                    LOGGER.warn("could not close command result, a http connection may be leaked !", closingException);
                }
                LOGGER.error("Unable to fully copy command result '{}'.", command.getClass(), e);
            }
        };
        // copy all headers from the command response so that the mime-type is correctly forwarded. Command has
        // the correct headers due to call to toBlocking() below.
        final MultiValueMap<String, String> headers = new HttpHeaders();
        final HttpStatus status = command.getStatus();
        for (Header header : command.getCommandResponseHeaders()) {
            headers.put(header.getName(), Collections.singletonList(header.getValue()));
        }
        return new ResponseEntity<>(body, headers, status == null ? HttpStatus.OK : status);
    }).toBlocking().first();
}
Also used : TDPException(org.talend.dataprep.exception.TDPException) Logger(org.slf4j.Logger) HttpHeaders(org.springframework.http.HttpHeaders) Publisher(org.reactivestreams.Publisher) FluxSink(reactor.core.publisher.FluxSink) LoggerFactory(org.slf4j.LoggerFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) MultiValueMap(org.springframework.util.MultiValueMap) StreamingResponseBody(org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody) IOException(java.io.IOException) Header(org.apache.http.Header) Observable(rx.Observable) IOUtils(org.apache.commons.io.IOUtils) HttpStatus(org.springframework.http.HttpStatus) Flux(reactor.core.publisher.Flux) HystrixCommand(com.netflix.hystrix.HystrixCommand) Stream(java.util.stream.Stream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CommonErrorCodes(org.talend.dataprep.exception.error.CommonErrorCodes) ResponseEntity(org.springframework.http.ResponseEntity) Collections(java.util.Collections) InputStream(java.io.InputStream) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) StreamingResponseBody(org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody) Header(org.apache.http.Header) HttpStatus(org.springframework.http.HttpStatus) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 19 with HttpStatus

use of org.springframework.http.HttpStatus in project spring-cloud-config by spring-cloud.

the class VaultEnvironmentRepository method read.

String read(String key) {
    String url = String.format("%s://%s:%s/v1/{backend}/{key}", this.scheme, this.host, this.port);
    HttpHeaders headers = new HttpHeaders();
    String token = request.getHeader(TOKEN_HEADER);
    if (!StringUtils.hasLength(token)) {
        throw new IllegalArgumentException("Missing required header: " + TOKEN_HEADER);
    }
    headers.add(VAULT_TOKEN, token);
    try {
        ResponseEntity<VaultResponse> response = this.rest.exchange(url, HttpMethod.GET, new HttpEntity<>(headers), VaultResponse.class, this.backend, key);
        HttpStatus status = response.getStatusCode();
        if (status == HttpStatus.OK) {
            return response.getBody().getData();
        }
    } catch (HttpStatusCodeException e) {
        if (e.getStatusCode() == HttpStatus.NOT_FOUND) {
            return null;
        }
        throw e;
    }
    return null;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpStatus(org.springframework.http.HttpStatus) HttpStatusCodeException(org.springframework.web.client.HttpStatusCodeException)

Example 20 with HttpStatus

use of org.springframework.http.HttpStatus in project metalnx-web by irods-contrib.

the class UploadController method getUploadResponse.

/**
 * Sets the HTTP response text and status for an upload request.
 *
 * @param uploadMessage
 *            string that identifies what kind of uploadMessage happened during
 *            an upload
 * @return status OK and response text "Upload Successful" if the upload has no
 *         errors. Otherwise, it returns an HTTP status 500 and response text
 *         with the corresponding uploadMessage.
 */
private ResponseEntity<?> getUploadResponse(final String uploadMessage, final String errorType) {
    logger.info("getUploadResponse()");
    logger.info("uploadMessage:{}", uploadMessage);
    logger.info("errorType:{}", errorType);
    HttpStatus status = HttpStatus.OK;
    String path = cc.getCurrentPath();
    JSONObject jsonUploadMsg = new JSONObject();
    try {
        jsonUploadMsg.put("path", path);
        jsonUploadMsg.put("msg", uploadMessage);
        if (!errorType.isEmpty()) {
            jsonUploadMsg.put("errorType", errorType);
            status = HttpStatus.INTERNAL_SERVER_ERROR;
            logger.warn("passing along internal server error:{}", errorType);
        }
    } catch (JSONException e) {
        logger.error("Could not create JSON object for upload response: {]", e.getMessage());
    }
    return new ResponseEntity<>(jsonUploadMsg.toString(), status);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) JSONObject(org.codehaus.jettison.json.JSONObject) HttpStatus(org.springframework.http.HttpStatus) JSONException(org.codehaus.jettison.json.JSONException)

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