Example usage for org.springframework.http HttpHeaders entrySet

List of usage examples for org.springframework.http HttpHeaders entrySet

Introduction

In this page you can find the example usage for org.springframework.http HttpHeaders entrySet.

Prototype

@Override
    public Set<Entry<String, List<String>>> entrySet() 

Source Link

Usage

From source file:org.greencheek.spring.rest.SSLCachingHttpComponentsClientHttpRequest.java

protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException {
    for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
        String headerName = entry.getKey();
        if (!headerName.equalsIgnoreCase(HTTP.CONTENT_LEN)
                && !headerName.equalsIgnoreCase(HTTP.TRANSFER_ENCODING)) {
            for (String headerValue : entry.getValue()) {
                this.httpRequest.addHeader(headerName, headerValue);
            }//  ww  w .jav a  2s . com
        }
    }
    if (this.httpRequest instanceof HttpEntityEnclosingRequest) {
        HttpEntityEnclosingRequest entityEnclosingRequest = (HttpEntityEnclosingRequest) this.httpRequest;
        HttpEntity requestEntity = new ByteArrayEntity(bufferedOutput);
        entityEnclosingRequest.setEntity(requestEntity);
    }

    setSSLPrinciple(this.httpContext);
    HttpResponse httpResponse = this.httpClient.execute(this.httpRequest, this.httpContext);
    saveSSLPrinciple(this.httpContext);
    return new SSLCachingHttpComponentsClientHttpResponse(httpResponse, this.httpContext);
}

From source file:it.tidalwave.northernwind.frontend.springmvc.SpringMvcResponseHolderTest.java

/*******************************************************************************************************************
 *
 * Note that here we are not testing the correctness of the actual output sent to the network: in fact, it's a
 * responsibility of Spring MVC. We are only testing the proper contents of ResponseEntity.
 *
 ******************************************************************************************************************/
@Override//from   www  . ja v  a  2 s .  c om
protected void assertContents(final @Nonnull ResponseBuilder<?> builder, final String fileName)
        throws Exception {
    final TestHelper.TestResource tr = helper.testResourceFor(fileName);
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final ResponseEntity<byte[]> responseEntity = (ResponseEntity<byte[]>) builder.build();
    final HttpHeaders headers = responseEntity.getHeaders();

    final PrintWriter pw = new PrintWriter(baos); // FIXME: charset?
    pw.printf("HTTP/1.1 %d%n", responseEntity.getStatusCode().value());
    headers.entrySet().stream().sorted(comparing(Entry::getKey))
            .forEach(e -> pw.printf("%s: %s%n", e.getKey(), e.getValue().get(0)));
    pw.println();
    pw.flush();

    baos.write(responseEntity.getBody());
    tr.writeToActualFile(baos.toByteArray());
    tr.assertActualFileContentSameAsExpected();
}

From source file:io.github.microcks.util.test.HttpTestRunner.java

/** Build domain headers from ClientHttpResponse ones. */
private Set<Header> buildHeaders(ClientHttpResponse httpResponse) {
    if (httpResponse.getHeaders() != null && !httpResponse.getHeaders().isEmpty()) {
        Set<Header> headers = new HashSet<>();
        HttpHeaders responseHeaders = httpResponse.getHeaders();
        for (Entry<String, List<String>> responseHeader : responseHeaders.entrySet()) {
            Header header = new Header();
            header.setName(responseHeader.getKey());
            header.setValues(new HashSet<>(responseHeader.getValue()));
            headers.add(header);//from   w  w w  .j a v  a  2s.co  m
        }
        return headers;
    }
    return null;
}

From source file:io.pivotal.poc.dispatcher.MessageDispatcher.java

private String sendMessage(String topic, Object body, HttpHeaders requestHeaders) {
    MessageChannel channel = resolver.resolveDestination(topic + ".input");
    MessageBuilder<?> builder = MessageBuilder.withPayload(body);
    builder.setHeader(MessageHeaders.CONTENT_TYPE, requestHeaders.getContentType());
    for (Map.Entry<String, List<String>> entry : requestHeaders.entrySet()) {
        String headerName = entry.getKey();
        if (requestHeadersToMap.contains(headerName)) {
            builder.setHeaderIfAbsent(headerName,
                    StringUtils.collectionToCommaDelimitedString(entry.getValue()));
        }/*from  w ww. ja  v a 2  s  .  c o m*/
    }
    Message<?> message = builder.build();
    channel.send(message);
    return message.getHeaders().getId().toString();
}

From source file:io.neba.core.mvc.MultipartSlingHttpServletRequestTest.java

@SuppressWarnings("rawtypes")
private void mockHeaders(final HttpHeaders headers) {
    Enumeration headerNames = fromIterator(headers.keySet().iterator());

    when(this.wrappedRequest.getHeaderNames()).thenReturn(headerNames);

    for (Entry<String, List<String>> entry : headers.entrySet()) {
        Enumeration headerValues = fromIterator(entry.getValue().iterator());
        when(this.wrappedRequest.getHeaders(entry.getKey())).thenReturn(headerValues);
    }// w  w  w  .j  a  v  a  2s . c  om
}

From source file:eionet.webq.web.interceptor.CdrAuthorizationInterceptor.java

/**
 * Calls a resource in CDR with redirect disabled. Then it is possible to catch if the user is redirected to login page.
 *
 * @param url CDR url to fetch.// www.  j  a va2 s  .  c  o m
 * @param headers HTTP headers to send.
 * @return HTTP response object
 * @throws IOException if network error occurs
 * @throws java.security.NoSuchAlgorithmException
 * @throws java.security.KeyManagementException
 */

