@Override public void messageReceived(final ChannelHandlerContext ctx, final FullHttpRequest request) throws Exception { final String path = new QueryStringDecoder(request.getUri()).path(); for (SockJsServiceFactory factory : factories.values()) { if (path.startsWith(factory.config().prefix())) { handleService(factory, request, ctx); return; } } writeNotFoundResponse(request, ctx); }
private static void writeNotFoundResponse(final HttpRequest request, final ChannelHandlerContext ctx) { final FullHttpResponse response = new DefaultFullHttpResponse(request.getProtocolVersion(), NOT_FOUND, Unpooled.copiedBuffer("Not found", CharsetUtil.UTF_8)); writeResponse(ctx.channel(), request, response); }
private static void handleService(final SockJsServiceFactory factory, final FullHttpRequest request, final ChannelHandlerContext ctx) throws Exception { if (logger.isDebugEnabled()) { logger.debug("RequestUri : [{}]", request.getUri()); } final String pathWithoutPrefix = request.getUri().replaceFirst(factory.config().prefix(), ""); final String path = new QueryStringDecoder(pathWithoutPrefix).path(); if (Greeting.matches(path)) { writeResponse(ctx.channel(), request, Greeting.response(request)); } else if (Info.matches(path)) { writeResponse(ctx.channel(), request, Info.response(factory.config(), request)); } else if (Iframe.matches(path)) { writeResponse(ctx.channel(), request, Iframe.response(factory.config(), request)); } else if (Transports.Type.WEBSOCKET.path().equals(path)) { addTransportHandler(new RawWebSocketTransport(factory.config(), factory.create()), ctx); ctx.fireChannelRead(request.retain()); } else { final PathParams sessionPath = matches(path); if (sessionPath.matches()) { handleSession(factory, request, ctx, sessionPath); } else { writeNotFoundResponse(request, ctx); } } }
switch (pathParams.transport()) { case XHR: addTransportHandler(new XhrPollingTransport(factory.config(), request), ctx); addSessionHandler(new PollingSessionState(sessions, getSession(factory, pathParams.sessionId())), ctx); break; case JSONP: addTransportHandler(new JsonpPollingTransport(factory.config(), request), ctx); addSessionHandler(new PollingSessionState(sessions, getSession(factory, pathParams.sessionId())), ctx); break; case XHR_SEND: checkSessionExists(pathParams.sessionId(), request); addTransportHandler(new XhrSendTransport(factory.config()), ctx); addSessionHandler(new SendingSessionState(sessions, sessions.get(pathParams.sessionId())), ctx); break; case XHR_STREAMING: addTransportHandler(new XhrStreamingTransport(factory.config(), request), ctx); addSessionHandler(new StreamingSessionState(sessions, getSession(factory, pathParams.sessionId())), ctx); break; case EVENTSOURCE: addTransportHandler(new EventSourceTransport(factory.config(), request), ctx); addSessionHandler(new StreamingSessionState(sessions, getSession(factory, pathParams.sessionId())), ctx); break; case HTMLFILE: addTransportHandler(new HtmlFileTransport(factory.config(), request), ctx); addSessionHandler(new StreamingSessionState(sessions, getSession(factory, pathParams.sessionId())), ctx); break; case JSONP_SEND: checkSessionExists(pathParams.sessionId(), request); addTransportHandler(new JsonpSendTransport(factory.config()), ctx);
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { if (cause instanceof SessionNotFoundException) { final SessionNotFoundException se = (SessionNotFoundException) cause; logger.debug("Could not find session [{}]", se.sessionId()); writeNotFoundResponse(se.httpRequest(), ctx); } else { logger.error("exception caught:", cause); ctx.fireExceptionCaught(cause); } }
@Override protected void initChannel(final SocketChannel socketChannel) throws Exception { final ChannelPipeline pipeline = socketChannel.pipeline(); if (sockjsConfig.isTls()) { final SSLEngine engine = sslContext.createSSLEngine(); engine.setUseClientMode(false); pipeline.addLast(new SslHandler(engine)); } pipeline.addLast(new HttpServerCodec()); pipeline.addLast(new HttpObjectAggregator(65536)); final DefaultSimplePushServer simplePushServer = new DefaultSimplePushServer(datastore, simplePushConfig, privateKey); pipeline.addLast(new NotificationHandler(simplePushServer)); pipeline.addLast(new CorsInboundHandler()); pipeline.addLast(new SockJsHandler(new SimplePushServiceFactory(sockjsConfig, simplePushServer))); pipeline.addLast(backgroundGroup, new UserAgentReaperHandler(simplePushServer)); pipeline.addLast(new CorsOutboundHandler()); }
private static void handleService(final SockJsServiceFactory factory, final FullHttpRequest request, final ChannelHandlerContext ctx) throws Exception { if (logger.isDebugEnabled()) { logger.debug("RequestUri : [{}]", request.getUri()); } final String pathWithoutPrefix = request.getUri().replaceFirst(factory.config().prefix(), ""); final String path = new QueryStringDecoder(pathWithoutPrefix).path(); if (Greeting.matches(path)) { writeResponse(ctx.channel(), request, Greeting.response(request)); } else if (Info.matches(path)) { writeResponse(ctx.channel(), request, Info.response(factory.config(), request)); } else if (Iframe.matches(path)) { writeResponse(ctx.channel(), request, Iframe.response(factory.config(), request)); } else if (Transports.Type.WEBSOCKET.path().equals(path)) { addTransportHandler(new RawWebSocketTransport(factory.config(), factory.create()), ctx); ctx.fireChannelRead(request.retain()); } else { final PathParams sessionPath = matches(path); if (sessionPath.matches()) { handleSession(factory, request, ctx, sessionPath); } else { writeNotFoundResponse(request, ctx); } } }
switch (pathParams.transport()) { case XHR: addTransportHandler(new XhrPollingTransport(factory.config(), request), ctx); addSessionHandler(new PollingSessionState(sessions, getSession(factory, pathParams.sessionId())), ctx); break; case JSONP: addTransportHandler(new JsonpPollingTransport(factory.config(), request), ctx); addSessionHandler(new PollingSessionState(sessions, getSession(factory, pathParams.sessionId())), ctx); break; case XHR_SEND: checkSessionExists(pathParams.sessionId(), request); addTransportHandler(new XhrSendTransport(factory.config()), ctx); addSessionHandler(new SendingSessionState(sessions, sessions.get(pathParams.sessionId())), ctx); break; case XHR_STREAMING: addTransportHandler(new XhrStreamingTransport(factory.config(), request), ctx); addSessionHandler(new StreamingSessionState(sessions, getSession(factory, pathParams.sessionId())), ctx); break; case EVENTSOURCE: addTransportHandler(new EventSourceTransport(factory.config(), request), ctx); addSessionHandler(new StreamingSessionState(sessions, getSession(factory, pathParams.sessionId())), ctx); break; case HTMLFILE: addTransportHandler(new HtmlFileTransport(factory.config(), request), ctx); addSessionHandler(new StreamingSessionState(sessions, getSession(factory, pathParams.sessionId())), ctx); break; case JSONP_SEND: checkSessionExists(pathParams.sessionId(), request); addTransportHandler(new JsonpSendTransport(factory.config()), ctx);
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { if (cause instanceof SessionNotFoundException) { final SessionNotFoundException se = (SessionNotFoundException) cause; logger.debug("Could not find session [{}]", se.sessionId()); writeNotFoundResponse(se.httpRequest(), ctx); } else { logger.error("exception caught:", cause); ctx.fireExceptionCaught(cause); } }
@Override protected void initChannel(final SocketChannel socketChannel) throws Exception { final ChannelPipeline pipeline = socketChannel.pipeline(); if (sockjsConfig.isTls()) { final SSLEngine engine = sslContext.createSSLEngine(); engine.setUseClientMode(false); pipeline.addLast(new SslHandler(engine)); } pipeline.addLast(new HttpServerCodec()); pipeline.addLast(new HttpObjectAggregator(65536)); final DefaultSimplePushServer simplePushServer = new DefaultSimplePushServer(datastore, simplePushConfig, privateKey); pipeline.addLast(new NotificationHandler(simplePushServer)); pipeline.addLast(new CorsInboundHandler()); pipeline.addLast(new SockJsHandler(new SimplePushServiceFactory(sockjsConfig, simplePushServer))); pipeline.addLast(backgroundGroup, new UserAgentReaperHandler(simplePushServer)); pipeline.addLast(new CorsOutboundHandler()); }
@Override public void messageReceived(final ChannelHandlerContext ctx, final FullHttpRequest request) throws Exception { final String path = new QueryStringDecoder(request.getUri()).path(); for (SockJsServiceFactory factory : factories.values()) { if (path.startsWith(factory.config().prefix())) { handleService(factory, request, ctx); return; } } writeNotFoundResponse(request, ctx); }
private static void writeNotFoundResponse(final HttpRequest request, final ChannelHandlerContext ctx) { final FullHttpResponse response = new DefaultFullHttpResponse(request.getProtocolVersion(), NOT_FOUND, Unpooled.copiedBuffer("Not found", CharsetUtil.UTF_8)); writeResponse(ctx.channel(), request, response); }