List of usage examples for io.netty.channel ChannelPipeline addAfter
ChannelPipeline addAfter(String baseName, String name, ChannelHandler handler);
From source file:org.dcache.xrootd.tpc.TpcClientConnectHandler.java
License:Open Source License
@Override protected void doOnLoginResponse(ChannelHandlerContext ctx, InboundLoginResponse response) throws XrootdException { int status = response.getStatus(); ChannelId id = ctx.channel().id();/*from w w w . java2s.c om*/ int streamId = client.getStreamId(); XrootdTpcInfo tpcInfo = client.getInfo(); LOGGER.trace("Login response on {}, channel {}, stream {}," + " received; sessionId {}, status {}.", tpcInfo.getSrc(), id, streamId, response.getSessionId(), status); if (status == kXR_ok) { client.setSessionId(response.getSessionId()); List<SecurityInfo> protocols = response.getProtocols(); Map<String, ChannelHandler> handlers = client.getAuthnHandlers(); /* * Name of this handler */ String last = "connect"; ChannelPipeline pipeline = ctx.pipeline(); for (SecurityInfo protocol : protocols) { String name = protocol.getProtocol(); ChannelHandler handler = handlers.get(name); if (handler != null) { pipeline.addAfter(last, name, handler); } LOGGER.trace( "Login to {}, channel {}, stream {}, sessionId {}, " + "adding {} handler to pipeline.", tpcInfo.getSrc(), id, streamId, client.getSessionId(), name); last = name; } LOGGER.trace("Login to {}, channel {}, stream {}," + " succeeded; sessionId {}; " + "passing to next handler.", tpcInfo.getSrc(), id, streamId, client.getSessionId()); ctx.fireChannelRead(response); } else { String error = String.format("Login to %s failed: status %d.", tpcInfo.getSrc(), status); throw new XrootdException(kXR_error, error); } }
From source file:org.eclipse.moquette.server.netty.NettyAcceptor.java
License:Open Source License
private void initializePlainTCPTransport(IMessaging messaging, Properties props) throws IOException { final NettyMQTTHandler handler = new NettyMQTTHandler(); handler.setMessaging(messaging);/* www. java2 s .co m*/ String host = props.getProperty(Constants.HOST_PROPERTY_NAME); int port = Integer.parseInt(props.getProperty(Constants.PORT_PROPERTY_NAME)); initFactory(host, port, new PipelineInitializer() { @Override void init(ChannelPipeline pipeline) { pipeline.addFirst("idleStateHandler", new IdleStateHandler(0, 0, Constants.DEFAULT_CONNECT_TIMEOUT)); pipeline.addAfter("idleStateHandler", "idleEventHandler", new MoquetteIdleTimoutHandler()); //pipeline.addLast("logger", new LoggingHandler("Netty", LogLevel.ERROR)); pipeline.addLast("decoder", new MQTTDecoder()); pipeline.addLast("encoder", new MQTTEncoder()); pipeline.addLast("metrics", new MessageMetricsHandler(m_metricsCollector)); pipeline.addLast("handler", handler); } }); }
From source file:org.eclipse.moquette.server.netty.NettyAcceptor.java
License:Open Source License
private void initializeWebSocketTransport(IMessaging messaging, Properties props) throws IOException { String webSocketPortProp = props.getProperty(Constants.WEB_SOCKET_PORT_PROPERTY_NAME); if (webSocketPortProp == null) { //Do nothing no WebSocket configured LOG.info("WebSocket is disabled"); return;/*from w w w . j a v a 2s .c o m*/ } int port = Integer.parseInt(webSocketPortProp); final NettyMQTTHandler handler = new NettyMQTTHandler(); handler.setMessaging(messaging); String host = props.getProperty(Constants.HOST_PROPERTY_NAME); initFactory(host, port, new PipelineInitializer() { @Override void init(ChannelPipeline pipeline) { pipeline.addLast("httpEncoder", new HttpResponseEncoder()); pipeline.addLast("httpDecoder", new HttpRequestDecoder()); pipeline.addLast("aggregator", new HttpObjectAggregator(65536)); pipeline.addLast("webSocketHandler", new WebSocketServerProtocolHandler("/mqtt"/*"/mqtt"*/, "mqttv3.1, mqttv3.1.1")); //pipeline.addLast("webSocketHandler", new WebSocketServerProtocolHandler(null, "mqtt")); pipeline.addLast("ws2bytebufDecoder", new WebSocketFrameToByteBufDecoder()); pipeline.addLast("bytebuf2wsEncoder", new ByteBufToWebSocketFrameEncoder()); pipeline.addFirst("idleStateHandler", new IdleStateHandler(0, 0, Constants.DEFAULT_CONNECT_TIMEOUT)); pipeline.addAfter("idleStateHandler", "idleEventHandler", new MoquetteIdleTimoutHandler()); pipeline.addLast("decoder", new MQTTDecoder()); pipeline.addLast("encoder", new MQTTEncoder()); pipeline.addLast("metrics", new MessageMetricsHandler(m_metricsCollector)); pipeline.addLast("handler", handler); } }); }
From source file:org.eclipse.moquette.server.netty.NettyAcceptor.java
License:Open Source License
private void initializeSSLTCPTransport(IMessaging messaging, Properties props) throws IOException { String sslPortProp = props.getProperty(Constants.SSL_PORT_PROPERTY_NAME); if (sslPortProp == null) { //Do nothing no SSL configured LOG.info("SSL is disabled"); return;/*from w w w . j a va 2s . c o m*/ } final String jksPath = props.getProperty(Constants.JKS_PATH_PROPERTY_NAME); if (jksPath == null || jksPath.isEmpty()) { //key_store_password or key_manager_password are empty LOG.warn("You have configured the SSL port but not the jks_path, SSL not started"); return; } //if we have the port also the jks then keyStorePassword and keyManagerPassword //has to be defined final String keyStorePassword = props.getProperty(Constants.KEY_STORE_PASSWORD_PROPERTY_NAME); final String keyManagerPassword = props.getProperty(Constants.KEY_MANAGER_PASSWORD_PROPERTY_NAME); if (keyStorePassword == null || keyStorePassword.isEmpty()) { //key_store_password or key_manager_password are empty LOG.warn("You have configured the SSL port but not the key_store_password, SSL not started"); return; } if (keyManagerPassword == null || keyManagerPassword.isEmpty()) { //key_manager_password or key_manager_password are empty LOG.warn("You have configured the SSL port but not the key_manager_password, SSL not started"); return; } int sslPort = Integer.parseInt(sslPortProp); String host = props.getProperty(Constants.HOST_PROPERTY_NAME); LOG.info("Starting SSL on port {} using keystore at {}", sslPort, jksPath); final NettyMQTTHandler handler = new NettyMQTTHandler(); handler.setMessaging(messaging); initFactory(host, sslPort, new PipelineInitializer() { @Override void init(ChannelPipeline pipeline) throws Exception { InputStream jksInputStream = jksDatastore(jksPath); SSLContext serverContext = SSLContext.getInstance("TLS"); final KeyStore ks = KeyStore.getInstance("JKS"); ks.load(jksInputStream, keyStorePassword.toCharArray()); final KeyManagerFactory kmf = KeyManagerFactory .getInstance(KeyManagerFactory.getDefaultAlgorithm()); kmf.init(ks, keyManagerPassword.toCharArray()); serverContext.init(kmf.getKeyManagers(), null, null); SSLEngine engine = serverContext.createSSLEngine(); engine.setUseClientMode(false); final SslHandler sslHandler = new SslHandler(engine); pipeline.addLast("ssl", sslHandler); //pipeline.addFirst("metrics", new BytesMetricsHandler(m_metricsCollector)); pipeline.addFirst("idleStateHandler", new IdleStateHandler(0, 0, Constants.DEFAULT_CONNECT_TIMEOUT)); pipeline.addAfter("idleStateHandler", "idleEventHandler", new MoquetteIdleTimoutHandler()); //pipeline.addLast("logger", new LoggingHandler("Netty", LogLevel.ERROR)); pipeline.addLast("decoder", new MQTTDecoder()); pipeline.addLast("encoder", new MQTTEncoder()); pipeline.addLast("metrics", new MessageMetricsHandler(m_metricsCollector)); pipeline.addLast("handler", handler); } }); }
From source file:org.emsg.smart_connector.netty.acceptor.NettyAcceptor.java
License:Open Source License
private void initializePlainTCPTransport(final NettyMQTTHandler handler, IConfig props) throws IOException { final SmartConnectorIdleTimeoutHandler timeoutHandler = new SmartConnectorIdleTimeoutHandler(); String host = props.getProperty(BrokerConstants.HOST_PROPERTY_NAME); int port = Integer.parseInt(props.getProperty(BrokerConstants.PORT_PROPERTY_NAME)); initFactory(host, port, new PipelineInitializer() { @Override/*from w w w. jav a 2 s. c o m*/ void init(ChannelPipeline pipeline) { pipeline.addFirst("idleStateHandler", new IdleStateHandler(0, 0, Constants.DEFAULT_CONNECT_TIMEOUT)); pipeline.addAfter("idleStateHandler", "idleEventHandler", timeoutHandler); //pipeline.addLast("logger", new LoggingHandler("Netty", LogLevel.ERROR)); pipeline.addFirst("bytemetrics", new BytesMetricsHandler(m_bytesMetricsCollector)); pipeline.addLast("decoder", new MQTTDecoder()); pipeline.addLast("encoder", new MQTTEncoder()); pipeline.addLast("metrics", new MessageMetricsHandler(m_metricsCollector)); pipeline.addLast("handler", handler); } }); }
From source file:org.emsg.smart_connector.netty.acceptor.NettyAcceptor.java
License:Open Source License
private void initializeWebSocketTransport(final NettyMQTTHandler handler, IConfig props) throws IOException { String webSocketPortProp = props.getProperty(BrokerConstants.WEB_SOCKET_PORT_PROPERTY_NAME); if (webSocketPortProp == null) { //Do nothing no WebSocket configured LOG.info("WebSocket is disabled"); return;/* ww w .java 2s. c o m*/ } int port = Integer.parseInt(webSocketPortProp); final SmartConnectorIdleTimeoutHandler timeoutHandler = new SmartConnectorIdleTimeoutHandler(); String host = props.getProperty(BrokerConstants.HOST_PROPERTY_NAME); initFactory(host, port, new PipelineInitializer() { @Override void init(ChannelPipeline pipeline) { pipeline.addLast("httpEncoder", new HttpResponseEncoder()); pipeline.addLast("httpDecoder", new HttpRequestDecoder()); pipeline.addLast("aggregator", new HttpObjectAggregator(65536)); pipeline.addLast("webSocketHandler", new WebSocketServerProtocolHandler("/mqtt", "mqtt, mqttv3.1, mqttv3.1.1")); pipeline.addLast("ws2bytebufDecoder", new WebSocketFrameToByteBufDecoder()); pipeline.addLast("bytebuf2wsEncoder", new ByteBufToWebSocketFrameEncoder()); pipeline.addFirst("idleStateHandler", new IdleStateHandler(0, 0, Constants.DEFAULT_CONNECT_TIMEOUT)); pipeline.addAfter("idleStateHandler", "idleEventHandler", timeoutHandler); pipeline.addFirst("bytemetrics", new BytesMetricsHandler(m_bytesMetricsCollector)); pipeline.addLast("decoder", new MQTTDecoder()); pipeline.addLast("encoder", new MQTTEncoder()); pipeline.addLast("metrics", new MessageMetricsHandler(m_metricsCollector)); pipeline.addLast("handler", handler); } }); }
From source file:org.emsg.smart_connector.netty.acceptor.NettyAcceptor.java
License:Open Source License
private void initializeSSLTCPTransport(final NettyMQTTHandler handler, IConfig props, final SSLContext sslContext) throws IOException { String sslPortProp = props.getProperty(BrokerConstants.SSL_PORT_PROPERTY_NAME); if (sslPortProp == null) { //Do nothing no SSL configured LOG.info("SSL is disabled"); return;/*from ww w . j a va 2 s . c o m*/ } int sslPort = Integer.parseInt(sslPortProp); LOG.info("Starting SSL on port {}", sslPort); final SmartConnectorIdleTimeoutHandler timeoutHandler = new SmartConnectorIdleTimeoutHandler(); String host = props.getProperty(BrokerConstants.HOST_PROPERTY_NAME); initFactory(host, sslPort, new PipelineInitializer() { @Override void init(ChannelPipeline pipeline) throws Exception { pipeline.addLast("ssl", createSslHandler(sslContext)); pipeline.addFirst("idleStateHandler", new IdleStateHandler(0, 0, Constants.DEFAULT_CONNECT_TIMEOUT)); pipeline.addAfter("idleStateHandler", "idleEventHandler", timeoutHandler); //pipeline.addLast("logger", new LoggingHandler("Netty", LogLevel.ERROR)); pipeline.addFirst("bytemetrics", new BytesMetricsHandler(m_bytesMetricsCollector)); pipeline.addLast("decoder", new MQTTDecoder()); pipeline.addLast("encoder", new MQTTEncoder()); pipeline.addLast("metrics", new MessageMetricsHandler(m_metricsCollector)); pipeline.addLast("handler", handler); } }); }
From source file:org.emsg.smart_connector.netty.acceptor.NettyAcceptor.java
License:Open Source License
private void initializeWSSTransport(final NettyMQTTHandler handler, IConfig props, final SSLContext sslContext) throws IOException { String sslPortProp = props.getProperty(BrokerConstants.WSS_PORT_PROPERTY_NAME); if (sslPortProp == null) { //Do nothing no SSL configured LOG.info("SSL is disabled"); return;//ww w .ja v a 2 s .c o m } int sslPort = Integer.parseInt(sslPortProp); final SmartConnectorIdleTimeoutHandler timeoutHandler = new SmartConnectorIdleTimeoutHandler(); String host = props.getProperty(BrokerConstants.HOST_PROPERTY_NAME); initFactory(host, sslPort, new PipelineInitializer() { @Override void init(ChannelPipeline pipeline) throws Exception { pipeline.addLast("ssl", createSslHandler(sslContext)); pipeline.addLast("httpEncoder", new HttpResponseEncoder()); pipeline.addLast("httpDecoder", new HttpRequestDecoder()); pipeline.addLast("aggregator", new HttpObjectAggregator(65536)); pipeline.addLast("webSocketHandler", new WebSocketServerProtocolHandler("/mqtt", "mqtt mqttv3.1, mqttv3.1.1")); pipeline.addLast("ws2bytebufDecoder", new WebSocketFrameToByteBufDecoder()); pipeline.addLast("bytebuf2wsEncoder", new ByteBufToWebSocketFrameEncoder()); pipeline.addFirst("idleStateHandler", new IdleStateHandler(0, 0, Constants.DEFAULT_CONNECT_TIMEOUT)); pipeline.addAfter("idleStateHandler", "idleEventHandler", timeoutHandler); pipeline.addFirst("bytemetrics", new BytesMetricsHandler(m_bytesMetricsCollector)); pipeline.addLast("decoder", new MQTTDecoder()); pipeline.addLast("encoder", new MQTTEncoder()); pipeline.addLast("metrics", new MessageMetricsHandler(m_metricsCollector)); pipeline.addLast("handler", handler); } }); }
From source file:org.glassfish.jersey.netty.httpserver.JerseyServerInitializer.java
License:Open Source License
/** * Configure the pipeline for a cleartext upgrade from HTTP to HTTP/2. *//*from w w w . j a v a 2 s .c om*/ private void configureClearText(SocketChannel ch) { final ChannelPipeline p = ch.pipeline(); final HttpServerCodec sourceCodec = new HttpServerCodec(); p.addLast(sourceCodec); p.addLast(new HttpServerUpgradeHandler(sourceCodec, new HttpServerUpgradeHandler.UpgradeCodecFactory() { @Override public HttpServerUpgradeHandler.UpgradeCodec newUpgradeCodec(CharSequence protocol) { if (AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, protocol)) { return new Http2ServerUpgradeCodec( new Http2Codec(true, new JerseyHttp2ServerHandler(baseUri, container))); } else { return null; } } })); p.addLast(new SimpleChannelInboundHandler<HttpMessage>() { @Override protected void channelRead0(ChannelHandlerContext ctx, HttpMessage msg) throws Exception { // If this handler is hit then no upgrade has been attempted and the client is just talking HTTP. // "Directly talking: " + msg.protocolVersion() + " (no upgrade was attempted)"); ChannelPipeline pipeline = ctx.pipeline(); ChannelHandlerContext thisCtx = pipeline.context(this); pipeline.addAfter(thisCtx.name(), null, new JerseyServerHandler(baseUri, container)); pipeline.replace(this, null, new ChunkedWriteHandler()); ctx.fireChannelRead(msg); } }); }
From source file:org.janusgraph.graphdb.tinkerpop.gremlin.server.handler.SaslAndHMACAuthenticationHandlerTest.java
License:Apache License
@Test public void testHttpChannelReadWhenAuthenticatorHasNotBeenAdded() throws Exception { final HMACAuthenticator hmacAuth = createMock(HMACAuthenticator.class); final SaslAndHMACAuthenticator authenticator = createMock(SaslAndHMACAuthenticator.class); final ChannelHandlerContext ctx = createMock(ChannelHandlerContext.class); final ChannelPipeline pipeline = createMock(ChannelPipeline.class); final HttpMessage msg = createMock(HttpMessage.class); final HttpHeaders headers = createMock(HttpHeaders.class); expect(authenticator.getHMACAuthenticator()).andReturn(hmacAuth); expect(authenticator.getSimpleAuthenticator()).andReturn(createMock(JanusGraphSimpleAuthenticator.class)); expect(ctx.pipeline()).andReturn(pipeline).times(2); expect(pipeline.get("hmac_authenticator")).andReturn(null); expect(pipeline.addAfter(eq(PIPELINE_AUTHENTICATOR), eq("hmac_authenticator"), isA(ChannelHandler.class))) .andReturn(null);/* www . jav a 2 s . com*/ expect(msg.headers()).andReturn(headers).times(2); expect(headers.get(isA(String.class))).andReturn(null).times(2); expect(ctx.fireChannelRead(eq(msg))).andReturn(ctx); replayAll(); final SaslAndHMACAuthenticationHandler handler = new SaslAndHMACAuthenticationHandler(authenticator, null); handler.channelRead(ctx, msg); }