All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.webpieces.http2client.impl.ResponseCacher Maven / Gradle / Ivy

There is a newer version: 2.1.109
Show newest version
package org.webpieces.http2client.impl;

import org.webpieces.util.futures.XFuture;
import java.util.function.Supplier;

import org.webpieces.data.api.DataWrapper;
import org.webpieces.http2client.api.dto.FullRequest;
import org.webpieces.http2client.api.dto.FullResponse;

import com.webpieces.http2.api.dto.highlevel.Http2Request;
import com.webpieces.http2.api.dto.highlevel.Http2Trailers;
import com.webpieces.http2.api.dto.lowlevel.DataFrame;
import com.webpieces.http2.api.streaming.RequestStreamHandle;
import com.webpieces.http2.api.streaming.StreamRef;
import com.webpieces.http2.api.streaming.StreamWriter;

public class ResponseCacher {

	private Supplier openStreamFunc;

	public ResponseCacher(Supplier openStreamFunc) {
		this.openStreamFunc = openStreamFunc;
	}

	public XFuture run(FullRequest request) {
		SingleResponseListener responseListener = new SingleResponseListener();
		
		RequestStreamHandle streamHandle = openStreamFunc.get();
		
		Http2Request req = request.getHeaders();
		
		if(request.getPayload() == null) {
			request.getHeaders().setEndOfStream(true);
			streamHandle.process(req, responseListener);
			return responseListener.fetchResponseFuture();
		} else if(request.getTrailingHeaders() == null) {
			request.getHeaders().setEndOfStream(false);
			DataFrame data = createData(request, true);

			StreamRef streamRef = streamHandle.process(request.getHeaders(), responseListener);

			return streamRef.getWriter()
						.thenCompose(writer -> {
							data.setStreamId(req.getStreamId());
							return writer.processPiece(data);
						})
						.thenCompose(writer -> responseListener.fetchResponseFuture());
		}
		
		request.getHeaders().setEndOfStream(false);
		DataFrame data = createData(request, false);
		Http2Trailers trailers = request.getTrailingHeaders();
		trailers.setEndOfStream(true);


		StreamRef streamRef = streamHandle.process(request.getHeaders(), responseListener);

		return streamRef.getWriter()
				.thenCompose(writer -> writeStuff(writer, req, data, trailers, responseListener));
	}

	private DataFrame createData(FullRequest request, boolean isEndOfStream) {
		DataWrapper payload = request.getPayload();
		DataFrame data = new DataFrame();
		data.setEndOfStream(isEndOfStream);
		data.setData(payload);
		return data;
	}
	
	private XFuture writeStuff(
			StreamWriter writer, Http2Request req, DataFrame data, Http2Trailers trailers, SingleResponseListener responseListener) {
		
		data.setStreamId(req.getStreamId());
		return writer.processPiece(data)
						.thenCompose(v -> {
							trailers.setStreamId(req.getStreamId());
							return writer.processPiece(trailers);
						})
						.thenCompose(v -> responseListener.fetchResponseFuture());
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy