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

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

Introduction

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

Prototype

@Nullable
public HttpHeaders getResponseHeaders() 

Source Link

Document

Return the HTTP response headers.

Usage

From source file:org.springframework.cloud.config.client.ConfigServicePropertySourceLocator.java

@Override
@Retryable(interceptor = "configServerRetryInterceptor")
public org.springframework.core.env.PropertySource<?> locate(
        org.springframework.core.env.Environment environment) {
    ConfigClientProperties properties = this.defaultProperties.override(environment);
    CompositePropertySource composite = new CompositePropertySource("configService");
    RestTemplate restTemplate = this.restTemplate == null ? getSecureRestTemplate(properties)
            : this.restTemplate;
    Exception error = null;//from ww  w . j a  va2  s. c  o  m
    String errorBody = null;
    logger.info("Fetching config from server at: " + properties.getRawUri());
    try {
        String[] labels = new String[] { "" };
        if (StringUtils.hasText(properties.getLabel())) {
            labels = StringUtils.commaDelimitedListToStringArray(properties.getLabel());
        }

        String state = ConfigClientStateHolder.getState();

        // Try all the labels until one works
        for (String label : labels) {
            Environment result = getRemoteEnvironment(restTemplate, properties, label.trim(), state);
            if (result != null) {
                logger.info(String.format(
                        "Located environment: name=%s, profiles=%s, label=%s, version=%s, state=%s",
                        result.getName(),
                        result.getProfiles() == null ? "" : Arrays.asList(result.getProfiles()),
                        result.getLabel(), result.getVersion(), result.getState()));

                for (PropertySource source : result.getPropertySources()) {
                    @SuppressWarnings("unchecked")
                    Map<String, Object> map = (Map<String, Object>) source.getSource();
                    composite.addPropertySource(new MapPropertySource(source.getName(), map));
                }

                if (StringUtils.hasText(result.getState()) || StringUtils.hasText(result.getVersion())) {
                    HashMap<String, Object> map = new HashMap<>();
                    putValue(map, "config.client.state", result.getState());
                    putValue(map, "config.client.version", result.getVersion());
                    composite.addFirstPropertySource(new MapPropertySource("configClient", map));
                }
                return composite;
            }
        }
    } catch (HttpServerErrorException e) {
        error = e;
        if (MediaType.APPLICATION_JSON.includes(e.getResponseHeaders().getContentType())) {
            errorBody = e.getResponseBodyAsString();
        }
    } catch (Exception e) {
        error = e;
    }
    if (properties.isFailFast()) {
        throw new IllegalStateException(
                "Could not locate PropertySource and the fail fast property is set, failing", error);
    }
    logger.warn("Could not locate PropertySource: "
            + (errorBody == null ? error == null ? "label not found" : error.getMessage() : errorBody));
    return null;

}