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

com.eg.agent.android.util.ErrorData Maven / Gradle / Ivy

There is a newer version: 2.1.3
Show newest version
package com.eg.agent.android.util;

import org.json.JSONObject;

import java.nio.ByteBuffer;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import com.eg.agent.android.Agent;
import com.eg.agent.android.logging.AgentLog;
import com.eg.agent.android.logging.AgentLogManager;

/**
 * Created by Venkateswari.J on 4/25/2017.
 */


public class ErrorData {
    private final String url;
    private final int httpStatusCode;
    private int count;
    private final String responseBody;
    private final String stackTrace;
    private final Map params;
    private final String digest;
    private static final AgentLog log = AgentLogManager.getAgentLog();

    public ErrorData(String url, int httpStatusCode, String responseBody, Map params) {
        this.url = url;
        this.httpStatusCode = httpStatusCode;
        this.count = 1;
        this.responseBody = responseBody;
        this.stackTrace = this.getSanitizedStackTrace();
        this.params = params;
        this.digest = this.computeHash();
    }

    public String getUrl() {
        return this.url;
    }

    public int getHttpStatusCode() {
        return this.httpStatusCode;
    }

    public int getCount() {
        return this.count;
    }

    public String getResponseBody() {
        return this.responseBody;
    }

    public String getStackTrace() {
        return this.stackTrace;
    }

    public Map getParams() {
        return this.params;
    }

    public String getHash() {
        return this.digest;
    }

    public List asList() {
        ArrayList list = new ArrayList();
        list.add(this.url);
        list.add(Integer.valueOf(this.httpStatusCode));
        list.add(Integer.valueOf(this.count));
        list.add(this.responseBody);
        list.add(this.stackTrace);
        TreeMap custom_params = new TreeMap();
        custom_params.put("custom_params", new JSONObject(this.params));
        list.add(new JSONObject(custom_params));
        return list;
    }

    public void incrementCount() {
        ++this.count;
    }

    public String computeHash() {
        MessageDigest digester;
        try {
            digester = MessageDigest.getInstance("SHA-1");
        } catch (NoSuchAlgorithmException var3) {
            log.error("Unable to initialize SHA-1 hash algorithm");
            return null;
        }

        digester.update(this.url.getBytes());
        digester.update(ByteBuffer.allocate(8).putInt(this.httpStatusCode).array());
        if(this.stackTrace != null && this.stackTrace.length() > 0) {
            digester.update(this.stackTrace.getBytes());
        }

        return new String(digester.digest());
    }

    public String getSanitizedStackTrace() {
        StringBuilder stackTrace = new StringBuilder();
        StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
        int numErrors = 0;

        for(int i = 0; i < stackTraceElements.length; ++i) {
            StackTraceElement frame = stackTraceElements[i];
            if(!this.shouldFilterStackTraceElement(frame)) {
                stackTrace.append(frame.toString());
                if(i <= stackTraceElements.length - 1) {
                    stackTrace.append("\n");
                }

                ++numErrors;
                if(numErrors >= Agent.getStackTraceLimit()) {
                    break;
                }
            }
        }

        return stackTrace.toString();
    }

    private boolean shouldFilterStackTraceElement(StackTraceElement element) {
        String className = element.getClassName();
        String method = element.getMethodName();
        return className.startsWith("com.newrelic")?true:(className.startsWith("dalvik.system.VMStack") && method.startsWith("getThreadStackTrace")?true:className.startsWith("java.lang.Thread") && method.startsWith("getStackTrace"));
    }
}