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

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

Introduction

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

Prototype

public HttpStatus getStatusCode() 

Source Link

Document

Return the HTTP status code.

Usage

From source file:org.terasoluna.gfw.functionaltest.app.exceptionhandling.ExceptionHandlingTest.java

@Test
public void test03_01_servletFrameworkHandling() {

    driver.findElement(By.id("servletFrameworkHandling_03_01")).click();

    // TODO Assert Output Log

    // Error Code assert
    assertThat(driver.findElement(By.id("exceptionCode")).getText(), is("i.xx.xxx"));

    // screen capture
    screenCapture.save(driver);/*  w ww .ja va2s.c om*/

    try {
        restTemplate.getForEntity(applicationContextUrl + "/exceptionhandling/3_1", String.class);
    } catch (HttpServerErrorException e) {
        // Response Header Error Code assert
        assertThat(e.getResponseHeaders().get("X-Exception-Code").get(0).toString(), is("i.xx.xxx"));
        // Response Code assert
        assertThat(e.getStatusCode().toString(), is("500"));
    }
}

From source file:org.terasoluna.gfw.functionaltest.app.exceptionhandling.ExceptionHandlingTest.java

@Test
public void test03_02_servletFrameworkHandling() {

    driver.findElement(By.id("servletFrameworkHandling_03_02")).click();

    // TODO Assert Output Log

    // Error Code assert
    assertThat(driver.findElement(By.id("exceptionCode")).getText(), is("w.xx.xxx"));

    // screen capture
    screenCapture.save(driver);//from  w ww  . j  a  v  a 2 s .c  om

    try {
        restTemplate.getForEntity(applicationContextUrl + "/exceptionhandling/3_2", String.class);
    } catch (HttpServerErrorException e) {
        // Response Header Error Code assert
        assertThat(e.getResponseHeaders().get("X-Exception-Code").get(0).toString(), is("w.xx.xxx"));
        // Response Code assert
        assertThat(e.getStatusCode().toString(), is("500"));
    }
}

From source file:org.terasoluna.gfw.functionaltest.app.exceptionhandling.ExceptionHandlingTest.java

@Test
public void test03_03_servletFrameworkHandling() {

    driver.findElement(By.id("servletFrameworkHandling_03_03")).click();

    // TODO Assert Output Log

    // Error Code assert
    assertThat(driver.findElement(By.id("exceptionCode")).getText(), is("e.xx.xxx"));

    // screen capture
    screenCapture.save(driver);/*w  ww.j a va 2s .  com*/

    try {
        restTemplate.getForEntity(applicationContextUrl + "/exceptionhandling/3_3", String.class);
    } catch (HttpServerErrorException e) {
        // Response Header Error Code assert
        assertThat(e.getResponseHeaders().get("X-Exception-Code").get(0).toString(), is("e.xx.xxx"));
        // Response Code assert
        assertThat(e.getStatusCode().toString(), is("500"));
    }
}

From source file:org.terasoluna.gfw.functionaltest.app.exceptionhandling.ExceptionHandlingTest.java

@Test
public void test03_06_servletFrameworkHandling() {

    driver.findElement(By.id("servletFrameworkHandling_03_06")).click();

    // TODO Assert Output Log

    // Error Code assert
    assertThat(driver.findElement(By.id("exceptionCode")).getText(), is("e.xx.9999"));

    // screen capture
    screenCapture.save(driver);//from   ww  w. j av a 2 s  . c om

    try {
        restTemplate.getForEntity(
                applicationContextUrl + "/exceptionHandlingIgnoreResultMessages/exceptionhandling/3_6",
                String.class);
    } catch (HttpServerErrorException e) {
        // Response Header Error Code assert
        assertThat(e.getResponseHeaders().get("X-Exception-Code").get(0).toString(), is("e.xx.9999"));
        // Response Code assert
        assertThat(e.getStatusCode().toString(), is("500"));
    }
}

From source file:org.terasoluna.gfw.functionaltest.app.exceptionhandling.ExceptionHandlingTest.java

@Test
public void test03_05_servletFrameworkHandling() {

    driver.findElement(By.id("servletFrameworkHandling_03_05")).click();

    // TODO Assert Output Log

    // Error Code assert
    assertThat(driver.findElement(By.id("exceptionCode")).getText(), is("e.xx.9999"));
    // Response Code assert
    assertThat(driver.findElement(By.xpath("//div[@id='messagesPanel']/div/ul/li")).getText(), is("w.yy.yyy"));

    // screen capture
    screenCapture.save(driver);//from   w  ww  . j a  v a 2s.  c  o m

    try {
        restTemplate.getForEntity(applicationContextUrl + "/exceptionhandling/3_5", String.class);
    } catch (HttpServerErrorException e) {
        // Response Header Error Code assert
        assertThat(e.getResponseHeaders().get("X-Exception-Code").get(0).toString(), is("e.xx.9999"));
        // Response Code assert
        assertThat(e.getStatusCode().toString(), is("500"));
    }

}

