Example usage for org.springframework.web.client HttpStatusCodeException getStatusCode

List of usage examples for org.springframework.web.client HttpStatusCodeException getStatusCode

Introduction

In this page you can find the example usage for org.springframework.web.client HttpStatusCodeException getStatusCode.

Prototype

public HttpStatus getStatusCode() 

Source Link

Document

Return the HTTP status code.

Usage

From source file:org.openlmis.fulfillment.service.DataRetrievalException.java

/**
 * Constructs the exception.//from   ww  w.  j  av  a  2  s  .  co m
 *
 * @param resource the resource that we were trying to retrieve
 * @param ex       exception with status code and response body from server
 */
DataRetrievalException(String resource, HttpStatusCodeException ex) {
    this(resource, ex.getStatusCode(), ex.getResponseBodyAsString());
}

From source file:com.xyxy.platform.examples.showcase.demos.hystrix.service.GetUserCommand.java

/**
 * ?HystrixBadRequestException?/*from  w w w . j a  v a2s. c om*/
 */
protected Exception handleException(HttpStatusCodeException e) {
    HttpStatus status = e.getStatusCode();
    if (status.equals(HttpStatus.BAD_REQUEST)) {
        throw new HystrixBadRequestException(e.getResponseBodyAsString(), e);
    }
    throw e;
}

From source file:fi.helsinki.opintoni.integration.unisport.UnisportRestClient.java

@Override
public Optional<UnisportUser> getUnisportUserByPrincipal(String username) {
    UnisportUser unisportUser = null;// w  ww. j  a  v  a 2 s .  co m
    try {
        unisportUser = restTemplate
                .exchange("{baseUrl}/api/v1/{locale}/ext/opintoni/authorization?eppn={userName}",
                        HttpMethod.GET, null, new ParameterizedTypeReference<UnisportUser>() {
                        }, baseUrl, new Locale("en"), username)
                .getBody();

    } catch (HttpStatusCodeException e) {
        if (!e.getStatusCode().equals(HttpStatus.NOT_FOUND)) {
            throw e;
        }
    }
    return Optional.ofNullable(unisportUser);
}

From source file:demo.service.RemoteVehicleDetailsService.java

@Override
public VehicleDetails getVehicleDetails(VehicleIdentificationNumber vin)
        throws VehicleIdentificationNumberNotFoundException {
    Assert.notNull(vin, "VIN must not be null");
    String url = this.properties.getRootUrl() + "vehicle/{vin}/details";
    logger.debug("Retrieving vehicle data for: " + vin + " from: " + url);
    try {/*  w w  w. j ava 2  s. c om*/
        return this.restTemplate.getForObject(url, VehicleDetails.class, vin);
    } catch (HttpStatusCodeException ex) {
        if (HttpStatus.NOT_FOUND.equals(ex.getStatusCode())) {
            throw new VehicleIdentificationNumberNotFoundException(vin, ex);
        }
        throw ex;
    }
}

From source file:org.openlmis.fulfillment.service.stockmanagement.StockEventStockManagementService.java

/**
 * Saves the given stock event to the stockmanagement service.
 *
 * @param stockEventDto  the physical inventory to be submitted
 *//*from  w  ww.  ja  v a 2 s .c  o m*/
@SuppressWarnings("PMD.PreserveStackTrace")
public void submit(StockEventDto stockEventDto) {
    String url = getServiceUrl() + getUrl();

    LOGGER.debug("Sending Stock Events to Stock Management: {}", stockEventDto);

    try {
        restTemplate.exchange(createUri(url), HttpMethod.POST, createEntity(stockEventDto), UUID.class);

    } catch (HttpStatusCodeException ex) {
        if (ex.getStatusCode() == HttpStatus.BAD_REQUEST) {
            try {
                LocalizedMessageDto localizedMessage = objectMapper.readValue(ex.getResponseBodyAsString(),
                        LocalizedMessageDto.class);

                throw new ExternalApiException(ex, localizedMessage);
            } catch (IOException ex2) {
                throw new ServerException(ex2, MessageKeys.ERROR_IO, ex2.getMessage());
            }
        } else {
            throw buildDataRetrievalException(ex);
        }
    }
}

From source file:com.allogy.amazonaws.elasticbeanstalk.worker.simulator.application.WorkerApplication.java

