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

com.eg.agent.android.instrumentation.okhttp3.InstrumentOkHttp3 Maven / Gradle / Ivy

The newest version!
/*
 * Decompiled with CFR 0.137.
 * 
 * Could not load the following classes:
 *  okhttp3.Call
 *  okhttp3.OkHttpClient
 *  okhttp3.OkUrlFactory
 *  okhttp3.Request
 *  okhttp3.Request$Builder
 *  okhttp3.Response
 *  okhttp3.Response$Builder
 *  okhttp3.ResponseBody
 *  okhttp3.internal.Internal
 *  okhttp3.internal.connection.StreamAllocation
 */
package com.eg.agent.android.instrumentation.okhttp3;

import com.eg.agent.android.connectivity.Payload;
import com.eg.agent.android.instrumentation.HttpURLConnectionExtension;
import com.eg.agent.android.instrumentation.HttpsURLConnectionExtension;
import com.eg.agent.android.instrumentation.httphelper.TransactionData;
import com.eg.agent.android.instrumentation.okhttp3.CallExtension;
import com.eg.agent.android.instrumentation.okhttp3.RequestBuilderExtension;
import com.eg.agent.android.instrumentation.okhttp3.ResponseBuilderExtension;
import com.eg.agent.android.logging.EGAgentLog;
import com.eg.agent.android.logging.EGAgentLogManager;
import com.eg.agent.android.EGAgent;
import com.eg.agent.android.Features;
import com.eg.agent.android.RewriteClass;

import java.lang.reflect.Method;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.OkUrlFactory;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.internal.Internal;
import okhttp3.internal.connection.StreamAllocation;

public class InstrumentOkHttp3 {

	private static EGAgentLog log = EGAgentLogManager.getAgentLog();
    
    private InstrumentOkHttp3() {
    }

    @RewriteClass
    public static Request build(Request.Builder builder) {
        return new RequestBuilderExtension(builder).build();
    }

    @RewriteClass
    public static Call newCall(OkHttpClient client, Request request) {
        TransactionData transactionState = new TransactionData();
        if (Features.featureEnabled(Features.DistributedTracing)) {
            transactionState.setCatPayload(EGAgent.getCatFacade().startTrip());
            try {
                Request instrumentedRequest = request.newBuilder().header("eg", transactionState.getCatPayload().asBase64Json()).build();
                return new CallExtension(client, instrumentedRequest, client.newCall(instrumentedRequest), transactionState);
            }
            catch (Exception e) {
                log.error(e.getMessage());
            }
        }
        return new CallExtension(client, request, client.newCall(request), transactionState);
    }

    @RewriteClass
    public static Response.Builder body(Response.Builder builder, ResponseBody body) {
        return new ResponseBuilderExtension(builder).body(body);
    }

    @RewriteClass
    public static Response.Builder newBuilder(Response.Builder builder) {
        return new ResponseBuilderExtension(builder);
    }

    @RewriteClass(isStatic=false, scope="okhttp3.OkUrlFactory")
    public static HttpURLConnection open(OkUrlFactory factory, URL url) {
        HttpURLConnection conn = factory.open(url);
        String protocol = url.getProtocol();
        if (protocol.equals("http")) {
            return new HttpURLConnectionExtension(conn);
        }
        if (protocol.equals("https") && conn instanceof HttpsURLConnection) {
            return new HttpsURLConnectionExtension((HttpsURLConnection)conn);
        }
        return new HttpURLConnectionExtension(conn);
    }

    private static void logReflectionError(String signature) {
        String crlf = System.getProperty("line.separator");
        log.error("Unable to resolve method \"" + signature + "\"." + crlf + "This is usually due to building the app with unsupported OkHttp versions." + crlf + "Check your build configuration for compatibility.");
    }

    public static class OkHttp35 {
        @RewriteClass
        public static void setCallWebSocket(Internal internal, Call call) {
            try {
                Method setCallWebSocket;
                if (call instanceof CallExtension) {
                    call = ((CallExtension)call).getImpl();
                }
                if ((setCallWebSocket = Internal.class.getMethod("setCallWebSocket", Call.class)) != null) {
                    setCallWebSocket.invoke((Object)internal, new Object[]{call});
                } else {
                    InstrumentOkHttp3.logReflectionError("setCallWebSocket(Lokhttp3/Call;)V");
                }
            }
            catch (Exception e) {
                log.error("InstrumentOkHttp3: " + e.getMessage());
            }
        }

        @RewriteClass
        public static StreamAllocation callEngineGetStreamAllocation(Internal internal, Call call) {
            StreamAllocation streamAllocation = null;
            try {
                Method callEngineGetStreamAllocation;
                if (call instanceof CallExtension) {
                    call = ((CallExtension)call).getImpl();
                }
                if ((callEngineGetStreamAllocation = Internal.class.getMethod("callEngineGetStreamAllocation", Call.class)) != null) {
                    streamAllocation = (StreamAllocation)callEngineGetStreamAllocation.invoke((Object)internal, new Object[]{call});
                } else {
                    InstrumentOkHttp3.logReflectionError("callEngineGetStreamAllocation(Lokhttp3/Call;)Lokhttp3/internal/connection/StreamAllocation;");
                }
            }
            catch (Exception e) {
                log.error("InstrumentOkHttp3: " + e.getMessage());
            }
            return streamAllocation;
        }

        @RewriteClass
        public static Call newWebSocketCall(Internal internal, OkHttpClient client, Request request) {
            CallExtension call = null;
            try {
                Method newWebSocketCall = Internal.class.getMethod("newWebSocketCall", OkHttpClient.class, Request.class);
                if (newWebSocketCall != null) {
                    TransactionData transactionState = new TransactionData();
                    if (Features.featureEnabled(Features.DistributedTracing)) {
                        transactionState.setCatPayload(EGAgent.getCatFacade().startTrip());
                        Request instrumentedRequest = request.newBuilder().header("eg", transactionState.getCatPayload().asBase64Json()).build();
                        Call impl = (Call)newWebSocketCall.invoke((Object)internal, new Object[]{client, instrumentedRequest});
                        call = new CallExtension(client, instrumentedRequest, impl, transactionState);
                    } else {
                        Call impl = (Call)newWebSocketCall.invoke((Object)internal, new Object[]{client, request});
                        call = new CallExtension(client, request, impl, transactionState);
                    }
                } else {
                    InstrumentOkHttp3.logReflectionError("newWebSocketCall(Lokhttp3/OkHttpClient;Lokhttp3/Request;)Lokhttp3/Call;");
                }
            }
            catch (Exception e) {
                log.error("InstrumentOkHttp3: " + e.getMessage());
            }
            return call;
        }
    }

}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy