Search in sources :

Example 46 with HttpClientErrorException

use of org.springframework.web.client.HttpClientErrorException in project goci by EBISPOT.

the class DepositionPublicationServiceImpl method retrievePublication.

@Override
public DepositionPublication retrievePublication(String id) {
    log.info("Retrieving publication using id [{}]", id);
    DepositionPublication publication = null;
    Map<String, String> params = new HashMap<>();
    params.put("pmID", id);
    String url = depositionIngestUri + "/publications/{pmID}?pmid=true";
    try {
        String response = template.getForObject(url, String.class, params);
        publication = template.getForObject(url, DepositionPublication.class, params);
    } catch (HttpClientErrorException e) {
        System.out.println(e.getMessage());
    }
    return publication;
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) DepositionPublication(uk.ac.ebi.spot.goci.model.deposition.DepositionPublication) HashMap(java.util.HashMap)

Example 47 with HttpClientErrorException

use of org.springframework.web.client.HttpClientErrorException in project mirrorgate-jira-stories-collector by BBVA.

the class SprintService method deleteIssue.

public void deleteIssue(final Long issueId) {
    final MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
    params.set("collectorId", appName);
    final UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(mirrorGateUrl + MIRRORGATE_DELETE_ISSUE_ENDPOINT).queryParams(params);
    try {
        restTemplate.delete(builder.build().toUriString(), issueId);
    } catch (final HttpClientErrorException e) {
        if (e.getStatusCode() == HttpStatus.NOT_FOUND) {
            LOGGER.warn("Issue {} already deleted", issueId);
        } else {
            LOGGER.error("Error trying to delete issue {}", issueId, e);
            throw e;
        }
    }
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder)

Example 48 with HttpClientErrorException

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

the class CloudFoundrySecurityService method getAccessLevel.

/**
 * Return the access level that should be granted to the given token.
 * @param token the token
 * @param applicationId the cloud foundry application ID
 * @return the access level that should be granted
 * @throws CloudFoundryAuthorizationException if the token is not authorized
 */
AccessLevel getAccessLevel(String token, String applicationId) throws CloudFoundryAuthorizationException {
    try {
        URI uri = getPermissionsUri(applicationId);
        RequestEntity<?> request = RequestEntity.get(uri).header("Authorization", "bearer " + token).build();
        Map<?, ?> body = this.restTemplate.exchange(request, Map.class).getBody();
        if (Boolean.TRUE.equals(body.get("read_sensitive_data"))) {
            return AccessLevel.FULL;
        }
        return AccessLevel.RESTRICTED;
    } catch (HttpClientErrorException ex) {
        if (ex.getStatusCode().equals(HttpStatus.FORBIDDEN)) {
            throw new CloudFoundryAuthorizationException(Reason.ACCESS_DENIED, "Access denied");
        }
        throw new CloudFoundryAuthorizationException(Reason.INVALID_TOKEN, "Invalid token", ex);
    } catch (HttpServerErrorException ex) {
        throw new CloudFoundryAuthorizationException(Reason.SERVICE_UNAVAILABLE, "Cloud controller not reachable");
    }
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) CloudFoundryAuthorizationException(org.springframework.boot.actuate.autoconfigure.cloudfoundry.CloudFoundryAuthorizationException) URI(java.net.URI) HashMap(java.util.HashMap) Map(java.util.Map) HttpServerErrorException(org.springframework.web.client.HttpServerErrorException)

Example 49 with HttpClientErrorException

use of org.springframework.web.client.HttpClientErrorException in project dhis2-core by dhis2.

the class SimplisticHttpGetGateWay method send.

@Override
public OutboundMessageResponse send(String subject, String text, Set<String> recipients, SmsGatewayConfig config) {
    GenericHttpGatewayConfig genericConfig = (GenericHttpGatewayConfig) config;
    UriComponentsBuilder uriBuilder;
    ResponseEntity<String> responseEntity = null;
    HttpEntity<String> requestEntity;
    URI uri;
    try {
        requestEntity = getRequestEntity(genericConfig, text, recipients);
        if (genericConfig.isSendUrlParameters()) {
            uriBuilder = UriComponentsBuilder.fromHttpUrl(config.getUrlTemplate() + "?" + requestEntity.getBody());
        } else {
            uriBuilder = UriComponentsBuilder.fromHttpUrl(config.getUrlTemplate());
        }
        uri = uriBuilder.build().encode().toUri();
        responseEntity = restTemplate.exchange(uri, genericConfig.isUseGet() ? HttpMethod.GET : HttpMethod.POST, requestEntity, String.class);
    } catch (HttpClientErrorException ex) {
        log.error("Client error " + ex.getMessage());
    } catch (HttpServerErrorException ex) {
        log.error("Server error " + ex.getMessage());
    } catch (Exception ex) {
        log.error("Error " + ex.getMessage());
    }
    return getResponse(responseEntity);
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) URI(java.net.URI) HttpServerErrorException(org.springframework.web.client.HttpServerErrorException) HttpServerErrorException(org.springframework.web.client.HttpServerErrorException) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException)

Example 50 with HttpClientErrorException

use of org.springframework.web.client.HttpClientErrorException in project dhis2-core by dhis2.

the class SyncUtils method isRemoteServerAvailable.

/**
 * Checks the availability of remote server
 *
 * @param systemSettingManager Reference to SystemSettingManager
 * @param restTemplate Reference to RestTemplate
 * @return AvailabilityStatus that says whether the server is available or
 *         not
 */
public static AvailabilityStatus isRemoteServerAvailable(SystemSettingManager systemSettingManager, RestTemplate restTemplate) {
    if (!isRemoteServerConfigured(systemSettingManager)) {
        return new AvailabilityStatus(false, "Remote server is not configured", HttpStatus.BAD_GATEWAY);
    }
    String url = systemSettingManager.getStringSetting(SettingKey.REMOTE_INSTANCE_URL) + PING_PATH;
    String username = systemSettingManager.getStringSetting(SettingKey.REMOTE_INSTANCE_USERNAME);
    String password = systemSettingManager.getStringSetting(SettingKey.REMOTE_INSTANCE_PASSWORD);
    log.debug(String.format("Remote server ping URL: %s, username: %s", url, username));
    HttpEntity<String> request = getBasicAuthRequestEntity(username, password);
    ResponseEntity<String> response = null;
    HttpStatus sc = null;
    String st = null;
    AvailabilityStatus status = null;
    try {
        response = restTemplate.exchange(url, HttpMethod.GET, request, String.class);
        sc = response.getStatusCode();
    } catch (HttpClientErrorException | HttpServerErrorException ex) {
        sc = ex.getStatusCode();
        st = ex.getStatusText();
    } catch (ResourceAccessException ex) {
        return new AvailabilityStatus(false, "Network is unreachable", HttpStatus.BAD_GATEWAY);
    }
    log.debug("Response status code: " + sc);
    if (HttpStatus.OK.equals(sc)) {
        status = new AvailabilityStatus(true, "Authentication was successful", sc);
    } else if (HttpStatus.FOUND.equals(sc)) {
        status = new AvailabilityStatus(false, "No authentication was provided", sc);
    } else if (HttpStatus.UNAUTHORIZED.equals(sc)) {
        status = new AvailabilityStatus(false, "Authentication failed", sc);
    } else if (HttpStatus.INTERNAL_SERVER_ERROR.equals(sc)) {
        status = new AvailabilityStatus(false, "Remote server experienced an internal error", sc);
    } else {
        status = new AvailabilityStatus(false, "Server is not available: " + st, sc);
    }
    log.info("Status: " + status);
    return status;
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) AvailabilityStatus(org.hisp.dhis.dxf2.synch.AvailabilityStatus) HttpStatus(org.springframework.http.HttpStatus) HttpServerErrorException(org.springframework.web.client.HttpServerErrorException) ResourceAccessException(org.springframework.web.client.ResourceAccessException)

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