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

com.eg.agent.android.instrumentation.TransactionStateUtil Maven / Gradle / Ivy

There is a newer version: 2.1.3
Show newest version
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.eg.agent.android.instrumentation;


import android.util.Log;

import com.eg.agent.android.DataSendToRum;
import com.eg.agent.android.common.TransactionData;
import com.eg.agent.android.instrumentation.NetworkInformation.ExceptionHelper;
import com.eg.agent.android.instrumentation.NetworkInformation.httpClient.ContentBufferingResponseEntityImpl;
import com.eg.agent.android.instrumentation.NetworkInformation.httpClient.HttpRequestEntityImpl;
import com.eg.agent.android.instrumentation.NetworkInformation.httpClient.HttpResponseEntityImpl;
import com.eg.agent.android.instrumentation.io.CountingInputStream;

import org.apache.http.Header;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.RequestLine;
import org.apache.http.client.methods.HttpUriRequest;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.util.TreeMap;


public class TransactionStateUtil
{
    public static final String CONTENT_LENGTH_HEADER = "Content-Length";
    public static final String CONTENT_TYPE_HEADER = "Content-Type";
    public static final String APP_DATA_HEADER = "App-Data";
    public static final String CROSS_PROCESS_ID_HEADER = "ID";

    public static void inspectAndInstrument(TransactionState transactionState, String url, String httpMethod)
    {
        transactionState.setUrl(url);
        transactionState.setHttpMethod(httpMethod);
    }
    public static void inspectAndInstrument(TransactionState transactionState, HttpURLConnection conn)
    {
        inspectAndInstrument(transactionState, conn.getURL().toString(), conn.getRequestMethod());
    }
    public static void inspectAndInstrumentResponse(TransactionState transactionState, String appData, int contentLength, int statusCode)
    {
        if (appData != null && !appData.equals(""))
        {
            transactionState.setAppData(appData);
        }

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

        DataSendToRum.sendData(transactionState);
    }
    public static void inspectAndInstrumentResponse(TransactionState transactionState, HttpURLConnection conn)
    {
        String appData = conn.getHeaderField(1);
        int contentLength = conn.getContentLength();
        int statusCode=0 ;
        try
        {
            statusCode = conn.getResponseCode();
            transactionState.setStatusCode(statusCode);
        }
        catch (Exception var6)
        {
            Log.e("eG Agent", "Failed to retrieve response code due to an I/O exception: " + var6.getMessage());
        }

        inspectAndInstrumentResponse(transactionState, appData, contentLength, statusCode);
    }
    public static HttpResponse insepectAndHttpClientData(HttpResponse httpResponse, TransactionState transactionState)
    {
        transactionState.setStatusCode(httpResponse.getStatusLine().getStatusCode());
        long content_length=httpResponse.getEntity().getContentLength();
        Header[] contentLengthHeader = httpResponse.getHeaders("Content-Length");

        long contentLengthFromHeader = -1L;
        if ((contentLengthHeader != null) && (contentLengthHeader.length > 0))
        {
            try
            {
                contentLengthFromHeader = Long.parseLong(contentLengthHeader[0].getValue());
                transactionState.setBytesReceived(contentLengthFromHeader);

                addTransactionAndErrorData(transactionState, httpResponse);
            }
            catch (NumberFormatException e)
            {
                Log.e("eG Agent","Failed to parse content length: " + e.toString());
            }
        }
        else if (httpResponse.getEntity() != null)
        {
            httpResponse.setEntity(new HttpResponseEntityImpl(httpResponse.getEntity(), transactionState, contentLengthFromHeader));
        }
        else
        {
            transactionState.setBytesReceived(0L);
            addTransactionAndErrorData(transactionState, null);
        }
        Log.e("eG Net", transactionState.toString());
        if((transactionState.getBytesSent()!=0)||(transactionState.getBytesReceived()!=0)) {
        Log.e("eG Net", transactionState.toString());
            DataSendToRum.sendData(transactionState);
        }
        return httpResponse;
    }
    public static void setErrorCodeFromException(TransactionState transactionState, Exception e)
    {
        StackTraceElement localStackTraceElement = e.getStackTrace()[0];
        Log.e("eG",localStackTraceElement.getFileName());
        Log.e("eG", String.valueOf(Integer.valueOf(localStackTraceElement.getLineNumber())));
                System.out.println("" + e.getClass().getName() + "" + e.getClass().getCanonicalName() + "" + localStackTraceElement.getFileName() + "" + Integer.valueOf(localStackTraceElement.getLineNumber()) + "" + e.getMessage() + "");
        //AgentHealth.noticeException(paramException, paramString);
        int exceptionAsErrorCode = ExceptionHelper.exceptionToErrorCode(e);
        Log.e("eG Agent", "TransactionStateUtil: Attempting to convert network exception " + e.getClass().getName() + "Error code" + exceptionAsErrorCode);
        final StringWriter sw = new StringWriter();
        final PrintWriter pw = new PrintWriter(sw, true);
        e.printStackTrace(pw);
        String errorMessage= sw.getBuffer().toString();
        Log.e("eG Agent", errorMessage);
        transactionState.setErrorCode(exceptionAsErrorCode, errorMessage);
     
            DataSendToRum.sendData(transactionState);
        
    }
    private static void addTransactionAndErrorData(TransactionState transactionState, HttpResponse response)
    {
        TransactionData transactionData = transactionState.end();
        if(transactionData != null)
        {
           // TaskQueue.queue(new HttpTransactionMeasurement(transactionData.getUrl(), transactionData.getHttpMethod(), transactionData.getStatusCode(), transactionData.getErrorCode(), transactionData.getTimestamp(), (double) transactionData.getTime(), transactionData.getBytesSent(), transactionData.getBytesReceived(), transactionData.getAppData()));
            if((long)transactionState.getStatusCode() >= 400L)
            {
                StringBuilder responseBody = new StringBuilder();
                TreeMap params = new TreeMap();
                if(response != null)
                {
                    try {
                        if(response.getEntity() != null)
                        {
                            if(!(response.getEntity() instanceof HttpRequestEntityImpl))
                            {
                                response.setEntity(new ContentBufferingResponseEntityImpl(response.getEntity()));
                            }

                            InputStream contentTypeHeader = response.getEntity().getContent();
                            if(contentTypeHeader instanceof CountingInputStream)
                            {
                                responseBody.append(((CountingInputStream)contentTypeHeader).getBufferAsString());
                            }
                            else
                            {
                                Log.e("eG", "Unable to wrap content  entity");
                            }
                        }
                        else
                        {
                            Log.e("eG", "null response entity. response-body will be reported empty");
                        }
                    }
                    catch (IllegalStateException var7)
                    {
                        Log.e("eG", var7.toString());
                    }
                    catch (IOException var8)
                    {
                        Log.e("eG", var8.toString());
                    }
                    Header[] contentTypeHeader1 = response.getHeaders("Content-Type");
                    String contentType = null;
                    if(contentTypeHeader1 != null && contentTypeHeader1.length > 0 && !"".equals(contentTypeHeader1[0].getValue())) {
                        contentType = contentTypeHeader1[0].getValue();
                    }

                    if(contentType != null && contentType.length() > 0) {
                        params.put("content_type", contentType);
                    }
                }

                params.put("content_length", transactionState.getBytesReceived() + "");
                //Measurements.addHttpError(transactionData.getUrl(), transactionData.getHttpMethod(), transactionData.getStatusCode(), responseBody.toString(), params);
            }

        }
    }

