org.webpieces.httpclientx.impl.Http2SocketImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of http2to1_1-client Show documentation
Show all versions of http2to1_1-client Show documentation
http2 client api that talks http1 in case you want to convert from http2 to http1.1 without changing code
package org.webpieces.httpclientx.impl;
import java.net.InetSocketAddress;
import java.util.concurrent.CompletableFuture;
import org.webpieces.http2client.api.Http2Socket;
import org.webpieces.http2client.api.dto.FullRequest;
import org.webpieces.http2client.api.dto.FullResponse;
import org.webpieces.http2translations.api.Http2ToHttp1_1;
import org.webpieces.httpclient11.api.HttpFullRequest;
import org.webpieces.httpclient11.api.HttpSocket;
import org.webpieces.httpparser.api.dto.HttpRequest;
import com.webpieces.hpack.api.dto.Http2Request;
import com.webpieces.http2engine.api.ResponseHandler;
import com.webpieces.http2engine.api.StreamHandle;
import com.webpieces.http2engine.api.StreamWriter;
import com.webpieces.http2parser.api.dto.CancelReason;
public class Http2SocketImpl implements Http2Socket {
private HttpSocket socket1_1;
public Http2SocketImpl(HttpSocket socket1_1) {
this.socket1_1 = socket1_1;
}
@Override
public CompletableFuture connect(InetSocketAddress addr) {
return socket1_1.connect(addr);
}
@Override
public StreamHandle openStream() {
return new StreamImpl();
}
private class StreamImpl implements StreamHandle {
@Override
public CompletableFuture process(Http2Request request, ResponseHandler responseListener) {
HttpRequest req = Http2ToHttp1_1.translateRequest(request);
return socket1_1.send(req, new ResponseListener(socket1_1+"", responseListener))
.thenApply(s -> new StreamWriterImpl(s, req));
}
@Override
public CompletableFuture cancel(CancelReason payload) {
throw new UnsupportedOperationException("In http1_1, you can only just close the socket. call socket.close instead to cancel all requests");
}
}
@Override
public CompletableFuture send(FullRequest request) {
HttpFullRequest req = Translations2.translate(request);
return socket1_1.send(req).thenApply(r -> Translations2.translate(r));
}
@Override
public CompletableFuture close() {
return socket1_1.close();
}
@Override
public CompletableFuture sendPing() {
throw new UnsupportedOperationException("Http1.1 does not support ping");
}
}