org.eclipse.jetty.reactive.client.ContentChunk Java Examples

The following examples show how to use org.eclipse.jetty.reactive.client.ContentChunk. 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: JettyClientHttpConnector.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private DataBuffer toDataBuffer(ContentChunk chunk) {

		// We must copy until this is resolved:
		// https://github.com/eclipse/jetty.project/issues/2429

		// Use copy instead of buffer wrapping because Callback#succeeded() is
		// used not only to release the buffer but also to request more data
		// which is a problem for codecs that buffer data.

		DataBuffer buffer = this.bufferFactory.allocateBuffer(chunk.buffer.capacity());
		buffer.write(chunk.buffer);
		chunk.callback.succeeded();
		return buffer;
	}
 
Example #2
Source File: JettyClientHttpRequest.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
	Flux<ContentChunk> chunks = Flux.from(body).map(this::toContentChunk);
	ReactiveRequest.Content content = ReactiveRequest.Content.fromPublisher(chunks, getContentType());
	this.reactiveRequest = ReactiveRequest.newBuilder(this.jettyRequest).content(content).build();
	return doCommit(this::completes);
}
 
Example #3
Source File: JettyClientHttpRequest.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public Mono<Void> writeAndFlushWith(Publisher<? extends Publisher<? extends DataBuffer>> body) {
	Flux<ContentChunk> chunks = Flux.from(body)
			.flatMap(Function.identity())
			.doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release)
			.map(this::toContentChunk);
	ReactiveRequest.Content content = ReactiveRequest.Content.fromPublisher(chunks, getContentType());
	this.reactiveRequest = ReactiveRequest.newBuilder(this.jettyRequest).content(content).build();
	return doCommit(this::completes);
}
 
Example #4
Source File: JettyClientHttpRequest.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private ContentChunk toContentChunk(DataBuffer buffer) {
	return new ContentChunk(buffer.asByteBuffer(), new Callback() {
		@Override
		public void succeeded() {
			DataBufferUtils.release(buffer);
		}

		@Override
		public void failed(Throwable x) {
			DataBufferUtils.release(buffer);
			throw Exceptions.propagate(x);
		}
	});
}
 
Example #5
Source File: JettyClientHttpConnector.java    From java-technology-stack with MIT License 5 votes vote down vote up
private DataBuffer toDataBuffer(ContentChunk chunk) {

		// We must copy until this is resolved:
		// https://github.com/eclipse/jetty.project/issues/2429

		// Use copy instead of buffer wrapping because Callback#succeeded() is
		// used not only to release the buffer but also to request more data
		// which is a problem for codecs that buffer data.

		DataBuffer buffer = this.bufferFactory.allocateBuffer(chunk.buffer.capacity());
		buffer.write(chunk.buffer);
		chunk.callback.succeeded();
		return buffer;
	}
 
Example #6
Source File: JettyClientHttpRequest.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
	Flux<ContentChunk> chunks = Flux.from(body).map(this::toContentChunk);
	ReactiveRequest.Content content = ReactiveRequest.Content.fromPublisher(chunks, getContentType());
	this.reactiveRequest = ReactiveRequest.newBuilder(this.jettyRequest).content(content).build();
	return doCommit(this::completes);
}
 
Example #7
Source File: JettyClientHttpRequest.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public Mono<Void> writeAndFlushWith(Publisher<? extends Publisher<? extends DataBuffer>> body) {
	Flux<ContentChunk> chunks = Flux.from(body)
			.flatMap(Function.identity())
			.doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release)
			.map(this::toContentChunk);
	ReactiveRequest.Content content = ReactiveRequest.Content.fromPublisher(chunks, getContentType());
	this.reactiveRequest = ReactiveRequest.newBuilder(this.jettyRequest).content(content).build();
	return doCommit(this::completes);
}
 
Example #8
Source File: JettyClientHttpRequest.java    From java-technology-stack with MIT License 5 votes vote down vote up
private ContentChunk toContentChunk(DataBuffer buffer) {
	return new ContentChunk(buffer.asByteBuffer(), new Callback() {
		@Override
		public void succeeded() {
			DataBufferUtils.release(buffer);
		}

		@Override
		public void failed(Throwable x) {
			DataBufferUtils.release(buffer);
			throw Exceptions.propagate(x);
		}
	});
}
 
