Search in sources :

Example 1 with Http2Header

use of com.webpieces.http2.api.dto.lowlevel.lib.Http2Header in project webpieces by deanhiller.

the class HttpsJsonClient method sendHttpRequest.

/**
 * <b>DO NOT USE FOR PUBLIC HTTP REQUEST THIS IS FOR INTERNAL USE ONLY</b>
 */
public <T> XFuture<T> sendHttpRequest(Method method, Object request, Endpoint endpoint, Class<T> responseType) {
    InetSocketAddress apiAddress = endpoint.getServerAddress();
    String httpMethod = endpoint.getHttpMethod();
    String endpointPath = endpoint.getUrlPath();
    Http2Request httpReq = createHttpReq(apiAddress, httpMethod, endpointPath);
    RequestCloseListener closeListener = new RequestCloseListener(schedulerSvc);
    Http2Socket httpSocket = createSocket(apiAddress, closeListener);
    XFuture<Void> connect = httpSocket.connect(apiAddress);
    String jsonRequest = marshal(request);
    byte[] reqAsBytes = jsonRequest.getBytes(StandardCharsets.UTF_8);
    if (jsonRequest.equals("null")) {
        // hack
        reqAsBytes = new byte[0];
    }
    DataWrapper data = WRAPPER_GEN.wrapByteArray(reqAsBytes);
    if (httpReq.getKnownMethod() == Http2Method.POST) {
        httpReq.addHeader(new Http2Header(Http2HeaderName.CONTENT_LENGTH, String.valueOf(data.getReadableSize())));
    }
    httpReq.addHeader(new Http2Header(Http2HeaderName.SCHEME, "https"));
    FullRequest fullRequest = new FullRequest(httpReq, data, null);
    log.info("curl request on socket(" + httpSocket + ")" + createCurl(fullRequest, apiAddress.getPort()));
    Map<String, Object> fullContext = Context.getContext();
    if (fullContext == null) {
        throw new IllegalStateException("Missing webserver filters? Context.getFullContext() must contain data");
    }
    Map<String, String> ctxMap = MDC.getCopyOfContextMap();
    Contexts contexts = new Contexts(ctxMap, fullContext);
    long start = System.currentTimeMillis();
    XFuture<T> future = futureUtil.catchBlockWrap(() -> sendAndTranslate(contexts, apiAddress, responseType, httpSocket, connect, fullRequest, jsonRequest), (t) -> translateException(httpReq, t));
    // // Track metrics with future.handle()
    // // If method is null, then no need to track metrics
    // // If monitoring is null, then this call probably came from OrderlyTest
    // if (method != null && monitoring != null) {
    // future = future.handle((r, e) -> {
    // String clientId = context.getRequest().getRequestState(OrderlyHeaders.CLIENT_ID.getHeaderName());
    // monitoring.endHttpClientTimer(method, clientId, endpoint, start);
    // 
    // if (e != null) {
    // monitoring.incrementHttpClientExceptionMetric(method, clientId, endpoint, e.getClass().getSimpleName());
    // return XFuture.<T>failedFuture(e);
    // }
    // 
    // monitoring.incrementHttpClientSuccessMetric(method, clientId, endpoint);
    // return XFuture.completedFuture(r);
    // }).thenCompose(Function.identity());
    // }
    // so we can cancel the future exactly when the socket closes
    closeListener.setFuture(future);
    return future;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) Contexts(org.webpieces.util.context.Contexts) DataWrapper(org.webpieces.data.api.DataWrapper) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) Http2Socket(org.webpieces.http2client.api.Http2Socket) FullRequest(org.webpieces.http2client.api.dto.FullRequest)

Example 2 with Http2Header

use of com.webpieces.http2.api.dto.lowlevel.lib.Http2Header in project webpieces by deanhiller.

the class HttpsJsonClient method createHttpReq.

public Http2Request createHttpReq(InetSocketAddress apiAddress, String method, String path) {
    Http2Request httpReq = new Http2Request();
    httpReq.addHeader(new Http2Header(Http2HeaderName.METHOD, method));
    httpReq.addHeader(new Http2Header(Http2HeaderName.AUTHORITY, apiAddress.getHostString()));
    httpReq.addHeader(new Http2Header(Http2HeaderName.PATH, path));
    httpReq.addHeader(new Http2Header(Http2HeaderName.USER_AGENT, "Webpieces Generated API Client"));
    // Http2HeaderName.ACCEPT
    httpReq.addHeader(new Http2Header(Http2HeaderName.ACCEPT, "application/json"));
    httpReq.addHeader(new Http2Header(Http2HeaderName.CONTENT_TYPE, "application/json"));
    Map<String, String> headers = (Map<String, String>) Context.get(Context.HEADERS);
    for (Map.Entry<String, String> entry : headers.entrySet()) {
        httpReq.addHeader(new Http2Header(entry.getKey(), entry.getValue()));
    }
    return httpReq;
}
Also used : Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) Map(java.util.Map)

Example 3 with Http2Header

use of com.webpieces.http2.api.dto.lowlevel.lib.Http2Header in project webpieces by deanhiller.

the class RequestCreator method createHttp2Request.

public static Http2Request createHttp2Request() {
    Http2Request request = new Http2Request();
    request.setEndOfStream(true);
    request.addHeader(new Http2Header(Http2HeaderName.METHOD, "/"));
    request.addHeader(new Http2Header(Http2HeaderName.PATH, "/"));
    request.addHeader(new Http2Header(Http2HeaderName.AUTHORITY, "myhost.com"));
    request.addHeader(new Http2Header(Http2HeaderName.SCHEME, "http"));
    return request;
}
Also used : Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)

Example 4 with Http2Header

use of com.webpieces.http2.api.dto.lowlevel.lib.Http2Header in project webpieces by deanhiller.

the class RequestCreator method createHttp2Response.

public static Http2Response createHttp2Response(int streamId) {
    Http2Response resp = new Http2Response();
    resp.addHeader(new Http2Header(Http2HeaderName.STATUS, "200"));
    resp.addHeader(new Http2Header("serverid", "3"));
    resp.setEndOfStream(true);
    resp.setStreamId(streamId);
    return resp;
}
Also used : Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)

Example 5 with Http2Header

use of com.webpieces.http2.api.dto.lowlevel.lib.Http2Header in project webpieces by deanhiller.

the class Http2Requests method createPush.

public static Http2Push createPush(int streamId) {
    Http2Push push = new Http2Push();
    push.setStreamId(streamId);
    push.addHeader(new Http2Header(Http2HeaderName.SERVER, "me"));
    return push;
}
Also used : Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) Http2Push(com.webpieces.http2.api.dto.highlevel.Http2Push)

Aggregations

Http2Header (com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)72 Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)26 Http2Request (com.webpieces.http2.api.dto.highlevel.Http2Request)23 ArrayList (java.util.ArrayList)23 DataWrapper (org.webpieces.data.api.DataWrapper)9 Http2HeaderName (com.webpieces.http2.api.dto.lowlevel.lib.Http2HeaderName)8 StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)8 Http2Push (com.webpieces.http2.api.dto.highlevel.Http2Push)7 XFuture (org.webpieces.util.futures.XFuture)7 InetSocketAddress (java.net.InetSocketAddress)6 Header (org.webpieces.httpparser.api.common.Header)6 CancelReason (com.webpieces.http2.api.dto.lowlevel.CancelReason)5 Http2Frame (com.webpieces.http2.api.dto.lowlevel.lib.Http2Frame)5 StreamRef (com.webpieces.http2.api.streaming.StreamRef)5 List (java.util.List)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 ResponseStreamHandle (com.webpieces.http2.api.streaming.ResponseStreamHandle)4 Executor (java.util.concurrent.Executor)4 Executors (java.util.concurrent.Executors)4