Search in sources :

Example 41 with ByteBufAllocator

use of io.netty.buffer.ByteBufAllocator in project riposte by Nike-Inc.

the class HttpChannelInitializerTest method beforeMethod.

@Before
public void beforeMethod() {
    socketChannelMock = mock(SocketChannel.class);
    channelPipelineMock = mock(ChannelPipeline.class);
    ByteBufAllocator byteBufAllocatorMock = mock(ByteBufAllocator.class);
    doReturn(channelPipelineMock).when(socketChannelMock).pipeline();
    doReturn(byteBufAllocatorMock).when(socketChannelMock).alloc();
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) ChannelPipeline(io.netty.channel.ChannelPipeline) Before(org.junit.Before)

Example 42 with ByteBufAllocator

use of io.netty.buffer.ByteBufAllocator in project curiostack by curioswitch.

the class ServiceAccountAccessTokenProvider method refreshRequestContent.

@Override
ByteBuf refreshRequestContent(Type type) {
    long currentTimeMillis = clock().millis();
    String assertion = createAssertion(type, currentTimeMillis);
    QueryStringEncoder formEncoder = new QueryStringEncoder("");
    formEncoder.addParam("grant_type", GRANT_TYPE);
    formEncoder.addParam("assertion", assertion);
    String contentWithQuestionMark = formEncoder.toString();
    ByteBufAllocator alloc = RequestContext.mapCurrent(RequestContext::alloc, () -> PooledByteBufAllocator.DEFAULT);
    assert alloc != null;
    ByteBuf content = alloc.buffer(contentWithQuestionMark.length() - 1);
    ByteBufUtil.writeAscii(content, contentWithQuestionMark.substring(1));
    return content;
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) RequestContext(com.linecorp.armeria.common.RequestContext) ByteBuf(io.netty.buffer.ByteBuf) QueryStringEncoder(io.netty.handler.codec.http.QueryStringEncoder)

Example 43 with ByteBufAllocator

use of io.netty.buffer.ByteBufAllocator in project reactor-netty by reactor.

the class HttpSendFileTests method doTestSendFileAsync.

private void doTestSendFileAsync(BiFunction<? super HttpServerRequest, ? super HttpServerResponse, ? extends Publisher<Void>> fn, int chunk, @Nullable byte[] expectedContent) throws IOException, URISyntaxException {
    Path largeFile = Paths.get(getClass().getResource("/largeFile.txt").toURI());
    Path largeFileParent = largeFile.getParent();
    assertThat(largeFileParent).isNotNull();
    Path tempFile = Files.createTempFile(largeFileParent, "temp", ".txt");
    tempFile.toFile().deleteOnExit();
    byte[] fileBytes = Files.readAllBytes(largeFile);
    for (int i = 0; i < 1000; i++) {
        Files.write(tempFile, fileBytes, StandardOpenOption.APPEND);
    }
    ByteBufAllocator allocator = ByteBufAllocator.DEFAULT;
    Flux<ByteBuf> content = Flux.using(() -> AsynchronousFileChannel.open(tempFile, StandardOpenOption.READ), ch -> Flux.<ByteBuf>create(fluxSink -> {
        TestCompletionHandler handler = new TestCompletionHandler(ch, fluxSink, allocator, chunk);
        fluxSink.onDispose(handler::dispose);
        ByteBuffer buf = ByteBuffer.allocate(chunk);
        ch.read(buf, 0, buf, handler);
    }), ch -> {
    /*the channel will be closed in the handler*/
    }).doOnDiscard(ByteBuf.class, ByteBuf::release).log("send", Level.INFO, SignalType.REQUEST, SignalType.ON_COMPLETE);
    disposableServer = customizeServerOptions(HttpServer.create().host("localhost")).handle(fn).bindNow();
    byte[] response = customizeClientOptions(HttpClient.create().remoteAddress(disposableServer::address)).request(HttpMethod.POST).uri("/").send(content).responseContent().aggregate().asByteArray().onErrorReturn(IOException.class, expectedContent == null ? new byte[0] : expectedContent).block();
    assertThat(response).isEqualTo(expectedContent == null ? Files.readAllBytes(tempFile) : expectedContent);
}
Also used : Path(java.nio.file.Path) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) URISyntaxException(java.net.URISyntaxException) BiFunction(java.util.function.BiFunction) FluxSink(reactor.core.publisher.FluxSink) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BaseHttpTest(reactor.netty.BaseHttpTest) AsynchronousFileChannel(java.nio.channels.AsynchronousFileChannel) Function(java.util.function.Function) Nullable(reactor.util.annotation.Nullable) ByteBuffer(java.nio.ByteBuffer) StandardCopyOption(java.nio.file.StandardCopyOption) Level(java.util.logging.Level) BiPredicate(java.util.function.BiPredicate) Charset(java.nio.charset.Charset) ByteBuf(io.netty.buffer.ByteBuf) Duration(java.time.Duration) Path(java.nio.file.Path) Channel(java.nio.channels.Channel) Files(java.nio.file.Files) Publisher(org.reactivestreams.Publisher) CompletionHandler(java.nio.channels.CompletionHandler) StandardOpenOption(java.nio.file.StandardOpenOption) HttpMethod(io.netty.handler.codec.http.HttpMethod) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) FileSystem(java.nio.file.FileSystem) SignalType(reactor.core.publisher.SignalType) StandardCharsets(java.nio.charset.StandardCharsets) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) AtomicLong(java.util.concurrent.atomic.AtomicLong) Flux(reactor.core.publisher.Flux) NettyOutbound(reactor.netty.NettyOutbound) Paths(java.nio.file.Paths) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) FileSystems(java.nio.file.FileSystems) HttpClient(reactor.netty.http.client.HttpClient) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer)

