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

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

Introduction

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

Prototype

public HttpStatus getStatusCode() 

Source Link

Document

Return the HTTP status code.

Usage

From source file:com.cicdaas.nasasoundapiautomation.test.NASASoundAPITest.java

@Test(groups = { "nasa-sound-api-regression", "nasa-sound-api-sanity" })
public void testNegNASASoundAPIGETCallwithoutKey() {
    try {//from  w w w .  j a  va 2 s  .  co  m
        client.getSoundTrackWithoutAPIKey();
        fail("Sound API returned valid response w/o API Key!");
    } catch (HttpClientErrorException hcee) {
        assertEquals(HttpStatus.FORBIDDEN, hcee.getStatusCode(), "HTTP Status code didn't match!");
    } catch (Exception e) {
        fail(defaultAPIClientErrorMsg, e);
    }
}

From source file:com.cicdaas.nasasoundapiautomation.test.NASASoundAPITest.java

@Test(groups = { "nasa-sound-api-regression", "real-svc-only", "nasa-sound-api-sanity" })
public void testNegNASASoundAPIGETCallwithInvalidKey() {
    try {//  w w  w  .  j a  va2 s .co m
        String key = "123";
        client.getSoundTrackWithSpecificAPIKey(key);
        fail("Sound API returned valid response for invalid API Key!");
    } catch (HttpClientErrorException hcee) {
        assertEquals(HttpStatus.FORBIDDEN, hcee.getStatusCode(), "HTTP Status code didn't match!");
    } catch (Exception e) {
        fail(defaultAPIClientErrorMsg, e);
    }
}

From source file:com.cicdaas.nasasoundapiautomation.test.NASASoundAPITest.java

@Test(groups = { "nasa-sound-api-regression", "real-svc-only", "nasa-sound-api-sanity" })
public void testNegNASASoundAPIGETCallWithInvalidHTTPProtocol() {
    try {//from w  w  w . j  a  v a  2  s .  c  o m
        client.getSoundTrackUsingHTTPProtocol();
        fail("Sound API returned valid response for non-secure protocol access (HTTP) - Bad Request succeeded!");
    } catch (HttpClientErrorException hcee) {
        assertEquals(HttpStatus.BAD_REQUEST, hcee.getStatusCode(), "HTTP Status code didn't match!");
    } catch (Exception e) {
        fail(defaultAPIClientErrorMsg, e);
    }
}

From source file:org.slc.sli.dashboard.client.RESTClient.java

/**
 * Make a request to a REST service and convert the result to JSON
 * //from   www .ja va2  s  . c o  m
 * @param path
 *            the unique portion of the requested REST service URL
 * @param token
 *            not used yet
 *
 * @param fullEntities 
 *             flag for returning expanded entities from the API
 *            
 * @return a {@link JsonElement} if the request is successful and returns valid JSON, otherwise
 *         null.
 * @throws NoSessionException
 */
public String makeJsonRequestWHeaders(String path, String token) {

    if (token != null) {

        URLBuilder url = null;
        if (!path.startsWith("http")) {
            url = new URLBuilder(getSecurityUrl());
            url.addPath(path);
        } else {
            url = new URLBuilder(path);
        }

        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "Bearer " + token);
        HttpEntity entity = new HttpEntity(headers);
        logger.debug("Accessing API at: {}", url);
        HttpEntity<String> response = null;
        try {
            response = exchange(template, url.toString(), HttpMethod.GET, entity, String.class);
        } catch (HttpClientErrorException e) {
            logger.debug("Catch HttpClientException: {}", e.getStatusCode());
        }
        if (response == null) {
            return null;
        }
        return response.getBody();
    }
    logger.debug("Token is null in call to RESTClient for path {}", path);

    return null;
}

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

@Test
public void delete() {
    restTemplate.delete(BASE_URL + "schema/user/delete/cool.json");
    SchemaTO firstname = null;//w  w w.  jav a 2  s  .  c  om
    try {
        firstname = restTemplate.getForObject(BASE_URL + "schema/user/read/cool.json", SchemaTO.class);
    } catch (HttpClientErrorException e) {
        assertEquals(HttpStatus.NOT_FOUND, e.getStatusCode());
    }
    assertNull(firstname);
}

From source file:spring.AbstractAuthorizationCodeProviderTests.java

@Test
@OAuth2ContextConfiguration(resource = MyTrustedClient.class, initialize = false)
public void testWrongClientIdTokenEndpoint() throws Exception {
    approveAccessTokenGrant("http://anywhere?key=value", true);
    ((BaseOAuth2ProtectedResourceDetails) context.getResource()).setClientId("non-existent");
    try {/* w  ww  . ja  v  a2  s  . com*/
        assertNotNull(context.getAccessToken());
        fail("Expected HttpClientErrorException");
    } catch (HttpClientErrorException e) {
        assertEquals(HttpStatus.UNAUTHORIZED, e.getStatusCode());
    }
}

From source file:org.cloudfoundry.identity.uaa.login.feature.AutologinIT.java

@Test
public void testClientAuthorization() throws Exception {
    Map<String, String> requestBody = new HashMap<>();
    requestBody.put("username", testAccounts.getUserName());
    requestBody.put("password", testAccounts.getPassword());

    try {/*from   w ww  . j  a  v a 2 s  . c o  m*/
        restOperations.exchange(baseUrl + "/autologin", HttpMethod.POST, new HttpEntity<>(requestBody),
                Map.class);
    } catch (HttpClientErrorException e) {
        assertEquals(HttpStatus.UNAUTHORIZED, e.getStatusCode());
    }
}

From source file:org.cloudfoundry.identity.uaa.login.feature.AutologinIT.java

@Test
public void testPasswordRequired() throws Exception {
    HttpHeaders headers = getAppBasicAuthHttpHeaders();

    Map<String, String> requestBody = new HashMap<>();
    requestBody.put("username", testAccounts.getUserName());

    try {//from w  w w.j  av  a2s.  com
        restOperations.exchange(baseUrl + "/autologin", HttpMethod.POST, new HttpEntity<>(requestBody, headers),
                Map.class);
    } catch (HttpClientErrorException e) {
        assertEquals(HttpStatus.UNAUTHORIZED, e.getStatusCode());
    }
}

From source file:com.marklogic.mgmt.admin.AdminManager.java

public void installAdmin(String username, String password) {
    final URI uri = adminConfig.buildUri("/admin/v1/instance-admin");

    String json = null;//  ww w. java  2s. c o  m
    if (username != null && password != null) {
        json = format("{\"admin-username\":\"%s\", \"admin-password\":\"%s\", \"realm\":\"public\"}", username,
                password);
    } else {
        json = "{}";
    }
    final String payload = json;

    logger.info("Installing admin user at: " + uri);
    invokeActionRequiringRestart(new ActionRequiringRestart() {
        @Override
        public boolean execute() {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity<String> entity = new HttpEntity<String>(payload, headers);
            try {
                ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.POST, entity,
                        String.class);
                logger.info("Admin installation response: " + response);
                // According to http://docs.marklogic.com/REST/POST/admin/v1/init, a 202 is sent back in the event a
                // restart is needed. A 400 or 401 will be thrown as an error by RestTemplate.
                return HttpStatus.ACCEPTED.equals(response.getStatusCode());
            } catch (HttpClientErrorException hcee) {
                if (HttpStatus.BAD_REQUEST.equals(hcee.getStatusCode())) {
                    logger.warn("Caught 400 error, assuming admin user already installed; response body: "
                            + hcee.getResponseBodyAsString());
                    return false;
                }
                throw hcee;
            }
        }
    });
}