@Override public ChannelBuffer getEncodedMessage() { ChannelBuffer encodedMessage = ChannelBuffers.dynamicBuffer(); encodedMessage.writeBytes("Command: StatusList\n".getBytes(CharsetUtil.UTF_8)); for (ExtensionStatus es : statuses) { encodedMessage.writeBytes( ("ExtensionStatus: " + new StringBuilder() .append(es.getExtension()) .append(" ") .append( es.getStatus() == null ? DEFAULT_STATUS : new StringBuilder() .append(es.getStatus()) .append(" ") .append(this.dateFormat.format(es.getSince())) .toString()) .toString() + "\n") .getBytes(CharsetUtil.UTF_8)); } encodedMessage.writeBytes("\n".getBytes(CharsetUtil.UTF_8)); encodedMessage = encodedMessage.slice(0, encodedMessage.writerIndex()); return encodedMessage; }
public void writeTo(ChannelBuffer data) { data.writeShort(fieldId); data.writeShort(offset); data.writeShort(length); data.writeShort(0); if (value == null) { data.writeZero(OFGlobal.OFP_MAX_FIELD_LENGTH_IN_BYTE); } else { if (value.length > OFGlobal.OFP_MAX_FIELD_LENGTH_IN_BYTE) { data.writeBytes( value, OFGlobal.OFP_MAX_FIELD_LENGTH_IN_BYTE - value.length, OFGlobal.OFP_MAX_FIELD_LENGTH_IN_BYTE); } else { data.writeBytes(value); data.writeZero(OFGlobal.OFP_MAX_FIELD_LENGTH_IN_BYTE - value.length); } } if (mask == null) { data.writeZero(OFGlobal.OFP_MAX_FIELD_LENGTH_IN_BYTE); } else { if (mask.length > OFGlobal.OFP_MAX_FIELD_LENGTH_IN_BYTE) { data.writeBytes( mask, OFGlobal.OFP_MAX_FIELD_LENGTH_IN_BYTE - mask.length, OFGlobal.OFP_MAX_FIELD_LENGTH_IN_BYTE); } else { data.writeBytes(mask); data.writeZero(OFGlobal.OFP_MAX_FIELD_LENGTH_IN_BYTE - mask.length); } } }
@Test public void testGoodEventGoodEvent() { String priority = "<10>"; String goodData1 = "Good good good data\n"; SyslogUtils util = new SyslogUtils(false); ChannelBuffer buff = ChannelBuffers.buffer(100); buff.writeBytes((priority + goodData1).getBytes()); String priority2 = "<20>"; String goodData2 = "Good really good data\n"; buff.writeBytes((priority2 + goodData2).getBytes()); Event e = util.extractEvent(buff); if (e == null) { throw new NullPointerException("Event is null"); } Map<String, String> headers = e.getHeaders(); Assert.assertEquals("1", headers.get(SyslogUtils.SYSLOG_FACILITY)); Assert.assertEquals("2", headers.get(SyslogUtils.SYSLOG_SEVERITY)); Assert.assertEquals(null, headers.get(SyslogUtils.EVENT_STATUS)); Assert.assertEquals(priority + goodData1.trim(), new String(e.getBody()).trim()); Event e2 = util.extractEvent(buff); if (e2 == null) { throw new NullPointerException("Event is null"); } Map<String, String> headers2 = e2.getHeaders(); Assert.assertEquals("2", headers2.get(SyslogUtils.SYSLOG_FACILITY)); Assert.assertEquals("4", headers2.get(SyslogUtils.SYSLOG_SEVERITY)); Assert.assertEquals(null, headers.get(SyslogUtils.EVENT_STATUS)); Assert.assertEquals(priority2 + goodData2.trim(), new String(e2.getBody()).trim()); }
@Test public void setmst() { String host = "host"; int port = 1978; long timestamp = System.currentTimeMillis(); int opts = RDB.ROCHKCON; Setmst dut = new Setmst(host, port, timestamp, opts); ChannelBuffer request = ChannelBuffers.buffer(2 + 4 + 4 + 8 + 4 + host.getBytes().length); request.writeBytes(new byte[] {(byte) 0xC8, (byte) 0x78}); request.writeInt(host.getBytes().length); request.writeInt(port); request.writeLong(timestamp); request.writeInt(opts); request.writeBytes(host.getBytes()); ChannelBuffer actual = ChannelBuffers.buffer(request.capacity()); dut.encode(actual); assertEquals(request, actual); ChannelBuffer response = ChannelBuffers.buffer(1); assertFalse(dut.decode(response)); response.writeByte(Command.ESUCCESS); assertTrue(dut.decode(response)); assertTrue(dut.getReturnValue()); // error response.clear(); response.writeByte(Command.EUNKNOWN); assertTrue(dut.decode(response)); assertFalse(dut.getReturnValue()); }
@Test public void testBadEventBadEvent() { String badData1 = "hi guys! <10F> bad bad data\n"; SyslogUtils util = new SyslogUtils(false); ChannelBuffer buff = ChannelBuffers.buffer(100); buff.writeBytes(badData1.getBytes()); String badData2 = "hi guys! <20> bad bad data\n"; buff.writeBytes((badData2).getBytes()); Event e = util.extractEvent(buff); if (e == null) { throw new NullPointerException("Event is null"); } Map<String, String> headers = e.getHeaders(); Assert.assertEquals("0", headers.get(SyslogUtils.SYSLOG_FACILITY)); Assert.assertEquals("0", headers.get(SyslogUtils.SYSLOG_SEVERITY)); Assert.assertEquals( SyslogUtils.SyslogStatus.INVALID.getSyslogStatus(), headers.get(SyslogUtils.EVENT_STATUS)); Assert.assertEquals(badData1.trim(), new String(e.getBody()).trim()); Event e2 = util.extractEvent(buff); if (e2 == null) { throw new NullPointerException("Event is null"); } Map<String, String> headers2 = e2.getHeaders(); Assert.assertEquals("0", headers2.get(SyslogUtils.SYSLOG_FACILITY)); Assert.assertEquals("0", headers2.get(SyslogUtils.SYSLOG_SEVERITY)); Assert.assertEquals( SyslogUtils.SyslogStatus.INVALID.getSyslogStatus(), headers2.get(SyslogUtils.EVENT_STATUS)); Assert.assertEquals(badData2.trim(), new String(e2.getBody()).trim()); }
public int td(ChannelBuffer buf, String str, String attributes) { if (str == null) { str = "null"; } byte[] data = str.getBytes(); int count = 0; if (attributes == null) { buf.writeBytes(TD1); count += TD1.length; } else { String tag = "<td " + attributes + ">"; byte[] bytes = tag.getBytes(); buf.writeBytes(bytes); count += bytes.length; } buf.writeBytes(data); count += data.length; buf.writeBytes(TD2); count += TD2.length; return count; }
private static void encodeHeader(ChannelBuffer buf, String header, List<String> values) throws UnsupportedEncodingException { if (values.size() == 0) { log.warn(String.format("encodeHeader. No values found for header[%s]", header)); buf.writeBytes(header.getBytes(charSet)); buf.writeByte(COLON); buf.writeByte(SP); buf.writeByte(CR); buf.writeByte(LF); return; } // // see http://tools.ietf.org/html/rfc3261#section-7.3.1 // for (String s : values) { buf.writeBytes(header.getBytes(charSet)); buf.writeByte(COLON); buf.writeByte(SP); buf.writeBytes(s.getBytes(charSet)); buf.writeByte(CR); buf.writeByte(LF); } }
@Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { Object m = e.getMessage(); if (!(m instanceof ChannelBuffer)) { ctx.sendUpstream(e); return; } ChannelBuffer input = (ChannelBuffer) m; if (!input.readable()) { return; } ChannelBuffer cumulation = cumulation(ctx); if (cumulation.readable()) { cumulation.discardReadBytes(); cumulation.writeBytes(input); callDecode(ctx, e.getChannel(), cumulation, e.getRemoteAddress()); } else { callDecode(ctx, e.getChannel(), input, e.getRemoteAddress()); if (input.readable()) { cumulation.writeBytes(input); } } }
@Test public void iternext() { Iternext dut = new Iternext(transcoder, transcoder); ChannelBuffer request = ChannelBuffers.buffer(2); request.writeBytes(new byte[] {(byte) 0xC8, (byte) 0x51}); ChannelBuffer actual = ChannelBuffers.buffer(request.capacity()); dut.encode(actual); assertEquals(request, actual); ChannelBuffer response = ChannelBuffers.buffer(1 + 4 + value.length); assertFalse(dut.decode(response)); response.writeByte(BinaryCommand.ESUCCESS); assertFalse(dut.decode(response)); response.resetReaderIndex(); response.writeInt(value.length); response.writeBytes(value); assertTrue(dut.decode(response)); assertArrayEquals(value, (byte[]) dut.getReturnValue()); // error response.clear(); response.writeByte(BinaryCommand.EUNKNOWN); assertTrue(dut.decode(response)); assertNull(dut.getReturnValue()); }
@Override public ChannelBuffer encode(EncryptionKeyRequestMessage message) { ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(); ChannelBufferUtils.writeString(buffer, message.getSessionId()); byte[] publicKey = message.getSecretArray(); buffer.writeShort((short) publicKey.length); buffer.writeBytes(publicKey); buffer.writeShort((short) message.getVerifyTokenArray().length); buffer.writeBytes(message.getVerifyTokenArray()); return buffer; }
@Override protected void encodeInitialLine(ChannelBuffer buf, HttpMessage message) throws Exception { HttpRequest request = (HttpRequest) message; buf.writeBytes(request.getMethod().toString().getBytes("ASCII")); buf.writeByte(SP); buf.writeBytes(request.getUri().getBytes("ASCII")); buf.writeByte(SP); buf.writeBytes(request.getProtocolVersion().toString().getBytes("ASCII")); buf.writeByte(CR); buf.writeByte(LF); }
@Override protected void encodeInitialLine(final ChannelBuffer buf, final HttpMessage message) throws Exception { final MappingHttpRequest request = (MappingHttpRequest) message; buf.writeBytes(request.getMethod().toString().getBytes("ASCII")); buf.writeByte(HttpUtils.SP); buf.writeBytes(request.getServicePath().getBytes("ASCII")); buf.writeByte(HttpUtils.SP); buf.writeBytes(request.getProtocolVersion().toString().getBytes("ASCII")); buf.writeBytes(HttpUtils.CRLF); }
@Override public ChannelBuffer getEncodedMessage() { ChannelBuffer encodedMessage = ChannelBuffers.dynamicBuffer(); encodedMessage.writeBytes("Command: Error\n".getBytes(CharsetUtil.UTF_8)); encodedMessage.writeBytes(("Number: 5\n").getBytes(CharsetUtil.UTF_8)); encodedMessage.writeBytes( ("Description: The register request failed as the extn is already in use\n") .getBytes(CharsetUtil.UTF_8)); encodedMessage.writeBytes("\n".getBytes(CharsetUtil.UTF_8)); encodedMessage = encodedMessage.slice(0, encodedMessage.writerIndex()); return encodedMessage; }
public int tr1(ChannelBuffer buf, String styleClass) { if (styleClass == null) { buf.writeBytes(TR1); return TR1.length; } else { String tag = "<tr class=\"" + styleClass + "\">"; byte[] bytes = tag.getBytes(); buf.writeBytes(bytes); return bytes.length; } }
public int td1(ChannelBuffer buf, String attributes) { if (attributes == null) { buf.writeBytes(TD1); return TD1.length; } else { String tag = "<td " + attributes + ">"; byte[] bytes = tag.getBytes(); buf.writeBytes(bytes); return bytes.length; } }
public void handler(Message msg, Channel inBoundChannel, ChannelBuffer cb) { if (ResultCode.RESULT_CODE_00.getCode().equals(msg.getResponseCode())) { TblMerchantPos merchantPos = ManageCacheService.findPos(msg.getTerminalIdentification()); if (merchantPos == null) { byte[] allBytes = msg.toMessgeBytes(); ChannelBuffer retCB = ChannelBuffers.dynamicBuffer(); retCB.writeBytes(allBytes); inBoundChannel.write(retCB); isContinue = false; return; } else { if (ApplicationContent.MSG_TYPE_SALE_RESP.equals(msg.getMSGType())) { // 查原来交易。将原交易金额负给4域 TblTransactionMessage tm = ApplicationContentSpringProvider.getInstance().getMessageService().findSelf(msg); if (tm != null) { msg.setBCDField(4, tm.getFldTransactionAmount()); } } if (ApplicationContent.MSG_TYPE_REVERSAL_RESP.equals(msg.getMSGType()) || ApplicationContent.MSG_TYPE_SALE_RESP.equals(msg.getMSGType()) || ApplicationContent.MSG_TYPE_SALE_RESP.equals(msg.getMSGType())) { msg.setUnicertainAscField(62, merchantPos.getFldBatchNo()); } String targetMac = DefaultMessageHandler.getMAC(msg); msg.setBCDField(64, targetMac); } } }
public void checkHeader(String msg1, String stamp1, String format1, String host1, String data1) throws ParseException { SyslogUtils util = new SyslogUtils(false); ChannelBuffer buff = ChannelBuffers.buffer(200); buff.writeBytes(msg1.getBytes()); Event e = util.extractEvent(buff); if (e == null) { throw new NullPointerException("Event is null"); } Map<String, String> headers2 = e.getHeaders(); if (stamp1 == null) { Assert.assertFalse(headers2.containsKey("timestamp")); } else { SimpleDateFormat formater = new SimpleDateFormat(format1); Assert.assertEquals( String.valueOf(formater.parse(stamp1).getTime()), headers2.get("timestamp")); } if (host1 == null) { Assert.assertFalse(headers2.containsKey("host")); } else { String host2 = headers2.get("host"); Assert.assertEquals(host2, host1); } Assert.assertEquals(data1, new String(e.getBody())); }
public int nbsp(ChannelBuffer buf, int count) { for (int i = 0; i < count; i++) { buf.writeBytes(NBSP); } return count * NBSP.length; }
private ChannelBuffer getHelloMessageBuffer() { // OFHello, OF version 1, xid of 0, total of 8 bytes byte[] messageData = {0x1, 0x0, 0x0, 0x8, 0x0, 0x0, 0x0, 0x0}; ChannelBuffer channelBuffer = ChannelBuffers.dynamicBuffer(); channelBuffer.writeBytes(messageData); return channelBuffer; }
public ChannelBuffer getContent() { ChannelBuffer cb = ChannelBuffers.dynamicBuffer(); for (Frame frame : frames) { cb.writeBytes(frame.getPayload(), frame.getFrameSize() - 12); } return cb; }
@Test public void testExtractBadEventLarge() { String badData1 = "<10> bad bad data bad bad\n"; // The minimum size (which is 10) overrides the 5 specified here. SyslogUtils util = new SyslogUtils(5, false, false); ChannelBuffer buff = ChannelBuffers.buffer(100); buff.writeBytes(badData1.getBytes()); Event e = util.extractEvent(buff); if (e == null) { throw new NullPointerException("Event is null"); } Map<String, String> headers = e.getHeaders(); Assert.assertEquals("1", headers.get(SyslogUtils.SYSLOG_FACILITY)); Assert.assertEquals("2", headers.get(SyslogUtils.SYSLOG_SEVERITY)); Assert.assertEquals( SyslogUtils.SyslogStatus.INCOMPLETE.getSyslogStatus(), headers.get(SyslogUtils.EVENT_STATUS)); Assert.assertEquals("<10> bad b".trim(), new String(e.getBody()).trim()); Event e2 = util.extractEvent(buff); if (e2 == null) { throw new NullPointerException("Event is null"); } Map<String, String> headers2 = e2.getHeaders(); Assert.assertEquals("0", headers2.get(SyslogUtils.SYSLOG_FACILITY)); Assert.assertEquals("0", headers2.get(SyslogUtils.SYSLOG_SEVERITY)); Assert.assertEquals( SyslogUtils.SyslogStatus.INVALID.getSyslogStatus(), headers2.get(SyslogUtils.EVENT_STATUS)); Assert.assertEquals("ad data ba".trim(), new String(e2.getBody()).trim()); }
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception { if (opcode == -1) { if (buffer.readableBytes() >= 1) { opcode = buffer.readByte() & 0xFF; opcode = (opcode - cipher.getNextValue()) & 0xFF; size = Client.PACKET_SIZES[opcode]; } else { return null; } } if (size == -1) { if (buffer.readableBytes() >= 1) { size = buffer.readByte() & 0xFF; } else { return null; } } if (buffer.readableBytes() >= size) { final byte[] data = new byte[size]; buffer.readBytes(data); final ChannelBuffer payload = ChannelBuffers.buffer(size); payload.writeBytes(data); try { return new Packet(opcode, Type.FIXED, payload); } finally { opcode = -1; size = -1; } } return null; }
@Override public void write(ChannelBuffer bb, OFBsnPduTxRequestVer10 message) { int startIndex = bb.writerIndex(); // fixed value property version = 1 bb.writeByte((byte) 0x1); // fixed value property type = 4 bb.writeByte((byte) 0x4); // length is length of variable message, will be updated at the end int lengthIndex = bb.writerIndex(); bb.writeShort(U16.t(0)); bb.writeInt(U32.t(message.xid)); // fixed value property experimenter = 0x5c16c7L bb.writeInt(0x5c16c7); // fixed value property subtype = 0x1fL bb.writeInt(0x1f); bb.writeInt(U32.t(message.txIntervalMs)); message.portNo.write2Bytes(bb); bb.writeByte(U8.t(message.slotNum)); // pad: 3 bytes bb.writeZero(3); bb.writeBytes(message.data); // update length field int length = bb.writerIndex() - startIndex; bb.setShort(lengthIndex, length); }
public GameDownBuffer putString(String src, String encode) throws UnsupportedEncodingException { if (src == null) src = ""; byte[] strData = src.getBytes(encode); buffer.writeShort(strData.length); buffer.writeBytes(strData); return this; }
/** * Encode and write this command to the supplied buffer using the new <a * href="http://redis.io/topics/protocol">Unified Request Protocol</a>. * * @param buf Buffer to write to. */ void encode(ChannelBuffer buf) { buf.writeByte('*'); writeInt(buf, 1 + (args != null ? args.count() : 0)); buf.writeBytes(CRLF); buf.writeByte('$'); writeInt(buf, type.bytes.length); buf.writeBytes(CRLF); buf.writeBytes(type.bytes); buf.writeBytes(CRLF); if (args != null) { buf.writeBytes(args.buffer()); } /*byte[] array = buf.array(); String s = new String(array); System.out.println("请求报文:" + s);*/ }
/* * (non-Javadoc) * * @see org.jboss.netty.handler.codec.oneone.OneToOneEncoder#encode(org.jboss * .netty.channel.ChannelHandlerContext, org.jboss.netty.channel.Channel, java.lang.Object) */ @Override protected Object encode(final ChannelHandlerContext ctx, final Channel channel, final Object msg) throws Exception { final ChannelBuffer message = (ChannelBuffer) msg; final AuthToClientChannelHandler channelHandler = (AuthToClientChannelHandler) ctx.getPipeline().getLast(); final int opcode = message.readUnsignedByte(); final int size = message.readableBytes(); final ChannelBuffer frame = ChannelBuffers.buffer(ByteOrder.LITTLE_ENDIAN, (size + 3)); frame.writeByte(opcode); frame.writeShort(size); final byte[] tmpa = new byte[message.readableBytes()]; message.readBytes(tmpa); frame.writeBytes(channelHandler.getCrypt().encrypt(tmpa)); log.debug(String.format("[SEND PACKET] : 0x%02X", opcode)); final List<String> d = breakStringInChunks(new BigInteger(1, tmpa).toString(16).toUpperCase(), 16); for (final String string : d) { log.debug(string); } return frame; }
private NettyHttpResponse content(ChannelBuffer content) { if (response.isChunked()) { throw new UnsupportedOperationException(); } responseBuffer.writeBytes(content); return this; }
@Override public ChannelBuffer encode(WorldEditCUIMessage message) { byte[] data = message.getMessage().getBytes(UTF_8_CHARSET); ChannelBuffer buffer = ChannelBuffers.buffer(data.length); buffer.writeBytes(data); return buffer; }
@Override public int write(ChannelBuffer c) { int iLenStartIndex = c.writerIndex(); c.writeShort(TYPE); c.writeShort(hLength); c.writeBytes(rawValue); return c.writerIndex() - iLenStartIndex; }
@Override protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception { ByteArrayOutputStream o = new ByteArrayOutputStream(); new Packer(o).pack(msg); byte[] b = o.toByteArray(); ChannelBuffer buf = ChannelBuffers.dynamicBuffer(); buf.writeBytes(b); return buf; }