org.apache.mina.filter.codec.ProtocolDecoderException Java Examples

The following examples show how to use org.apache.mina.filter.codec.ProtocolDecoderException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ProtocolDeserializerImpl.java    From sumk with Apache License 2.0 6 votes vote down vote up
@Override
public Object deserialize(Object message) throws Exception {
	if (message == null) {
		return null;
	}
	if (!ProtocolObject.class.isInstance(message)) {
		SumkException.throwException(458223, message.getClass().getName() + " is error type");
	}
	ProtocolObject obj = ProtocolObject.class.cast(message);
	if (obj.getData().length == 0) {
		return null;
	}
	int protocol = obj.getProtocol();
	for (SumkMinaDeserializer<?> decoder : this.decoders) {
		if (decoder.accept(protocol)) {
			return decoder.decode(protocol, obj.getData());
		}
	}
	throw new ProtocolDecoderException("no sumk decoder:" + Integer.toHexString(protocol));
}
 
Example #2
Source File: ConsumeToEndOfSessionDecodingState.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out) throws Exception {
    if (buffer == null) {
        buffer = IoBuffer.allocate(256).setAutoExpand(true);
    }

    if (buffer.position() + in.remaining() > maxLength) {
        throw new ProtocolDecoderException("Received data exceeds " + maxLength + " byte(s).");
    }
    buffer.put(in);
    return this;
}
 
Example #3
Source File: CrLfDecodingState.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out) throws Exception {
    boolean found = false;
    boolean finished = false;
    while (in.hasRemaining()) {
        byte b = in.get();
        if (!hasCR) {
            if (b == CR) {
                hasCR = true;
            } else {
                if (b == LF) {
                    found = true;
                } else {
                    in.position(in.position() - 1);
                    found = false;
                }
                finished = true;
                break;
            }
        } else {
            if (b == LF) {
                found = true;
                finished = true;
                break;
            }

            throw new ProtocolDecoderException("Expected LF after CR but was: " + (b & 0xff));
        }
    }

    if (finished) {
        hasCR = false;
        return finishDecode(found, out);
    }

    return this;
}
 
Example #4
Source File: SumkProtocolDecoder.java    From sumk with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out)
		throws ProtocolDecoderException, CharacterCodingException {
	if (in.remaining() < 5) {
		return false;
	}
	final int pos = in.position();
	if (!this.innerDecode(session, in, out)) {
		in.position(pos);
		return false;
	}
	return true;
}
 
Example #5
Source File: HackedProtocolCodecFilter.java    From sailfish-core with Apache License 2.0 4 votes vote down vote up
/**
 * Process the incoming message, calling the session decoder. As the incoming
 * buffer might contains more than one messages, we have to loop until the decoder
 * throws an exception.
 *
 *  while ( buffer not empty )
 *    try
 *      decode ( buffer )
 *    catch
 *      break;
 *
 */
@Override
public void messageReceived(NextFilter nextFilter, IoSession session, Object message) throws Exception {
    LOGGER.debug("Processing a MESSAGE_RECEIVED for session {}", session.getId());

    if (!(message instanceof IoBuffer)) {
        nextFilter.messageReceived(session, message);
        return;
    }

    IoBuffer in = (IoBuffer) message;
    ProtocolDecoder decoder = factory.getDecoder(session);
    ProtocolDecoderOutput decoderOut = getDecoderOut(session, nextFilter);

    // Loop until we don't have anymore byte in the buffer,
    // or until the decoder throws an unrecoverable exception or
    // can't decoder a message, because there are not enough
    // data in the buffer
    while (in.hasRemaining()) {
        int oldPos = in.position();

        try {
            synchronized (decoderOut) {
                // Call the decoder with the read bytes
                decoder.decode(session, in, decoderOut);
            }

            // Finish decoding if no exception was thrown.
            decoderOut.flush(nextFilter, session);
        } catch (Exception e) {
            ProtocolDecoderException pde = e instanceof ProtocolDecoderException ? (ProtocolDecoderException)e : new ProtocolDecoderException(e);

            if (pde.getHexdump() == null) {
                // Generate a message hex dump
                int curPos = in.position();
                in.position(oldPos);
                pde.setHexdump(in.getHexDump());
                in.position(curPos);
            }

            // Fire the exceptionCaught event.
            decoderOut.flush(nextFilter, session);
            nextFilter.exceptionCaught(session, pde);

            // Retry only if the type of the caught exception is
            // recoverable and the buffer position has changed.
            // We check buffer position additionally to prevent an
            // infinite loop.
            if (!(e instanceof RecoverableProtocolDecoderException) || (in.position() == oldPos)) {
                break;
            }
        }
    }
}
 
Example #6
Source File: TextLineDecoder.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Decode a line using the default delimiter on the current system
 */
