List of usage examples for io.netty.handler.codec DecoderException DecoderException
public DecoderException(Throwable cause)
From source file:at.yawk.dbus.protocol.auth.DirectionValidatorAdapter.java
private void validate(Object msg, AuthDirection expectedProtocol) { if (msg instanceof Command) { Command command = (Command) msg; if (command.getDirection() != null && command.getDirection() != expectedProtocol) { throw new DecoderException("Invalid command (wrong protocol direction): " + command); }//from w ww . j a v a2s. c o m } }
From source file:at.yawk.dbus.protocol.auth.mechanism.DbusCookieSha1AuthMechanism.java
private Data doAuth(String cookieData) throws IOException, NoSuchAlgorithmException { String[] parts = cookieData.split(" "); if (parts.length != 3) { throw new DecoderException("Expected exactly 3 parts, got " + parts.length); }//from w w w. j a v a 2s .c om String cookieContext = parts[0]; String cookieId = parts[1]; String serverChallenge = parts[2]; String userHome = System.getProperty("user.home"); Path cookiePath = Paths.get(userHome, ".dbus-keyrings", cookieContext); Optional<String> cookieOption = Files.lines(cookiePath).map(line -> line.split(" ")) .filter(arr -> arr.length == 3).filter(arr -> arr[0].equals(cookieId)).map(arr -> arr[2]).findAny(); String cookie = cookieOption.orElseThrow( () -> new DecoderException("Could not find cookie with id " + cookieId + " in " + cookiePath)); String clientChallenge = DbusUtil.printHex(rng.generateSeed(16)); String blob = serverChallenge + ':' + clientChallenge + ':' + cookie; MessageDigest digest = MessageDigest.getInstance("SHA1"); byte[] hash = digest.digest(blob.getBytes()); String response = clientChallenge + " " + DbusUtil.printHex(hash); return new Data(response.getBytes()); }
From source file:at.yawk.dbus.protocol.codec.BodyDecoder.java
@Override protected void decode(ChannelHandlerContext ctx, AlignableByteBuf in, List<Object> out) throws Exception { MessageHeader header = ctx.channel().attr(Local.CURRENT_HEADER).get(); if (header == null) { // message should be skipped return;/*from ww w . j a v a 2s . c o m*/ } DbusObject signature = header.getHeaderFields().get(HeaderField.SIGNATURE); if (signature == null) { throw new DecoderException("Non-empty body but missing signature header"); } List<TypeDefinition> types = signature.typeValue(); List<DbusObject> bodyObjects = new ArrayList<>(); for (TypeDefinition type : types) { DbusObject object = type.deserialize(in); bodyObjects.add(object); log.trace("Decoded object {}", object); } MessageBody body = new MessageBody(); body.setArguments(bodyObjects); out.add(body); }
From source file:at.yawk.dbus.protocol.codec.MessageHeaderCodec.java
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf rawBuf, List<Object> out) throws Exception { if (toRead != 0) { if (rawBuf.readableBytes() < toRead) { return; }/*from w w w . ja v a 2s . co m*/ ByteBuf slice = rawBuf.slice().order(byteOrder); slice.writerIndex(slice.readerIndex() + toRead); slice.retain(); AlignableByteBuf decoding = AlignableByteBuf.decoding(slice); log.trace("INBOUND {}", decoding); out.add(decoding); rawBuf.readerIndex(rawBuf.readerIndex() + toRead); toRead = 0; } if (rawBuf.readableBytes() < MIN_HEADER_LENGTH) { return; } rawBuf.markReaderIndex(); byte endianness = rawBuf.readByte(); ByteOrder order; switch (endianness) { case 'l': order = ByteOrder.LITTLE_ENDIAN; break; case 'B': order = ByteOrder.BIG_ENDIAN; break; default: throw new DecoderException("Unknown byte order byte " + endianness); } AlignableByteBuf buf = AlignableByteBuf.decoding(rawBuf.resetReaderIndex().order(order)); buf.getBuffer().markReaderIndex(); buf.readByte(); // skip endianness byte we read above @Nullable MessageType type = MessageType.byId(buf.readByte()); byte flags = buf.readByte(); byte majorProtocolVersion = buf.readByte(); if (majorProtocolVersion != PROTOCOL_VERSION) { throw new DecoderException("Unsupported major protocol version " + majorProtocolVersion); } long bodyLength = buf.readUnsignedInt(); int serial = buf.readInt(); MessageHeader header = new MessageHeader(); header.setByteOrder(order); header.setMessageType(type); header.setNoReplyExpected((flags & NO_REPLY_EXPECTED) != 0); header.setNoAutoStart((flags & NO_AUTO_START) != 0); header.setAllowInteractiveAuthorization((flags & ALLOW_INTERACTIVE_AUTHORIZATION) != 0); header.setMajorProtocolVersion(majorProtocolVersion); header.setMessageBodyLength(bodyLength); header.setSerial(serial); header.setHeaderFields(new EnumMap<>(HeaderField.class)); ArrayObject headers = (ArrayObject) tryDecode(HEADER_FIELD_LIST_TYPE, buf); if (headers == null) { // not enough data buf.getBuffer().resetReaderIndex(); return; } for (DbusObject struct : headers.getValues()) { HeaderField field = HeaderField.byId(struct.get(0).byteValue()); if (field != null) { DbusObject value = struct.get(1).getValue(); if (!value.getType().equals(field.getType())) { throw new DecoderException("Invalid header type on " + field + ": got " + value.getType() + " but expected " + field.getType()); } header.getHeaderFields().put(field, value); } } if (type != null) { checkRequiredHeaderFieldsPresent(header); } if (!buf.canAlignRead(8)) { buf.getBuffer().resetReaderIndex(); return; } buf.alignRead(8); toRead = Math.toIntExact(header.getMessageBodyLength()); byteOrder = order; out.add(header); }
From source file:at.yawk.dbus.protocol.codec.MessageHeaderCodec.java
private void checkRequiredHeaderFieldsPresent(MessageHeader header) { for (HeaderField required : header.getMessageType().getRequiredHeaders()) { if (!header.getHeaderFields().containsKey(required)) { throw new DecoderException("Missing required header field " + required); }/*w w w. jav a 2 s . c om*/ } }
From source file:code.google.nfs.rpc.netty.serialize.NettyProtocolDecoder.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { RecyclableArrayList out = RecyclableArrayList.newInstance(); try {/*w w w .j a v a 2s . c om*/ if (msg instanceof ByteBuf) { ByteBuf data = (ByteBuf) msg; if (cumulation == null) { cumulation = data; try { callDecode(ctx, cumulation, out); } finally { if (cumulation != null && !cumulation.isReadable()) { cumulation.release(); cumulation = null; } } } else { try { if (cumulation.writerIndex() > cumulation.maxCapacity() - data.readableBytes()) { ByteBuf oldCumulation = cumulation; cumulation = ctx.alloc().buffer(oldCumulation.readableBytes() + data.readableBytes()); cumulation.writeBytes(oldCumulation); oldCumulation.release(); } cumulation.writeBytes(data); callDecode(ctx, cumulation, out); } finally { if (cumulation != null) { if (!cumulation.isReadable()) { cumulation.release(); cumulation = null; } else { cumulation.discardSomeReadBytes(); } } data.release(); } } } else { out.add(msg); } } catch (DecoderException e) { throw e; } catch (Throwable t) { throw new DecoderException(t); } finally { if (out.isEmpty()) { decodeWasNull = true; } List<Object> results = new ArrayList<Object>(); for (Object result : out) { results.add(result); } ctx.fireChannelRead(results); out.recycle(); } }
From source file:code.google.nfs.rpc.netty.serialize.NettyProtocolDecoder.java
License:Apache License
@Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { RecyclableArrayList out = RecyclableArrayList.newInstance(); try {/*from ww w . j a va 2s . c o m*/ if (cumulation != null) { callDecode(ctx, cumulation, out); decodeLast(ctx, cumulation, out); } else { decodeLast(ctx, Unpooled.EMPTY_BUFFER, out); } } catch (DecoderException e) { throw e; } catch (Exception e) { throw new DecoderException(e); } finally { if (cumulation != null) { cumulation.release(); cumulation = null; } for (int i = 0; i < out.size(); i++) { ctx.fireChannelRead(out.get(i)); } ctx.fireChannelInactive(); } }
From source file:code.google.nfs.rpc.netty.serialize.NettyProtocolDecoder.java
License:Apache License
/** * Called once data should be decoded from the given {@link ByteBuf}. This method will call * {@link #decode(ChannelHandlerContext, ByteBuf, List)} as long as decoding should take place. * * @param ctx the {@link ChannelHandlerContext} which this {@link ByteToMessageDecoder} belongs to * @param in the {@link ByteBuf} from which to read data * @param out the {@link List} to which decoded messages should be added *///from w ww . j a v a 2 s . c o m protected void callDecode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { try { while (in.isReadable()) { int outSize = out.size(); int oldInputLength = in.readableBytes(); decode(ctx, in, out); // Check if this handler was removed before try to continue the loop. // If it was removed it is not safe to continue to operate on the buffer // // See https://github.com/netty/netty/issues/1664 if (ctx.isRemoved()) { break; } if (outSize == out.size()) { if (oldInputLength == in.readableBytes()) { break; } else { continue; } } if (oldInputLength == in.readableBytes()) { throw new DecoderException(StringUtil.simpleClassName(getClass()) + ".decode() did not read anything but decoded a message."); } if (isSingleDecode()) { break; } } } catch (DecoderException e) { throw e; } catch (Throwable cause) { throw new DecoderException(cause); } }
From source file:com.chat.common.netty.handler.decode.LengthFieldBasedFrameDecoder.java
License:Apache License
/** * Decodes the specified region of the buffer into an unadjusted frame length. The default implementation is * capable of decoding the specified region into an unsigned 8/16/24/32/64 bit integer. Override this method to * decode the length field encoded differently. Note that this method must not modify the state of the specified * buffer (e.g. {@code readerIndex}, {@code writerIndex}, and the content of the buffer.) * * @throws DecoderException if failed to decode the specified region *///from w ww.j a v a 2 s .c o m protected long getUnadjustedFrameLength(ByteBuf buf, int offset, int length, ByteOrder order) { buf = buf.order(order); long frameLength; switch (length) { case 1: frameLength = buf.getUnsignedByte(offset); break; case 2: frameLength = buf.getUnsignedShort(offset); break; case 3: frameLength = buf.getUnsignedMedium(offset); break; case 4: frameLength = buf.getUnsignedInt(offset); break; case 8: frameLength = buf.getLong(offset); break; default: throw new DecoderException( "unsupported lengthFieldLength: " + lengthFieldLength + " (expected: 1, 2, 3, 4, or 8)"); } return frameLength; }
From source file:com.digitalpetri.modbus.codec.ModbusRequestDecoder.java
License:Apache License
@Override public ModbusPdu decode(ByteBuf buffer) throws DecoderException { int code = buffer.readByte(); FunctionCode functionCode = FunctionCode.fromCode(code); if (functionCode == null) { throw new DecoderException("invalid function code: " + code); }/*w w w .j a va2 s. c o m*/ return decodeResponse(functionCode, buffer); }