Search in sources :

Example 1 with HOST

use of com.google.common.net.HttpHeaders.HOST in project mockserver by mock-server.

the class CallbackWebSocketServerHandler method upgradeChannel.

private void upgradeChannel(final ChannelHandlerContext ctx, FullHttpRequest httpRequest) {
    handshaker = new WebSocketServerHandshakerFactory((isSslEnabledUpstream(ctx.channel()) ? "wss" : "ws") + "://" + httpRequest.headers().get(HOST) + UPGRADE_CHANNEL_FOR_CALLBACK_WEB_SOCKET_URI, null, true, Integer.MAX_VALUE).newHandshaker(httpRequest);
    if (handshaker == null) {
        WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
    } else {
        final String clientId = httpRequest.headers().contains(CLIENT_REGISTRATION_ID_HEADER) ? httpRequest.headers().get(CLIENT_REGISTRATION_ID_HEADER) : UUIDService.getUUID();
        if (LocalCallbackRegistry.responseClientExists(clientId) || LocalCallbackRegistry.forwardClientExists(clientId)) {
            // found locally to indicate to client
            HttpResponse res = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.RESET_CONTENT, ctx.channel().alloc().buffer(0));
            HttpUtil.setContentLength(res, 0);
            ctx.channel().writeAndFlush(res, ctx.channel().newPromise());
        } else {
            handshaker.handshake(ctx.channel(), httpRequest, new DefaultHttpHeaders().add(CLIENT_REGISTRATION_ID_HEADER, clientId), ctx.channel().newPromise()).addListener((ChannelFutureListener) future -> {
                ctx.pipeline().remove(DashboardWebSocketHandler.class);
                ctx.pipeline().remove(MockServerHttpServerCodec.class);
                ctx.pipeline().remove(HttpRequestHandler.class);
                if (MockServerLogger.isEnabled(Level.TRACE)) {
                    mockServerLogger.logEvent(new LogEntry().setLogLevel(Level.TRACE).setMessageFormat("registering client " + clientId));
                }
                webSocketClientRegistry.registerClient(clientId, ctx);
                future.channel().closeFuture().addListener((ChannelFutureListener) closeFuture -> {
                    if (MockServerLogger.isEnabled(Level.TRACE)) {
                        mockServerLogger.logEvent(new LogEntry().setLogLevel(Level.TRACE).setMessageFormat("unregistering callback for client " + clientId));
                    }
                    webSocketClientRegistry.unregisterClient(clientId);
                });
            });
        }
    }
}
Also used : AttributeKey(io.netty.util.AttributeKey) LocalCallbackRegistry(org.mockserver.closurecallback.websocketregistry.LocalCallbackRegistry) WebSocketClientRegistry(org.mockserver.closurecallback.websocketregistry.WebSocketClientRegistry) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) UUIDService(org.mockserver.uuid.UUIDService) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) PortUnificationHandler.isSslEnabledUpstream(org.mockserver.netty.unification.PortUnificationHandler.isSslEnabledUpstream) io.netty.handler.codec.http(io.netty.handler.codec.http) CLIENT_REGISTRATION_ID_HEADER(org.mockserver.closurecallback.websocketclient.WebSocketClient.CLIENT_REGISTRATION_ID_HEADER) HttpRequestHandler(org.mockserver.netty.HttpRequestHandler) HOST(com.google.common.net.HttpHeaders.HOST) DashboardWebSocketHandler(org.mockserver.dashboard.DashboardWebSocketHandler) io.netty.handler.codec.http.websocketx(io.netty.handler.codec.http.websocketx) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) LogEntry(org.mockserver.log.model.LogEntry) Level(org.slf4j.event.Level) HttpState(org.mockserver.mock.HttpState) ChannelHandler(io.netty.channel.ChannelHandler) MockServerHttpServerCodec(org.mockserver.codec.MockServerHttpServerCodec) MockServerLogger(org.mockserver.logging.MockServerLogger) ExceptionHandling.connectionClosedException(org.mockserver.exception.ExceptionHandling.connectionClosedException) HttpRequestHandler(org.mockserver.netty.HttpRequestHandler) DashboardWebSocketHandler(org.mockserver.dashboard.DashboardWebSocketHandler) ChannelFutureListener(io.netty.channel.ChannelFutureListener) LogEntry(org.mockserver.log.model.LogEntry) MockServerHttpServerCodec(org.mockserver.codec.MockServerHttpServerCodec)

Example 2 with HOST

use of com.google.common.net.HttpHeaders.HOST in project mockserver by mock-server.

the class EchoWebSocketServerHandler method upgradeChannel.

