@Override public void channelRead(final ChannelHandlerContext ctx, final Object mesg) throws Exception { if (mesg instanceof FullHttpRequest) { final FullHttpRequest req = (FullHttpRequest) mesg; if ((req.method() == HttpMethod.GET) && (PushProtocol.SSE.getPath().equals(req.uri()))) { ctx.pipeline().fireUserEventTriggered(PushProtocol.SSE.getHandshakeCompleteEvent()); final DefaultHttpResponse resp = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.OK); final HttpHeaders headers = resp.headers(); headers.add("Connection", "keep-alive"); headers.add("Content-Type", "text/event-stream"); headers.add("Transfer-Encoding", "chunked"); final ChannelFuture cf = ctx.channel().writeAndFlush(resp); cf.addListener(future -> { if (future.isSuccess()) { ChannelPipeline pipeline = ctx.pipeline(); if (pipeline.get(HttpObjectAggregator.class) != null) { pipeline.remove(HttpObjectAggregator.class); } if (pipeline.get(HttpContentCompressor.class) != null) { pipeline.remove(HttpContentCompressor.class); } final String reconnetInterval = "retry: " + SSE_RETRY_BASE_INTERVAL.get() + "\r\n\r\n"; ctx.writeAndFlush(reconnetInterval); } }); } } }
@Override public final void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { this.ctx = ctx; if (! destroyed.get()) { if (evt == pushProtocol.getHandshakeCompleteEvent()) { pushConnection = new PushConnection(pushProtocol, ctx); // Unauthenticated connection, wait for small amount of time for a client to send auth token in // a first web socket frame, otherwise close connection ctx.executor().schedule(this::closeIfNotAuthenticated, UNAUTHENTICATED_CONN_TTL.get(), TimeUnit.SECONDS); logger.debug("WebSocket handshake complete."); } else if (evt instanceof PushUserAuth) { authEvent = (PushUserAuth) evt; if (authEvent.isSuccess()) { logger.debug("registering client {}", authEvent); ctx.pipeline().remove(PushAuthHandler.NAME); registerClient(ctx, authEvent, pushConnection, pushConnectionRegistry); logger.debug("Authentication complete {}", authEvent); } else { pushProtocol.sendErrorAndClose(ctx,1008, "Auth failed"); } } } super.userEventTriggered(ctx, evt); }
@Override public void channelRead(final ChannelHandlerContext ctx, final Object mesg) throws Exception { if (mesg instanceof FullHttpRequest) { final FullHttpRequest req = (FullHttpRequest) mesg; if ((req.method() == HttpMethod.GET) && (PushProtocol.SSE.getPath().equals(req.uri()))) { ctx.pipeline().fireUserEventTriggered(PushProtocol.SSE.getHandshakeCompleteEvent()); final DefaultHttpResponse resp = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.OK); final HttpHeaders headers = resp.headers(); headers.add("Connection", "keep-alive"); headers.add("Content-Type", "text/event-stream"); headers.add("Transfer-Encoding", "chunked"); final ChannelFuture cf = ctx.channel().writeAndFlush(resp); cf.addListener(future -> { if (future.isSuccess()) { ChannelPipeline pipeline = ctx.pipeline(); if (pipeline.get(HttpObjectAggregator.class) != null) { pipeline.remove(HttpObjectAggregator.class); } if (pipeline.get(HttpContentCompressor.class) != null) { pipeline.remove(HttpContentCompressor.class); } final String reconnetInterval = "retry: " + SSE_RETRY_BASE_INTERVAL.get() + "\r\n\r\n"; ctx.writeAndFlush(reconnetInterval); } }); } } }
@Override public final void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { this.ctx = ctx; if (! destroyed.get()) { if (evt == pushProtocol.getHandshakeCompleteEvent()) { pushConnection = new PushConnection(pushProtocol, ctx); // Unauthenticated connection, wait for small amount of time for a client to send auth token in // a first web socket frame, otherwise close connection ctx.executor().schedule(this::closeIfNotAuthenticated, UNAUTHENTICATED_CONN_TTL.get(), TimeUnit.SECONDS); logger.debug("WebSocket handshake complete."); } else if (evt instanceof PushUserAuth) { authEvent = (PushUserAuth) evt; if (authEvent.isSuccess()) { logger.debug("registering client {}", authEvent); ctx.pipeline().remove(PushAuthHandler.NAME); registerClient(ctx, authEvent, pushConnection, pushConnectionRegistry); logger.debug("Authentication complete {}", authEvent); } else { pushProtocol.sendErrorAndClose(ctx,1008, "Auth failed"); } } } super.userEventTriggered(ctx, evt); }
@Override public final void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { this.ctx = ctx; if (! destroyed.get()) { if (evt == pushProtocol.getHandshakeCompleteEvent()) { pushConnection = new PushConnection(pushProtocol, ctx); // Unauthenticated connection, wait for small amount of time for a client to send auth token in // a first web socket frame, otherwise close connection ctx.executor().schedule(this::closeIfNotAuthenticated, UNAUTHENTICATED_CONN_TTL.get(), TimeUnit.SECONDS); logger.debug("WebSocket handshake complete."); } else if (evt instanceof PushUserAuth) { authEvent = (PushUserAuth) evt; if (authEvent.isSuccess()) { logger.debug("registering client {}", authEvent); ctx.pipeline().remove(PushAuthHandler.NAME); registerClient(ctx, authEvent, pushConnection, pushConnectionRegistry); logger.debug("Authentication complete {}", authEvent); } else { sendErrorAndClose(1008, "Auth Failed"); } } } super.userEventTriggered(ctx, evt); }