Search in sources :

Example 1 with Buffer

use of org.vertx.java.core.buffer.Buffer in project fabric8 by jboss-fuse.

the class StompFrame method decodeHeader.

/*
    public int size() {
        int rc = action.length() + 1;
        if( headerList!=null ) {
            for (HeaderEntry entry : headerList) {
                rc += entry.getKey().length() + entry.getValue().length() + 2;
            }
        } else {
            for (Map.Entry<Ascii,Ascii> entry : headerMap.entrySet()) {
                rc += entry.getKey().length() + entry.getValue().length() + 2;
            }
        }
        rc += content.length() + 3;
        return rc;
    }
    */
/*
    public void write(DataOutput out, boolean includeBody) throws IOException {
        write(out, action);
        out.writeByte(NEWLINE_BYTE);

        if( headerList!=null ) {
            for (HeaderEntry entry : headerList) {
                write(out, entry.getKey());
                out.writeByte(COLON_BYTE);
                write(out, entry.getValue());
                out.writeByte(NEWLINE_BYTE);
            }
        } else {
            for (Map.Entry<Ascii,Ascii> entry : headerMap.entrySet()) {
                write(out, entry.getKey());
                out.writeByte(COLON_BYTE);
                write(out, entry.getValue());
                out.writeByte(NEWLINE_BYTE);
            }
        }

        //denotes end of headers with a new line
        out.writeByte(NEWLINE_BYTE);
        if (includeBody) {
            write(out, content);
            out.writeByte(NULL_BYTE);
            out.writeByte(NEWLINE_BYTE);
        }
    }
    */
/*
    public String toString() {
        return toBuffer(false).ascii().toString();
    }

    public String errorMessage() {
        Ascii value = getHeader(MESSAGE_HEADER);
        if (value != null) {
            return decodeHeader(value);
        } else {
            return contentAsString();
        }
    }
    */
public static String decodeHeader(Buffer value) {
    if (value == null)
        return null;
    Buffer rc = new Buffer(value.length());
    int pos = 0;
    int max = value.length();
    while (pos < max) {
        if (startsWith(value, pos, ESCAPE_ESCAPE_SEQ.toBuffer())) {
            rc.appendByte(ESCAPE_BYTE);
            pos += 2;
        } else if (startsWith(value, pos, COLON_ESCAPE_SEQ.toBuffer())) {
            rc.appendByte(COLON_BYTE);
            pos += 2;
        } else if (startsWith(value, pos, NEWLINE_ESCAPE_SEQ.toBuffer())) {
            rc.appendByte(NEWLINE_BYTE);
            pos += 2;
        } else {
            rc.appendByte(value.getByte(pos));
            pos += 1;
        }
    }
    return rc.toString();
}
Also used : Buffer(org.vertx.java.core.buffer.Buffer)

Example 2 with Buffer

use of org.vertx.java.core.buffer.Buffer in project fabric8 by jboss-fuse.

the class StompProtocolDecoder method read_binary_body.

private Action<StompFrame> read_binary_body(final StompFrame frame, final int contentLength) {
    return new Action<StompFrame>() {

        public StompFrame apply() throws IOException {
            Buffer content = readBytes(contentLength + 1);
            if (content != null) {
                if (content.getByte(contentLength) != 0) {
                    throw new IOException("Expected null terminator after " + contentLength + " content bytes");
                }
                frame.content(chomp(content));
                nextDecodeAction = read_action;
                return frame;
            } else {
                return null;
            }
        }
    };
}
Also used : Buffer(org.vertx.java.core.buffer.Buffer) IOException(java.io.IOException)

Example 3 with Buffer

use of org.vertx.java.core.buffer.Buffer in project fabric8 by jboss-fuse.

the class SslSocketWrapper method handshake.

public void handshake() {
    if (failed)
        return;
    try {
        while (true) {
            SSLEngineResult.HandshakeStatus status = engine.getHandshakeStatus();
            switch(status) {
                case FINISHED:
                case NOT_HANDSHAKING:
                    return;
                case NEED_TASK:
                    final Runnable task = engine.getDelegatedTask();
                    if (task != null) {
                        task.run();
                    }
                    break;
                case NEED_WRAP:
                    if (plainWriteBuffer == null) {
                        plainWriteBuffer = new Buffer();
                    }
                    pumpWrites(false);
                    break;
                case NEED_UNWRAP:
                    if (encryptedReadBuffer != null) {
                        pumpReads(false);
                        break;
                    } else {
                        return;
                    }
                default:
                    System.err.println("Unexpected ssl engine handshake status: " + status);
                    break;
            }
        }
    } finally {
        SSLEngineResult.HandshakeStatus status = engine.getHandshakeStatus();
        if (status == NOT_HANDSHAKING) {
            pumpWrites(false);
            pumpReads(false);
        }
    }
}
Also used : Buffer(org.vertx.java.core.buffer.Buffer) ByteBuffer(java.nio.ByteBuffer) SSLEngineResult(javax.net.ssl.SSLEngineResult)

