Search in sources :

Example 56 with HttpStatus

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

the class ClassPathChangeUploader method performUpload.

private void performUpload(ClassLoaderFiles classLoaderFiles, byte[] bytes) throws IOException {
    try {
        while (true) {
            try {
                ClientHttpRequest request = this.requestFactory.createRequest(this.uri, HttpMethod.POST);
                HttpHeaders headers = request.getHeaders();
                headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
                headers.setContentLength(bytes.length);
                FileCopyUtils.copy(bytes, request.getBody());
                ClientHttpResponse response = request.execute();
                HttpStatus statusCode = response.getStatusCode();
                Assert.state(statusCode == HttpStatus.OK, () -> "Unexpected " + statusCode + " response uploading class files");
                logUpload(classLoaderFiles);
                return;
            } catch (SocketException ex) {
                logger.warn(LogMessage.format("A failure occurred when uploading to %s. Upload will be retried in 2 seconds", this.uri));
                logger.debug("Upload failure", ex);
                Thread.sleep(2000);
            }
        }
    } catch (InterruptedException ex) {
        Thread.currentThread().interrupt();
        throw new IllegalStateException(ex);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) SocketException(java.net.SocketException) HttpStatus(org.springframework.http.HttpStatus) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse)

Example 57 with HttpStatus

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

the class BasicErrorController method errorHtml.

@RequestMapping(produces = MediaType.TEXT_HTML_VALUE)
public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
    HttpStatus status = getStatus(request);
    Map<String, Object> model = Collections.unmodifiableMap(getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.TEXT_HTML)));
    response.setStatus(status.value());
    ModelAndView modelAndView = resolveErrorView(request, response, status, model);
    return (modelAndView != null) ? modelAndView : new ModelAndView("error", model);
}
Also used : HttpStatus(org.springframework.http.HttpStatus) ModelAndView(org.springframework.web.servlet.ModelAndView) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 58 with HttpStatus

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

the class BasicErrorController method error.

@RequestMapping
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
    HttpStatus status = getStatus(request);
    if (status == HttpStatus.NO_CONTENT) {
        return new ResponseEntity<>(status);
    }
    Map<String, Object> body = getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.ALL));
    return new ResponseEntity<>(body, status);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) HttpStatus(org.springframework.http.HttpStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 59 with HttpStatus

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

the class DefaultErrorAttributes method getErrorAttributes.

private Map<String, Object> getErrorAttributes(ServerRequest request, boolean includeStackTrace) {
    Map<String, Object> errorAttributes = new LinkedHashMap<>();
    errorAttributes.put("timestamp", new Date());
    errorAttributes.put("path", request.path());
    Throwable error = getError(request);
    MergedAnnotation<ResponseStatus> responseStatusAnnotation = MergedAnnotations.from(error.getClass(), SearchStrategy.TYPE_HIERARCHY).get(ResponseStatus.class);
    HttpStatus errorStatus = determineHttpStatus(error, responseStatusAnnotation);
    errorAttributes.put("status", errorStatus.value());
    errorAttributes.put("error", errorStatus.getReasonPhrase());
    errorAttributes.put("message", determineMessage(error, responseStatusAnnotation));
    errorAttributes.put("requestId", request.exchange().getRequest().getId());
    handleException(errorAttributes, determineException(error), includeStackTrace);
    return errorAttributes;
}
Also used : ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) HttpStatus(org.springframework.http.HttpStatus) Date(java.util.Date) LinkedHashMap(java.util.LinkedHashMap)

Example 60 with HttpStatus

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

the class ClickatellHttpGateway method send.

@Override
public OutboundMessageResponse send(String subject, String text, Set<String> recipients, SmsGatewayConfig config) {
    ClickatellGatewayConfig clickatellConfiguration = (ClickatellGatewayConfig) config;
    HttpEntity<ClickatellRequestEntity> request = new HttpEntity<>(getRequestBody(text, recipients), getRequestHeaderParameters(clickatellConfiguration));
    HttpStatus httpStatus = send(clickatellConfiguration.getUrlTemplate() + MAX_MESSAGE_PART, request, HttpMethod.POST, ClickatellResponseEntity.class);
    return wrapHttpStatus(httpStatus);
}
Also used : ClickatellRequestEntity(org.hisp.dhis.sms.outbound.ClickatellRequestEntity) HttpEntity(org.springframework.http.HttpEntity) HttpStatus(org.springframework.http.HttpStatus)

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