Search in sources :

Example 71 with HttpClientErrorException

use of org.springframework.web.client.HttpClientErrorException in project spring-boot by spring-projects.

the class MavenMetadataVersionResolver method resolveVersions.

private Set<String> resolveVersions(String groupId, String artifactId, String repositoryUrl) {
    Set<String> versions = new HashSet<>();
    String url = repositoryUrl + "/" + groupId.replace('.', '/') + "/" + artifactId + "/maven-metadata.xml";
    try {
        String metadata = this.rest.getForObject(url, String.class);
        Document metadataDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(metadata)));
        NodeList versionNodes = (NodeList) XPathFactory.newInstance().newXPath().evaluate("/metadata/versioning/versions/version", metadataDocument, XPathConstants.NODESET);
        for (int i = 0; i < versionNodes.getLength(); i++) {
            versions.add(versionNodes.item(i).getTextContent());
        }
    } catch (HttpClientErrorException ex) {
        if (ex.getStatusCode() != HttpStatus.NOT_FOUND) {
            System.err.println("Failed to download maven-metadata.xml for " + groupId + ":" + artifactId + " from " + url + ": " + ex.getMessage());
        }
    } catch (Exception ex) {
        System.err.println("Failed to resolve versions for module " + groupId + ":" + artifactId + " in repository " + repositoryUrl + ": " + ex.getMessage());
    }
    return versions;
}
Also used : InputSource(org.xml.sax.InputSource) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) NodeList(org.w3c.dom.NodeList) StringReader(java.io.StringReader) Document(org.w3c.dom.Document) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) HashSet(java.util.HashSet)

Example 72 with HttpClientErrorException

use of org.springframework.web.client.HttpClientErrorException in project hippo by NHS-digital-website.

the class MicrosoftGraphAuthorizationProviderTest method throwsAuthorizationExceptionIfGraphApiThrowsHttpExceptionOnRequestAccessToken.

@Test(expected = AuthorizationException.class)
public void throwsAuthorizationExceptionIfGraphApiThrowsHttpExceptionOnRequestAccessToken() throws Exception {
    when(restTemplate.postForEntity(any(URI.class), any(HttpEntity.class), any())).thenThrow(new HttpClientErrorException(HttpStatus.BAD_REQUEST));
    authorizationProvider.processAuthorizationResponse(AUTHORIZATION_CODE);
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) URI(java.net.URI) Test(org.junit.Test)

Example 73 with HttpClientErrorException

use of org.springframework.web.client.HttpClientErrorException in project hippo by NHS-digital-website.

the class MicrosoftGraphAuthorizationProviderTest method throwsAuthorizationExceptionIfGraphApiThrowsHttpExceptionOnRefreshAccessToken.

@Test(expected = AuthorizationException.class)
public void throwsAuthorizationExceptionIfGraphApiThrowsHttpExceptionOnRefreshAccessToken() throws Exception {
    when(restTemplate.postForEntity(any(URI.class), any(HttpEntity.class), any())).thenThrow(new HttpClientErrorException(HttpStatus.BAD_REQUEST));
    final AccessToken oldAccessToken = new AccessToken(TOKEN, REFRESH_TOKEN, 1);
    authorizationProvider.refreshAccessToken(oldAccessToken);
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) AccessToken(uk.nhs.digital.intranet.model.AccessToken) URI(java.net.URI) Test(org.junit.Test)

Example 74 with HttpClientErrorException

use of org.springframework.web.client.HttpClientErrorException in project CzechIdMng by bcvsolutions.

the class AbstractPasswordFilterIntegrationTest method process.

// FIXME: use Spring MVC and remove this method at all
// FIXME: admin authentication is used internally => security cannot be tested
protected IdmResponse process(PasswordRequest request, String url, boolean success) {
    RestTemplate template = new RestTemplate();
    HttpEntity<PasswordRequest> requestUpdate = new HttpEntity<>(request, prepareHeaderBasicAuth());
    ResponseEntity<Object> result = null;
    IdmResponse response = new IdmResponse();
    try {
        result = template.exchange(url, HttpMethod.PUT, requestUpdate, Object.class);
        if (success) {
            assertEquals(HttpStatus.OK, result.getStatusCode());
            assertNull(result.getBody());
        } else {
            assertNotEquals(HttpStatus.OK, result.getStatusCode());
            assertNotNull(result.getBody());
        }
        response.message = String.valueOf(result.getBody());
        response.status = result.getStatusCode();
    } catch (HttpClientErrorException e) {
        response.message = e.getResponseBodyAsString();
        response.status = e.getStatusCode();
    }
    return response;
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) HttpEntity(org.springframework.http.HttpEntity) RestTemplate(org.springframework.web.client.RestTemplate) IcConnectorObject(eu.bcvsolutions.idm.ic.api.IcConnectorObject)

Example 75 with HttpClientErrorException

use of org.springframework.web.client.HttpClientErrorException in project apollo by ctripcorp.

the class ClusterControllerTest method shouldFailWhenRequestBodyInvalid.

@Test
public void shouldFailWhenRequestBodyInvalid() {
    ClusterDTO cluster = new ClusterDTO();
    cluster.setAppId("valid");
    cluster.setName("notBlank");
    ResponseEntity<ClusterDTO> response = restTemplate.postForEntity(baseUrl() + "/apps/{appId}/clusters", cluster, ClusterDTO.class, cluster.getAppId());
    ClusterDTO createdCluster = response.getBody();
    Assert.assertNotNull(createdCluster);
    Assert.assertEquals(cluster.getAppId(), createdCluster.getAppId());
    Assert.assertEquals(cluster.getName(), createdCluster.getName());
    cluster.setName("invalid app name");
    try {
        restTemplate.postForEntity(baseUrl() + "/apps/{appId}/clusters", cluster, ClusterDTO.class, cluster.getAppId());
        Assert.fail("Should throw");
    } catch (HttpClientErrorException e) {
        Assert.assertThat(new String(e.getResponseBodyAsByteArray()), containsString(InputValidator.INVALID_CLUSTER_NAMESPACE_MESSAGE));
    }
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) ClusterDTO(com.ctrip.framework.apollo.common.dto.ClusterDTO) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Aggregations

HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)109 HttpHeaders (org.springframework.http.HttpHeaders)31 Test (org.junit.Test)25 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)24 HashMap (java.util.HashMap)21 HttpServerErrorException (org.springframework.web.client.HttpServerErrorException)21 MediaType (org.springframework.http.MediaType)19 RestTemplate (org.springframework.web.client.RestTemplate)19 ParameterizedTypeReference (org.springframework.core.ParameterizedTypeReference)18 HttpEntity (org.springframework.http.HttpEntity)16 URI (java.net.URI)15 Map (java.util.Map)8 HttpStatus (org.springframework.http.HttpStatus)8 RestClientException (org.springframework.web.client.RestClientException)8 ResourceAccessException (org.springframework.web.client.ResourceAccessException)7 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)6 Date (java.util.Date)5 List (java.util.List)5 OpenAppNamespaceDTO (com.ctrip.framework.apollo.openapi.dto.OpenAppNamespaceDTO)4 ArrayList (java.util.ArrayList)4