Example usage for io.netty.buffer ByteBuf toString

List of usage examples for io.netty.buffer ByteBuf toString

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf toString.

Prototype

public abstract String toString(Charset charset);

Source Link

Document

Decodes this buffer's readable bytes into a string with the specified character set name.

Usage

From source file:io.reactiverse.pgclient.UtilTest.java

License:Apache License

private static void assertWriteHexString(String expected, byte... data) {
    ByteBuf buff = Unpooled.buffer();
    Util.writeHexString(Buffer.buffer().appendBytes(data), buff);
    String hex = buff.toString(StandardCharsets.UTF_8);
    assertEquals(expected, hex);// www  .j a  v  a 2  s.  c  om
}

From source file:io.reactivex.netty.examples.http.chunk.HttpChunkClient.java

License:Apache License

public int filterWords(final String word) {
    PipelineConfigurator<HttpClientResponse<ByteBuf>, HttpClientRequest<ByteBuf>> configurator = new HttpClientPipelineConfigurator<ByteBuf, ByteBuf>();

    HttpClient<ByteBuf, ByteBuf> client = RxNetty.createHttpClient("localhost", port, configurator);

    int count = client.submit(HttpClientRequest.createGet("/chunkedResponse"))
            .flatMap(new Func1<HttpClientResponse<ByteBuf>, Observable<String>>() {
                @Override/*w  w  w . j  a  v  a 2s.  co  m*/
                public Observable<String> call(HttpClientResponse<ByteBuf> response) {
                    return response.getContent().map(new Func1<ByteBuf, String>() {
                        @Override
                        public String call(ByteBuf content) {
                            return content.toString(Charset.defaultCharset());
                        }
                    });
                }
            }).lift(new WordSplitOperator()).map(new Func1<String, Integer>() {
                @Override
                public Integer call(String someWord) {
                    return someWord.equals(word) ? 1 : 0;
                }
            }).reduce(new Func2<Integer, Integer, Integer>() {
                @Override
                public Integer call(Integer accumulator, Integer value) {
                    return accumulator + value;
                }
            }).toBlocking().last();
    return count;
}

From source file:io.reactivex.netty.examples.http.post.SimplePostClient.java

License:Apache License

public String postMessage() {
    PipelineConfigurator<HttpClientResponse<ByteBuf>, HttpClientRequest<String>> pipelineConfigurator = PipelineConfigurators
            .httpClientConfigurator();/* w  ww.j  a  va2  s.  c om*/

    HttpClient<String, ByteBuf> client = RxNetty.<String, ByteBuf>newHttpClientBuilder("localhost", port)
            .pipelineConfigurator(pipelineConfigurator).enableWireLogging(LogLevel.ERROR).build();

    HttpClientRequest<String> request = HttpClientRequest.create(HttpMethod.POST, "test/post");
    request.withRawContentSource(Observable.just(MESSAGE), StringTransformer.DEFAULT_INSTANCE);

    String result = client.submit(request)
            .flatMap(new Func1<HttpClientResponse<ByteBuf>, Observable<String>>() {
                @Override
                public Observable<String> call(HttpClientResponse<ByteBuf> response) {
                    return response.getContent().map(new Func1<ByteBuf, String>() {
                        @Override
                        public String call(ByteBuf byteBuf) {
                            return byteBuf.toString(Charset.defaultCharset());
                        }
                    });
                }
            }).toBlocking().single();

    return result;
}

From source file:io.reactivex.netty.examples.http.post.SimplePostServer.java

License:Apache License

public HttpServer<ByteBuf, ByteBuf> createServer() {
    HttpServer<ByteBuf, ByteBuf> server = RxNetty
            .newHttpServerBuilder(port, new RequestHandler<ByteBuf, ByteBuf>() {
                @Override/*w w w  .  j  a  va2s . co  m*/
                public Observable<Void> handle(HttpServerRequest<ByteBuf> request,
                        final HttpServerResponse<ByteBuf> response) {
                    return request.getContent().map(new Func1<ByteBuf, String>() {
                        @Override
                        public String call(ByteBuf byteBuf) {
                            return byteBuf.toString(Charset.defaultCharset());
                        }
                    }).reduce("", new Func2<String, String, String>() {
                        @Override
                        public String call(String accumulator, String value) {
                            return accumulator + value;
                        }
                    }).flatMap(new Func1<String, Observable<Void>>() {
                        @Override
                        public Observable<Void> call(String clientMessage) {
                            response.writeString(clientMessage.toUpperCase());
                            return response.close(false);
                        }
                    });
                }
            }).enableWireLogging(LogLevel.ERROR).build();
    System.out.println("Simple POST server started...");
    return server;
}

