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.codebullets.external.party.simulator.connections.websocket.inbound.WebSocketServerIndexPage.java

License:Apache License

/**
 * Gets the byte content of the server page.
 *//*from   ww w.jav  a  2s . c  o  m*/
public static ByteBuf getContent(final String webSocketLocation) {
    return Unpooled.copiedBuffer("<html><head><title>Web Socket Test</title></head>" + NEWLINE + "<body>"
            + NEWLINE + "<script type=\"text/javascript\">" + NEWLINE + "var socket;" + NEWLINE
            + "if (!window.WebSocket) {" + NEWLINE + "  window.WebSocket = window.MozWebSocket;" + NEWLINE + '}'
            + NEWLINE + "if (window.WebSocket) {" + NEWLINE + "  socket = new WebSocket(\"" + webSocketLocation
            + "\");" + NEWLINE + "  socket.onmessage = function(event) {" + NEWLINE
            + "    var ta = document.getElementById('responseText');" + NEWLINE
            + "    ta.value = ta.value + '\\n' + event.data" + NEWLINE + "  };" + NEWLINE
            + "  socket.onopen = function(event) {" + NEWLINE
            + "    var ta = document.getElementById('responseText');" + NEWLINE
            + "    ta.value = \"Web Socket opened!\";" + NEWLINE + "  };" + NEWLINE
            + "  socket.onclose = function(event) {" + NEWLINE
            + "    var ta = document.getElementById('responseText');" + NEWLINE
            + "    ta.value = ta.value + \"Web Socket closed\"; " + NEWLINE + "  };" + NEWLINE + "} else {"
            + NEWLINE + "  alert(\"Your browser does not support Web Socket.\");" + NEWLINE + '}' + NEWLINE
            + NEWLINE + "function send(message) {" + NEWLINE + "  if (!window.WebSocket) { return; }" + NEWLINE
            + "  if (socket.readyState == WebSocket.OPEN) {" + NEWLINE + "    socket.send(message);" + NEWLINE
            + "  } else {" + NEWLINE + "    alert(\"The socket is not open.\");" + NEWLINE + "  }" + NEWLINE
            + '}' + NEWLINE + "</script>" + NEWLINE + "<form onsubmit=\"return false;\">" + NEWLINE
            + "<input type=\"text\" name=\"message\" value=\"Hello, World!\"/>"
            + "<input type=\"button\" value=\"Send Web Socket Data\"" + NEWLINE
            + "       onclick=\"send(this.form.message.value)\" />" + NEWLINE + "<h3>Output</h3>" + NEWLINE
            + "<textarea id=\"responseText\" style=\"width:500px;height:300px;\"></textarea>" + NEWLINE
            + "</form>" + NEWLINE + "</body>" + NEWLINE + "</html>" + NEWLINE, CharsetUtil.US_ASCII);
}

From source file:com.corundumstudio.socketio.handler.ResourceHandler.java

License:Apache License

/**
 * Sends an Error response with status message
 *
 * @param ctx//  w w  w . j  a  v  a2  s . c o m
 * @param status
 */
private void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) {
    HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status);
    HttpHeaders.setHeader(response, CONTENT_TYPE, "text/plain; charset=UTF-8");
    ByteBuf content = Unpooled.copiedBuffer("Failure: " + status.toString() + "\r\n", CharsetUtil.UTF_8);

    ctx.channel().write(response);
    // Close the connection as soon as the error message is sent.
    ctx.channel().write(content).addListener(ChannelFutureListener.CLOSE);
}

From source file:com.corundumstudio.socketio.parser.Decoder.java

License:Apache License

public Packet decodePacket(String string, UUID uuid) throws IOException {
    ByteBuf buf = Unpooled.copiedBuffer(string, CharsetUtil.UTF_8);
    try {/* www .  j  a  v  a  2  s . c om*/
        Packet packet = decodePacket(buf, uuid);
        return packet;
    } finally {
        buf.release();
    }
}

From source file:com.corundumstudio.socketio.protocol.PacketDecoder.java

License:Apache License

@Deprecated
public Packet decodePacket(String string, UUID uuid) throws IOException {
    ByteBuf buf = Unpooled.copiedBuffer(string, CharsetUtil.UTF_8);
    try {//from w  w w .ja  v a2s .  c  o m
        return null;
    } finally {
        buf.release();
    }
}

From source file:com.couchbase.client.core.cluster.BinaryMessageTest.java

License:Open Source License

@Test
public void shouldUpsertAndGetDocument() {
    String key = "upsert-key";
    String content = "Hello World!";
    UpsertRequest upsert = new UpsertRequest(key, Unpooled.copiedBuffer(content, CharsetUtil.UTF_8), bucket);
    cluster.<UpsertResponse>send(upsert).toBlockingObservable().single();

    GetRequest request = new GetRequest(key, bucket);
    assertEquals(content, cluster.<GetResponse>send(request).toBlockingObservable().single().content()
            .toString(CharsetUtil.UTF_8));
}

From source file:com.couchbase.client.core.cluster.BinaryMessageTest.java

License:Open Source License