From source file:org.alfresco.integrations.google.docs.service.GoogleDocsServiceImpl.java

/**
 * Get a connection to the Google APIs. Will attempt to refresh tokens if they are invalid. If unable to refresh return a
 * GoogleDocsRefreshTokenException./* ww w .ja v  a2 s .  c  om*/
 * 
 * @return
 * @throws GoogleDocsAuthenticationException
 * @throws GoogleDocsRefreshTokenException
 * @throws GoogleDocsServiceException
 */
private Connection<Google> getConnection()
        throws GoogleDocsAuthenticationException, GoogleDocsRefreshTokenException, GoogleDocsServiceException {
    Connection<Google> connection = null;

    // OAuth credentials for the current user, if the exist
    OAuth2CredentialsInfo credentialInfo = oauth2CredentialsStoreService
            .getPersonalOAuth2Credentials(GoogleDocsConstants.REMOTE_SYSTEM);

    if (credentialInfo != null) {
        log.debug("OAuth Access Token Exists: " + credentialInfo.getOAuthAccessToken());
        AccessGrant accessGrant = new AccessGrant(credentialInfo.getOAuthAccessToken());

        try {
            log.debug("Attempt to create OAuth Connection");
            connection = connectionFactory.createConnection(accessGrant);
        } catch (HttpClientErrorException hcee) {
            log.debug(hcee.getResponseBodyAsString());
            if (hcee.getStatusCode().value() == HttpStatus.SC_UNAUTHORIZED) {
                try {
                    accessGrant = refreshAccessToken();
                    connection = connectionFactory.createConnection(accessGrant);
                } catch (GoogleDocsRefreshTokenException gdrte) {
                    throw gdrte;
                } catch (GoogleDocsServiceException gdse) {
                    throw gdse;
                }
            } else {
                throw new GoogleDocsServiceException(hcee.getMessage(), hcee, hcee.getStatusCode().value());
            }
        } catch (HttpServerErrorException hsee) {
            throw new GoogleDocsServiceException(hsee.getMessage(), hsee, hsee.getStatusCode().value());
        }
    }

    log.debug("Connection Created");
    return connection;
}

From source file:org.apache.fineract.restwebservice.PlatformRestClient.java

/**
  * Executes a HTTP request using the spring framework RestTemplate
  * //ww  w.  j a v a2s.  c  o m
  * @param url the URL
  * @param method the HTTP method (GET, POST, etc)
  * @param requestEntity the entity (headers and/or body) to write to the request, may be null
  * @param responseType the type of the return value
  * @return the response as entity
  * @throws InterruptedException
  * @throws RestClientException
  */
public <T> ResponseEntity<T> executeHttpRequest(final URI url, final HttpMethod method,
        final HttpEntity<?> requestEntity, final Class<T> responseType) {
    final RestTemplate restTemplate = new RestTemplate();

    HttpStatus statusCode = null;
    ResponseEntity<T> responseEntity = null;

    // increment the number of request attempts by 1
    this.numberOfHttpRequestAttempts++;

    try {
        // execute the HTTP request
        responseEntity = restTemplate.exchange(url, method, requestEntity, responseType);
        statusCode = responseEntity.getStatusCode();

        // catch all server HTTP error exceptions
    } catch (HttpServerErrorException exception) {
        statusCode = exception.getStatusCode();

        // if HTTP status is 503 or 504, sleep for 5 seconds and retry
        if ((statusCode.equals(HttpStatus.SERVICE_UNAVAILABLE) || statusCode.equals(HttpStatus.GATEWAY_TIMEOUT))
                && (this.numberOfHttpRequestAttempts < this.numberOfHttpRequestRetries)) {

            logger.info("Server returned an error response with status: " + statusCode
                    + ", retrying again in 5 seconds");
            logger.info("Number of attempts: " + this.numberOfHttpRequestAttempts);

            try {
                // sleep for 5 seconds and try again
                Thread.sleep(5000);

            } catch (InterruptedException interruptedException) {
                logger.error(interruptedException.getMessage(), interruptedException);
            }

            // execute HTTP request again
            this.executeHttpRequest(url, method, requestEntity, responseType);

        } else {
            // in other cases, throw back the exception
            throw exception;
        }
    }

    return responseEntity;
}

From source file:org.springframework.web.client.RestTemplateIntegrationTests.java

@Test
public void serverError() {
    try {/*from   w  ww  .j  av  a  2s .  c o m*/
        template.execute(baseUrl + "/status/server", HttpMethod.GET, null, null);
        fail("HttpServerErrorException expected");
    } catch (HttpServerErrorException ex) {
        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, ex.getStatusCode());
        assertNotNull(ex.getStatusText());
        assertNotNull(ex.getResponseBodyAsString());
    }
}