From source file:io.reactivex.netty.examples.http.ssl.SslHelloWorldClient.java

License:Apache License

public HttpResponseStatus sendHelloRequest() throws Exception {
    HttpClient<ByteBuf, ByteBuf> rxClient = RxNetty.<ByteBuf, ByteBuf>newHttpClientBuilder("localhost", port)
            .withSslEngineFactory(DefaultFactories.TRUST_ALL).build();

    HttpResponseStatus statusCode = rxClient.submit(HttpClientRequest.createGet("/hello"))
            .mergeMap(new Func1<HttpClientResponse<ByteBuf>, Observable<ByteBuf>>() {
                @Override/*from ww  w.  jav  a 2 s  .  com*/
                public Observable<ByteBuf> call(HttpClientResponse<ByteBuf> response) {
                    return response.getContent();
                }
            }, new Func2<HttpClientResponse<ByteBuf>, ByteBuf, HttpResponseStatus>() {
                @Override
                public HttpResponseStatus call(HttpClientResponse<ByteBuf> response, ByteBuf content) {
                    System.out.println(content.toString(Charset.defaultCharset()));
                    return response.getStatus();
                }
            }).doOnTerminate(new Action0() {
                @Override
                public void call() {
                    System.out.println("=======================");
                }
            }).toBlocking().last();

    return statusCode;
}

From source file:io.reactivex.netty.examples.http.wordcounter.WordCounterServer.java

License:Apache License

public HttpServer<ByteBuf, ByteBuf> createServer() {
    HttpServer<ByteBuf, ByteBuf> server = RxNetty.createHttpServer(port,
            new RequestHandler<ByteBuf, ByteBuf>() {
                @Override// w w  w.  j  a  va2 s.co m
                public Observable<Void> handle(HttpServerRequest<ByteBuf> request,
                        final HttpServerResponse<ByteBuf> response) {
                    return request.getContent().map(new Func1<ByteBuf, String>() {
                        @Override
                        public String call(ByteBuf content) {
                            return content.toString(Charset.defaultCharset());
                        }
                    }).lift(new WordSplitOperator()).count().flatMap(new Func1<Integer, Observable<Void>>() {
                        @Override
                        public Observable<Void> call(Integer counter) {
                            response.writeString(counter.toString());
                            return response.close(false);
                        }
                    });
                }
            });
    System.out.println("Started word counter server...");
    return server;
}

From source file:io.reactivex.netty.examples.java.HelloHttpClient.java

License:Apache License

public static void main(String[] args) {
    Observable<HttpClientResponse<ByteBuf>> response = RxNetty.createHttpClient("localhost", 8080)
            .submit(HttpClientRequest.createGet("/hello"));
    response.toBlockingObservable().forEach(new Action1<HttpClientResponse<ByteBuf>>() {
        @Override/*from w  ww .  j  a  v a  2s  .  c om*/
        public void call(HttpClientResponse<ByteBuf> response) {
            System.out.println("New response recieved.");
            System.out.println("========================");
            System.out.println(response.getHttpVersion().text() + ' ' + response.getStatus().code() + ' '
                    + response.getStatus().reasonPhrase());
            for (Map.Entry<String, String> header : response.getHeaders().entries()) {
                System.out.println(header.getKey() + ": " + header.getValue());
            }

            response.getContent().subscribe(new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf content) {
                    System.out.print(content.toString(Charset.defaultCharset()));
                    System.out.println("========================");
                }
            });
        }
    });
}

From source file:io.reactivex.netty.protocol.http.client.HttpClientPoolTest.java

License:Apache License

