Example usage for com.google.common.net HttpHeaders HOST

List of usage examples for com.google.common.net HttpHeaders HOST

Introduction

In this page you can find the example usage for com.google.common.net HttpHeaders HOST.

Prototype

String HOST

To view the source code for com.google.common.net HttpHeaders HOST.

Click Source Link

Document

The HTTP Host header field name.

Usage

From source file:org.openmrs.module.webservices.rest.web.controller.SwaggerSpecificationController.java

@RequestMapping(method = RequestMethod.GET)
public @ResponseBody String getSwaggerSpecification(HttpServletRequest request) throws Exception {

    String host = request.getHeader(HttpHeaders.HOST);

    String scheme = request.getHeader(HttpHeaders.X_FORWARDED_PROTO);
    if (scheme == null) {
        scheme = request.getScheme();/* w ww.j  ava 2s . c  om*/
    }

    return new SwaggerSpecificationCreator().host(host).basePath(request.getContextPath() + "/ws/rest/v1")
            .scheme(Scheme.forValue(scheme))

            .getJSON();
}

From source file:org.sfs.util.SfsHttpUtil.java

public static String getRemoteServiceUrl(HttpServerRequest httpServerRequest) {
    try {//from  ww  w . j a v  a 2s.  com
        URI absoluteRequestURI = new URI(httpServerRequest.absoluteURI());
        MultiMap headers = httpServerRequest.headers();

        String host = getFirstHeader(httpServerRequest, "X-Forwarded-Host");
        String contextRoot = getFirstHeader(httpServerRequest, SfsHttpHeaders.X_CONTEXT_ROOT);
        if (host == null)
            host = getFirstHeader(httpServerRequest, HttpHeaders.HOST);
        if (host == null)
            host = absoluteRequestURI.getHost();
        String proto = headers.get(HttpHeaders.X_FORWARDED_PROTO);
        if (proto == null)
            proto = absoluteRequestURI.getScheme();

        String serviceUrl;
        if (contextRoot != null) {
            serviceUrl = String.format("%s://%s/%s", proto, host, contextRoot);
        } else {
            serviceUrl = String.format("%s://%s", proto, host);
        }
        return serviceUrl;
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.clitherproject.clither.server.net.Handshaker.java

@Override
protected void channelRead0(ChannelHandlerContext handlerContext, Object request) throws Exception {

    if (request instanceof FullHttpRequest) {
        FullHttpRequest fullRequest = (FullHttpRequest) request;
        WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
                "ws://" + fullRequest.headers().get(HttpHeaders.HOST) + "/", null, true);
        serverHandshaker = wsFactory.newHandshaker(fullRequest);

        if (serverHandshaker == null) {
            WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(handlerContext.channel());
        } else {/* w  w  w .ja  va 2  s  . c om*/
            serverHandshaker.handshake(handlerContext.channel(), fullRequest);
        }
    } else if (request instanceof WebSocketFrame) {

        WebSocketFrame frame = (WebSocketFrame) request;

        if (request instanceof CloseWebSocketFrame) {
            if (serverHandshaker != null) {
                serverHandshaker.close(handlerContext.channel(), ((CloseWebSocketFrame) request).retain());
            }
        } else if (request instanceof PingWebSocketFrame) {
            handlerContext.channel().write(new PongWebSocketFrame(frame.content().retain()));
        } else {
            handlerContext.fireChannelRead(frame.retain());
        }
    }
}

From source file:io.gravitee.gateway.core.reactor.RouteMatcher.java

public Api match(final Request request) {
    // Matching rules:
    // - Context Path
    // - Virtual host (using HOST header)
    return FluentIterable.from(registry.listAll()).firstMatch(Predicates.and(new Predicate<Api>() {
        @Override/*  w  ww  .  ja  v  a2s .  c o  m*/
        public boolean apply(final Api api) {
            return request.path().startsWith(api.getPublicURI().getPath());
        }
    }, new Predicate<Api>() {
        @Override
        public boolean apply(Api api) {
            return api.getPublicURI().getHost().equalsIgnoreCase(request.headers().get(HttpHeaders.HOST));
        }
    })).orNull();
}

From source file:org.clitherproject.clither.server.net.WebSocketHandler.java

@Override
protected void channelRead0(ChannelHandlerContext ctx, Object req) throws Exception {
    if (req instanceof FullHttpRequest) {
        FullHttpRequest request = (FullHttpRequest) req;
        // ----- Client authenticity check code -----
        String origin = request.headers().get(HttpHeaders.ORIGIN);
        if (origin != null) {
            switch (origin) {
            case "http://slither.io":
            case "https://slither.io":
            case "http://localhost":
            case "https://localhost":
            case "http://127.0.0.1":
            case "https://127.0.0.1":
                break;
            default:
                ctx.channel().close();//ww w .ja v  a  2 s .  c  om
                return;
            }
        }
        // -----/Client authenticity check code -----

        WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
                "ws://" + request.headers().get(HttpHeaders.HOST) + "/", null, true);
        handshaker = wsFactory.newHandshaker(request);
        if (handshaker == null) {
            WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
        } else {
            handshaker.handshake(ctx.channel(), request);
        }
    } else if (req instanceof WebSocketFrame) {
        WebSocketFrame frame = (WebSocketFrame) req;

        if (req instanceof CloseWebSocketFrame) {
            if (handshaker != null) {
                handshaker.close(ctx.channel(), ((CloseWebSocketFrame) req).retain());
            }
        } else if (req instanceof PingWebSocketFrame) {
            ctx.channel().write(new PongWebSocketFrame(frame.content().retain()));
        } else {
            ctx.fireChannelRead(frame.retain());
        }
    }
}

