Example usage for io.netty.buffer Unpooled copiedBuffer

List of usage examples for io.netty.buffer Unpooled copiedBuffer

Introduction

In this page you can find the example usage for io.netty.buffer Unpooled copiedBuffer.

Prototype

private static ByteBuf copiedBuffer(CharBuffer buffer, Charset charset) 

Source Link

Usage

From source file:com.couchbase.client.core.endpoint.util.ClosingPositionBufProcessorTest.java

License:Apache License

@Test
public void shouldIgnoreJsonStringWithClosingSectionCharEvenIfStreamInterrupted() {
    ByteBuf source = Unpooled.copiedBuffer("{ this is \"a string \\\"with }", CharsetUtil.UTF_8);

    int closingPos = source.forEachByte(new ClosingPositionBufProcessor('{', '}', true));

    assertEquals(-1, closingPos);//from  w  w  w .ja  v  a  2 s.c o  m
    assertEquals(0, source.readerIndex());
}

From source file:com.couchbase.client.core.endpoint.util.ClosingPositionBufProcessorTest.java

License:Apache License

@Test
public void shouldSkipStringWithEscapedBackslashJustBeforeClosingQuote() {
    ByteBuf source = Unpooled.copiedBuffer("{\"some\": \"weird }object\\\\\"}", CharsetUtil.UTF_8);
    int closingPos = source.forEachByte(new ClosingPositionBufProcessor('{', '}', true));

    assertEquals(26, closingPos);/* www.  j  a va2s .  c  o  m*/
    assertEquals(0, source.readerIndex());
}

From source file:com.couchbase.client.core.endpoint.util.ClosingPositionBufProcessorTest.java

License:Apache License

@Test
public void shouldSkipEmptyString() {
    ByteBuf source = Unpooled.copiedBuffer("{\"some\": \"\"}", CharsetUtil.UTF_8);
    int closingPos = source.forEachByte(new ClosingPositionBufProcessor('{', '}', true));

    assertEquals(11, closingPos);/*from w ww  . jav  a 2s  . c  o  m*/
    assertEquals(0, source.readerIndex());
}

From source file:com.couchbase.client.core.endpoint.util.ClosingPositionBufProcessorTest.java

License:Apache License

@Test
public void shouldSkipStringWithEscapedBackslashOnly() {
    ByteBuf source = Unpooled.copiedBuffer("{\"some\": \"\\\\\"}", CharsetUtil.UTF_8);
    int closingPos = source.forEachByte(new ClosingPositionBufProcessor('{', '}', true));

    assertEquals(13, closingPos);//www .j a  va  2  s  .  c o  m
    assertEquals(0, source.readerIndex());
}

From source file:com.couchbase.client.core.endpoint.util.StringClosingPositionBufProcessorTest.java

License:Apache License

@Test
public void testClosingPosFoundInSimpleString() {
    ByteBuf source = Unpooled.copiedBuffer("\" \"", CharsetUtil.UTF_8);

    int closingPos = source.forEachByte(new StringClosingPositionBufProcessor());

    assertEquals(2, closingPos);/* ww w  .  ja v a 2  s  .  c  o m*/
    assertEquals(0, source.readerIndex());
}

From source file:com.couchbase.client.core.endpoint.util.StringClosingPositionBufProcessorTest.java

License:Apache License

@Test
public void testClosingPosFoundInStringWithEscapedContent() {
    ByteBuf source = Unpooled.copiedBuffer(" \"Some string with {\\\"escaped\\\"} strings\" \"otherString\"",
            CharsetUtil.UTF_8);/*from ww w  . ja  v a  2  s  .  co  m*/

    int closingPos = source.forEachByte(new StringClosingPositionBufProcessor());

    assertEquals(40, closingPos);
    assertEquals(0, source.readerIndex());
}

From source file:com.couchbase.client.core.endpoint.util.StringClosingPositionBufProcessorTest.java

License:Apache License

@Test
public void testClosingPosNotFoundInPartialStringLeftPart() {
    ByteBuf source = Unpooled.copiedBuffer(" \"\\\"Partial\\\" str", CharsetUtil.UTF_8);

    int closingPos = source.forEachByte(new StringClosingPositionBufProcessor());

    assertEquals(-1, closingPos);/*from  w w  w. j  a  v a2  s .  c om*/
    assertEquals(0, source.readerIndex());
}

From source file:com.couchbase.client.core.endpoint.util.StringClosingPositionBufProcessorTest.java

License:Apache License

@Test
public void testClosingPosNotFoundInPartialStringRightPart() {
    ByteBuf source = Unpooled.copiedBuffer("ring\"", CharsetUtil.UTF_8);

    int closingPos = source.forEachByte(new StringClosingPositionBufProcessor());

    assertEquals(-1, closingPos);/*w w  w  .  j  a v  a2 s .  c  o m*/
    assertEquals(0, source.readerIndex());
}

