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);
});
});
}
}
}
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());
});
});
}
}
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());
});
});
}
}
Aggregations