protected CloseableHttpResponse fetchUrlWithoutRedirection(String url, HttpHeaders headers)
        throws IOException, NoSuchAlgorithmException, KeyManagementException {
    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    httpClientBuilder.setSSLContext(SSLContexts.custom().useProtocol("TLSv1.2").build())
            .setRedirectStrategy(new RedirectStrategy() {
                @Override
                public boolean isRedirected(HttpRequest httpRequest, HttpResponse httpResponse,
                        HttpContext httpContext) throws ProtocolException {
                    return false;
                }

                @Override
                public HttpUriRequest getRedirect(HttpRequest httpRequest, HttpResponse httpResponse,
                        HttpContext httpContext) throws ProtocolException {
                    return null;
                }
            });
    HttpGet httpget = new HttpGet(url);

    for (Map.Entry<String, List<String>> header : headers.entrySet()) {
        for (String value : header.getValue()) {
            httpget.addHeader(header.getKey(), value);
        }
    }
    CloseableHttpClient client = httpClientBuilder.build();
    CloseableHttpResponse httpResponse = client.execute(httpget);
    return httpResponse;
}

From source file:org.springframework.cloud.netflix.eureka.http.RestTemplateEurekaHttpClient.java

private static Map<String, String> headersOf(ResponseEntity<?> response) {
    HttpHeaders httpHeaders = response.getHeaders();
    if (httpHeaders == null || httpHeaders.isEmpty()) {
        return Collections.emptyMap();
    }//from  w  w w .  j  a v a2  s.c om
    Map<String, String> headers = new HashMap<>();
    for (Entry<String, List<String>> entry : httpHeaders.entrySet()) {
        if (!entry.getValue().isEmpty()) {
            headers.put(entry.getKey(), entry.getValue().get(0));
        }
    }
    return headers;
}

From source file:org.springframework.data.keyvalue.riak.core.AbstractRiakTemplate.java

protected RiakMetaData extractMetaData(HttpHeaders headers) throws IOException {
    Map<String, Object> props = new LinkedHashMap<String, Object>();
    for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
        List<String> val = entry.getValue();
        Object prop = (1 == val.size() ? val.get(0) : val);
        try {//from ww  w . ja v  a 2s.co  m
            if (entry.getKey().equals("Last-Modified") || entry.getKey().equals("Date")) {
                prop = httpDate.parse(val.get(0));
            }
        } catch (ParseException e) {
            log.error(e.getMessage(), e);
        }

        if (entry.getKey().equals("Link")) {
            List<String> links = new ArrayList<String>();
            for (String link : entry.getValue()) {
                String[] parts = link.split(",");
                for (String part : parts) {
                    String s = part.replaceAll("<(.+)>; rel=\"(\\S+)\"[,]?", "").trim();
                    if (!"".equals(s)) {
                        links.add(s);
                    }
                }
            }
            props.put("Link", links);
        } else {
            props.put(entry.getKey().toString(), prop);
        }
    }
    props.put("ETag", headers.getETag());
    RiakMetaData meta = new RiakMetaData(headers.getContentType(), props);

    return meta;
}

From source file:org.springframework.http.client.CommonsClientHttpRequest.java

@Override
public ClientHttpResponse executeInternal(HttpHeaders headers, byte[] output) throws IOException {
    for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
        String headerName = entry.getKey();
        for (String headerValue : entry.getValue()) {
            httpMethod.addRequestHeader(headerName, headerValue);
        }/*  w w  w  .j av a  2  s .c o  m*/
    }
    if (this.httpMethod instanceof EntityEnclosingMethod) {
        EntityEnclosingMethod entityEnclosingMethod = (EntityEnclosingMethod) this.httpMethod;
        RequestEntity requestEntity = new ByteArrayRequestEntity(output);
        entityEnclosingMethod.setRequestEntity(requestEntity);
    }
    this.httpClient.executeMethod(this.httpMethod);
    return new CommonsClientHttpResponse(this.httpMethod);
}

From source file:org.springframework.web.servlet.resource.ResourceHttpRequestHandler.java

/**
 * Set headers on the given servlet response.
 * Called for GET requests as well as HEAD requests.
 * @param response current servlet response
 * @param resource the identified resource (never {@code null})
 * @param mediaType the resource's media type (never {@code null})
 * @throws IOException in case of errors while setting the headers
 *//*from  ww  w  .  j av  a  2s  .co m*/
protected void setHeaders(HttpServletResponse response, Resource resource, @Nullable MediaType mediaType)
        throws IOException {

    long length = resource.contentLength();
    if (length > Integer.MAX_VALUE) {
        response.setContentLengthLong(length);
    } else {
        response.setContentLength((int) length);
    }

    if (mediaType != null) {
        response.setContentType(mediaType.toString());
    }
    if (resource instanceof HttpResource) {
        HttpHeaders resourceHeaders = ((HttpResource) resource).getResponseHeaders();
        for (Map.Entry<String, List<String>> entry : resourceHeaders.entrySet()) {
            String headerName = entry.getKey();
            boolean first = true;
            for (String headerValue : entry.getValue()) {
                if (first) {
                    response.setHeader(headerName, headerValue);
                } else {
                    response.addHeader(headerName, headerValue);
                }
                first = false;
            }
        }
    }
    response.setHeader(HttpHeaders.ACCEPT_RANGES, "bytes");
}