Example usage for org.springframework.web.context.request WebRequest getHeaderValues

List of usage examples for org.springframework.web.context.request WebRequest getHeaderValues

Introduction

In this page you can find the example usage for org.springframework.web.context.request WebRequest getHeaderValues.

Prototype

@Nullable
String[] getHeaderValues(String headerName);

Source Link

Document

Return the request header values for the given header name, or null if none.

Usage

From source file:be.solidx.hot.rest.RestController.java

protected Object serializeRequestBody(String requestBody, WebRequest webRequest) {
    try {/*from  w  w  w .  j  a  va 2s. c om*/
        boolean json = false;
        if (webRequest.getHeaderValues("Content-Type") != null) {
            for (String ct : webRequest.getHeaderValues("Content-Type")) {
                if (ct.toLowerCase().contains("application/json")) {
                    json = true;
                    break;
                }
            }
            if (json) {
                return readJson(requestBody);
            } else {
                return requestBody;
            }
        } else {
            return requestBody;
        }
    } catch (Exception e) {
        return requestBody;
    }
}

From source file:org.fao.geonet.api.records.formatters.FormatterApi.java

private String getXmlFromUrl(ServiceContext context, String lang, String url, WebRequest request)
        throws IOException, URISyntaxException {
    String adjustedUrl = url;//w  ww. ja  va2  s . c o  m
    if (!url.startsWith("http")) {
        adjustedUrl = context.getBean(SettingManager.class).getSiteURL(lang) + url;
    } else {
        final URI uri = new URI(url);
        Set allowedRemoteHosts = context.getApplicationContext().getBean("formatterRemoteFormatAllowedHosts",
                Set.class);
        Assert.isTrue(allowedRemoteHosts.contains(uri.getHost()),
                "xml.format is not allowed to make requests to " + uri.getHost());
    }

    HttpUriRequest getXmlRequest = new HttpGet(adjustedUrl);
    final Iterator<String> headerNames = request.getHeaderNames();
    while (headerNames.hasNext()) {
        String headerName = headerNames.next();
        final String[] headers = request.getHeaderValues(headerName);
        for (String header : headers) {
            getXmlRequest.addHeader(headerName, header);
        }
    }

    GeonetHttpRequestFactory requestFactory = context.getBean(GeonetHttpRequestFactory.class);
    final ClientHttpResponse execute = requestFactory.execute(getXmlRequest);
    if (execute.getRawStatusCode() != 200) {
        throw new IllegalArgumentException("Request " + adjustedUrl + " did not succeed.  Response Status: "
                + execute.getStatusCode() + ", status text: " + execute.getStatusText());
    }
    return new String(ByteStreams.toByteArray(execute.getBody()), Constants.CHARSET);
}