private static List<String> submitAndConsumeContent(HttpClientImpl<ByteBuf, ByteBuf> client,
        HttpClientRequest<ByteBuf> request) throws InterruptedException {
    final List<String> toReturn = new ArrayList<String>();
    final CountDownLatch completionLatch = new CountDownLatch(1);
    Observable<HttpClientResponse<ByteBuf>> response = client.submit(request);
    response.flatMap(new Func1<HttpClientResponse<ByteBuf>, Observable<String>>() {
        @Override// w  w  w  .jav  a 2 s  . co m
        public Observable<String> call(HttpClientResponse<ByteBuf> response) {
            if (response.getStatus().code() == 200) {
                return response.getContent().map(new Func1<ByteBuf, String>() {
                    @Override
                    public String call(ByteBuf byteBuf) {
                        return byteBuf.toString(Charset.defaultCharset());
                    }
                });
            } else {
                return Observable
                        .error(new AssertionError("Unexpected response code: " + response.getStatus().code()));
            }
        }
    }).finallyDo(new Action0() {
        @Override
        public void call() {
            completionLatch.countDown();
        }
    }).toBlocking().forEach(new Action1<String>() {
        @Override
        public void call(String s) {
            toReturn.add(s);
        }
    });

    completionLatch.await(1, TimeUnit.MINUTES);
    return toReturn;
}

From source file:io.reactivex.netty.protocol.http.client.HttpClientTest.java

License:Apache License

@Test
public void testSingleEntity() throws Exception {
    HttpClient<ByteBuf, ByteBuf> client = RxNetty.<ByteBuf, ByteBuf>newHttpClientBuilder("localhost", port)
            .enableWireLogging(LogLevel.ERROR).build();
    Observable<HttpClientResponse<ByteBuf>> response = client
            .submit(HttpClientRequest.createGet("test/singleEntity"));
    final List<String> result = new ArrayList<String>();
    response.flatMap(new Func1<HttpClientResponse<ByteBuf>, Observable<String>>() {
        @Override//  w w w.j a va 2s .c  o m
        public Observable<String> call(HttpClientResponse<ByteBuf> response) {
            return response.getContent().map(new Func1<ByteBuf, String>() {
                @Override
                public String call(ByteBuf byteBuf) {
                    return byteBuf.toString(Charset.defaultCharset());
                }
            });
        }
    }).toBlocking().forEach(new Action1<String>() {

        @Override
        public void call(String t1) {
            result.add(t1);
        }
    });
    assertEquals("Response not found.", 1, result.size());
    assertEquals("Hello world", result.get(0));
}

From source file:io.reactivex.netty.protocol.http.client.HttpClientTest.java

License:Apache License

@Test
public void testPost() throws Exception {
    HttpClient<ByteBuf, ByteBuf> client = RxNetty.createHttpClient("localhost", port);
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createPost("test/post").withContent("Hello world");
    Observable<HttpClientResponse<ByteBuf>> response = client.submit(request);
    final List<String> result = new ArrayList<String>();
    response.flatMap(new Func1<HttpClientResponse<ByteBuf>, Observable<String>>() {
        @Override/*from  w w  w  . j  a v  a  2s  .com*/
        public Observable<String> call(HttpClientResponse<ByteBuf> response) {
            return response.getContent().map(new Func1<ByteBuf, String>() {
                @Override
                public String call(ByteBuf byteBuf) {
                    return byteBuf.toString(Charset.defaultCharset());
                }
            });
        }
    }).toBlocking().forEach(new Action1<String>() {

        @Override
        public void call(String t1) {
            result.add(t1);
        }
    });
    assertEquals(1, result.size());
    assertEquals("Hello world", result.get(0));

    // resend the same request to make sure it is repeatable
    response = client.submit(request);
    result.clear();
    response.flatMap(new Func1<HttpClientResponse<ByteBuf>, Observable<String>>() {
        @Override
        public Observable<String> call(HttpClientResponse<ByteBuf> response) {
            return response.getContent().map(new Func1<ByteBuf, String>() {
                @Override
                public String call(ByteBuf byteBuf) {
                    return byteBuf.toString(Charset.defaultCharset());
                }
            });
        }
    }).toBlocking().forEach(new Action1<String>() {

        @Override
        public void call(String t1) {
            result.add(t1);
        }
    });
    assertEquals(1, result.size());
    assertEquals("Hello world", result.get(0));
}