Package org.glassfish.grizzly

Examples of org.glassfish.grizzly.Buffer


            }
        } catch (SSLException e) {
            throw new TransformationException(e);
        }
       
        final Buffer targetBuffer = memoryManager.allocate(
                    sslEngine.getSession().getApplicationBufferSize());

        TransformationResult<Buffer, Buffer> transformationResult = null;

        try {
            if (LOGGER.isLoggable(Level.FINE)) {
                LOGGER.log(Level.FINE, "SSLDecoder engine: {0} input: {1} output: {2}",
                        new Object[]{sslEngine, originalMessage, targetBuffer});
            }

            final int pos = originalMessage.position();
            final SSLEngineResult sslEngineResult;
            if (!originalMessage.isComposite()) {
                sslEngineResult = sslEngine.unwrap(originalMessage.toByteBuffer(),
                        targetBuffer.toByteBuffer());
            } else {
                final ByteBuffer originalByteBuffer =
                        originalMessage.toByteBuffer(pos,
                        pos + expectedLength);

                sslEngineResult = sslEngine.unwrap(originalByteBuffer,
                        targetBuffer.toByteBuffer());
            }
           
            originalMessage.position(pos + sslEngineResult.bytesConsumed());
            targetBuffer.position(sslEngineResult.bytesProduced());


            final SSLEngineResult.Status status = sslEngineResult.getStatus();

            if (LOGGER.isLoggable(Level.FINE)) {
                LOGGER.log(Level.FINE, "SSLDecoderr done engine: {0} result: {1} input: {2} output: {3}",
                        new Object[]{sslEngine, sslEngineResult, originalMessage, targetBuffer});
            }

            if (status == SSLEngineResult.Status.OK) {
                targetBuffer.trim();

                return TransformationResult.createCompletedResult(
                        targetBuffer, originalMessage);
            } else if (status == SSLEngineResult.Status.CLOSED) {
                targetBuffer.dispose();

                return TransformationResult.createCompletedResult(
                        Buffers.EMPTY_BUFFER, originalMessage);
            } else {
                targetBuffer.dispose();

                if (status == SSLEngineResult.Status.BUFFER_UNDERFLOW) {
                    transformationResult =
                            TransformationResult.createIncompletedResult(
                            originalMessage);
                } else if (status == SSLEngineResult.Status.BUFFER_OVERFLOW) {
                    transformationResult =
                            TransformationResult.createErrorResult(
                            BUFFER_OVERFLOW_ERROR,
                            "Buffer overflow during unwrap operation");
                }
            }
        } catch (SSLException e) {
            targetBuffer.dispose();
            throw new TransformationException(e);
        }

        return transformationResult;
    }
