Example usage for org.springframework.http HttpHeaders get

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

Introduction

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

Prototype

@Override
    @Nullable
    public List<String> get(Object key) 

Source Link

Usage

From source file:org.springframework.integration.http.support.DefaultHttpHeaderMapper.java

private Object getHttpHeader(HttpHeaders source, String name) {
    if (ACCEPT.equalsIgnoreCase(name)) {
        return source.getAccept();
    } else if (ACCEPT_CHARSET.equalsIgnoreCase(name)) {
        return source.getAcceptCharset();
    } else if (ALLOW.equalsIgnoreCase(name)) {
        return source.getAllow();
    } else if (CACHE_CONTROL.equalsIgnoreCase(name)) {
        String cacheControl = source.getCacheControl();
        return (StringUtils.hasText(cacheControl)) ? cacheControl : null;
    } else if (CONTENT_LENGTH.equalsIgnoreCase(name)) {
        long contentLength = source.getContentLength();
        return (contentLength > -1) ? contentLength : null;
    } else if (CONTENT_TYPE.equalsIgnoreCase(name)) {
        return source.getContentType();
    } else if (DATE.equalsIgnoreCase(name)) {
        long date = source.getDate();
        return (date > -1) ? date : null;
    } else if (ETAG.equalsIgnoreCase(name)) {
        String eTag = source.getETag();
        return (StringUtils.hasText(eTag)) ? eTag : null;
    } else if (EXPIRES.equalsIgnoreCase(name)) {
        try {/* ww w .  ja v  a2s  .c  om*/
            long expires = source.getExpires();
            return (expires > -1) ? expires : null;
        } catch (Exception e) {
            if (logger.isDebugEnabled()) {
                logger.debug(e.getMessage());
            }
            // According to RFC 2616
            return null;
        }
    } else if (IF_NONE_MATCH.equalsIgnoreCase(name)) {
        return source.getIfNoneMatch();
    } else if (IF_UNMODIFIED_SINCE.equalsIgnoreCase(name)) {
        long unmodifiedSince = source.getIfNotModifiedSince();
        return (unmodifiedSince > -1) ? unmodifiedSince : null;
    } else if (LAST_MODIFIED.equalsIgnoreCase(name)) {
        long lastModified = source.getLastModified();
        return (lastModified > -1) ? lastModified : null;
    } else if (LOCATION.equalsIgnoreCase(name)) {
        return source.getLocation();
    } else if (PRAGMA.equalsIgnoreCase(name)) {
        String pragma = source.getPragma();
        return (StringUtils.hasText(pragma)) ? pragma : null;
    }
    return source.get(name);
}

From source file:org.springframework.web.client.interceptors.ZeroLeggedOAuthInterceptorTest.java

@Test
public void testInterceptor() throws Exception {
    final String url = "http://www.test.com/lrs?param1=val1&param2=val2";
    final String data = "test";
    final String id = "test";
    final String realm = "realm";
    final String consumerKey = "consumerKey";
    final String secretKey = "secretKey";

    PropertyResolver resolver = mock(PropertyResolver.class);
    when(resolver.getProperty(eq("org.jasig.rest.interceptor.oauth." + id + ".realm"))).thenReturn(realm);
    when(resolver.getProperty(eq("org.jasig.rest.interceptor.oauth." + id + ".consumerKey")))
            .thenReturn(consumerKey);/*  ww w.j a  va  2s .  com*/
    when(resolver.getProperty(eq("org.jasig.rest.interceptor.oauth." + id + ".secretKey")))
            .thenReturn(secretKey);

    // holder for the headers...
    HttpHeaders headers = new HttpHeaders();

    // Mock guts of RestTemplate so no need to actually hit the web...
    ClientHttpResponse resp = mock(ClientHttpResponse.class);
    when(resp.getStatusCode()).thenReturn(HttpStatus.ACCEPTED);
    when(resp.getHeaders()).thenReturn(new HttpHeaders());

    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    ClientHttpRequest client = mock(ClientHttpRequest.class);
    when(client.getHeaders()).thenReturn(headers);
    when(client.getBody()).thenReturn(buffer);
    when(client.execute()).thenReturn(resp);

    ClientHttpRequestFactory factory = mock(ClientHttpRequestFactory.class);
    when(factory.createRequest(any(URI.class), any(HttpMethod.class))).thenReturn(client);

    // add the new interceptor...
    ZeroLeggedOAuthInterceptor interceptor = new ZeroLeggedOAuthInterceptor();
    interceptor.setPropertyResolver(resolver);
    interceptor.setId(id);
    List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
    interceptors.add(interceptor);

    RestTemplate rest = new RestTemplate(factory);
    rest.setInterceptors(interceptors);

    rest.postForLocation(url, data, Collections.emptyMap());

    // make sure auth header is correctly set...
    assertThat(headers, hasKey(Headers.Authorization.name()));

    String authHeader = headers.get(Headers.Authorization.name()).get(0);
    assertThat(authHeader, containsString("OAuth realm=\"" + realm + "\""));
    assertThat(authHeader, containsString("oauth_consumer_key=\"" + consumerKey + "\""));
    // for now, only supports HMAC-SHA1.  May have to fix later...
    assertThat(authHeader, containsString("oauth_signature_method=\"HMAC-SHA1\""));
    assertThat(authHeader, containsString("oauth_version=\"1.0\""));
    assertThat(authHeader, containsString("oauth_timestamp="));
    assertThat(authHeader, containsString("oauth_nonce="));
    assertThat(authHeader, containsString("oauth_signature="));

    // oauth lib will create 2 oauth_signature params if you call sign
    // multiple times.  Make sure only get 1.
    assertThat(StringUtils.countMatches(authHeader, "oauth_signature="), is(1));
}

From source file:org.springframework.web.socket.sockjs.client.SockJsClient.java

@Nullable
private HttpHeaders getHttpRequestHeaders(@Nullable HttpHeaders webSocketHttpHeaders) {
    if (getHttpHeaderNames() == null || webSocketHttpHeaders == null) {
        return webSocketHttpHeaders;
    } else {// www .  j av a  2s  . co m
        HttpHeaders httpHeaders = new HttpHeaders();
        for (String name : getHttpHeaderNames()) {
            List<String> values = webSocketHttpHeaders.get(name);
            if (values != null) {
                httpHeaders.put(name, values);
            }
        }
        return httpHeaders;
    }
}

From source file:software.coolstuff.springframework.owncloud.service.impl.rest.AbstractOwncloudRestServiceImpl.java

protected String getAuthorizationUserFromHeaders(HttpHeaders headers) {
    Validate.notNull(headers);//  ww  w.j  a  va 2  s .co  m

    List<String> authorizations = headers.get(HttpHeaders.AUTHORIZATION);
    if (CollectionUtils.isEmpty(authorizations)) {
        return null;
    }

    String encodedCredentials = authorizations.get(0);
    if (StringUtils.startsWith(encodedCredentials, AUTHORIZATION_METHOD_PREFIX)) {
        encodedCredentials = StringUtils.substring(encodedCredentials, AUTHORIZATION_METHOD_PREFIX.length());
    }
    final byte[] rawDecodedCredentials = Base64.getDecoder().decode(encodedCredentials.getBytes());
    final String decodedCredentials = new String(rawDecodedCredentials);
    if (!StringUtils.contains(decodedCredentials, ':')) {
        return null;
    }
    return StringUtils.split(decodedCredentials, ':')[0];
}