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

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

Introduction

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

Prototype

public String getResponseBodyAsString() 

Source Link

Document

Return the response body as a string.

Usage

From source file:org.trustedanalytics.servicecatalog.service.RestErrorHandler.java

@ExceptionHandler(HttpStatusCodeException.class)
public void handleHttpStatusCodeException(HttpStatusCodeException e, HttpServletResponse response)
        throws IOException {
    String message = extractErrorFromJSON(e.getResponseBodyAsString());
    message = StringUtils.isNotBlank(message) ? message : e.getMessage();
    ErrorLogger.logAndSendErrorResponse(LOGGER, response, e.getStatusCode(), message, e);
}

From source file:com.tikal.tallerWeb.data.access.rest.CustomerDAOImp.java

@Override
public void guardar(Customer dato) {
    try {/*from w w  w.  j  a v  a 2 s  .  com*/
        if (dato.getId() == null) {
            //                URI resource = factory.getTemplate().postForLocation(factory.getRootUlr() + "/customer", dato);
            //                String[] uri = StringUtils.split(resource.toString(), '/');
            //                String id = uri[uri.length - 1];
            //                CustomerPojo datoPjo = (CustomerPojo) dato;
            //                datoPjo.setId(Long.valueOf(id));
        } else {
            //                Map<String, Object> map = new HashMap<>();
            //                map.put("id", dato.getId());
            //                factory.getTemplate().postForLocation(factory.getRootUlr() + "/customer/{id}", dato, map);
        }
    } catch (HttpStatusCodeException ex) {
        try {
            BasicErrorMessage errorObj = objectMapper.readValue(ex.getResponseBodyAsString(),
                    BasicErrorMessage.class);
            CustomerDAOImp.LOGGER.error("error msg: " + Arrays.toString(errorObj.getMessage()));
        } catch (Exception exd) {
            CustomerDAOImp.LOGGER.error(exd);
        }
        throw ex;
    }
}

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

@Test
public void invalidInput() {

    // create//www  .ja  v a2 s. com
    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. ja  v  a 2s.c om*/
    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", "?");
    }
}

From source file:com.asiainfo.tfsPlatform.web.functional.rest.TaskRestFT.java

@Test
public void invalidInput() {

    // create//from  www .ja  v  a  2 s.c o  m
    TaskDto titleBlankTask = new TaskDto();
    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: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  w  w  .jav  a2  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.bill99.yn.webmgmt.functional.rest.TaskRestFT.java

@Test
public void invalidInput() {

    // create/*from w  w  w  .j a  v a 2 s. c o  m*/
    Task titleBlankTask = new Task();
    try {
        restTemplate.postForLocation(resoureUrl, 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(resoureUrl + "/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:$.TaskRestFT.java

@Test
    public void invalidInput() {

        // create
        Task titleBlankTask = new Task();
        try {//w ww .  java 2  s.  c om
            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:com.wolkabout.hexiwear.activity.SignUpActivity.java

@Background
void startSignUp() {
    final SignUpDto signUpDto = parseUserInput();
    try {/*from  w w w  .  ja v  a  2 s  .  c  om*/
        authenticationService.signUp(signUpDto);
        onSignUpSuccess();
    } catch (HttpStatusCodeException e) {
        if (e.getStatusCode() == HttpStatus.BAD_REQUEST
                && e.getResponseBodyAsString().contains("USER_EMAIL_IS_NOT_UNIQUE")) {
            onSignUpError(R.string.registration_error_email_already_exists);
            return;
        }

        onSignUpError(R.string.registration_failed);
    } catch (Exception e) {
        onSignUpError(R.string.registration_failed);
    } finally {
        dismissSigningUpLabel();
    }
}

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

@Test
public void shouldRetryObtainingAccessTokenIfResponseBodyIsEmpty() throws Exception {
    // given//from ww w.jav a  2  s  . c o m
    BaseCommunicationService<T> service = prepareService();
    HttpStatusCodeException exception = mock(HttpStatusCodeException.class);
    when(exception.getStatusCode()).thenReturn(HttpStatus.UNAUTHORIZED);
    when(exception.getResponseBodyAsString()).thenReturn("");

    // when
    when(restTemplate.exchange(any(URI.class), eq(HttpMethod.GET), any(HttpEntity.class),
            eq(service.getArrayResultClass()))).thenThrow(exception);

    expectedException.expect(DataRetrievalException.class);
    service.findAll("", RequestParameters.init());

    verify(authService, times(1)).clearTokenCache();
    verify(authService, times(2)).obtainAccessToken();
}