Example 44 with ByteBufAllocator

use of io.netty.buffer.ByteBufAllocator in project netty by netty.

the class AbstractComboTestsuiteTest method run.

protected void run() throws Throwable {
    List<TestsuitePermutation.BootstrapComboFactory<SB, CB>> combos = newFactories();
    for (ByteBufAllocator allocator : newAllocators()) {
        int i = 0;
        for (TestsuitePermutation.BootstrapComboFactory<SB, CB> e : combos) {
            sb = e.newServerInstance();
            cb = e.newClientInstance();
            configure(sb, cb, allocator);
            logger.info(String.format("Running: %s %d of %d (%s + %s) with %s", testName.getMethodName(), ++i, combos.size(), sb, cb, StringUtil.simpleClassName(allocator)));
            try {
                Method m = getClass().getMethod(TestUtils.testMethodName(testName), sbClazz, cbClazz);
                m.invoke(this, sb, cb);
            } catch (InvocationTargetException ex) {
                throw ex.getCause();
            }
        }
    }
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 45 with ByteBufAllocator

use of io.netty.buffer.ByteBufAllocator in project ratpack by ratpack.

the class MetricsWebsocketBroadcastHandler method handle.

@Override
public void handle(final Context context) throws Exception {
    final MetricsBroadcaster broadcaster = context.get(MetricsBroadcaster.class);
    final ByteBufAllocator byteBufAllocator = context.get(ByteBufAllocator.class);
    final DropwizardMetricsConfig config = context.get(DropwizardMetricsConfig.class);
    MetricFilter filter = MetricFilter.ALL;
    if (config.getWebSocket().isPresent()) {
        filter = new RegexMetricFilter(config.getWebSocket().get().getIncludeFilter(), config.getWebSocket().get().getExcludeFilter());
    }
    websocketByteBufBroadcast(context, broadcaster.map(new MetricRegistryJsonMapper(byteBufAllocator, filter)));
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) MetricsBroadcaster(ratpack.dropwizard.metrics.internal.MetricsBroadcaster) MetricFilter(com.codahale.metrics.MetricFilter) RegexMetricFilter(ratpack.dropwizard.metrics.internal.RegexMetricFilter) MetricRegistryJsonMapper(ratpack.dropwizard.metrics.internal.MetricRegistryJsonMapper) RegexMetricFilter(ratpack.dropwizard.metrics.internal.RegexMetricFilter)

Aggregations

ByteBufAllocator (io.netty.buffer.ByteBufAllocator)79 ByteBuf (io.netty.buffer.ByteBuf)48 ArrayList (java.util.ArrayList)17 Test (org.junit.Test)17 Test (org.junit.jupiter.api.Test)11 PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)10 Channel (io.netty.channel.Channel)9 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)8 IOException (java.io.IOException)8 UnpooledByteBufAllocator (io.netty.buffer.UnpooledByteBufAllocator)7 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)7 ChannelFuture (io.netty.channel.ChannelFuture)6 Mono (reactor.core.publisher.Mono)6 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)4 InetSocketAddress (java.net.InetSocketAddress)4 StandardCharsets (java.nio.charset.StandardCharsets)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 NettyDataBufferFactory (org.springframework.core.io.buffer.NettyDataBufferFactory)4 RecvByteBufAllocator (io.netty.channel.RecvByteBufAllocator)3 ByteBuffer (java.nio.ByteBuffer)3