From source file:com.couchbase.client.core.endpoint.util.StringClosingPositionBufProcessorTest.java

License:Apache License

@Test
public void testClosingPosFoundInStringWithEscapedBackslashLast() {
    ByteBuf source = Unpooled.copiedBuffer("\"abc\\\\\"", CharsetUtil.UTF_8);
    int closingPos = source.forEachByte(new StringClosingPositionBufProcessor());

    assertEquals(6, closingPos);//from  w  ww .  j a  va 2s  .  co  m
    assertEquals(0, source.readerIndex());
}

From source file:com.couchbase.client.core.endpoint.view.ViewHandler.java

License:Apache License

@Override
protected HttpRequest encodeRequest(final ChannelHandlerContext ctx, final ViewRequest msg) throws Exception {
    if (msg instanceof KeepAliveRequest) {
        FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.HEAD, "/",
                Unpooled.EMPTY_BUFFER);/*  w  w w.  j  a v  a  2s  .co  m*/
        request.headers().set(HttpHeaders.Names.USER_AGENT, env().userAgent());
        request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, 0);
        return request;
    }

    StringBuilder path = new StringBuilder();

    HttpMethod method = HttpMethod.GET;
    ByteBuf content = null;
    if (msg instanceof ViewQueryRequest) {
        ViewQueryRequest queryMsg = (ViewQueryRequest) msg;
        path.append("/").append(msg.bucket()).append("/_design/");
        path.append(queryMsg.development() ? "dev_" + queryMsg.design() : queryMsg.design());
        if (queryMsg.spatial()) {
            path.append("/_spatial/");
        } else {
            path.append("/_view/");
        }
        path.append(queryMsg.view());

        int queryLength = queryMsg.query() == null ? 0 : queryMsg.query().length();
        int keysLength = queryMsg.keys() == null ? 0 : queryMsg.keys().length();
        boolean hasQuery = queryLength > 0;
        boolean hasKeys = keysLength > 0;

        if (hasQuery || hasKeys) {
            if (queryLength + keysLength < MAX_GET_LENGTH) {
                //the query is short enough for GET
                //it has query, query+keys or keys only
                if (hasQuery) {
                    path.append("?").append(queryMsg.query());
                    if (hasKeys) {
                        path.append("&keys=").append(encodeKeysGet(queryMsg.keys()));
                    }
                } else {
                    //it surely has keys if not query
                    path.append("?keys=").append(encodeKeysGet(queryMsg.keys()));
                }
            } else {
                //the query is too long for GET, use the keys as JSON body
                if (hasQuery) {
                    path.append("?").append(queryMsg.query());
                }
                String keysContent = encodeKeysPost(queryMsg.keys());

                //switch to POST
                method = HttpMethod.POST;
                //body is "keys" but in JSON
                content = ctx.alloc().buffer(keysContent.length());
                content.writeBytes(keysContent.getBytes(CHARSET));
            }
        }
    } else if (msg instanceof GetDesignDocumentRequest) {
        GetDesignDocumentRequest queryMsg = (GetDesignDocumentRequest) msg;
        path.append("/").append(msg.bucket()).append("/_design/");
        path.append(queryMsg.development() ? "dev_" + queryMsg.name() : queryMsg.name());
    } else if (msg instanceof UpsertDesignDocumentRequest) {
        method = HttpMethod.PUT;
        UpsertDesignDocumentRequest queryMsg = (UpsertDesignDocumentRequest) msg;
        path.append("/").append(msg.bucket()).append("/_design/");
        path.append(queryMsg.development() ? "dev_" + queryMsg.name() : queryMsg.name());
        content = Unpooled.copiedBuffer(queryMsg.body(), CHARSET);
    } else if (msg instanceof RemoveDesignDocumentRequest) {
        method = HttpMethod.DELETE;
        RemoveDesignDocumentRequest queryMsg = (RemoveDesignDocumentRequest) msg;
        path.append("/").append(msg.bucket()).append("/_design/");
        path.append(queryMsg.development() ? "dev_" + queryMsg.name() : queryMsg.name());
    } else {
        throw new IllegalArgumentException("Unknown incoming ViewRequest type " + msg.getClass());
    }

    if (content == null) {
        content = Unpooled.buffer(0);
    }
    FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, method, path.toString(),
            content);
    request.headers().set(HttpHeaders.Names.USER_AGENT, env().userAgent());
    request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, content.readableBytes());
    request.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json");
    request.headers().set(HttpHeaders.Names.HOST, remoteHttpHost(ctx));
    addHttpBasicAuth(ctx, request, msg.bucket(), msg.password());

    return request;
}