From source file:org.jclouds.glacier.filters.RequestAuthorizeSignature.java

@Override
public HttpRequest filter(HttpRequest request) throws HttpException {
    request = request.toBuilder().removeHeader(HttpHeaders.DATE)
            .replaceHeader(GlacierHeaders.ALTERNATE_DATE, timeStampProvider.get())
            .replaceHeader(HttpHeaders.HOST, request.getEndpoint().getHost()).build();
    utils.logRequest(signatureLog, request, ">>");
    request = this.signer.sign(request);
    utils.logRequest(signatureLog, request, "<<");
    return request;
}

From source file:org.jclouds.s3.binders.BindAsHostPrefixIfConfigured.java

@SuppressWarnings("unchecked")
@Override//ww w.  j a v  a  2  s. co m
public <R extends HttpRequest> R bindToRequest(R request, Object payload) {
    // If we have a payload/bucket/container that is not all lowercase, vhost-style URLs are not an option and must be
    // automatically converted to their path-based equivalent.  This should only be possible for AWS-S3 since it is
    // the only S3 implementation configured to allow uppercase payload/bucket/container names.
    //
    // http://code.google.com/p/jclouds/issues/detail?id=992
    String payloadAsString = payload.toString();

    if (isVhostStyle && payloadAsString.equals(payloadAsString.toLowerCase())) {
        request = bindAsHostPrefix.bindToRequest(request, payload);
        String host = request.getEndpoint().getHost();
        if (request.getEndpoint().getPort() != -1) {
            host += ":" + request.getEndpoint().getPort();
        }
        return (R) request.toBuilder().replaceHeader(HttpHeaders.HOST, host).build();
    } else {
        StringBuilder path = new StringBuilder(request.getEndpoint().getRawPath());
        if (servicePath.equals("/")) {
            if (path.toString().equals("/"))
                path.append(payloadAsString);
            else
                path.insert(0, "/" + payloadAsString);
        } else {
            int indexToInsert = 0;
            indexToInsert = path.indexOf(servicePath);
            indexToInsert = indexToInsert == -1 ? 0 : indexToInsert;
            indexToInsert += servicePath.length();
            path.insert(indexToInsert, "/" + payloadAsString);
        }
        return (R) request.toBuilder().replacePath(path.toString()).build();
    }
}

From source file:com.facebook.nifty.client.HttpClientChannel.java

@Override
protected ChannelFuture writeRequest(ChannelBuffer request) {
    HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, endpointUri);

    httpRequest.setHeader(HttpHeaders.HOST, hostName);
    httpRequest.setHeader(HttpHeaders.CONTENT_LENGTH, request.readableBytes());
    httpRequest.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-thrift");
    httpRequest.setHeader(HttpHeaders.USER_AGENT, "Java/Swift-HttpThriftClientChannel");

    if (headerDictionary != null) {
        for (Map.Entry<String, String> entry : headerDictionary.entrySet()) {
            httpRequest.setHeader(entry.getKey(), entry.getValue());
        }/*from  w ww .j  ava 2  s .  co  m*/
    }

    httpRequest.setContent(request);

    return underlyingNettyChannel.write(httpRequest);
}

From source file:com.ogarproject.ogar.server.net.WebSocketHandler.java