private void decodeAuto(Context ctx, IoSession session, IoBuffer in, ProtocolDecoderOutput out)
        throws CharacterCodingException, ProtocolDecoderException {
    int matchCount = ctx.getMatchCount();

    // Try to find a match
    int oldPos = in.position();
    int oldLimit = in.limit();

    while (in.hasRemaining()) {
        byte b = in.get();
        boolean matched = false;

        switch (b) {
        case '\r':
            // Might be Mac, but we don't auto-detect Mac EOL
            // to avoid confusion.
            matchCount++;
            break;

        case '\n':
            // UNIX
            matchCount++;
            matched = true;
            break;

        default:
            matchCount = 0;
        }

        if (matched) {
            // Found a match.
            int pos = in.position();
            in.limit(pos);
            in.position(oldPos);

            ctx.append(in);

            in.limit(oldLimit);
            in.position(pos);

            if (ctx.getOverflowPosition() == 0) {
                IoBuffer buf = ctx.getBuffer();
                buf.flip();
                buf.limit(buf.limit() - matchCount);

                try {
                    byte[] data = new byte[buf.limit()];
                    buf.get(data);
                    CharsetDecoder decoder = ctx.getDecoder();

                    CharBuffer buffer = decoder.decode(ByteBuffer.wrap(data));
                    String str = new String(buffer.array());
                    writeText(session, str, out);
                } finally {
                    buf.clear();
                }
            } else {
                int overflowPosition = ctx.getOverflowPosition();
                ctx.reset();
                throw new RecoverableProtocolDecoderException("Line is too long: " + overflowPosition);
            }

            oldPos = pos;
            matchCount = 0;
        }
    }

    // Put remainder to buf.
    in.position(oldPos);
    ctx.append(in);

    ctx.setMatchCount(matchCount);
}
 
Example #7
Source File: TextLineDecoder.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Decode a line using the delimiter defined by the caller
 */
private void decodeNormal(Context ctx, IoSession session, IoBuffer in, ProtocolDecoderOutput out)
        throws CharacterCodingException, ProtocolDecoderException {
    int matchCount = ctx.getMatchCount();

    // Try to find a match
    int oldPos = in.position();
    int oldLimit = in.limit();

    while (in.hasRemaining()) {
        byte b = in.get();

        if (delimBuf.get(matchCount) == b) {
            matchCount++;

            if (matchCount == delimBuf.limit()) {
                // Found a match.
                int pos = in.position();
                in.limit(pos);
                in.position(oldPos);

                ctx.append(in);

                in.limit(oldLimit);
                in.position(pos);

                if (ctx.getOverflowPosition() == 0) {
                    IoBuffer buf = ctx.getBuffer();
                    buf.flip();
                    buf.limit(buf.limit() - matchCount);

                    try {
                        writeText(session, buf.getString(ctx.getDecoder()), out);
                    } finally {
                        buf.clear();
                    }
                } else {
                    int overflowPosition = ctx.getOverflowPosition();
                    ctx.reset();
                    throw new RecoverableProtocolDecoderException("Line is too long: " + overflowPosition);
                }

                oldPos = pos;
                matchCount = 0;
            }
        } else {
            // fix for DIRMINA-506 & DIRMINA-536
            in.position(Math.max(0, in.position() - matchCount));
            matchCount = 0;
        }
    }

    // Put remainder to buf.
    in.position(oldPos);
    ctx.append(in);

    ctx.setMatchCount(matchCount);
}
 
Example #8
Source File: IntegerDecodingState.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public DecodingState finishDecode(ProtocolDecoderOutput out) throws Exception {
    throw new ProtocolDecoderException("Unexpected end of session while waiting for an integer.");
}
 
Example #9
Source File: SingleByteDecodingState.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public DecodingState finishDecode(ProtocolDecoderOutput out) throws Exception {
    throw new ProtocolDecoderException("Unexpected end of session while waiting for a single byte.");
}
 
Example #10
Source File: ShortIntegerDecodingState.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public DecodingState finishDecode(ProtocolDecoderOutput out) throws Exception {
    throw new ProtocolDecoderException("Unexpected end of session while waiting for a short integer.");
}
 
Example #11
Source File: SumkProtocolDecoder.java    From sumk with Apache License 2.0 4 votes vote down vote up
protected boolean innerDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out)
		throws CharacterCodingException, ProtocolDecoderException {
	int protocol = in.getInt();
	if ((protocol & 0xFF000000) != Protocols.MAGIC) {
		if (Logs.rpc().isTraceEnabled()) {
			Logs.rpc().trace(in.getString(Profile.UTF8.newDecoder()));
		}
		throw new ProtocolDecoderException("error magic," + Integer.toHexString(protocol));
	}
	int prefixLength = 0, maxDataLength = 0;
	if ((protocol & Protocols.ONE) != 0) {
		prefixLength = 1;
		maxDataLength = 0xFF;
	} else if ((protocol & Protocols.TWO) != 0) {
		prefixLength = 2;
		maxDataLength = 0xFFFF;
	} else if ((protocol & Protocols.FOUR) != 0) {
		prefixLength = 4;
		maxDataLength = Protocols.MAX_LENGTH;
	} else {
		if (AppInfo.getBoolean("sumk.rpc.log.code.error", true)) {
			Logs.rpc().error("error byte length protocol," + Integer.toHexString(protocol));
		}
		throw new ProtocolDecoderException("error byte length protocol," + Integer.toHexString(protocol));
	}

	if (in.remaining() < prefixLength) {
		return false;
	}
	int dataSize = 0;
	switch (prefixLength) {
	case 1:
		dataSize = in.getUnsigned();
		break;
	case 2:
		dataSize = in.getUnsignedShort();
		break;
	case 4:
		dataSize = in.getInt();
		break;
	}
	if (dataSize < 0 || dataSize > maxDataLength) {
		throw new BufferDataException("dataLength: " + dataSize);
	}
	if (in.remaining() < dataSize) {
		return false;
	}

	byte[] bs = new byte[dataSize];
	in.get(bs);
	out.write(new ProtocolObject(protocol, bs));
	return true;
}