Example 4 with Buffer

use of org.vertx.java.core.buffer.Buffer in project fabric8 by jboss-fuse.

the class SslSocketWrapper method pumpReads.

private void pumpReads(boolean allowHandshake) {
    boolean pump = true;
    while (pump) {
        pump = false;
        if (readPaused > 0 || failed) {
            return;
        }
        if (encryptedReadBuffer != null && plainReadBuffer == null && !encryptedReadBufferUnderflow) {
            ByteBuffer input = ByteBuffer.wrap(encryptedReadBuffer.getBytes());
            ByteBuffer output = ByteBuffer.allocate(engine.getSession().getApplicationBufferSize());
            try {
                boolean done = false;
                while (!done) {
                    done = true;
                    SSLEngineResult result = engine.unwrap(input, output);
                    switch(result.getStatus()) {
                        case CLOSED:
                            engine.closeInbound();
                            break;
                        case BUFFER_UNDERFLOW:
                            encryptedReadBufferUnderflow = true;
                            break;
                        case OK:
                            switch(engine.getHandshakeStatus()) {
                                case NEED_TASK:
                                case NEED_WRAP:
                                    break;
                                default:
                                    done = !input.hasRemaining();
                            }
                            break;
                        case BUFFER_OVERFLOW:
                            throw new SSLException("BUFFER_OVERFLOW");
                    }
                    // Lets fill the plain buffer..
                    output.flip();
                    if (output.remaining() > 0) {
                        pump = true;
                        int len = output.remaining();
                        if (plainReadBuffer == null) {
                            plainReadBuffer = new Buffer(len);
                        }
                        plainReadBuffer.appendBytes(output.array(), output.arrayOffset() + output.position(), len);
                    }
                    output.clear();
                }
            } catch (SSLException e) {
                onFailure(e);
                return;
            } finally {
                int len = input.remaining();
                if (len > 0) {
                    // we need to compact the encryptedReadBuffer
                    if (input.position() != 0) {
                        encryptedReadBuffer = new Buffer(len);
                        encryptedReadBuffer.appendBytes(input.array(), input.arrayOffset() + input.position(), len);
                    }
                } else {
                    // everything was consumed.
                    encryptedReadBuffer = null;
                }
            }
        }
        // Send the plain buffer to the the data handler...
        if (plainReadBuffer != null && readPaused == 0) {
            pump = true;
            Buffer data = plainReadBuffer;
            plainReadBuffer = null;
            Handler<Buffer> handler = plainDataHandler;
            if (handler != null) {
                handler.handle(data);
            }
        }
        if (encryptedReadBuffer == null && plainReadBuffer == null && encryptedReadEOF) {
            encryptedReadEOF = false;
            Handler<Void> handler = plainEndHandler;
            if (handler != null) {
                handler.handle(null);
            }
        }
        if (engine.getHandshakeStatus() != NOT_HANDSHAKING) {
            if (allowHandshake) {
                handshake();
            }
            return;
        }
    }
}
Also used : Buffer(org.vertx.java.core.buffer.Buffer) ByteBuffer(java.nio.ByteBuffer) SSLEngineResult(javax.net.ssl.SSLEngineResult) ByteBuffer(java.nio.ByteBuffer) SSLException(javax.net.ssl.SSLException)

Example 5 with Buffer

use of org.vertx.java.core.buffer.Buffer in project fabric8 by jboss-fuse.

the class Ascii method encode.

public static Buffer encode(String value) {
    int size = value.length();
    Buffer rc = new Buffer(size);
    for (int i = 0; i < size; i++) {
        rc.appendByte((byte) (value.charAt(i) & 0xFF));
    }
    return rc;
}
Also used : Buffer(org.vertx.java.core.buffer.Buffer)

Aggregations

Buffer (org.vertx.java.core.buffer.Buffer)14 ConnectionParameters (io.fabric8.gateway.handlers.loadbalancer.ConnectionParameters)3 ByteBuffer (java.nio.ByteBuffer)3 SSLEngineResult (javax.net.ssl.SSLEngineResult)3 IOException (java.io.IOException)2 SSLException (javax.net.ssl.SSLException)2 Ascii (io.fabric8.gateway.handlers.detecting.protocol.Ascii)1 SslSocketWrapper (io.fabric8.gateway.handlers.detecting.protocol.ssl.SslSocketWrapper)1 ConnectTimeoutException (io.netty.channel.ConnectTimeoutException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 InetSocketAddress (java.net.InetSocketAddress)1 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1