@Override
protected void channelRead0(ChannelHandlerContext ctx, Object req) throws Exception {
    if (req instanceof FullHttpRequest) {
        FullHttpRequest request = (FullHttpRequest) req;
        // ----- Client authenticity check code -----
        // !!!!! WARNING !!!!!
        // THE BELOW SECTION OF CODE CHECKS TO ENSURE THAT CONNECTIONS ARE COMING
        // FROM THE OFFICIAL AGAR.IO CLIENT. IF YOU REMOVE OR MODIFY THE BELOW
        // SECTION OF CODE TO ALLOW CONNECTIONS FROM A CLIENT ON A DIFFERENT DOMAIN,
        // YOU MAY BE COMMITTING COPYRIGHT INFRINGEMENT AND LEGAL ACTION MAY BE TAKEN
        // AGAINST YOU. THIS SECTION OF CODE WAS ADDED ON JULY 9, 2015 AT THE REQUEST
        // OF THE AGAR.IO DEVELOPERS.
        String origin = request.headers().get(HttpHeaders.ORIGIN);
        if (origin != null) {
            switch (origin) {
            case "http://agar.io":
            case "https://agar.io":
            case "http://localhost":
            case "https://localhost":
            case "http://127.0.0.1":
            case "https://127.0.0.1":
                break;
            default:
                ctx.channel().close();/*from w  w  w  .  j av a 2s.co  m*/
                return;
            }
        }
        // -----/Client authenticity check code -----

        WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
                "ws://" + request.headers().get(HttpHeaders.HOST) + "/", null, true);
        handshaker = wsFactory.newHandshaker(request);
        if (handshaker == null) {
            WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
        } else {
            handshaker.handshake(ctx.channel(), request);
        }
    } else if (req instanceof WebSocketFrame) {
        WebSocketFrame frame = (WebSocketFrame) req;

        if (req instanceof CloseWebSocketFrame) {
            if (handshaker != null) {
                handshaker.close(ctx.channel(), ((CloseWebSocketFrame) req).retain());
            }
        } else if (req instanceof PingWebSocketFrame) {
            ctx.channel().write(new PongWebSocketFrame(frame.content().retain()));
        } else {
            ctx.fireChannelRead(frame.retain());
        }
    }
}

From source file:ru.calypso.ogar.server.net.WebSocketHandler.java

@Override
protected void channelRead0(ChannelHandlerContext ctx, Object req) throws Exception {
    if (req instanceof FullHttpRequest) {
        FullHttpRequest request = (FullHttpRequest) req;
        // ----- Client authenticity check code -----
        // !!!!! WARNING !!!!!
        // THE BELOW SECTION OF CODE CHECKS TO ENSURE THAT CONNECTIONS ARE COMING
        // FROM THE OFFICIAL AGAR.IO CLIENT. IF YOU REMOVE OR MODIFY THE BELOW
        // SECTION OF CODE TO ALLOW CONNECTIONS FROM A CLIENT ON A DIFFERENT DOMAIN,
        // YOU MAY BE COMMITTING COPYRIGHT INFRINGEMENT AND LEGAL ACTION MAY BE TAKEN
        // AGAINST YOU. THIS SECTION OF CODE WAS ADDED ON JULY 9, 2015 AT THE REQUEST
        // OF THE AGAR.IO DEVELOPERS.
        String origin = request.headers().get(HttpHeaders.ORIGIN);
        switch (origin) {
        // TODO move to config
        case "http://agar.io":
        case "https://agar.io":
        case "http://localhost":
        case "https://localhost":
        case "http://127.0.0.1":
        case "https://127.0.0.1":
        case "http://ogar.pp.ua":
            break;
        default://  ww w .  j a v a  2  s.  com
            _log.info(String.format("User kicked by invalid origin! IP %s, ORIGIN %s",
                    ctx.channel().remoteAddress(), origin));
            ctx.channel().close();
            return;
        }
        // -----/Client authenticity check code -----

        WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
                "ws://" + request.headers().get(HttpHeaders.HOST) + "/", null, true);
        handshaker = wsFactory.newHandshaker(request);
        if (handshaker == null) {
            WebSocketServerHandshakerFactory.sendUnsupportedWebSocketVersionResponse(ctx.channel());
        } else {
            handshaker.handshake(ctx.channel(), request);
        }
    } else if (req instanceof WebSocketFrame) {
        WebSocketFrame frame = (WebSocketFrame) req;

        if (req instanceof CloseWebSocketFrame) {
            if (handshaker != null) {
                handshaker.close(ctx.channel(), ((CloseWebSocketFrame) req).retain());
            }
        } else if (req instanceof PingWebSocketFrame) {
            ctx.channel().write(new PongWebSocketFrame(frame.content().retain()));
        } else {
            ctx.fireChannelRead(frame.retain());
        }
    }
}