private void upgradeChannel(final ChannelHandlerContext ctx, FullHttpRequest httpRequest) {
    handshaker = new WebSocketServerHandshakerFactory((isSecure ? "wss" : "ws") + "://" + httpRequest.headers().get(HOST) + UPGRADE_CHANNEL_FOR_CALLBACK_WEB_SOCKET_URI, null, true, Integer.MAX_VALUE).newHandshaker(httpRequest);
    if (handshaker == null) {
        WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
    } else {
        final String clientId = httpRequest.headers().contains(CLIENT_REGISTRATION_ID_HEADER) ? httpRequest.headers().get(CLIENT_REGISTRATION_ID_HEADER) : UUIDService.getUUID();
        handshaker.handshake(ctx.channel(), httpRequest, new DefaultHttpHeaders().add(CLIENT_REGISTRATION_ID_HEADER, clientId), ctx.channel().newPromise()).addListener((ChannelFutureListener) future -> {
            ctx.pipeline().remove(MockServerHttpServerCodec.class);
            if (MockServerLogger.isEnabled(Level.TRACE)) {
                mockServerLogger.logEvent(new LogEntry().setLogLevel(Level.TRACE).setMessageFormat("registering client " + clientId));
            }
            registeredClients.add(clientId);
            websocketChannels.add(future.channel());
            future.channel().closeFuture().addListener((ChannelFutureListener) closeFuture -> {
                if (MockServerLogger.isEnabled(Level.TRACE)) {
                    mockServerLogger.logEvent(new LogEntry().setLogLevel(Level.TRACE).setMessageFormat("unregistering callback for client " + clientId));
                }
                registeredClients.remove(clientId);
                websocketChannels.remove(future.channel());
            });
        });
    }
}
Also used : AttributeKey(io.netty.util.AttributeKey) UUIDService(org.mockserver.uuid.UUIDService) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) List(java.util.List) CLIENT_REGISTRATION_ID_HEADER(org.mockserver.closurecallback.websocketclient.WebSocketClient.CLIENT_REGISTRATION_ID_HEADER) HOST(com.google.common.net.HttpHeaders.HOST) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) io.netty.handler.codec.http.websocketx(io.netty.handler.codec.http.websocketx) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) LogEntry(org.mockserver.log.model.LogEntry) Level(org.slf4j.event.Level) io.netty.channel(io.netty.channel) MockServerHttpServerCodec(org.mockserver.codec.MockServerHttpServerCodec) MockServerLogger(org.mockserver.logging.MockServerLogger) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) LogEntry(org.mockserver.log.model.LogEntry) MockServerHttpServerCodec(org.mockserver.codec.MockServerHttpServerCodec)

Example 3 with HOST

use of com.google.common.net.HttpHeaders.HOST in project mockserver by mock-server.

the class WebSocketServerHandler method upgradeChannel.

private void upgradeChannel(final ChannelHandlerContext ctx, FullHttpRequest httpRequest) {
    handshaker = new WebSocketServerHandshakerFactory((isSecure ? "wss" : "ws") + "://" + httpRequest.headers().get(HOST) + UPGRADE_CHANNEL_FOR_CALLBACK_WEB_SOCKET_URI, null, true, Integer.MAX_VALUE).newHandshaker(httpRequest);
    if (handshaker == null) {
        WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
    } else {
        final String clientId = httpRequest.headers().contains(CLIENT_REGISTRATION_ID_HEADER) ? httpRequest.headers().get(CLIENT_REGISTRATION_ID_HEADER) : UUIDService.getUUID();
        handshaker.handshake(ctx.channel(), httpRequest, new DefaultHttpHeaders().add(CLIENT_REGISTRATION_ID_HEADER, clientId), ctx.channel().newPromise()).addListener((ChannelFutureListener) future -> {
            ctx.pipeline().remove(MockServerHttpServerCodec.class);
            if (MockServerLogger.isEnabled(Level.TRACE)) {
                mockServerLogger.logEvent(new LogEntry().setLogLevel(Level.TRACE).setMessageFormat("registering client " + clientId));
            }
            registeredClients.add(clientId);
            websocketChannels.add(future.channel());
            future.channel().closeFuture().addListener((ChannelFutureListener) closeFuture -> {
                if (MockServerLogger.isEnabled(Level.TRACE)) {
                    mockServerLogger.logEvent(new LogEntry().setLogLevel(Level.TRACE).setMessageFormat("unregistering callback for client " + clientId));
                }
                registeredClients.remove(clientId);
                websocketChannels.remove(future.channel());
            });
        });
    }
}
Also used : AttributeKey(io.netty.util.AttributeKey) UUIDService(org.mockserver.uuid.UUIDService) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) List(java.util.List) CLIENT_REGISTRATION_ID_HEADER(org.mockserver.closurecallback.websocketclient.WebSocketClient.CLIENT_REGISTRATION_ID_HEADER) HOST(com.google.common.net.HttpHeaders.HOST) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) io.netty.handler.codec.http.websocketx(io.netty.handler.codec.http.websocketx) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) LogEntry(org.mockserver.log.model.LogEntry) Level(org.slf4j.event.Level) io.netty.channel(io.netty.channel) MockServerHttpServerCodec(org.mockserver.codec.MockServerHttpServerCodec) MockServerLogger(org.mockserver.logging.MockServerLogger) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) LogEntry(org.mockserver.log.model.LogEntry) MockServerHttpServerCodec(org.mockserver.codec.MockServerHttpServerCodec)

Aggregations

HOST (com.google.common.net.HttpHeaders.HOST)3 io.netty.handler.codec.http.websocketx (io.netty.handler.codec.http.websocketx)3 AttributeKey (io.netty.util.AttributeKey)3 ReferenceCountUtil (io.netty.util.ReferenceCountUtil)3 CLIENT_REGISTRATION_ID_HEADER (org.mockserver.closurecallback.websocketclient.WebSocketClient.CLIENT_REGISTRATION_ID_HEADER)3 MockServerHttpServerCodec (org.mockserver.codec.MockServerHttpServerCodec)3 LogEntry (org.mockserver.log.model.LogEntry)3 MockServerLogger (org.mockserver.logging.MockServerLogger)3 UUIDService (org.mockserver.uuid.UUIDService)3 Level (org.slf4j.event.Level)3 io.netty.channel (io.netty.channel)2 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)2 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)2 List (java.util.List)2 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 ChannelHandler (io.netty.channel.ChannelHandler)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)1 io.netty.handler.codec.http (io.netty.handler.codec.http)1 LocalCallbackRegistry (org.mockserver.closurecallback.websocketregistry.LocalCallbackRegistry)1