List of usage examples for io.netty.util Attribute get
T get();
From source file:com.ebay.jetstream.messaging.transport.netty.serializer.NettyObjectEncoder.java
License:MIT License
@Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { Attribute<Boolean> attr = ctx.channel().attr(EventProducer.m_kskey); Boolean enableKryo = attr.get(); if ((enableKryo != null) && (enableKryo == true)) ctx.write(msg, promise);/*from w w w . ja v a 2s . co m*/ else super.write(ctx, msg, promise); }
From source file:com.github.netfreer.shadowducks.client.handler.SocksServerConnectHandler.java
License:Apache License
@Override public void channelRead0(final ChannelHandlerContext ctx, final SocksMessage message) throws Exception { if (message instanceof Socks4CommandRequest) { final Socks4CommandRequest request = (Socks4CommandRequest) message; Promise<Channel> promise = ctx.executor().newPromise(); promise.addListener(new FutureListener<Channel>() { @Override//from w ww. ja v a 2s . co m public void operationComplete(final Future<Channel> future) throws Exception { final Channel outboundChannel = future.getNow(); if (future.isSuccess()) { ChannelFuture responseFuture = ctx.channel() .writeAndFlush(new DefaultSocks4CommandResponse(Socks4CommandStatus.SUCCESS)); responseFuture.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture channelFuture) { ctx.pipeline().remove(SocksServerConnectHandler.this); outboundChannel.pipeline().addLast(new RelayHandler(ctx.channel())); ctx.pipeline().addLast(new RelayHandler(outboundChannel)); } }); } else { ctx.channel().writeAndFlush( new DefaultSocks4CommandResponse(Socks4CommandStatus.REJECTED_OR_FAILED)); SocksServerUtils.closeOnFlush(ctx.channel()); } } }); final Channel inboundChannel = ctx.channel(); b.group(inboundChannel.eventLoop()).channel(NioSocketChannel.class) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000).option(ChannelOption.SO_KEEPALIVE, true) .handler(new DirectClientHandler(promise)); b.connect(request.dstAddr(), request.dstPort()).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { // Connection established use handler provided results } else { // Close the connection if the connection attempt has failed. ctx.channel().writeAndFlush( new DefaultSocks4CommandResponse(Socks4CommandStatus.REJECTED_OR_FAILED)); SocksServerUtils.closeOnFlush(ctx.channel()); } } }); } else if (message instanceof Socks5CommandRequest) { final Socks5CommandRequest request = (Socks5CommandRequest) message; Promise<Channel> promise = ctx.executor().newPromise(); promise.addListener(new FutureListener<Channel>() { @Override public void operationComplete(final Future<Channel> future) throws Exception { final Channel outboundChannel = future.getNow(); if (future.isSuccess()) { ChannelFuture responseFuture = ctx.channel() .writeAndFlush(new DefaultSocks5CommandResponse(Socks5CommandStatus.SUCCESS, request.dstAddrType(), request.dstAddr(), request.dstPort())); responseFuture.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture channelFuture) { ctx.pipeline().remove(SocksServerConnectHandler.this); outboundChannel.pipeline().addLast(new RelayHandler(ctx.channel())); ctx.pipeline().addLast(new RelayHandler(outboundChannel)); } }); } else { ctx.channel().writeAndFlush(new DefaultSocks5CommandResponse(Socks5CommandStatus.FAILURE, request.dstAddrType())); SocksServerUtils.closeOnFlush(ctx.channel()); } } }); final Channel inboundChannel = ctx.channel(); b.group(inboundChannel.eventLoop()).channel(NioSocketChannel.class) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000).option(ChannelOption.SO_KEEPALIVE, true) .handler(new DirectClientHandler(promise)); b.connect(request.dstAddr(), request.dstPort()).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { Attribute<Long> beginTimeAttr = ctx.channel().attr(AttrKeys.CHANNEL_BEGIN_TIME); final long parseTime = beginTimeAttr.get(); long usedTime = System.currentTimeMillis() - parseTime; if (future.isSuccess()) { // Connection established use handler provided results logger.info("connect {}:{} success, use time {} millis.", request.dstAddr(), request.dstPort(), usedTime); } else { // Close the connection if the connection attempt has failed. ctx.channel().writeAndFlush(new DefaultSocks5CommandResponse(Socks5CommandStatus.FAILURE, request.dstAddrType())); SocksServerUtils.closeOnFlush(ctx.channel()); logger.info("connect {}:{} failure, use time {} millis.", request.dstAddr(), request.dstPort(), usedTime); } beginTimeAttr.set(null); } }); } else { ctx.close(); } }
From source file:com.github.sylvek.wsmqttfwd.decoder.ConnectDecoder.java
License:Open Source License
@Override public ConnectMessage decode(AttributeMap ctx, ByteBuf in) throws UnsupportedEncodingException { in.resetReaderIndex();//from ww w .j a v a 2 s.c o m //Common decoding part ConnectMessage message = new ConnectMessage(); if (!decodeCommonHeader(message, 0x00, in)) { in.resetReaderIndex(); return null; } int remainingLength = message.getRemainingLength(); int start = in.readerIndex(); int protocolNameLen = in.readUnsignedShort(); byte[] encProtoName; String protoName; Attribute<Integer> versionAttr = ctx.attr(PROTOCOL_VERSION); switch (protocolNameLen) { case 6: //MQTT version 3.1 "MQIsdp" //ProtocolName 8 bytes or 6 bytes if (in.readableBytes() < 10) { in.resetReaderIndex(); return null; } encProtoName = new byte[6]; in.readBytes(encProtoName); protoName = new String(encProtoName, "UTF-8"); if (!"MQIsdp".equals(protoName)) { in.resetReaderIndex(); throw new CorruptedFrameException("Invalid protoName: " + protoName); } message.setProtocolName(protoName); versionAttr.set((int) Utils.VERSION_3_1); break; case 4: //MQTT version 3.1.1 "MQTT" //ProtocolName 6 bytes if (in.readableBytes() < 8) { in.resetReaderIndex(); return null; } encProtoName = new byte[4]; in.readBytes(encProtoName); protoName = new String(encProtoName, "UTF-8"); if (!"MQTT".equals(protoName)) { in.resetReaderIndex(); throw new CorruptedFrameException("Invalid protoName: " + protoName); } message.setProtocolName(protoName); versionAttr.set((int) Utils.VERSION_3_1_1); break; default: //protocol broken throw new CorruptedFrameException("Invalid protoName size: " + protocolNameLen); } //ProtocolVersion 1 byte (value 0x03 for 3.1, 0x04 for 3.1.1) message.setProtocolVersion(in.readByte()); if (message.getProtocolVersion() == Utils.VERSION_3_1_1) { //if 3.1.1, check the flags (dup, retain and qos == 0) if (message.isDupFlag() || message.isRetainFlag() || message.getQos() != AbstractMessage.QOSType.MOST_ONE) { throw new CorruptedFrameException("Received a CONNECT with fixed header flags != 0"); } //check if this is another connect from the same client on the same session Attribute<Boolean> connectAttr = ctx.attr(ConnectDecoder.CONNECT_STATUS); Boolean alreadyConnected = connectAttr.get(); if (alreadyConnected == null) { //never set connectAttr.set(true); } else if (alreadyConnected) { throw new CorruptedFrameException("Received a second CONNECT on the same network connection"); } } //Connection flag byte connFlags = in.readByte(); if (message.getProtocolVersion() == Utils.VERSION_3_1_1) { if ((connFlags & 0x01) != 0) { //bit(0) of connection flags is != 0 throw new CorruptedFrameException("Received a CONNECT with connectionFlags[0(bit)] != 0"); } } boolean cleanSession = ((connFlags & 0x02) >> 1) == 1; boolean willFlag = ((connFlags & 0x04) >> 2) == 1; byte willQos = (byte) ((connFlags & 0x18) >> 3); if (willQos > 2) { in.resetReaderIndex(); throw new CorruptedFrameException("Expected will QoS in range 0..2 but found: " + willQos); } boolean willRetain = ((connFlags & 0x20) >> 5) == 1; boolean passwordFlag = ((connFlags & 0x40) >> 6) == 1; boolean userFlag = ((connFlags & 0x80) >> 7) == 1; //a password is true iff user is true. if (!userFlag && passwordFlag) { in.resetReaderIndex(); throw new CorruptedFrameException( "Expected password flag to true if the user flag is true but was: " + passwordFlag); } message.setCleanSession(cleanSession); message.setWillFlag(willFlag); message.setWillQos(willQos); message.setWillRetain(willRetain); message.setPasswordFlag(passwordFlag); message.setUserFlag(userFlag); //Keep Alive timer 2 bytes //int keepAlive = Utils.readWord(in); int keepAlive = in.readUnsignedShort(); message.setKeepAlive(keepAlive); if ((remainingLength == 12 && message.getProtocolVersion() == Utils.VERSION_3_1) || (remainingLength == 10 && message.getProtocolVersion() == Utils.VERSION_3_1_1)) { return message; } //Decode the ClientID String clientID = Utils.decodeString(in); if (clientID == null) { in.resetReaderIndex(); return null; } message.setClientID(clientID); //Decode willTopic if (willFlag) { String willTopic = Utils.decodeString(in); if (willTopic == null) { in.resetReaderIndex(); return null; } message.setWillTopic(willTopic); } //Decode willMessage if (willFlag) { byte[] willMessage = Utils.readFixedLengthContent(in); if (willMessage == null) { in.resetReaderIndex(); return null; } message.setWillMessage(willMessage); } //Compatibility check with v3.0, remaining length has precedence over //the user and password flags int readed = in.readerIndex() - start; if (readed == remainingLength) { return message; } //Decode username if (userFlag) { String userName = Utils.decodeString(in); if (userName == null) { in.resetReaderIndex(); return null; } message.setUsername(userName); } readed = in.readerIndex() - start; if (readed == remainingLength) { return message; } //Decode password if (passwordFlag) { byte[] password = Utils.readFixedLengthContent(in); if (password == null) { in.resetReaderIndex(); return null; } message.setPassword(password); } return message; }
From source file:com.kixeye.kixmpp.server.module.muc.MucRoomTest.java
License:Apache License
@Test public void joinRoom_firstTime_isOpen() { KixmppServer server = (KixmppServer) Mockito.when(Mockito.mock(KixmppServer.class).getEventEngine()) .thenReturn(new KixmppEventEngine()).getMock(); MucService mucService = (MucService) Mockito.when(Mockito.mock(MucService.class).getServer()) .thenReturn(server).getMock(); KixmppJid roomJid = new KixmppJid("testnode", "testdomain"); MucRoom mucRoom = new MucRoom(mucService, roomJid, new MucRoomSettings(false, true, null)); Channel channel = Mockito.mock(Channel.class); Attribute<KixmppJid> jidAttribute = Mockito.mock(Attribute.class); Mockito.when(jidAttribute.get()).thenReturn(new KixmppJid("test.user", "testdomain", "testresource")); Mockito.when(channel.attr(BindKixmppServerModule.JID)).thenReturn(jidAttribute); Mockito.when(channel.closeFuture()).thenReturn(Mockito.mock(ChannelFuture.class)); Assert.assertEquals(0, mucRoom.getUsers().size()); mucRoom.join(channel, "nickname"); Assert.assertEquals(1, mucRoom.getUsers().size()); }
From source file:com.kixeye.kixmpp.server.module.muc.MucRoomTest.java
License:Apache License
@Test public void joinRoom_multipleConnectionsSameUser_isOpen() { KixmppServer server = (KixmppServer) Mockito.when(Mockito.mock(KixmppServer.class).getEventEngine()) .thenReturn(new KixmppEventEngine()).getMock(); MucService mucService = (MucService) Mockito.when(Mockito.mock(MucService.class).getServer()) .thenReturn(server).getMock(); KixmppJid roomJid = new KixmppJid("testnode", "testdomain"); MucRoom mucRoom = new MucRoom(mucService, roomJid, new MucRoomSettings(false, true, null)); Channel channel = Mockito.mock(Channel.class); Attribute<KixmppJid> jidAttribute = Mockito.mock(Attribute.class); Mockito.when(jidAttribute.get()).thenReturn(new KixmppJid("test.user", "testdomain", "testresource")); Mockito.when(channel.attr(BindKixmppServerModule.JID)).thenReturn(jidAttribute); Mockito.when(channel.closeFuture()).thenReturn(Mockito.mock(ChannelFuture.class)); Assert.assertEquals(0, mucRoom.getUsers().size()); mucRoom.join(channel, "nickname"); Assert.assertEquals(1, mucRoom.getUsers().size()); Channel channel2 = Mockito.mock(Channel.class); Attribute<KixmppJid> jidAttribute2 = Mockito.mock(Attribute.class); Mockito.when(jidAttribute2.get()).thenReturn(new KixmppJid("test.user", "testdomain", "testresource2")); Mockito.when(channel2.attr(BindKixmppServerModule.JID)).thenReturn(jidAttribute2); Mockito.when(channel2.closeFuture()).thenReturn(Mockito.mock(ChannelFuture.class)); mucRoom.join(channel2, "nickname"); Assert.assertEquals(1, mucRoom.getUsers().size()); Assert.assertEquals(2, mucRoom.getUser("nickname").getConnections().size()); }
From source file:com.kixeye.kixmpp.server.module.muc.MucRoomTest.java
License:Apache License
@Test public void joinRoom_conflictingNickname_isOpen() { KixmppServer server = (KixmppServer) Mockito.when(Mockito.mock(KixmppServer.class).getEventEngine()) .thenReturn(new KixmppEventEngine()).getMock(); MucService mucService = (MucService) Mockito.when(Mockito.mock(MucService.class).getServer()) .thenReturn(server).getMock(); KixmppJid roomJid = new KixmppJid("testnode", "testdomain"); MucRoom mucRoom = new MucRoom(mucService, roomJid, new MucRoomSettings(false, true, null)); Channel channel = Mockito.mock(Channel.class); Attribute<KixmppJid> jidAttribute = Mockito.mock(Attribute.class); Mockito.when(jidAttribute.get()).thenReturn(new KixmppJid("test.user1", "testdomain", "testresource")); Mockito.when(channel.attr(BindKixmppServerModule.JID)).thenReturn(jidAttribute); Mockito.when(channel.closeFuture()).thenReturn(Mockito.mock(ChannelFuture.class)); Assert.assertEquals(0, mucRoom.getUsers().size()); mucRoom.join(channel, "nickname"); Assert.assertEquals(1, mucRoom.getUsers().size()); Channel channel2 = Mockito.mock(Channel.class); Attribute<KixmppJid> jidAttribute2 = Mockito.mock(Attribute.class); Mockito.when(jidAttribute2.get()).thenReturn(new KixmppJid("test.user2", "testdomain", "testresource")); Mockito.when(channel2.attr(BindKixmppServerModule.JID)).thenReturn(jidAttribute2); Mockito.when(channel2.closeFuture()).thenReturn(Mockito.mock(ChannelFuture.class)); try {// w ww . ja v a 2 s.c o m mucRoom.join(channel2, "nickname"); Assert.fail(); } catch (NicknameInUseException e) { } }
From source file:com.kixeye.kixmpp.server.module.muc.MucRoomTest.java
License:Apache License
@Test(expected = RoomJoinNotAllowedException.class) public void joinRoom_firstTime_isNotOpen_noMemberAdded() { KixmppServer server = (KixmppServer) Mockito.when(Mockito.mock(KixmppServer.class).getEventEngine()) .thenReturn(new KixmppEventEngine()).getMock(); MucService mucService = (MucService) Mockito.when(Mockito.mock(MucService.class).getServer()) .thenReturn(server).getMock(); KixmppJid roomJid = new KixmppJid("testnode", "testdomain"); MucRoom mucRoom = new MucRoom(mucService, roomJid, new MucRoomSettings(false, false, null)); Channel channel = Mockito.mock(Channel.class); Attribute<KixmppJid> jidAttribute = Mockito.mock(Attribute.class); Mockito.when(jidAttribute.get()).thenReturn(new KixmppJid("test.user", "testdomain", "testresource")); Mockito.when(channel.attr(BindKixmppServerModule.JID)).thenReturn(jidAttribute); Mockito.when(channel.closeFuture()).thenReturn(Mockito.mock(ChannelFuture.class)); mucRoom.join(channel, "nickname"); }
From source file:com.kixeye.kixmpp.server.module.muc.MucRoomTest.java
License:Apache License
@Test public void joinRoom_firstTime_isNotOpen_memberAdded() { KixmppServer server = (KixmppServer) Mockito.when(Mockito.mock(KixmppServer.class).getEventEngine()) .thenReturn(new KixmppEventEngine()).getMock(); MucService mucService = (MucService) Mockito.when(Mockito.mock(MucService.class).getServer()) .thenReturn(server).getMock(); KixmppJid roomJid = new KixmppJid("testnode", "testdomain"); MucRoom mucRoom = new MucRoom(mucService, roomJid, new MucRoomSettings(false, false, null)); Channel channel = Mockito.mock(Channel.class); Attribute<KixmppJid> jidAttribute = Mockito.mock(Attribute.class); Mockito.when(jidAttribute.get()).thenReturn(new KixmppJid("test.user", "testdomain", "testresource")); Mockito.when(channel.attr(BindKixmppServerModule.JID)).thenReturn(jidAttribute); Mockito.when(channel.closeFuture()).thenReturn(Mockito.mock(ChannelFuture.class)); Assert.assertEquals(0, mucRoom.getUsers().size()); mucRoom.addUser(new KixmppJid("test.user", "testdomain", "testresource"), "nickname", MucRole.Participant, MucAffiliation.Member); mucRoom.join(channel, "nickname"); Assert.assertEquals(1, mucRoom.getUsers().size()); }
From source file:com.kixeye.kixmpp.server.module.muc.MucRoomTest.java
License:Apache License
@Test public void removeUser_userInRoom() { KixmppServer server = (KixmppServer) Mockito.when(Mockito.mock(KixmppServer.class).getEventEngine()) .thenReturn(new KixmppEventEngine()).getMock(); MucService mucService = (MucService) Mockito.when(Mockito.mock(MucService.class).getServer()) .thenReturn(server).getMock(); KixmppJid roomJid = new KixmppJid("testnode", "testdomain"); MucRoom mucRoom = new MucRoom(mucService, roomJid, new MucRoomSettings(false, true, null)); KixmppJid clientJid = new KixmppJid("test.user", "testdomain", "testresource"); Channel channel = Mockito.mock(Channel.class); Attribute<KixmppJid> jidAttribute = Mockito.mock(Attribute.class); Mockito.when(jidAttribute.get()).thenReturn(clientJid); Mockito.when(channel.attr(BindKixmppServerModule.JID)).thenReturn(jidAttribute); Mockito.when(channel.closeFuture()).thenReturn(Mockito.mock(ChannelFuture.class)); Assert.assertEquals(0, mucRoom.getUsers().size()); mucRoom.join(channel, "nickname"); Assert.assertEquals(1, mucRoom.getUsers().size()); Assert.assertTrue(mucRoom.removeUser(clientJid)); Assert.assertEquals(0, mucRoom.getUsers().size()); }
From source file:com.kixeye.kixmpp.server.module.muc.MucRoomTest.java
License:Apache License
@Test public void removeAndRejoinUser() { KixmppServer server = (KixmppServer) Mockito.when(Mockito.mock(KixmppServer.class).getEventEngine()) .thenReturn(new KixmppEventEngine()).getMock(); MucService mucService = (MucService) Mockito.when(Mockito.mock(MucService.class).getServer()) .thenReturn(server).getMock(); KixmppJid roomJid = new KixmppJid("testnode", "testdomain"); MucRoom mucRoom = new MucRoom(mucService, roomJid, new MucRoomSettings(false, false, null)); KixmppJid clientJid = new KixmppJid("test.user", "testdomain", "testresource"); Channel channel = Mockito.mock(Channel.class); Attribute<KixmppJid> jidAttribute = Mockito.mock(Attribute.class); Mockito.when(jidAttribute.get()).thenReturn(clientJid); Mockito.when(channel.attr(BindKixmppServerModule.JID)).thenReturn(jidAttribute); Mockito.when(channel.closeFuture()).thenReturn(Mockito.mock(ChannelFuture.class)); Assert.assertEquals(0, mucRoom.getUsers().size()); try {//from ww w.jav a2 s.c o m mucRoom.join(channel, "nickname"); Assert.fail(); } catch (RoomJoinNotAllowedException e) { //expected } Assert.assertEquals(0, mucRoom.getUsers().size()); mucRoom.addUser(clientJid, "nickname", MucRole.Participant, MucAffiliation.Member); mucRoom.join(channel, "nickname"); Assert.assertEquals(1, mucRoom.getUsers().size()); Assert.assertTrue(mucRoom.removeUser(clientJid)); Assert.assertEquals(0, mucRoom.getUsers().size()); mucRoom.addUser(clientJid, "nickname", MucRole.Participant, MucAffiliation.Member); mucRoom.join(channel, "nickname"); Assert.assertEquals(1, mucRoom.getUsers().size()); }