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

com.eg.agent.android.instrumentation.okhttp2.OkHttp2TransactionStateUtil Maven / Gradle / Ivy

The newest version!
/*
 * Decompiled with CFR 0.137.
 * 
 * Could not load the following classes:
 *  com.squareup.okhttp.Request
 *  com.squareup.okhttp.Response
 *  com.squareup.okhttp.Response$Builder
 *  com.squareup.okhttp.ResponseBody
 *  okio.Buffer
 *  okio.BufferedSource
 */
package com.eg.agent.android.instrumentation.okhttp2;

import com.eg.agent.android.common.Data;
import com.eg.agent.android.instrumentation.httphelper.TransactionData;
import com.eg.agent.android.instrumentation.httphelper.TransactionUtil;
import com.eg.agent.android.instrumentation.okhttp2.PrebufferedResponseBody;
import com.eg.agent.android.logging.EGAgentLog;
import com.eg.agent.android.logging.EGAgentLogManager;
import com.eg.agent.android.measurement.http.HttpTransactionMetrics;
import com.eg.agent.android.DataMeasurements;
import com.eg.agent.android.MobileAgentUpload;
import com.eg.agent.android.Queue;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.ResponseBody;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Map;
import java.util.TreeMap;
import okio.Buffer;
import okio.BufferedSource;

public class OkHttp2TransactionStateUtil
extends TransactionUtil {
	private static EGAgentLog log = EGAgentLogManager.getAgentLog();
    
    public static void inspectAndInstrument(TransactionData transactionState, Request request) {
        if (request == null) {
            log.warning("Missing request");
        } else {
        	 OkHttp2TransactionStateUtil.inspectAndInstrument(transactionState, request.urlString(), request.method());
        	if(request != null && request.body() != null) {
        		try {
					transactionState.setBytesSent(request.body().contentLength());
				} catch (IOException e) {
					log.error(e.getMessage());
				}
        	}
           
        }
    }

    public static Response inspectAndInstrumentResponse(TransactionData transactionState, Response response) {
        String appData = "";
        int statusCode = -1;
        long contentLength = 0L;
        if (response == null) {
            statusCode = 500;
            log.warning("Missing response");
        } else {
            appData = response.header("X-EG-App-Data");
            statusCode = response.code();
            try {
            	if(response.body() != null && response.body().contentType() != null) {
            		transactionState.setContentType(response.body().contentType().toString());
            	}
                contentLength = response.body().contentLength();
                
                if(transactionState.getUrl() == null && response.request() != null && response.request().url() != null) {
                	transactionState.setUrl( response.request().url().toString());
            	}
            }
            catch (Exception e) {
                log.warning("Missing body or content length");
            }
           
        }
//        response = 
        OkHttp2TransactionStateUtil.inspectAndInstrumentResponse(transactionState, appData, (int)contentLength, statusCode);
        return OkHttp2TransactionStateUtil.addTransactionAndErrorData(transactionState, response);
    }
    
    public static void inspectAndInstrumentResponse(TransactionData transactionState, String appData, int contentLength, int statusCode) {
    	transactionState.setStatusCode(statusCode);
    	log.debug("inspectAndInstrumentResponse:transactionState:URL: "+transactionState.getUrl());
    	log.debug("inspectAndInstrumentResponse:transactionState: "+transactionState);
    	log.debug("inspectAndInstrumentResponse:appData: "+appData);
    	log.debug("inspectAndInstrumentResponse:contentLength: "+contentLength);
    	log.debug("inspectAndInstrumentResponse:statuscode: "+statusCode);
//    	new Throwable().printStackTrace();
        if (appData != null && !appData.equals("")) {
            transactionState.setAppData(appData);
        }

        if (contentLength >= 0) {
            transactionState.setBytesReceived((long) contentLength);
        }

        Data transactionData = transactionState.end();
//        HttpTransactionMetrics metrics=  new HttpTransactionMetrics(transactionData);
//       	metrics.setMainJson(MobileAgentUpload.getJSONObjet(transactionState));
//        Queue.queue(metrics);

//        MobileAgentUpload.sendData(transactionState);
    }


    protected static Response addTransactionAndErrorData(TransactionData transactionState, Response response) {
        Data transactionData = transactionState.end();
        if (transactionData != null) {
            if (response != null && transactionState.isErrorOrFailure()) {
                TreeMap params;
                String responseBodyString;
                block8 : {
                    String contentTypeHeader = response.header("Content-Type");
                    params = new TreeMap();
                    if (contentTypeHeader != null && contentTypeHeader.length() > 0 && !"".equals(contentTypeHeader)) {
                        params.put("content_type", contentTypeHeader);
                    }
                    params.put("content_length", transactionState.getBytesReceived() + "");
                    responseBodyString = "";
                    try {
                        if (response.body() != null) {
                            ResponseBody body = response.body();
                            ByteBuffer byteBuffer = ByteBuffer.wrap(body.bytes());
                            Buffer buffer = new Buffer().write(byteBuffer.array());
                            PrebufferedResponseBody newBody = new PrebufferedResponseBody(body, (BufferedSource)buffer);
                            response = response.newBuilder().body((ResponseBody)newBody).build();
                            responseBodyString = new String(byteBuffer.array());
                        } else if (response.message() != null) {
                            log.warning("Missing response body, using response message");
                            responseBodyString = response.message();
                        }
                    }
                    catch (Exception e) {
                        if (response.message() == null) break block8;
                        log.warning("Missing response body, using response message");
                        responseBodyString = response.message();
                    }
                }
                transactionData.setResponseBody(responseBodyString);
                transactionData.setParams(params);
                DataMeasurements.addHttpError(transactionData);
            }
            HttpTransactionMetrics metrics=  new HttpTransactionMetrics(transactionData);
           	metrics.setMainJson(MobileAgentUpload.getJSONObjet(transactionState));
            Queue.queue(metrics);
        }
        return response;
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy