Search in sources :

Example 81 with HttpStatus

use of org.springframework.http.HttpStatus in project logback-access-spring-boot-starter by akihyro.

the class ResponseEntityAssert method hasStatusCode.

/**
     * Verifies that the status code is equal to the given one.
     *
     * @param status the status.
     * @return this instance.
     * @see ResponseEntity#getStatusCode()
     */
public S hasStatusCode(HttpStatus status) {
    HttpStatus actualStatusCode = actual.getStatusCode();
    Assertions.assertThat(actualStatusCode).isEqualTo(status);
    return myself;
}
Also used : HttpStatus(org.springframework.http.HttpStatus)

Example 82 with HttpStatus

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

the class WebMessageService method send.

public void send(WebMessage webMessage, HttpServletResponse response, HttpServletRequest request) {
    String type = request.getHeader("Accept");
    type = !StringUtils.isEmpty(type) ? type : request.getContentType();
    type = !StringUtils.isEmpty(type) ? type : MediaType.APPLICATION_JSON_VALUE;
    HttpStatus httpStatus = HttpStatus.valueOf(webMessage.getHttpStatusCode());
    if (httpStatus.is4xxClientError() || httpStatus.is5xxServerError()) {
        response.setHeader("Cache-Control", CacheControl.noCache().cachePrivate().getHeaderValue());
    }
    // allow type to be overridden by path extension
    if (request.getPathInfo().endsWith(".json")) {
        type = MediaType.APPLICATION_JSON_VALUE;
    } else if (request.getPathInfo().endsWith(".xml")) {
        type = MediaType.APPLICATION_XML_VALUE;
    }
    if (isCompatibleWith(type, MediaType.APPLICATION_JSON)) {
        sendJson(webMessage, response);
    } else if (isCompatibleWith(type, MediaType.APPLICATION_XML)) {
        sendXml(webMessage, response);
    } else {
        // default to json
        sendJson(webMessage, response);
    }
}
Also used : HttpStatus(org.springframework.http.HttpStatus)

Example 83 with HttpStatus

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

the class ClickatellGateway 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, ClickatellResponseEntity.class);
    return wrapHttpStatus(httpStatus);
}
Also used : ClickatellRequestEntity(org.hisp.dhis.sms.outbound.ClickatellRequestEntity) HttpEntity(org.springframework.http.HttpEntity) HttpStatus(org.springframework.http.HttpStatus)

Example 84 with HttpStatus

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

the class DefaultSynchronizationManager method isRemoteServerAvailable.

// -------------------------------------------------------------------------
// SynchronizatonManager implementation
// -------------------------------------------------------------------------
@Override
public AvailabilityStatus isRemoteServerAvailable() {
    Configuration config = configurationService.getConfiguration();
    if (!isRemoteServerConfigured(config)) {
        return new AvailabilityStatus(false, "Remote server is not configured", HttpStatus.BAD_GATEWAY);
    }
    String url = systemSettingManager.getSystemSetting(SettingKey.REMOTE_INSTANCE_URL) + PING_PATH;
    String username = (String) systemSettingManager.getSystemSetting(SettingKey.REMOTE_INSTANCE_USERNAME);
    String password = (String) systemSettingManager.getSystemSetting(SettingKey.REMOTE_INSTANCE_PASSWORD);
    log.debug(String.format("Remote server ping URL: %s, username: %s", url, username));
    HttpEntity<String> request = getBasicAuthRequestEntity(username, password);
    ResponseEntity<String> response = null;
    HttpStatus sc = null;
    String st = null;
    AvailabilityStatus status = null;
    try {
        response = restTemplate.exchange(url, HttpMethod.GET, request, String.class);
        sc = response.getStatusCode();
    } catch (HttpClientErrorException ex) {
        sc = ex.getStatusCode();
        st = ex.getStatusText();
    } catch (HttpServerErrorException ex) {
        sc = ex.getStatusCode();
        st = ex.getStatusText();
    } catch (ResourceAccessException ex) {
        return new AvailabilityStatus(false, "Network is unreachable", HttpStatus.BAD_GATEWAY);
    }
    log.debug("Response status code: " + sc);
    if (HttpStatus.FOUND.equals(sc)) {
        status = new AvailabilityStatus(false, "No authentication was provided", sc);
    } else if (HttpStatus.UNAUTHORIZED.equals(sc)) {
        status = new AvailabilityStatus(false, "Authentication failed", sc);
    } else if (HttpStatus.INTERNAL_SERVER_ERROR.equals(sc)) {
        status = new AvailabilityStatus(false, "Remote server experienced an internal error", sc);
    } else if (HttpStatus.OK.equals(sc)) {
        status = new AvailabilityStatus(true, "Authentication was successful", sc);
    } else {
        status = new AvailabilityStatus(false, "Server is not available: " + st, sc);
    }
    log.info("Status: " + status);
    return status;
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) Configuration(org.hisp.dhis.configuration.Configuration) HttpStatus(org.springframework.http.HttpStatus) HttpServerErrorException(org.springframework.web.client.HttpServerErrorException) ResourceAccessException(org.springframework.web.client.ResourceAccessException)

Example 85 with HttpStatus

use of org.springframework.http.HttpStatus in project cuba by cuba-platform.

the class ServicesController method invokeServiceMethodPost.

@PostMapping("/{serviceName}/{methodName}")
public ResponseEntity<String> invokeServiceMethodPost(@PathVariable String serviceName, @PathVariable String methodName, @RequestParam(required = false) String modelVersion, @RequestBody(required = false) String paramsJson) {
    ServicesControllerManager.ServiceCallResult result = servicesControllerManager.invokeServiceMethodPost(serviceName, methodName, paramsJson, modelVersion);
    HttpStatus status;
    if (result == null) {
        status = HttpStatus.NO_CONTENT;
        result = new ServicesControllerManager.ServiceCallResult("", false);
    } else {
        status = HttpStatus.OK;
    }
    String contentType = result.isValidJson() ? "application/json;charset=UTF-8" : "text/plain;charset=UTF-8";
    return ResponseEntity.status(status).header("Content-Type", contentType).body(result.getStringValue());
}
Also used : HttpStatus(org.springframework.http.HttpStatus) ServicesControllerManager(com.haulmont.restapi.service.ServicesControllerManager)

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