Example usage for org.springframework.http HttpHeaders getFirst

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

Introduction

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

Prototype

@Override
@Nullable
public String getFirst(String headerName) 

Source Link

Document

Return the first header value for the given header name, if any.

Usage

From source file:org.springframework.cloud.stream.app.http.source.DefaultMixedCaseContentTypeHttpHeaderMapper.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 {//from  w  ww. j ava  2  s  .c  o  m
            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_MODIFIED_SINCE.equalsIgnoreCase(name)) {
        long modifiedSince = source.getIfModifiedSince();
        return (modifiedSince > -1) ? modifiedSince : null;
    } else if (IF_UNMODIFIED_SINCE.equalsIgnoreCase(name)) {
        String unmodifiedSince = source.getFirst(IF_UNMODIFIED_SINCE);
        return unmodifiedSince != null ? this.getFirstDate(unmodifiedSince, IF_UNMODIFIED_SINCE) : 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.data.keyvalue.riak.core.AbstractRiakTemplate.java

protected <B, K> Class<?> getType(B bucket, K key, ClassLoader classLoader) {
    Class<?> clazz = null;/*  w  w  w . j  a  va2s.c  om*/
    try {
        HttpHeaders headers = getRestTemplate().headForHeaders(defaultUri, bucket, key);
        if (null != headers) {
            String s = headers.getFirst(RIAK_META_CLASSNAME);
            if (null != s) {
                try {
                    if (null != classLoader) {
                        clazz = Class.forName(s, false, classLoader);
                    } else {
                        clazz = Class.forName(s);
                    }
                } catch (ClassNotFoundException ignored) {
                }
            }
        }
        if (null == clazz) {
            if (headers.getContentType().equals(MediaType.APPLICATION_JSON)) {
                clazz = Map.class;
            } else if (headers.getContentType().equals(MediaType.TEXT_PLAIN)) {
                clazz = String.class;
            } else {
                // handle as bytes
                log.error("Need to handle bytes!");
                clazz = byte[].class;
            }
        }
    } catch (ResourceAccessException notFound) {
        clazz = String.class;
    }
    return clazz;
}

From source file:org.springframework.web.reactive.socket.client.JettyWebSocketClient.java

private HandshakeInfo createHandshakeInfo(URI url, Session jettySession) {
    HttpHeaders headers = new HttpHeaders();
    jettySession.getUpgradeResponse().getHeaders().forEach(headers::put);
    String protocol = headers.getFirst("Sec-WebSocket-Protocol");
    return new HandshakeInfo(url, headers, Mono.empty(), protocol);
}

From source file:org.springframework.web.reactive.socket.client.ReactorNettyWebSocketClient.java

@Override
public Mono<Void> execute(URI url, HttpHeaders requestHeaders, WebSocketHandler handler) {
    return getHttpClient().headers(nettyHeaders -> setNettyHeaders(requestHeaders, nettyHeaders))
            .websocket(StringUtils.collectionToCommaDelimitedString(handler.getSubProtocols()))
            .uri(url.toString()).handle((inbound, outbound) -> {
                HttpHeaders responseHeaders = toHttpHeaders(inbound);
                String protocol = responseHeaders.getFirst("Sec-WebSocket-Protocol");
                HandshakeInfo info = new HandshakeInfo(url, responseHeaders, Mono.empty(), protocol);
                NettyDataBufferFactory factory = new NettyDataBufferFactory(outbound.alloc());
                WebSocketSession session = new ReactorNettyWebSocketSession(inbound, outbound, info, factory);
                if (logger.isDebugEnabled()) {
                    logger.debug("Started session '" + session.getId() + "' for " + url);
                }// w  ww  .  j  a v  a  2s .c  o  m
                return handler.handle(session);
            }).doOnRequest(n -> {
                if (logger.isDebugEnabled()) {
                    logger.debug("Connecting to " + url);
                }
            }).next();
}

From source file:org.springframework.web.reactive.socket.client.StandardWebSocketClient.java

private HandshakeInfo createHandshakeInfo(URI url, DefaultConfigurator configurator) {
    HttpHeaders responseHeaders = configurator.getResponseHeaders();
    String protocol = responseHeaders.getFirst("Sec-WebSocket-Protocol");
    return new HandshakeInfo(url, responseHeaders, Mono.empty(), protocol);
}

From source file:org.springframework.web.reactive.socket.client.UndertowWebSocketClient.java

private HandshakeInfo createHandshakeInfo(URI url, DefaultNegotiation negotiation) {
    HttpHeaders responseHeaders = negotiation.getResponseHeaders();
    String protocol = responseHeaders.getFirst("Sec-WebSocket-Protocol");
    return new HandshakeInfo(url, responseHeaders, Mono.empty(), protocol);
}

From source file:org.springframework.web.reactive.socket.client.WebSocketClientSupport.java

protected HandshakeInfo afterHandshake(URI url, HttpHeaders responseHeaders) {
    if (logger.isDebugEnabled()) {
        logger.debug("Handshake response: " + url + ", " + responseHeaders);
    }/*from   w w  w  . j  a  va2s  . co  m*/
    String protocol = responseHeaders.getFirst(SEC_WEBSOCKET_PROTOCOL);
    return new HandshakeInfo(url, responseHeaders, Mono.empty(), protocol);
}

From source file:org.springframework.web.reactive.socket.server.support.HandshakeWebSocketService.java

@Override
public Mono<Void> handleRequest(ServerWebExchange exchange, WebSocketHandler handler) {
    ServerHttpRequest request = exchange.getRequest();
    HttpMethod method = request.getMethod();
    HttpHeaders headers = request.getHeaders();

    if (logger.isDebugEnabled()) {
        logger.debug("Handling " + request.getURI() + " with headers: " + headers);
    }/*from   w  w w  .  ja v a2 s  .  c o m*/

    if (HttpMethod.GET != method) {
        return Mono.error(
                new MethodNotAllowedException(request.getMethodValue(), Collections.singleton(HttpMethod.GET)));
    }

    if (!"WebSocket".equalsIgnoreCase(headers.getUpgrade())) {
        return handleBadRequest("Invalid 'Upgrade' header: " + headers);
    }

    List<String> connectionValue = headers.getConnection();
    if (!connectionValue.contains("Upgrade") && !connectionValue.contains("upgrade")) {
        return handleBadRequest("Invalid 'Connection' header: " + headers);
    }

    String key = headers.getFirst(SEC_WEBSOCKET_KEY);
    if (key == null) {
        return handleBadRequest("Missing \"Sec-WebSocket-Key\" header");
    }

    String protocol = selectProtocol(headers, handler);
    return this.upgradeStrategy.upgrade(exchange, handler, protocol);
}

From source file:org.springframework.web.reactive.socket.server.support.HandshakeWebSocketService.java

@Nullable
private String selectProtocol(HttpHeaders headers, WebSocketHandler handler) {
    String protocolHeader = headers.getFirst(SEC_WEBSOCKET_PROTOCOL);
    if (protocolHeader != null) {
        List<String> supportedProtocols = handler.getSubProtocols();
        for (String protocol : StringUtils.commaDelimitedListToStringArray(protocolHeader)) {
            if (supportedProtocols.contains(protocol)) {
                return protocol;
            }/* w w w  .jav a  2 s .  co  m*/
        }
    }
    return null;
}

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

@Test
public void echoXhrWithHeaders() throws Exception {
    AbstractXhrTransport xhrTransport = createXhrTransport();
    xhrTransport.setXhrStreamingDisabled(true);

    WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
    headers.add("auth", "123");
    testEcho(10, xhrTransport, headers);

    for (Map.Entry<String, HttpHeaders> entry : this.testFilter.requests.entrySet()) {
        HttpHeaders httpHeaders = entry.getValue();
        assertEquals("No auth header for: " + entry.getKey(), "123", httpHeaders.getFirst("auth"));
    }/*from   w w w  .j av a 2  s. co m*/
}