@Test
public void shouldUpsertWithExpiration() throws Exception {
    String key = "upsert-key-vanish";
    String content = "Hello World!";
    UpsertRequest upsert = new UpsertRequest(key, Unpooled.copiedBuffer(content, CharsetUtil.UTF_8), 1, 0,
            bucket);/*from   w w  w . j a va  2  s.  com*/
    cluster.<UpsertResponse>send(upsert).toBlockingObservable().single();

    Thread.sleep(2000);

    GetRequest request = new GetRequest(key, bucket);
    assertEquals(ResponseStatus.NOT_EXISTS,
            cluster.<GetResponse>send(request).toBlockingObservable().single().status());
}

From source file:com.couchbase.client.core.cluster.BinaryMessageTest.java

License:Open Source License

@Test
public void shouldHandleDoubleInsert() {
    String key = "insert-key";
    String content = "Hello World!";
    InsertRequest insert = new InsertRequest(key, Unpooled.copiedBuffer(content, CharsetUtil.UTF_8), bucket);
    assertEquals(ResponseStatus.SUCCESS,
            cluster.<InsertResponse>send(insert).toBlockingObservable().single().status());

    insert = new InsertRequest(key, Unpooled.copiedBuffer(content, CharsetUtil.UTF_8), bucket);
    assertEquals(ResponseStatus.EXISTS,//from  w  w w .j a v a 2 s  .  co m
            cluster.<InsertResponse>send(insert).toBlockingObservable().single().status());
}

From source file:com.couchbase.client.core.cluster.BinaryMessageTest.java

License:Open Source License

@Test
public void shouldReplaceWithoutCAS() {
    final String key = "replace-key";
    final String content = "replace content";

    ReplaceRequest insert = new ReplaceRequest(key, Unpooled.copiedBuffer(content, CharsetUtil.UTF_8), bucket);
    assertEquals(ResponseStatus.NOT_EXISTS,
            cluster.<ReplaceResponse>send(insert).toBlockingObservable().single().status());

    UpsertRequest upsert = new UpsertRequest(key, Unpooled.copiedBuffer("insert content", CharsetUtil.UTF_8),
            bucket);/*  ww  w.j av a  2  s .  c  om*/
    ReplaceResponse response = cluster.<UpsertResponse>send(upsert)
            .flatMap(new Func1<UpsertResponse, Observable<ReplaceResponse>>() {
                @Override
                public Observable<ReplaceResponse> call(UpsertResponse response) {
                    return cluster.send(
                            new ReplaceRequest(key, Unpooled.copiedBuffer(content, CharsetUtil.UTF_8), bucket));
                }
            }).toBlockingObservable().single();

    assertEquals(ResponseStatus.SUCCESS, response.status());
}

From source file:com.couchbase.client.core.cluster.BinaryMessageTest.java

License:Open Source License

@Test
public void shouldReplaceWithFailingCAS() {
    final String key = "replace-key-cas-fail";
    final String content = "replace content";

    ReplaceRequest insert = new ReplaceRequest(key, Unpooled.copiedBuffer(content, CharsetUtil.UTF_8), bucket);
    assertEquals(ResponseStatus.NOT_EXISTS,
            cluster.<ReplaceResponse>send(insert).toBlockingObservable().single().status());

    UpsertRequest upsert = new UpsertRequest(key, Unpooled.copiedBuffer("insert content", CharsetUtil.UTF_8),
            bucket);/*from  w w  w .jav a  2 s. c  o m*/
    ReplaceResponse response = cluster.<UpsertResponse>send(upsert)
            .flatMap(new Func1<UpsertResponse, Observable<ReplaceResponse>>() {
                @Override
                public Observable<ReplaceResponse> call(UpsertResponse response) {
                    return cluster.send(new ReplaceRequest(key,
                            Unpooled.copiedBuffer(content, CharsetUtil.UTF_8), 24234234L, bucket));
                }
            }).toBlockingObservable().single();

    assertEquals(ResponseStatus.EXISTS, response.status());
}

From source file:com.couchbase.client.core.cluster.BinaryMessageTest.java

License:Open Source License

@Test
public void shouldReplaceWithMatchingCAS() {
    final String key = "replace-key-cas-match";
    final String content = "replace content";

    ReplaceRequest insert = new ReplaceRequest(key, Unpooled.copiedBuffer(content, CharsetUtil.UTF_8), bucket);
    assertEquals(ResponseStatus.NOT_EXISTS,
            cluster.<ReplaceResponse>send(insert).toBlockingObservable().single().status());

    UpsertRequest upsert = new UpsertRequest(key, Unpooled.copiedBuffer("insert content", CharsetUtil.UTF_8),
            bucket);//from w  ww .ja  va 2 s  .co  m
    ReplaceResponse response = cluster.<UpsertResponse>send(upsert)
            .flatMap(new Func1<UpsertResponse, Observable<ReplaceResponse>>() {
                @Override
                public Observable<ReplaceResponse> call(UpsertResponse response) {
                    return cluster.send(new ReplaceRequest(key,
                            Unpooled.copiedBuffer(content, CharsetUtil.UTF_8), response.cas(), bucket));
                }
            }).toBlockingObservable().single();

    assertEquals(ResponseStatus.SUCCESS, response.status());
}