mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Yannick Lecaillez
20.44.2016 5511a94238385a30b5b516ee360b234ff56d7c3f
opendj-grizzly/src/main/java/org/forgerock/opendj/grizzly/LdapCodec.java
@@ -36,7 +36,7 @@
import org.glassfish.grizzly.filterchain.FilterChainContext;
import org.glassfish.grizzly.filterchain.NextAction;
final class LdapCodec extends LDAPBaseFilter {
abstract class LdapCodec extends LDAPBaseFilter {
    LdapCodec(final int maxElementSize, final DecodeOptions decodeOptions) {
        super(decodeOptions, maxElementSize);
@@ -44,15 +44,29 @@
    @Override
    public NextAction handleRead(final FilterChainContext ctx) throws IOException {
        final Buffer buffer = ctx.getMessage();
        final LdapRawMessage message = readMessage(buffer);
        if (message != null) {
            ctx.setMessage(message);
            return ctx.getInvokeAction(getRemainingBuffer(buffer));
        try {
            final Buffer buffer = ctx.getMessage();
            final LdapRawMessage message;
            message = readMessage(buffer);
            if (message != null) {
                ctx.setMessage(message);
                return ctx.getInvokeAction(getRemainingBuffer(buffer));
            }
            return ctx.getStopAction(getRemainingBuffer(buffer));
        } catch (Exception e) {
            onLdapCodecError(ctx, e);
            // make the connection deaf to any following input
            // onLdapDecodeError call will take care of error processing
            // and closing the connection
            final NextAction suspendAction = ctx.getSuspendAction();
            ctx.completeAndRecycle();
            return suspendAction;
        }
        return ctx.getStopAction(getRemainingBuffer(buffer));
    }
    protected abstract void onLdapCodecError(FilterChainContext ctx, Throwable error);
    private LdapRawMessage readMessage(final Buffer buffer) throws IOException {
        try (final ASN1BufferReader reader = new ASN1BufferReader(maxASN1ElementSize, buffer)) {
            final int packetStart = buffer.position();
@@ -123,10 +137,18 @@
        try {
            final Buffer buffer = toBuffer(writer, ctx.<LdapResponseMessage> getMessage());
            ctx.setMessage(buffer);
            return ctx.getInvokeAction();
        } catch (Exception e) {
            onLdapCodecError(ctx, e);
            // make the connection deaf to any following input
            // onLdapDecodeError call will take care of error processing
            // and closing the connection
            final NextAction suspendAction = ctx.getSuspendAction();
            ctx.completeAndRecycle();
            return suspendAction;
        } finally {
            GrizzlyUtils.recycleWriter(writer);
        }
        return ctx.getInvokeAction();
    }
    private Buffer toBuffer(final LDAPWriter<ASN1BufferWriter> writer, final LdapResponseMessage message)