public HttpStatus forward(MessageWrapper messageWrapper) {
    Message message = messageWrapper.getMessage();
    HttpEntity<String> messageEntity = createHttpEntity(messageWrapper, message);

    logger.info("Forwarding message: messageId={}", messageWrapper.getMessage().getMessageId());

    try {/* w  w  w  .  j  a v  a 2 s . c  o m*/
        ResponseEntity<String> responseEntity = restTemplate.exchange(targetUrl, HttpMethod.POST, messageEntity,
                String.class);
        return responseEntity.getStatusCode();
    } catch (HttpStatusCodeException ex) {
        HttpStatus statusCode = ex.getStatusCode();
        logger.error("The HTTP Worker Application returned a non successful error code. statusCode={}",
                statusCode);
        return statusCode;
    } catch (RestClientException ex) {
        logger.error("Unable to connect to the HTTP Worker Application.");
        return HttpStatus.NOT_FOUND;
    }
}

From source file:at.ac.tuwien.dsg.cloud.utilities.gateway.registry.RestUtilities.java

public <T> ResponseEntity<T> simpleRestExchange(RequestEntity reg, Class<T> respType) {
    HttpStatus failedCode;/*  www  .jav a2s. c  om*/
    try {
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.exchange(reg, respType);
    } catch (HttpStatusCodeException e) {
        String serverResp = e.getResponseBodyAsString();
        failedCode = e.getStatusCode();
        logger.error(String.format(
                "Exception from server! " + "With status code %s! " + "Following body was responded %s",
                failedCode.toString(), serverResp), e);
    }

    //todo: think of throwing an exception instead of returning a null object
    return new ResponseEntity(null, failedCode);
}

From source file:org.syncope.core.rest.ConfigurationTestITCase.java

@Test
public void delete() throws UnsupportedEncodingException {

    try {/*from w w  w . ja  va 2  s . com*/
        restTemplate.delete(BASE_URL + "configuration/delete/{key}.json", "nonExistent");
    } catch (HttpStatusCodeException e) {
        assertEquals(HttpStatus.NOT_FOUND, e.getStatusCode());
    }

    ConfigurationTO tokenLengthTO = restTemplate.getForObject(BASE_URL + "configuration/read/{key}.json",
            ConfigurationTO.class, "token.length");

    restTemplate.delete(BASE_URL + "configuration/delete/{key}.json", "token.length");
    try {
        restTemplate.getForObject(BASE_URL + "configuration/read/{key}.json", ConfigurationTO.class,
                "token.length");
    } catch (HttpStatusCodeException e) {
        assertEquals(e.getStatusCode(), HttpStatus.NOT_FOUND);
    }

    ConfigurationTO newConfigurationTO = restTemplate.postForObject(BASE_URL + "configuration/create",
            tokenLengthTO, ConfigurationTO.class);
    assertEquals(tokenLengthTO, newConfigurationTO);
}

From source file:cn.aozhi.songify.functional.rest.TaskRestFT.java

@Test
public void invalidInput() {

    // create/*from  w ww  .  j a va  2  s  .c  o  m*/
    Task titleBlankTask = new Task();
    try {
        restTemplate.postForLocation(resourceUrl, titleBlankTask);
        fail("Create should fail while title is blank");
    } catch (HttpStatusCodeException e) {
        assertThat(e.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
        Map messages = jsonMapper.fromJson(e.getResponseBodyAsString(), Map.class);
        assertThat(messages).hasSize(1);
        assertThat(messages.get("title")).isIn("may not be empty", "?");
    }

    // update
    titleBlankTask.setId(1L);
    try {
        restTemplate.put(resourceUrl + "/1", titleBlankTask);
        fail("Update should fail while title is blank");
    } catch (HttpStatusCodeException e) {
        assertThat(e.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
        Map messages = jsonMapper.fromJson(e.getResponseBodyAsString(), Map.class);
        assertThat(messages).hasSize(1);
        assertThat(messages.get("title")).isIn("may not be empty", "?");
    }
}

From source file:cn.dsgrp.field.stock.functional.rest.TaskRestFT.java

@Test
public void invalidInput() {

    // create/* w w w  . j ava2 s  . c  o m*/
    Task titleBlankTask = new Task();
    try {
        restTemplate.postForLocation(resourceUrl, titleBlankTask);
        fail("Create should fail while title is blank");
    } catch (HttpStatusCodeException e) {
        assertThat(e.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
        Map messages = jsonMapper.fromJson(e.getResponseBodyAsString(), Map.class);
        assertThat(messages).hasSize(1);
        assertThat(messages.get("title")).isIn("may not be empty", "?");
    }

    // update
    titleBlankTask.setId(BigInteger.valueOf(1));
    try {
        restTemplate.put(resourceUrl + "/1", titleBlankTask);
        fail("Update should fail while title is blank");
    } catch (HttpStatusCodeException e) {
        assertThat(e.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
        Map messages = jsonMapper.fromJson(e.getResponseBodyAsString(), Map.class);
        assertThat(messages).hasSize(1);
        assertThat(messages.get("title")).isIn("may not be empty", "?");
    }
}