use of cn.taketoday.core.io.buffer.NettyDataBuffer in project today-infrastructure by TAKETODAY.
the class NettyByteBufDecoder method decode.
@Override
public ByteBuf decode(DataBuffer dataBuffer, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
if (logger.isDebugEnabled()) {
logger.debug("{}Read {} bytes", Hints.getLogPrefix(hints), dataBuffer.readableByteCount());
}
if (dataBuffer instanceof NettyDataBuffer) {
return ((NettyDataBuffer) dataBuffer).getNativeBuffer();
}
ByteBuf byteBuf;
byte[] bytes = new byte[dataBuffer.readableByteCount()];
dataBuffer.read(bytes);
byteBuf = Unpooled.wrappedBuffer(bytes);
DataBufferUtils.release(dataBuffer);
return byteBuf;
}
use of cn.taketoday.core.io.buffer.NettyDataBuffer in project today-framework by TAKETODAY.
the class BodyExtractorsTests method unsupportedMediaTypeShouldConsumeAndCancel.
// SPR-17054
@Test
public void unsupportedMediaTypeShouldConsumeAndCancel() {
NettyDataBufferFactory factory = new NettyDataBufferFactory(new PooledByteBufAllocator(true));
NettyDataBuffer buffer = factory.wrap(ByteBuffer.wrap("spring".getBytes(StandardCharsets.UTF_8)));
TestPublisher<DataBuffer> body = TestPublisher.create();
MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);
response.getHeaders().setContentType(MediaType.APPLICATION_PDF);
response.setBody(body.flux());
BodyExtractor<Mono<User>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(User.class);
StepVerifier.create(extractor.extract(response, this.context)).then(() -> {
body.assertWasSubscribed();
body.emit(buffer);
}).expectErrorSatisfies(throwable -> {
boolean condition = throwable instanceof UnsupportedMediaTypeException;
assertThat(condition).isTrue();
assertThatExceptionOfType(IllegalReferenceCountException.class).isThrownBy(buffer::release);
body.assertCancelled();
}).verify();
}
use of cn.taketoday.core.io.buffer.NettyDataBuffer in project today-framework by TAKETODAY.
the class NettyByteBufDecoder method decode.
@Override
public ByteBuf decode(DataBuffer dataBuffer, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
if (logger.isDebugEnabled()) {
logger.debug("{}Read {} bytes", Hints.getLogPrefix(hints), dataBuffer.readableByteCount());
}
if (dataBuffer instanceof NettyDataBuffer) {
return ((NettyDataBuffer) dataBuffer).getNativeBuffer();
}
ByteBuf byteBuf;
byte[] bytes = new byte[dataBuffer.readableByteCount()];
dataBuffer.read(bytes);
byteBuf = Unpooled.wrappedBuffer(bytes);
DataBufferUtils.release(dataBuffer);
return byteBuf;
}
use of cn.taketoday.core.io.buffer.NettyDataBuffer in project today-infrastructure by TAKETODAY.
the class BodyExtractorsTests method unsupportedMediaTypeShouldConsumeAndCancel.
// SPR-17054
@Test
public void unsupportedMediaTypeShouldConsumeAndCancel() {
NettyDataBufferFactory factory = new NettyDataBufferFactory(new PooledByteBufAllocator(true));
NettyDataBuffer buffer = factory.wrap(ByteBuffer.wrap("spring".getBytes(StandardCharsets.UTF_8)));
TestPublisher<DataBuffer> body = TestPublisher.create();
MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);
response.getHeaders().setContentType(MediaType.APPLICATION_PDF);
response.setBody(body.flux());
BodyExtractor<Mono<User>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(User.class);
StepVerifier.create(extractor.extract(response, this.context)).then(() -> {
body.assertWasSubscribed();
body.emit(buffer);
}).expectErrorSatisfies(throwable -> {
boolean condition = throwable instanceof UnsupportedMediaTypeException;
assertThat(condition).isTrue();
assertThatExceptionOfType(IllegalReferenceCountException.class).isThrownBy(buffer::release);
body.assertCancelled();
}).verify();
}
Aggregations