Example #9
Source File: JettyReactiveHttpClient.java    From feign-reactive with Apache License 2.0 5 votes vote down vote up
protected ReactiveRequest.Content provideBody(ReactiveHttpRequest request) {
	Publisher<ContentChunk> bodyPublisher;
	String contentType;
	if(request.body() instanceof Mono){
		if(bodyActualClass == ByteBuffer.class){
			bodyPublisher = ((Mono)request.body()).map(this::toByteBufferChunk);
			contentType = APPLICATION_OCTET_STREAM;
		}
		else if(bodyActualClass == byte[].class){
			bodyPublisher = Flux.from(request.body()).map(this::toByteArrayChunk);
			contentType = APPLICATION_OCTET_STREAM;
		}
		else if (CharSequence.class.isAssignableFrom(bodyActualClass)){
			bodyPublisher = Flux.from(request.body()).map(this::toCharSequenceChunk);
			contentType = TEXT_UTF_8;
		}
		else {
			bodyPublisher = Flux.from(request.body()).map(data -> toJsonChunk(data, false));
			contentType = APPLICATION_JSON_UTF_8;
		}

	} else {
		if(bodyActualClass == ByteBuffer.class){
			bodyPublisher = Flux.from(request.body()).map(this::toByteBufferChunk);
			contentType = APPLICATION_OCTET_STREAM;
		}
		else if(bodyActualClass == byte[].class){
			bodyPublisher = Flux.from(request.body()).map(this::toByteArrayChunk);
			contentType = APPLICATION_OCTET_STREAM;
		}
		else {
			bodyPublisher = Flux.from(request.body()).map(data -> toJsonChunk(data, true));
			contentType = APPLICATION_STREAM_JSON_UTF_8;
		}
	}

	return ReactiveRequest.Content.fromPublisher(bodyPublisher, contentType);
}
 
Example #10
Source File: JettyReactiveHttpClient.java    From feign-reactive with Apache License 2.0 5 votes vote down vote up
protected ContentChunk toJsonChunk(Object data, boolean stream){
	try {
		ByteArrayBuilder byteArrayBuilder = new ByteArrayBuilder();
		bodyWriter.writeValue(byteArrayBuilder, data);
		if(stream) {
			byteArrayBuilder.write(NEWLINE_SEPARATOR);
		}
		ByteBuffer buffer = ByteBuffer.wrap(byteArrayBuilder.toByteArray());
		return new ContentChunk(buffer);
	} catch (java.io.IOException e) {
		throw new UncheckedIOException(e);
	}
}
 
Example #11
Source File: JettyReactiveHttpResponse.java    From feign-reactive with Apache License 2.0 5 votes vote down vote up
JettyReactiveHttpResponse(Response clientResponse, Publisher<ContentChunk> contentChunks,
						  Class returnPublisherType, Class returnActualClass,
						  JsonFactory jsonFactory, ObjectReader objectReader) {
	this.clientResponse = clientResponse;
	this.contentChunks = contentChunks;
	this.returnPublisherType = returnPublisherType;
	this.returnActualClass = returnActualClass;
	this.objectReader = objectReader;
	this.jsonFactory = jsonFactory;
}
 
Example #12
Source File: JettyReactiveHttpClient.java    From feign-reactive with Apache License 2.0 4 votes vote down vote up
protected ContentChunk toByteBufferChunk(Object data){
	return new ContentChunk((ByteBuffer)data);
}
 
Example #13
Source File: JettyReactiveHttpClient.java    From feign-reactive with Apache License 2.0 4 votes vote down vote up
protected ContentChunk toByteArrayChunk(Object data){
	return new ContentChunk(ByteBuffer.wrap((byte[])data));
}
 
Example #14
Source File: JettyReactiveHttpClient.java    From feign-reactive with Apache License 2.0 4 votes vote down vote up
protected ContentChunk toCharSequenceChunk(Object data){
	CharBuffer charBuffer = CharBuffer.wrap((CharSequence) data);
	ByteBuffer byteBuffer = UTF_8.encode(charBuffer);
	return new ContentChunk(byteBuffer);
}