static ConnectionObserver compositeConnectionObserver(ConnectionObserver observer, ConnectionObserver other) { if (observer == ConnectionObserver.emptyListener()) { return other; if (other == ConnectionObserver.emptyListener()) { return observer;
static ConnectionObserver compositeConnectionObserver(ConnectionObserver observer, ConnectionObserver other) { if (observer == ConnectionObserver.emptyListener()) { return other; if (other == ConnectionObserver.emptyListener()) { return observer;
/** * Obtain and remove the current childHandler {@link ConnectionObserver} from the * bootstrap. * * @param b the bootstrap to scan * * @return current {@link ConnectionObserver} or null * */ @SuppressWarnings("unchecked") public static ConnectionObserver childConnectionObserver(ServerBootstrap b) { Objects.requireNonNull(b, "bootstrap"); ConnectionObserver obs = (ConnectionObserver) b.config() .childOptions() .get(OBSERVER_OPTION); b.childOption(OBSERVER_OPTION, null); if (obs == null) { return ConnectionObserver.emptyListener(); //will not be triggered in } return obs; }
/** * Obtain and remove the current childHandler {@link ConnectionObserver} from the * bootstrap. * * @param b the bootstrap to scan * * @return current {@link ConnectionObserver} or null * */ @SuppressWarnings("unchecked") public static ConnectionObserver childConnectionObserver(ServerBootstrap b) { Objects.requireNonNull(b, "bootstrap"); ConnectionObserver obs = (ConnectionObserver) b.config() .childOptions() .get(OBSERVER_OPTION); b.childOption(OBSERVER_OPTION, null); if (obs == null) { return ConnectionObserver.emptyListener(); //will not be triggered in } return obs; }
/** * Obtain and remove the current {@link ConnectionObserver} from the bootstrap. * * @param b the bootstrap to scan * * @return current {@link ConnectionObserver} or null * */ @SuppressWarnings("unchecked") public static ConnectionObserver connectionObserver(AbstractBootstrap<?, ?> b) { Objects.requireNonNull(b, "bootstrap"); ConnectionObserver obs = (ConnectionObserver) b.config() .options() .get(OBSERVER_OPTION); b.option(OBSERVER_OPTION, null); if (obs == null) { return ConnectionObserver.emptyListener(); //will not be triggered in } return obs; }
/** * Obtain and remove the current {@link ConnectionObserver} from the bootstrap. * * @param b the bootstrap to scan * * @return current {@link ConnectionObserver} or null * */ @SuppressWarnings("unchecked") public static ConnectionObserver connectionObserver(AbstractBootstrap<?, ?> b) { Objects.requireNonNull(b, "bootstrap"); ConnectionObserver obs = (ConnectionObserver) b.config() .options() .get(OBSERVER_OPTION); b.option(OBSERVER_OPTION, null); if (obs == null) { return ConnectionObserver.emptyListener(); //will not be triggered in } return obs; }
.getAndSet(ConnectionObserver.emptyListener());
.getAndSet(ConnectionObserver.emptyListener());
@Test public void addNamedEncoderReplaysLastHttp() { ByteBuf buf = Unpooled.copiedBuffer("{\"foo\":1}", CharsetUtil.UTF_8); EmbeddedChannel channel = new EmbeddedChannel(); HttpClientOperations ops = new HttpClientOperations(() -> channel, ConnectionObserver.emptyListener(), ClientCookieEncoder.STRICT, ClientCookieDecoder.STRICT) .addHandler("json", new JsonObjectDecoder()); channel.writeInbound(new DefaultLastHttpContent(buf)); assertThat(channel.pipeline().names().iterator().next(), is("json$extractor")); Object content = channel.readInbound(); assertThat(content, instanceOf(ByteBuf.class)); ((ByteBuf) content).release(); content = channel.readInbound(); assertThat(content, instanceOf(LastHttpContent.class)); ((LastHttpContent) content).release(); assertThat(channel.readInbound(), nullValue()); }
@Test public void addNamedDecoderReplaysLastHttp() { ByteBuf buf = Unpooled.copiedBuffer("{\"foo\":1}", CharsetUtil.UTF_8); EmbeddedChannel channel = new EmbeddedChannel(); HttpClientOperations ops = new HttpClientOperations(() -> channel, ConnectionObserver.emptyListener(), ClientCookieEncoder.STRICT, ClientCookieDecoder.STRICT) .addHandler("json", new JsonObjectDecoder()); channel.writeInbound(new DefaultLastHttpContent(buf)); assertThat(channel.pipeline().names().iterator().next(), is("json$extractor")); Object content = channel.readInbound(); assertThat(content, instanceOf(ByteBuf.class)); ((ByteBuf) content).release(); content = channel.readInbound(); assertThat(content, instanceOf(LastHttpContent.class)); ((LastHttpContent) content).release(); assertThat(channel.readInbound(), nullValue()); }
@Test public void addDecoderReplaysLastHttp() { ByteBuf buf = Unpooled.copiedBuffer("{\"foo\":1}", CharsetUtil.UTF_8); EmbeddedChannel channel = new EmbeddedChannel(); HttpClientOperations ops = new HttpClientOperations(() -> channel, ConnectionObserver.emptyListener(), ClientCookieEncoder.STRICT, ClientCookieDecoder.STRICT) .addHandler(new JsonObjectDecoder()); channel.writeInbound(new DefaultLastHttpContent(buf)); assertThat(channel.pipeline().names().iterator().next(), is("JsonObjectDecoder$extractor")); Object content = channel.readInbound(); assertThat(content, instanceOf(ByteBuf.class)); ((ByteBuf) content).release(); content = channel.readInbound(); assertThat(content, instanceOf(LastHttpContent.class)); ((LastHttpContent) content).release(); assertThat(channel.readInbound(), nullValue()); }
@Test public void addEncoderReplaysLastHttp() { ByteBuf buf = Unpooled.copiedBuffer("{\"foo\":1}", CharsetUtil.UTF_8); EmbeddedChannel channel = new EmbeddedChannel(); HttpClientOperations ops = new HttpClientOperations(() -> channel, ConnectionObserver.emptyListener(), ClientCookieEncoder.STRICT, ClientCookieDecoder.STRICT) .addHandler(new JsonObjectDecoder()); channel.writeInbound(new DefaultLastHttpContent(buf)); assertThat(channel.pipeline().names().iterator().next(), is("JsonObjectDecoder$extractor")); Object content = channel.readInbound(); assertThat(content, instanceOf(ByteBuf.class)); ((ByteBuf) content).release(); content = channel.readInbound(); assertThat(content, instanceOf(LastHttpContent.class)); ((LastHttpContent) content).release(); assertThat(channel.readInbound(), nullValue()); }
@Test public void testConstructorWithProvidedReplacement() { EmbeddedChannel channel = new EmbeddedChannel(); channel.pipeline().addFirst(NettyPipeline.SslHandler, new ChannelHandlerAdapter() { }); HttpClientOperations ops1 = new HttpClientOperations(() -> channel, ConnectionObserver.emptyListener(), ClientCookieEncoder.STRICT, ClientCookieDecoder.STRICT); ops1.followRedirectPredicate((req, res) -> true); HttpClientOperations ops2 = new HttpClientOperations(ops1); assertSame(ops1.channel(), ops2.channel()); assertSame(ops1.started, ops2.started); assertSame(ops1.redirectedFrom, ops2.redirectedFrom); assertSame(ops1.isSecure, ops2.isSecure); assertSame(ops1.nettyRequest, ops2.nettyRequest); assertSame(ops1.responseState, ops2.responseState); assertSame(ops1.followRedirectPredicate, ops2.followRedirectPredicate); assertSame(ops1.requestHeaders, ops2.requestHeaders); } }