org.springframework.core.io.buffer.NettyDataBufferFactory Java Examples
The following examples show how to use
org.springframework.core.io.buffer.NettyDataBufferFactory.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DataBufferFactoryWrapperTest.java From armeria with Apache License 2.0 | 6 votes |
@Test public void usingNettyDataBufferFactory_HttpData() { final DataBufferFactoryWrapper<?> wrapper = new DataBufferFactoryWrapper<>(new NettyDataBufferFactory(UnpooledByteBufAllocator.DEFAULT)); final HttpData httpData1 = HttpData.ofUtf8("abc"); final DataBuffer buffer = wrapper.toDataBuffer(httpData1); assertThat(buffer).isInstanceOf(NettyDataBuffer.class); assertThat(((NettyDataBuffer) buffer).getNativeBuffer().refCnt()).isOne(); final HttpData httpData2 = wrapper.toHttpData(buffer); assertThat(httpData2).isInstanceOf(PooledHttpData.class); assertThat(((PooledHttpData) httpData2).content()) .isEqualTo(((NettyDataBuffer) buffer).getNativeBuffer()); assertThat(((PooledHttpData) httpData2).refCnt()).isOne(); }
Example #2
Source File: NettyWebSocketSessionSupport.java From java-technology-stack with MIT License | 6 votes |
protected WebSocketFrame toFrame(WebSocketMessage message) { ByteBuf byteBuf = NettyDataBufferFactory.toByteBuf(message.getPayload()); if (WebSocketMessage.Type.TEXT.equals(message.getType())) { return new TextWebSocketFrame(byteBuf); } else if (WebSocketMessage.Type.BINARY.equals(message.getType())) { return new BinaryWebSocketFrame(byteBuf); } else if (WebSocketMessage.Type.PING.equals(message.getType())) { return new PingWebSocketFrame(byteBuf); } else if (WebSocketMessage.Type.PONG.equals(message.getType())) { return new PongWebSocketFrame(byteBuf); } else { throw new IllegalArgumentException("Unexpected message type: " + message.getType()); } }
Example #3
Source File: ReactorNettyWebSocketClient.java From java-technology-stack with MIT License | 6 votes |
@Override public Mono<Void> execute(URI url, HttpHeaders requestHeaders, WebSocketHandler handler) { return getHttpClient() .headers(nettyHeaders -> setNettyHeaders(requestHeaders, nettyHeaders)) .websocket(StringUtils.collectionToCommaDelimitedString(handler.getSubProtocols())) .uri(url.toString()) .handle((inbound, outbound) -> { HttpHeaders responseHeaders = toHttpHeaders(inbound); String protocol = responseHeaders.getFirst("Sec-WebSocket-Protocol"); HandshakeInfo info = new HandshakeInfo(url, responseHeaders, Mono.empty(), protocol); NettyDataBufferFactory factory = new NettyDataBufferFactory(outbound.alloc()); WebSocketSession session = new ReactorNettyWebSocketSession(inbound, outbound, info, factory); if (logger.isDebugEnabled()) { logger.debug("Started session '" + session.getId() + "' for " + url); } return handler.handle(session); }) .doOnRequest(n -> { if (logger.isDebugEnabled()) { logger.debug("Connecting to " + url); } }) .next(); }
Example #4
Source File: ReactorNettyRequestUpgradeStrategy.java From java-technology-stack with MIT License | 6 votes |
@Override public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler, @Nullable String subProtocol, Supplier<HandshakeInfo> handshakeInfoFactory) { ServerHttpResponse response = exchange.getResponse(); HttpServerResponse reactorResponse = ((AbstractServerHttpResponse) response).getNativeResponse(); HandshakeInfo handshakeInfo = handshakeInfoFactory.get(); NettyDataBufferFactory bufferFactory = (NettyDataBufferFactory) response.bufferFactory(); return reactorResponse.sendWebsocket(subProtocol, this.maxFramePayloadLength, (in, out) -> { ReactorNettyWebSocketSession session = new ReactorNettyWebSocketSession( in, out, handshakeInfo, bufferFactory, this.maxFramePayloadLength); return handler.handle(session); }); }
Example #5
Source File: ReactorNettyRequestUpgradeStrategy.java From spring-analysis-note with MIT License | 6 votes |
@Override public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler, @Nullable String subProtocol, Supplier<HandshakeInfo> handshakeInfoFactory) { ServerHttpResponse response = exchange.getResponse(); HttpServerResponse reactorResponse = ((AbstractServerHttpResponse) response).getNativeResponse(); HandshakeInfo handshakeInfo = handshakeInfoFactory.get(); NettyDataBufferFactory bufferFactory = (NettyDataBufferFactory) response.bufferFactory(); return reactorResponse.sendWebsocket(subProtocol, this.maxFramePayloadLength, (in, out) -> { ReactorNettyWebSocketSession session = new ReactorNettyWebSocketSession( in, out, handshakeInfo, bufferFactory, this.maxFramePayloadLength); URI uri = exchange.getRequest().getURI(); return handler.handle(session).checkpoint(uri + " [ReactorNettyRequestUpgradeStrategy]"); }); }
Example #6
Source File: ReactorNettyWebSocketClient.java From spring-analysis-note with MIT License | 6 votes |
@Override public Mono<Void> execute(URI url, HttpHeaders requestHeaders, WebSocketHandler handler) { String protocols = StringUtils.collectionToCommaDelimitedString(handler.getSubProtocols()); return getHttpClient() .headers(nettyHeaders -> setNettyHeaders(requestHeaders, nettyHeaders)) .websocket(protocols, getMaxFramePayloadLength()) .uri(url.toString()) .handle((inbound, outbound) -> { HttpHeaders responseHeaders = toHttpHeaders(inbound); String protocol = responseHeaders.getFirst("Sec-WebSocket-Protocol"); HandshakeInfo info = new HandshakeInfo(url, responseHeaders, Mono.empty(), protocol); NettyDataBufferFactory factory = new NettyDataBufferFactory(outbound.alloc()); WebSocketSession session = new ReactorNettyWebSocketSession( inbound, outbound, info, factory, getMaxFramePayloadLength()); if (logger.isDebugEnabled()) { logger.debug("Started session '" + session.getId() + "' for " + url); } return handler.handle(session).checkpoint(url + " [ReactorNettyWebSocketClient]"); }) .doOnRequest(n -> { if (logger.isDebugEnabled()) { logger.debug("Connecting to " + url); } }) .next(); }
Example #7
Source File: NettyWebSocketSessionSupport.java From spring-analysis-note with MIT License | 6 votes |
protected WebSocketFrame toFrame(WebSocketMessage message) { ByteBuf byteBuf = NettyDataBufferFactory.toByteBuf(message.getPayload()); if (WebSocketMessage.Type.TEXT.equals(message.getType())) { return new TextWebSocketFrame(byteBuf); } else if (WebSocketMessage.Type.BINARY.equals(message.getType())) { return new BinaryWebSocketFrame(byteBuf); } else if (WebSocketMessage.Type.PING.equals(message.getType())) { return new PingWebSocketFrame(byteBuf); } else if (WebSocketMessage.Type.PONG.equals(message.getType())) { return new PongWebSocketFrame(byteBuf); } else { throw new IllegalArgumentException("Unexpected message type: " + message.getType()); } }
Example #8
Source File: DataBufferFactoryWrapperTest.java From armeria with Apache License 2.0 | 6 votes |
@Test public void usingNettyDataBufferFactory_PooledHttpData() { final DataBufferFactoryWrapper<?> wrapper = new DataBufferFactoryWrapper<>(new NettyDataBufferFactory(UnpooledByteBufAllocator.DEFAULT)); final PooledHttpData httpData1 = PooledHttpData.wrap(Unpooled.wrappedBuffer("abc".getBytes())); final DataBuffer buffer = wrapper.toDataBuffer(httpData1); assertThat(buffer).isInstanceOf(NettyDataBuffer.class); assertThat(((NettyDataBuffer) buffer).getNativeBuffer().refCnt()).isOne(); final HttpData httpData2 = wrapper.toHttpData(buffer); assertThat(httpData2).isInstanceOf(PooledHttpData.class); assertThat(((PooledHttpData) httpData2).content()) .isEqualTo(((NettyDataBuffer) buffer).getNativeBuffer()); assertThat(((PooledHttpData) httpData2).refCnt()).isOne(); }
Example #9
Source File: BrokerActuatorHandlerRegistration.java From spring-cloud-rsocket with Apache License 2.0 | 5 votes |
private ConnectionSetupPayload getConnectionSetupPayload() { DataBufferFactory dataBufferFactory = messageHandler.getRSocketStrategies() .dataBufferFactory(); NettyDataBufferFactory ndbf = (NettyDataBufferFactory) dataBufferFactory; ByteBufAllocator byteBufAllocator = ndbf.getByteBufAllocator(); Payload setupPayload = DefaultPayload.create(Unpooled.EMPTY_BUFFER, Unpooled.EMPTY_BUFFER); ByteBuf setup = SetupFrameFlyweight.encode(byteBufAllocator, false, 1, 1, MESSAGE_RSOCKET_COMPOSITE_METADATA.getString(), // TODO: configurable? APPLICATION_CBOR.getString(), setupPayload); return ConnectionSetupPayload.create(setup); }
Example #10
Source File: NettyClientResponsePlugin.java From soul with Apache License 2.0 | 5 votes |
@Override public Mono<Void> execute(final ServerWebExchange exchange, final SoulPluginChain chain) { return Mono.defer(() -> { Connection connection = exchange.getAttribute(Constants.CLIENT_RESPONSE_CONN_ATTR); if (connection == null) { return Mono.empty(); } if (log.isTraceEnabled()) { log.trace("NettyWriteResponseFilter start inbound: " + connection.channel().id().asShortText() + ", outbound: " + exchange.getLogPrefix()); } ServerHttpResponse response = exchange.getResponse(); NettyDataBufferFactory factory = (NettyDataBufferFactory) response.bufferFactory(); final Flux<NettyDataBuffer> body = connection .inbound() .receive() .retain() .map(factory::wrap); MediaType contentType = response.getHeaders().getContentType(); return isStreamingMediaType(contentType) ? response.writeAndFlushWith(body.map(Flux::just)) : response.writeWith(body); }) .then(chain.execute(exchange) .doOnError(throwable -> cleanup(exchange))).doOnCancel(() -> cleanup(exchange)); }
Example #11
Source File: MicronautServerHttpRequest.java From micronaut-spring with Apache License 2.0 | 5 votes |
@SuppressWarnings("SubscriberImplementation") @Override public reactor.core.publisher.Flux<DataBuffer> getBody() { final Optional<Channel> opt = channelResolver.resolveChannel(request); if (opt.isPresent()) { final Channel channel = opt.get(); final NettyDataBufferFactory nettyDataBufferFactory = new NettyDataBufferFactory(channel.alloc()); final Optional<HttpContentProcessor<ByteBufHolder>> httpContentProcessor = channelResolver.resolveContentProcessor(request); if (httpContentProcessor.isPresent()) { final HttpContentProcessor<ByteBufHolder> processor = httpContentProcessor.get(); return Flux.from(subscriber -> processor.subscribe(new Subscriber<ByteBufHolder>() { @Override public void onSubscribe(Subscription s) { subscriber.onSubscribe(s); } @Override public void onNext(ByteBufHolder byteBufHolder) { subscriber.onNext(nettyDataBufferFactory.wrap(byteBufHolder.content())); } @Override public void onError(Throwable t) { subscriber.onError(t); } @Override public void onComplete() { subscriber.onComplete(); } })); } } return Flux.empty(); }
Example #12
Source File: MetadataEncoder.java From spring-cloud-rsocket with Apache License 2.0 | 5 votes |
private DataBuffer asDataBuffer(ByteBuf byteBuf) { if (bufferFactory() instanceof NettyDataBufferFactory) { return ((NettyDataBufferFactory) bufferFactory()).wrap(byteBuf); } else { DataBuffer buffer = bufferFactory().wrap(byteBuf.nioBuffer()); byteBuf.release(); return buffer; } }
Example #13
Source File: MetadataEncoder.java From spring-cloud-rsocket with Apache License 2.0 | 5 votes |
public MetadataEncoder(MimeType metadataMimeType, RSocketStrategies strategies) { Assert.notNull(metadataMimeType, "'metadataMimeType' is required"); Assert.notNull(strategies, "RSocketStrategies is required"); this.metadataMimeType = metadataMimeType; this.strategies = strategies; this.isComposite = this.metadataMimeType.toString() .equals(WellKnownMimeType.MESSAGE_RSOCKET_COMPOSITE_METADATA.getString()); this.allocator = bufferFactory() instanceof NettyDataBufferFactory ? ((NettyDataBufferFactory) bufferFactory()).getByteBufAllocator() : ByteBufAllocator.DEFAULT; }
Example #14
Source File: RouteSetup.java From spring-cloud-rsocket with Apache License 2.0 | 5 votes |
@Override public DataBuffer encodeValue(RouteSetup value, DataBufferFactory bufferFactory, ResolvableType valueType, MimeType mimeType, Map<String, Object> hints) { NettyDataBufferFactory factory = (NettyDataBufferFactory) bufferFactory; ByteBuf encoded = RouteSetup.encode(factory.getByteBufAllocator(), value); return factory.wrap(encoded); }
Example #15
Source File: Forwarding.java From spring-cloud-rsocket with Apache License 2.0 | 5 votes |
@Override public DataBuffer encodeValue(Forwarding value, DataBufferFactory bufferFactory, ResolvableType valueType, MimeType mimeType, Map<String, Object> hints) { NettyDataBufferFactory factory = (NettyDataBufferFactory) bufferFactory; ByteBuf encoded = Forwarding.encode(factory.getByteBufAllocator(), value); return factory.wrap(encoded); }
Example #16
Source File: ReactorServerHttpRequest.java From java-technology-stack with MIT License | 5 votes |
public ReactorServerHttpRequest(HttpServerRequest request, NettyDataBufferFactory bufferFactory) throws URISyntaxException { super(initUri(request), "", initHeaders(request)); Assert.notNull(bufferFactory, "DataBufferFactory must not be null"); this.request = request; this.bufferFactory = bufferFactory; }
Example #17
Source File: ReactorClientHttpRequest.java From java-technology-stack with MIT License | 5 votes |
@Override public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) { return doCommit(() -> { Flux<ByteBuf> byteBufFlux = Flux.from(body).map(NettyDataBufferFactory::toByteBuf); return this.outbound.send(byteBufFlux).then(); }); }
Example #18
Source File: ReactorClientHttpRequest.java From java-technology-stack with MIT License | 5 votes |
public ReactorClientHttpRequest(HttpMethod method, URI uri, HttpClientRequest request, NettyOutbound outbound) { this.httpMethod = method; this.uri = uri; this.request = request; this.outbound = outbound; this.bufferFactory = new NettyDataBufferFactory(outbound.alloc()); }
Example #19
Source File: WebClientDataBufferAllocatingTests.java From java-technology-stack with MIT License | 5 votes |
private ReactorClientHttpConnector initConnector() { if (bufferFactory instanceof NettyDataBufferFactory) { ByteBufAllocator allocator = ((NettyDataBufferFactory) bufferFactory).getByteBufAllocator(); return new ReactorClientHttpConnector(this.factory, httpClient -> httpClient.tcpConfiguration(tcpClient -> tcpClient.option(ChannelOption.ALLOCATOR, allocator))); } else { return new ReactorClientHttpConnector(); } }
Example #20
Source File: PayloadUtils.java From spring-analysis-note with MIT License | 5 votes |
/** * Use this method to slice, retain and wrap the data portion of the * {@code Payload}, and also to release the {@code Payload}. This assumes * the Payload metadata has been read by now and ensures downstream code * need only be aware of {@code DataBuffer}s. * @param payload the payload to process * @param bufferFactory the DataBufferFactory to wrap with * @return the created {@code DataBuffer} instance */ public static DataBuffer retainDataAndReleasePayload(Payload payload, DataBufferFactory bufferFactory) { try { if (bufferFactory instanceof NettyDataBufferFactory) { ByteBuf byteBuf = payload.sliceData().retain(); return ((NettyDataBufferFactory) bufferFactory).wrap(byteBuf); } else { return bufferFactory.wrap(payload.getData()); } } finally { if (payload.refCnt() > 0) { payload.release(); } } }
Example #21
Source File: ByteBufLeakTest.java From armeria with Apache License 2.0 | 5 votes |
@Bean public DataBufferFactory dataBufferFactory() { return new NettyDataBufferFactory(PooledByteBufAllocator.DEFAULT) { // This method will be called when emitting string from Mono/Flux. @Override public NettyDataBuffer allocateBuffer(int initialCapacity) { final NettyDataBuffer buffer = super.allocateBuffer(initialCapacity); // Keep allocated buffers. allocatedBuffers.offer(buffer); return buffer; } }; }
Example #22
Source File: BodyExtractorsTests.java From java-technology-stack with MIT License | 5 votes |
@Test // SPR-17054 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 -> { assertTrue(throwable instanceof UnsupportedMediaTypeException); try { buffer.release(); Assert.fail("releasing the buffer should have failed"); } catch (IllegalReferenceCountException exc) { } body.assertCancelled(); }).verify(); }
Example #23
Source File: RSocketServerToClientIntegrationTests.java From spring-analysis-note with MIT License | 5 votes |
@Bean public RSocketStrategies rsocketStrategies() { return RSocketStrategies.builder() .decoder(StringDecoder.allMimeTypes()) .encoder(CharSequenceEncoder.allMimeTypes()) .dataBufferFactory(new NettyDataBufferFactory(PooledByteBufAllocator.DEFAULT)) .build(); }
Example #24
Source File: ReactorClientHttpRequest.java From spring-analysis-note with MIT License | 5 votes |
@Override public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) { return doCommit(() -> { // Send as Mono if possible as an optimization hint to Reactor Netty if (body instanceof Mono) { Mono<ByteBuf> byteBufMono = Mono.from(body).map(NettyDataBufferFactory::toByteBuf); return this.outbound.send(byteBufMono).then(); } else { Flux<ByteBuf> byteBufFlux = Flux.from(body).map(NettyDataBufferFactory::toByteBuf); return this.outbound.send(byteBufFlux).then(); } }); }
Example #25
Source File: ReactorClientHttpRequest.java From spring-analysis-note with MIT License | 5 votes |
public ReactorClientHttpRequest(HttpMethod method, URI uri, HttpClientRequest request, NettyOutbound outbound) { this.httpMethod = method; this.uri = uri; this.request = request; this.outbound = outbound; this.bufferFactory = new NettyDataBufferFactory(outbound.alloc()); }
Example #26
Source File: WebClientDataBufferAllocatingTests.java From spring-analysis-note with MIT License | 5 votes |
private ReactorClientHttpConnector initConnector() { if (bufferFactory instanceof NettyDataBufferFactory) { ByteBufAllocator allocator = ((NettyDataBufferFactory) bufferFactory).getByteBufAllocator(); return new ReactorClientHttpConnector(this.factory, httpClient -> httpClient.tcpConfiguration(tcpClient -> tcpClient.option(ChannelOption.ALLOCATOR, allocator))); } else { return new ReactorClientHttpConnector(); } }
Example #27
Source File: ReactorNettyWebSocketSession.java From spring-analysis-note with MIT License | 5 votes |
/** * Constructor with an additional maxFramePayloadLength argument. * @since 5.1 */ public ReactorNettyWebSocketSession(WebsocketInbound inbound, WebsocketOutbound outbound, HandshakeInfo info, NettyDataBufferFactory bufferFactory, int maxFramePayloadLength) { super(new WebSocketConnection(inbound, outbound), info, bufferFactory); this.maxFramePayloadLength = maxFramePayloadLength; }
Example #28
Source File: ReactorServerHttpRequest.java From spring-analysis-note with MIT License | 5 votes |
public ReactorServerHttpRequest(HttpServerRequest request, NettyDataBufferFactory bufferFactory) throws URISyntaxException { super(initUri(request), "", initHeaders(request)); Assert.notNull(bufferFactory, "DataBufferFactory must not be null"); this.request = request; this.bufferFactory = bufferFactory; }
Example #29
Source File: ByteBufEncoder.java From alibaba-rsocket-broker with Apache License 2.0 | 5 votes |
public DataBuffer encodeValue(ByteBuf byteBuf, DataBufferFactory bufferFactory, ResolvableType valueType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) { DataBuffer dataBuffer = ((NettyDataBufferFactory) bufferFactory).wrap(byteBuf); if (this.logger.isDebugEnabled() && !Hints.isLoggingSuppressed(hints)) { String logPrefix = Hints.getLogPrefix(hints); this.logger.debug(logPrefix + "Writing " + dataBuffer.readableByteCount() + " bytes"); } return dataBuffer; }
Example #30
Source File: ByteBufEncoder.java From alibaba-rsocket-broker with Apache License 2.0 | 5 votes |
public DataBuffer encodeValue(ByteBuf byteBuf, DataBufferFactory bufferFactory, ResolvableType valueType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) { DataBuffer dataBuffer = ((NettyDataBufferFactory) bufferFactory).wrap(byteBuf); if (this.logger.isDebugEnabled() && !Hints.isLoggingSuppressed(hints)) { String logPrefix = Hints.getLogPrefix(hints); this.logger.debug(logPrefix + "Writing " + dataBuffer.readableByteCount() + " bytes"); } return dataBuffer; }