List of usage examples for io.netty.util CharsetUtil UTF_8
Charset UTF_8
To view the source code for io.netty.util CharsetUtil UTF_8.
Click Source Link
From source file:com.corundumstudio.socketio.handler.EncoderHandler.java
License:Apache License
private void sendMessage(HttpMessage msg, Channel channel, ByteBuf out, HttpResponse res) { channel.write(res);/*from w w w.jav a 2 s . c om*/ if (log.isTraceEnabled()) { log.trace("Out message: {} - sessionId: {}", out.toString(CharsetUtil.UTF_8), msg.getSessionId()); } if (out.isReadable()) { channel.write(out); } else { out.release(); } channel.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT).addListener(ChannelFutureListener.CLOSE); }
From source file:com.corundumstudio.socketio.handler.EncoderHandler.java
License:Apache License
private void handleWebsocket(final OutPacketMessage msg, ChannelHandlerContext ctx) throws IOException { while (true) { Queue<Packet> queue = msg.getClientHead().getPacketsQueue(msg.getTransport()); Packet packet = queue.poll();/* w ww. ja v a 2 s. c om*/ if (packet == null) { break; } final ByteBuf out = encoder.allocateBuffer(ctx.alloc()); encoder.encodePacket(packet, out, ctx.alloc(), true); WebSocketFrame res = new TextWebSocketFrame(out); if (log.isTraceEnabled()) { log.trace("Out message: {} sessionId: {}", out.toString(CharsetUtil.UTF_8), msg.getSessionId()); } if (out.isReadable()) { ctx.channel().writeAndFlush(res); } else { out.release(); } for (ByteBuf buf : packet.getAttachments()) { ByteBuf outBuf = encoder.allocateBuffer(ctx.alloc()); outBuf.writeByte(4); outBuf.writeBytes(buf); if (log.isTraceEnabled()) { log.trace("Out attachment: {} sessionId: {}", ByteBufUtil.hexDump(outBuf), msg.getSessionId()); } ctx.channel().writeAndFlush(new BinaryWebSocketFrame(outBuf)); } } }
From source file:com.corundumstudio.socketio.handler.InPacketHandler.java
License:Apache License
@Override protected void channelRead0(io.netty.channel.ChannelHandlerContext ctx, PacketsMessage message) throws Exception { ByteBuf content = message.getContent(); ClientHead client = message.getClient(); if (log.isTraceEnabled()) { log.trace("In message: {} sessionId: {}", content.toString(CharsetUtil.UTF_8), client.getSessionId()); }//from w ww. j a v a2 s . c o m while (content.isReadable()) { try { Packet packet = decoder.decodePackets(content, client); if (packet.hasAttachments() && !packet.isAttachmentsLoaded()) { return; } Namespace ns = namespacesHub.get(packet.getNsp()); if (ns == null) { if (packet.getSubType() == PacketType.CONNECT) { Packet p = new Packet(PacketType.MESSAGE); p.setSubType(PacketType.ERROR); p.setNsp(packet.getNsp()); p.setData("Invalid namespace"); client.send(p); return; } log.debug("Can't find namespace for endpoint: {}, sessionId: {} probably it was removed.", packet.getNsp(), client.getSessionId()); return; } if (packet.getSubType() == PacketType.CONNECT) { client.addNamespaceClient(ns); } NamespaceClient nClient = client.getChildClient(ns); if (nClient == null) { log.debug( "Can't find namespace client in namespace: {}, sessionId: {} probably it was disconnected.", ns.getName(), client.getSessionId()); return; } packetListener.onPacket(packet, nClient, message.getTransport()); } catch (Exception ex) { String c = content.toString(CharsetUtil.UTF_8); log.error( "Error during data processing. Client sessionId: " + client.getSessionId() + ", data: " + c, ex); throw ex; } } }
From source file:com.corundumstudio.socketio.handler.PacketHandler.java
License:Apache License
@Override protected void channelRead0(io.netty.channel.ChannelHandlerContext ctx, PacketsMessage message) throws Exception { ByteBuf content = message.getContent(); MainBaseClient client = message.getClient(); if (log.isTraceEnabled()) { log.trace("In message: {} sessionId: {}", content.toString(CharsetUtil.UTF_8), client.getSessionId()); }/*from w w w . ja v a2 s . com*/ while (content.isReadable()) { try { Packet packet = decoder.decodePackets(content, client.getSessionId()); Namespace ns = namespacesHub.get(packet.getEndpoint()); if (ns == null) { log.debug("Can't find namespace for endpoint: {}, sessionId: {} probably it was removed.", packet.getEndpoint(), client.getSessionId()); return; } if (packet.getType() == PacketType.CONNECT) { client.addChildClient(ns); } NamespaceClient nClient = (NamespaceClient) client.getChildClient(ns); if (nClient == null) { log.debug( "Can't find namespace client in namespace: {}, sessionId: {} probably it was disconnected.", ns.getName(), client.getSessionId()); return; } packetListener.onPacket(packet, nClient); } catch (Exception ex) { String c = content.toString(CharsetUtil.UTF_8); log.error( "Error during data processing. Client sessionId: " + client.getSessionId() + ", data: " + c, ex); return; } } }
From source file:com.corundumstudio.socketio.handler.ResourceHandler.java
License:Apache License
/** * Sends an Error response with status message * * @param ctx// w w w . j av a 2 s .co m * @param status */ private void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) { HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status); HttpHeaders.setHeader(response, CONTENT_TYPE, "text/plain; charset=UTF-8"); ByteBuf content = Unpooled.copiedBuffer("Failure: " + status.toString() + "\r\n", CharsetUtil.UTF_8); ctx.channel().write(response); // Close the connection as soon as the error message is sent. ctx.channel().write(content).addListener(ChannelFutureListener.CLOSE); }
From source file:com.corundumstudio.socketio.parser.Decoder.java
License:Apache License
private Packet decodePacket(ByteBuf buffer, UUID uuid) throws IOException { if (buffer.readableBytes() < 3) { throw new DecoderException("Can't parse " + buffer.toString(CharsetUtil.UTF_8)); }/*from w ww .j a v a 2s .c o m*/ PacketType type = getType(buffer); int readerIndex = buffer.readerIndex() + 1; // 'null' to avoid unnecessary StringBuilder creation boolean hasData = false; StringBuilder messageId = null; for (readerIndex += 1; readerIndex < buffer.readableBytes(); readerIndex++) { if (messageId == null) { messageId = new StringBuilder(4); } byte msg = buffer.getByte(readerIndex); if (msg == Packet.SEPARATOR) { break; } if (msg != (byte) '+') { messageId.append((char) msg); } else { hasData = true; } } Long id = null; if (messageId != null && messageId.length() > 0) { id = Long.valueOf(messageId.toString()); } // 'null' to avoid unnecessary StringBuilder creation StringBuilder endpointBuffer = null; for (readerIndex += 1; readerIndex < buffer.readableBytes(); readerIndex++) { if (endpointBuffer == null) { endpointBuffer = new StringBuilder(); } byte msg = buffer.getByte(readerIndex); if (msg == Packet.SEPARATOR) { break; } endpointBuffer.append((char) msg); } String endpoint = Namespace.DEFAULT_NAME; if (endpointBuffer != null && endpointBuffer.length() > 0) { endpoint = endpointBuffer.toString(); } if (buffer.readableBytes() == readerIndex) { buffer.readerIndex(buffer.readableBytes()); } else { readerIndex += 1; buffer.readerIndex(readerIndex); } Packet packet = new Packet(type); packet.setEndpoint(endpoint); if (id != null) { packet.setId(id); if (hasData) { packet.setAck(Packet.ACK_DATA); } else { packet.setAck(true); } } switch (type) { case ERROR: { if (!buffer.isReadable()) { break; } String[] pieces = buffer.toString(CharsetUtil.UTF_8).split("\\+"); if (pieces.length > 0 && pieces[0].trim().length() > 0) { ErrorReason reason = ErrorReason.valueOf(Integer.valueOf(pieces[0])); packet.setReason(reason); if (pieces.length > 1) { ErrorAdvice advice = ErrorAdvice.valueOf(Integer.valueOf(pieces[1])); packet.setAdvice(advice); } } break; } case MESSAGE: { if (buffer.isReadable()) { packet.setData(buffer.toString(CharsetUtil.UTF_8)); } else { packet.setData(""); } break; } case EVENT: { ByteBufInputStream in = new ByteBufInputStream(buffer); Event event = jsonSupport.readValue(in, Event.class); packet.setName(event.getName()); if (event.getArgs() != null) { packet.setArgs(event.getArgs()); } break; } case JSON: { ByteBufInputStream in = new ByteBufInputStream(buffer); JsonObject obj = jsonSupport.readValue(in, JsonObject.class); if (obj != null) { packet.setData(obj.getObject()); } else { in.reset(); Object object = jsonSupport.readValue(in, Object.class); packet.setData(object); } break; } case CONNECT: { if (buffer.isReadable()) { packet.setQs(buffer.toString(CharsetUtil.UTF_8)); } break; } case ACK: { if (!buffer.isReadable()) { break; } boolean validFormat = true; int plusIndex = -1; for (int i = buffer.readerIndex(); i < buffer.readerIndex() + buffer.readableBytes(); i++) { byte dataChar = buffer.getByte(i); if (!Character.isDigit(dataChar)) { if (dataChar == '+') { plusIndex = i; break; } else { validFormat = false; break; } } } if (!validFormat) { break; } if (plusIndex == -1) { packet.setAckId(parseLong(buffer)); break; } else { packet.setAckId(parseLong(buffer, plusIndex)); buffer.readerIndex(plusIndex + 1); ByteBufInputStream in = new ByteBufInputStream(buffer); AckCallback<?> callback = ackManager.getCallback(uuid, packet.getAckId()); AckArgs args = jsonSupport.readAckArgs(in, callback); packet.setArgs(args.getArgs()); } break; } case DISCONNECT: case HEARTBEAT: case NOOP: break; } buffer.readerIndex(buffer.readerIndex() + buffer.readableBytes()); return packet; }
From source file:com.corundumstudio.socketio.parser.Decoder.java
License:Apache License
private PacketType getType(ByteBuf buffer) { int typeId = buffer.getByte(buffer.readerIndex()) & 0xF; if (typeId >= PacketType.VALUES.length || buffer.getByte(buffer.readerIndex() + 1) != Packet.SEPARATOR) { throw new DecoderException("Can't parse " + buffer.toString(CharsetUtil.UTF_8)); }// ww w .j a v a 2s. co m return PacketType.valueOf(typeId); }
From source file:com.corundumstudio.socketio.parser.Decoder.java
License:Apache License
public Packet decodePacket(String string, UUID uuid) throws IOException { ByteBuf buf = Unpooled.copiedBuffer(string, CharsetUtil.UTF_8); try {//from ww w . j ava2 s . c o m Packet packet = decodePacket(buf, uuid); return packet; } finally { buf.release(); } }
From source file:com.corundumstudio.socketio.parser.EncoderAckPacketTest.java
License:Apache License
@Test public void testEncode() throws IOException { Packet packet = new Packet(PacketType.ACK); packet.setAckId(140L);/* ww w.jav a 2s.c om*/ ByteBuf result = Unpooled.buffer(); // encoder.encodePacket(packet, result); Assert.assertEquals("6:::140", result.toString(CharsetUtil.UTF_8)); }
From source file:com.corundumstudio.socketio.parser.EncoderAckPacketTest.java
License:Apache License
@Test public void testEncodeWithArgs() throws IOException { Packet packet = new Packet(PacketType.ACK); packet.setAckId(12L);/*w w w . ja v a2 s.c o m*/ // packet.setArgs(Arrays.<Object>asList("woot", "wa")); ByteBuf result = Unpooled.buffer(); // encoder.encodePacket(packet, result); Assert.assertEquals("6:::12+[\"woot\",\"wa\"]", result.toString(CharsetUtil.UTF_8)); }