abstract class AbstractKQueueChannel extends AbstractChannel implements UnixChannel { private static final ChannelMetadata METADATA = new ChannelMetadata(false);
/** * Generate a new instance of an {@link EmbeddedChannel} capable of compressing data * @param ctx the context. * @param wrapper Defines what type of encoder should be used */ private EmbeddedChannel newCompressionChannel(final ChannelHandlerContext ctx, ZlibWrapper wrapper) { return new EmbeddedChannel(ctx.channel().id(), ctx.channel().metadata().hasDisconnect(), ctx.channel().config(), ZlibCodecFactory.newZlibEncoder(wrapper, compressionLevel, windowBits, memLevel)); }
/** * Set the {@link RecvByteBufAllocator} which is used for the channel to allocate receive buffers. * @param allocator the allocator to set. * @param metadata Used to set the {@link ChannelMetadata#defaultMaxMessagesPerRead()} if {@code allocator} * is of type {@link MaxMessagesRecvByteBufAllocator}. */ private void setRecvByteBufAllocator(RecvByteBufAllocator allocator, ChannelMetadata metadata) { if (allocator instanceof MaxMessagesRecvByteBufAllocator) { ((MaxMessagesRecvByteBufAllocator) allocator).maxMessagesPerRead(metadata.defaultMaxMessagesPerRead()); } else if (allocator == null) { throw new NullPointerException("allocator"); } setRecvByteBufAllocator(allocator); }
@Override protected EmbeddedChannel newContentDecoder(String contentEncoding) throws Exception { if (GZIP.contentEqualsIgnoreCase(contentEncoding) || X_GZIP.contentEqualsIgnoreCase(contentEncoding)) { return new EmbeddedChannel(ctx.channel().id(), ctx.channel().metadata().hasDisconnect(), ctx.channel().config(), ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP)); } if (DEFLATE.contentEqualsIgnoreCase(contentEncoding) || X_DEFLATE.contentEqualsIgnoreCase(contentEncoding)) { final ZlibWrapper wrapper = strict ? ZlibWrapper.ZLIB : ZlibWrapper.ZLIB_OR_NONE; // To be strict, 'deflate' means ZLIB, but some servers were not implemented correctly. return new EmbeddedChannel(ctx.channel().id(), ctx.channel().metadata().hasDisconnect(), ctx.channel().config(), ZlibCodecFactory.newZlibDecoder(wrapper)); } // 'identity' or unsupported return null; } }
@DataProvider(value = { "GZIP", "X_GZIP", "DEFLATE", "X_DEFLATE", "CONTENT_ENCODING_THAT_DOES_NOT_REPRESENT_COMPRESSED_PAYLOAD", "ENDPOINT_DOES_NOT_WANT_DECOMPRESS", "NULL_ENDPOINT" }) @Test public void newContentDecoder_works_as_expected(NewContentDecoderScenario scenario) throws Exception { // given SmartHttpContentDecompressor decompressor = new SmartHttpContentDecompressor(); TestUtil.ChannelHandlerContextMocks mocks = TestUtil.mockChannelHandlerContext(); Whitebox.setInternalState(decompressor, "ctx", mocks.mockContext); ChannelMetadata channelMetadata = new ChannelMetadata(false); ChannelConfig channelConfigMock = mock(ChannelConfig.class); doReturn(scenario.endpoint).when(mocks.mockHttpProcessingState).getEndpointForExecution(); doReturn(channelMetadata).when(mocks.mockChannel).metadata(); doReturn(channelConfigMock).when(mocks.mockChannel).config(); // when EmbeddedChannel result = decompressor.newContentDecoder(scenario.contentEncoding); // then if (scenario.expectValidDecompressor) { assertThat(result).isNotNull(); } else { assertThat(result).isNull(); } }
@Override public void run() { if (!channel().metadata().hasDisconnect()) { next.invokeClose(promise); } else { next.invokeDisconnect(promise); } } }, promise, null);
@Override protected void doDisconnect() throws Exception { if (!metadata.hasDisconnect()) { doClose(); } }
@Override public final ChannelFuture disconnect(ChannelPromise promise) { ChannelFuture future = super.disconnect(promise); finishPendingTasks(!metadata.hasDisconnect()); return future; }
ChannelMetadata channelMetadata = new ChannelMetadata(); channelMetadata.setCreateGroupMessage(ChannelMetadata.ADMIN_NAME + " created " + ChannelMetadata.GROUP_NAME); channelMetadata.setAddMemberMessage(ChannelMetadata.ADMIN_NAME + " added " + ChannelMetadata.USER_NAME); channelMetadata.setRemoveMemberMessage(ChannelMetadata.ADMIN_NAME + " removed " + ChannelMetadata.USER_NAME); channelMetadata.setGroupNameChangeMessage(ChannelMetadata.USER_NAME + " changed group name " + ChannelMetadata.GROUP_NAME); channelMetadata.setJoinMemberMessage(ChannelMetadata.USER_NAME + " joined"); channelMetadata.setGroupLeftMessage(ChannelMetadata.USER_NAME + " left group " + ChannelMetadata.GROUP_NAME); channelMetadata.setGroupIconChangeMessage(ChannelMetadata.USER_NAME + " changed icon"); channelMetadata.setDeletedGroupMessage(ChannelMetadata.ADMIN_NAME + " deleted group " + ChannelMetadata.GROUP_NAME);
/** * Returns a new {@link EmbeddedChannel} that decodes the HTTP2 message content encoded in the specified * {@code contentEncoding}. * * @param contentEncoding the value of the {@code content-encoding} header * @return a new {@link ByteToMessageDecoder} if the specified encoding is supported. {@code null} otherwise * (alternatively, you can throw a {@link Http2Exception} to block unknown encoding). * @throws Http2Exception If the specified encoding is not not supported and warrants an exception */ protected EmbeddedChannel newContentDecompressor(final ChannelHandlerContext ctx, CharSequence contentEncoding) throws Http2Exception { if (GZIP.contentEqualsIgnoreCase(contentEncoding) || X_GZIP.contentEqualsIgnoreCase(contentEncoding)) { return new EmbeddedChannel(ctx.channel().id(), ctx.channel().metadata().hasDisconnect(), ctx.channel().config(), ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP)); } if (DEFLATE.contentEqualsIgnoreCase(contentEncoding) || X_DEFLATE.contentEqualsIgnoreCase(contentEncoding)) { final ZlibWrapper wrapper = strict ? ZlibWrapper.ZLIB : ZlibWrapper.ZLIB_OR_NONE; // To be strict, 'deflate' means ZLIB, but some servers were not implemented correctly. return new EmbeddedChannel(ctx.channel().id(), ctx.channel().metadata().hasDisconnect(), ctx.channel().config(), ZlibCodecFactory.newZlibDecoder(wrapper)); } // 'identity' or unsupported return null; }
@Override public void run() { if (!channel().metadata().hasDisconnect()) { next.invokeClose(promise); } else { next.invokeDisconnect(promise); } } }, promise, null);
@Override protected void doDisconnect() throws Exception { if (!metadata.hasDisconnect()) { doClose(); } }
@Override public final ChannelFuture disconnect(ChannelPromise promise) { ChannelFuture future = super.disconnect(promise); finishPendingTasks(!metadata.hasDisconnect()); return future; }
new EmbeddedChannel(ctx.channel().id(), ctx.channel().metadata().hasDisconnect(), ctx.channel().config(), ZlibCodecFactory.newZlibEncoder( wrapper, compressionLevel, windowBits, memLevel)));
@Override protected EmbeddedChannel newContentDecoder(String contentEncoding) throws Exception { if (GZIP.contentEqualsIgnoreCase(contentEncoding) || X_GZIP.contentEqualsIgnoreCase(contentEncoding)) { return new EmbeddedChannel(ctx.channel().id(), ctx.channel().metadata().hasDisconnect(), ctx.channel().config(), ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP)); } if (DEFLATE.contentEqualsIgnoreCase(contentEncoding) || X_DEFLATE.contentEqualsIgnoreCase(contentEncoding)) { final ZlibWrapper wrapper = strict ? ZlibWrapper.ZLIB : ZlibWrapper.ZLIB_OR_NONE; // To be strict, 'deflate' means ZLIB, but some servers were not implemented correctly. return new EmbeddedChannel(ctx.channel().id(), ctx.channel().metadata().hasDisconnect(), ctx.channel().config(), ZlibCodecFactory.newZlibDecoder(wrapper)); } // 'identity' or unsupported return null; } }
@Override public void run() { if (!channel().metadata().hasDisconnect()) { next.invokeClose(promise); } else { next.invokeDisconnect(promise); } } }, promise, null);
@Override protected void doDisconnect() throws Exception { if (!metadata.hasDisconnect()) { doClose(); } }
@Override public final ChannelFuture disconnect(ChannelPromise promise) { ChannelFuture future = super.disconnect(promise); finishPendingTasks(!metadata.hasDisconnect()); return future; }
abstract class AbstractKQueueChannel extends AbstractChannel implements UnixChannel { private static final ChannelMetadata METADATA = new ChannelMetadata(false);
/** * Set the {@link RecvByteBufAllocator} which is used for the channel to allocate receive buffers. * @param allocator the allocator to set. * @param metadata Used to set the {@link ChannelMetadata#defaultMaxMessagesPerRead()} if {@code allocator} * is of type {@link MaxMessagesRecvByteBufAllocator}. */ private void setRecvByteBufAllocator(RecvByteBufAllocator allocator, ChannelMetadata metadata) { if (allocator instanceof MaxMessagesRecvByteBufAllocator) { ((MaxMessagesRecvByteBufAllocator) allocator).maxMessagesPerRead(metadata.defaultMaxMessagesPerRead()); } else if (allocator == null) { throw new NullPointerException("allocator"); } setRecvByteBufAllocator(allocator); }