View Full Code Here


            // if we have just parsed a HTTP message header
            // assign chunkRemainder to the HTTP message content length
            contentParsingState.chunkRemainder = httpPacket.getContentLength();
        }

        Buffer remainder = null;

        final long thisPacketRemaining = contentParsingState.chunkRemainder;
        final int available = input.remaining();

        if (available > thisPacketRemaining) {
View Full Code Here

    public void setNewConnectionFilterChain(FilterChain newConnectionFilterChain) {
        this.newConnectionFilterChain = newConnectionFilterChain;
    }

    Buffer resetLastOutputBuffer() {
        final Buffer tmp = lastOutputBuffer;
        lastOutputBuffer = null;
        return tmp;
    }
View Full Code Here

        final ByteBufferArray bba =
                input.toByteBufferArray(inputByteBufferArray);
        final ByteBuffer[] inputArray = bba.getArray();
        final int inputArraySize = bba.size();
       
        Buffer output = null;
        SslResult result = null;
        try {
            result = wrap(input, inputArray, inputArraySize, null, allocator);
           
            if (result.isError()) {
                throw result.getError();
            }
           
            output = result.getOutput();
            output.trim();
           
            if (input.hasRemaining()) {
                do {
                    result = wrap(input, inputArray, inputArraySize,
                            null, allocator);
                   
                    if (result.isError()) {
                        throw result.getError();
                    }
                   
                    final Buffer newOutput = result.getOutput();
                    newOutput.trim();
                   
                    output = Buffers.appendBuffers(memoryManager, output,
                            newOutput);
                } while (input.hasRemaining());
            }
View Full Code Here

                sslEngine.beginHandshake();
                sslCtx.configure(sslEngine);
                notifyHandshakeStart(connection);
            }

            final Buffer buffer;
            if (handshakeTimeoutMillis >= 0) {
                buffer = doHandshakeSync(sslCtx, ctx, (Buffer) ctx.getMessage(),
                        handshakeTimeoutMillis);
            } else {
                buffer = makeInputRemainder(sslCtx, ctx,
                        doHandshakeStep(sslCtx, ctx, (Buffer) ctx.getMessage()));
            }
       
            final boolean hasRemaining = buffer != null && buffer.hasRemaining();
           
            final boolean isHandshaking = isHandshaking(sslEngine);
            if (!isHandshaking) {
                notifyHandshakeComplete(connection, sslEngine);
                final FilterChain connectionFilterChain = sslCtx.getNewConnectionFilterChain();
View Full Code Here

        }

        final Connection connection = ctx.getConnection();
       
        synchronized(connection) {
            final Buffer output =
                    wrapAll(ctx, obtainSslConnectionContext(connection));

            final FilterChainContext.TransportContext transportContext =
                    ctx.getTransportContext();
View Full Code Here

    // ------------------------------------------------------- Protected Methods

    protected NextAction unwrapAll(final FilterChainContext ctx,
            final SSLConnectionContext sslCtx) throws SSLException {
        Buffer input = ctx.getMessage();
       
        Buffer output = null;
       
        boolean isClosed = false;
       
        _outter:
        do {
            final int len = getSSLPacketSize(input);
           
            if (len == -1 || input.remaining() < len) {
                break;
            }

            final SSLConnectionContext.SslResult result =
                    sslCtx.unwrap(input, output, MM_ALLOCATOR);
           
            if (isHandshaking(sslCtx.getSslEngine())) {
                input = rehandshake(ctx, sslCtx);
                if (input == null) {
                    break;
                }
            }
           
            output = result.getOutput();

            if (result.isError()) {
                output.dispose();
                throw result.getError();
            }
           
            switch (result.getSslEngineResult().getStatus()) {
                case OK:
                    if (input.hasRemaining()) {
                        break;
                    }

                    break _outter;
                case CLOSED:
                    isClosed = true;
                    break _outter;
                default:
                    // Should not reach this point
                    throw new IllegalStateException("Unexpected status: " +
                            result.getSslEngineResult().getStatus());
            }
        } while (true);
       
        if (output != null) {
            output.trim();

            if (output.hasRemaining() || isClosed) {
                ctx.setMessage(output);
                return ctx.getInvokeAction(makeInputRemainder(sslCtx, ctx, input));
            }
        }
View Full Code Here

    }

    protected Buffer wrapAll(final FilterChainContext ctx,
            final SSLConnectionContext sslCtx) throws SSLException {
       
        final Buffer input = ctx.getMessage();
       
        final Buffer output = sslCtx.wrapAll(input, OUTPUT_BUFFER_ALLOCATOR);

        input.tryDispose();

        return output;
    }
View Full Code Here

            final long timeoutMillis) throws IOException {
       
        final Connection connection = ctx.getConnection();
        final SSLEngine sslEngine = sslCtx.getSslEngine();
       
        final Buffer tmpAppBuffer = allocateOutputBuffer(sslCtx.getAppBufferSize());
       
        final long oldReadTimeout = connection.getReadTimeout(TimeUnit.MILLISECONDS);
       
        try {
            connection.setReadTimeout(timeoutMillis, TimeUnit.MILLISECONDS);
           
            inputBuffer = makeInputRemainder(sslCtx, ctx,
                    doHandshakeStep(sslCtx, ctx, inputBuffer, tmpAppBuffer));

            while (isHandshaking(sslEngine)) {
                final ReadResult rr = ctx.read();
                final Buffer newBuf = (Buffer) rr.getMessage();
                inputBuffer = Buffers.appendBuffers(ctx.getMemoryManager(),
                        inputBuffer, newBuf);
                inputBuffer = makeInputRemainder(sslCtx, ctx,
                        doHandshakeStep(sslCtx, ctx, inputBuffer, tmpAppBuffer));
            }
View Full Code Here

        final SSLEngine sslEngine = sslCtx.getSslEngine();
        final Connection connection = ctx.getConnection();
       
        final boolean isLoggingFinest = LOGGER.isLoggable(Level.FINEST);
        Buffer tmpInputToDispose = null;
        Buffer tmpNetBuffer = null;
       
        Buffer tmpAppBuffer = tmpAppBuffer0;
       
        try {
            HandshakeStatus handshakeStatus = sslEngine.getHandshakeStatus();

            _exitWhile:
           
            while (true) {

                if (isLoggingFinest) {
                    LOGGER.log(Level.FINEST, "Loop Engine: {0} handshakeStatus={1}",
                            new Object[]{sslEngine, sslEngine.getHandshakeStatus()});
                }

                switch (handshakeStatus) {
                    case NEED_UNWRAP: {

                        if (isLoggingFinest) {
                            LOGGER.log(Level.FINEST, "NEED_UNWRAP Engine: {0}", sslEngine);
                        }

                        if (inputBuffer == null || !inputBuffer.hasRemaining()) {
                            break _exitWhile;
                        }

                        final int expectedLength = getSSLPacketSize(inputBuffer);
                        if (expectedLength == -1
                                || inputBuffer.remaining() < expectedLength) {
                            break _exitWhile;
                        }
                       
                        if (tmpAppBuffer == null) {
                            tmpAppBuffer = allocateOutputBuffer(sslCtx.getAppBufferSize());
                        }
                       
                        final SSLEngineResult sslEngineResult =
                                handshakeUnwrap(sslCtx, inputBuffer, tmpAppBuffer);

                        if (!inputBuffer.hasRemaining()) {
                            tmpInputToDispose = inputBuffer;
                            inputBuffer = null;
                        }

                        final SSLEngineResult.Status status = sslEngineResult.getStatus();

                        if (status == Status.BUFFER_UNDERFLOW ||
                                status == Status.BUFFER_OVERFLOW) {
                            throw new SSLException("SSL unwrap error: " + status);
                        }

                        handshakeStatus = sslEngine.getHandshakeStatus();
                        break;
                    }

                    case NEED_WRAP: {
                        if (isLoggingFinest) {
                            LOGGER.log(Level.FINEST, "NEED_WRAP Engine: {0}", sslEngine);
                        }

                        tmpNetBuffer = handshakeWrap(
                                connection, sslCtx, tmpNetBuffer);
                        handshakeStatus = sslEngine.getHandshakeStatus();

                        break;
                    }

                    case NEED_TASK: {
                        if (isLoggingFinest) {
                            LOGGER.log(Level.FINEST, "NEED_TASK Engine: {0}", sslEngine);
                        }
                        executeDelegatedTask(sslEngine);
                        handshakeStatus = sslEngine.getHandshakeStatus();
                        break;
                    }

                    case FINISHED:
                    case NOT_HANDSHAKING: {
                        break _exitWhile;
                    }
                }

                if (handshakeStatus == HandshakeStatus.FINISHED) {
                    break _exitWhile;
                }
            }
        } catch (IOException ioe) {
            notifyHandshakeFailed(connection, ioe);
            throw ioe;
        } finally {
            if (tmpAppBuffer0 == null && tmpAppBuffer != null) {
                tmpAppBuffer.dispose();
            }
           
            if (tmpInputToDispose != null) {
                tmpInputToDispose.tryDispose();
                inputBuffer = null;
View Full Code Here

TOP

Related Classes of org.glassfish.grizzly.Buffer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.