    public static HttpUriRequest insepectAndHttpClientData(HttpUriRequest httpUriRequest, TransactionState localTransactionState) {
        Log.e("eG","HttpUriRequest");
        inspectAndInstrument(localTransactionState, httpUriRequest.getURI().toString(), httpUriRequest.getMethod());
        wrapRequestEntity(localTransactionState, httpUriRequest);
        return httpUriRequest;
    }
    private static void wrapRequestEntity(TransactionState transactionState, HttpRequest request)
    {
        Log.e("eG","wrapRequestEntity");
        if ((request instanceof HttpEntityEnclosingRequest))
        {
            HttpEntityEnclosingRequest entityEnclosingRequest = (HttpEntityEnclosingRequest)request;
            if (entityEnclosingRequest.getEntity() != null) {
                entityEnclosingRequest.setEntity(new HttpRequestEntityImpl(entityEnclosingRequest.getEntity(), transactionState));
            }
        }
    }

    public static HttpRequest insepectAndHttpClientData(TransactionState transactionState, HttpHost host, HttpRequest request)
    {
        String url = null;
        RequestLine requestLine = request.getRequestLine();
        if (requestLine != null)
        {
            String uri = requestLine.getUri();
            boolean isAbsoluteUri = (uri != null) && (uri.length() >= 10) && (uri.substring(0, 10).indexOf("://") >= 0);
            if ((!isAbsoluteUri) && (uri != null) && (host != null))
            {
                String prefix = host.toURI().toString();
                url = prefix + ((prefix.endsWith("/")) || (uri.startsWith("/")) ? "" : "/") + uri;
            }
            else if (isAbsoluteUri)
            {
                url = uri;
            }
            inspectAndInstrument(transactionState, url, requestLine.getMethod());
        }
        if ((transactionState.getUrl() == null) || (transactionState.getHttpMethod() == null))
        {
            try
            {
                throw new Exception("TransactionData constructor was not provided with a valid URL, host or HTTP method");
            }
            catch (Exception e)
            {
                return request;
            }
        }
        wrapRequestEntity(transactionState, request);
        return request;
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy