Search in sources :

Example 1 with Contexts

use of org.webpieces.util.context.Contexts 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)

Aggregations

Http2Request (com.webpieces.http2.api.dto.highlevel.Http2Request)1 Http2Header (com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)1 InetSocketAddress (java.net.InetSocketAddress)1 DataWrapper (org.webpieces.data.api.DataWrapper)1 Http2Socket (org.webpieces.http2client.api.Http2Socket)1 FullRequest (org.webpieces.http2client.api.dto.FullRequest)1 Contexts (org.webpieces.util.context.Contexts)1