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

com.eg.agent.android.instrumentation.NetworkInformation.HttpsURLConnectionExtension Maven / Gradle / Ivy

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

import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ProtocolException;
import java.net.URL;
import java.security.Permission;
import java.security.Principal;
import java.security.cert.Certificate;
import java.util.List;
import java.util.Map;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSocketFactory;

import com.eg.agent.android.DataSendToRum;
import com.eg.agent.android.common.TransactionData;
import com.eg.agent.android.instrumentation.TransactionState;
import com.eg.agent.android.instrumentation.TransactionStateUtil;
import com.eg.agent.android.instrumentation.io.CountingInputStream;
import com.eg.agent.android.instrumentation.io.CountingOutputStream;
import com.eg.agent.android.instrumentation.io.StreamCompleteEvent;
import com.eg.agent.android.instrumentation.io.StreamCompleteListener;

public class HttpsURLConnectionExtension extends HttpsURLConnection
{
    private HttpsURLConnection impl;
    private TransactionState transactionState;
    private TransactionState getTransactionState()
    {
        if (this.transactionState == null)
        {
            this.transactionState = new TransactionState();
            TransactionStateUtil.inspectAndInstrument(this.transactionState, this.impl);
        }
        return this.transactionState;
    }
    private void error(Exception e)
    {
        TransactionState transactionState = getTransactionState();
        TransactionStateUtil.setErrorCodeFromException(transactionState, e);
        TransactionStateUtil.inspectAndInstrumentResponse(transactionState, this.impl);
        TransactionData transactionData = transactionState.end();
        //addTransactionAndErrorData(transactionState);
    }
    private void addTransactionAndErrorData(TransactionState transactionState)
    {
        Log.e("eG", "addTransactionAndErrorData");
        TransactionData transactionData = transactionState.end();
        if(transactionData != null)
        {
                transactionState.setStatusCode(transactionData.getStatusCode());
                transactionState.setUrl(transactionData.getUrl());
                transactionState.setAppData(transactionData.getAppData());
                transactionState.setBytesReceived(transactionData.getBytesReceived());
                transactionState.setBytesSent(transactionData.getBytesSent());
                transactionState.setHttpMethod(transactionData.getHttpMethod());
                transactionState.setStatusCode(transactionData.getStatusCode());
                transactionState.setErrorCode(transactionData.getErrorCode(), transactionData.getErrorMessage());
                try
                {
                    DataSendToRum.sendData(transactionState);
                }
                catch (Exception e){e.printStackTrace();}


        }
    }
    public HttpsURLConnectionExtension(HttpsURLConnection paramHttpsURLConnection)
    {
        super(paramHttpsURLConnection.getURL());
        try
        {

            this.impl = paramHttpsURLConnection;
        }
        catch (Exception e)
        {
            Log.e("eGAndroidAgent", e.getMessage());
        }

    }
    private void checkResponse()
    {
        if (!this.getTransactionState().isComplete())
        {
            TransactionStateUtil.inspectAndInstrumentResponse(this.getTransactionState(), this.impl);
        }
    }
    public void addRequestProperty(String paramString1, String paramString2)
    {
        this.impl.addRequestProperty(paramString1, paramString2);
    }
    public void connect() throws IOException
    {
        try
        {
            this.impl.connect();
        }
        catch (IOException localIOException)
        {
            this.error(localIOException);
            throw localIOException;
        }
    }
    public void disconnect()
    {
        if(this.transactionState != null && !this.transactionState.isComplete())
        {
            this.addTransactionAndErrorData(this.transactionState);
        }
        this.impl.disconnect();
    }
    public boolean getAllowUserInteraction()
    {
        return this.impl.getAllowUserInteraction();
    }
    public int getConnectTimeout()
    {
        return this.impl.getConnectTimeout();
    }
    public Object getContent() throws IOException
    {
        this.getTransactionState();
        Object object;
        try
        {
            object = this.impl.getContent();
        }
        catch (IOException e)
        {
            error(e);
            throw e;
        }
        int contentLength = this.impl.getContentLength();
        if (contentLength >= 0)
        {
            TransactionState transactionState = this.getTransactionState();
            if (!transactionState.isComplete())
            {
                transactionState.setBytesReceived(contentLength);
                this.addTransactionAndErrorData(transactionState);
            }

        }
        return object;
    }
    public Object getContent(Class[] paramArrayOfClass) throws IOException
    {
        this.getTransactionState();
        Object object;
        try
        {
            object = this.impl.getContent(paramArrayOfClass);
        }
        catch (IOException var4)
        {
            this.error(var4);
            throw var4;
        }
        this.checkResponse();
        return object;
    }
    public String getContentEncoding()
    {
        this.getTransactionState();
        String contentEncoding = this.impl.getContentEncoding();
        this.checkResponse();
        return contentEncoding;
    }
    public int getContentLength()
    {
        this.getTransactionState();
        int contentLength = this.impl.getContentLength();
        this.checkResponse();
        return contentLength;
    }
    public String getContentType()
    {
        this.getTransactionState();
        String contentType = this.impl.getContentType();
        this.checkResponse();
        return contentType;
    }
    public long getDate()
    {
        this.getTransactionState();
        long date = this.impl.getDate();
        this.checkResponse();
        return date;
    }
    public boolean getDefaultUseCaches()
    {
        return this.impl.getDefaultUseCaches();
    }
    public boolean getDoInput()
    {
        return this.impl.getDoInput();
    }
    public boolean getDoOutput()
    {
        return this.impl.getDoOutput();
    }
    public InputStream getErrorStream()
    {
        return this.impl.getErrorStream();
    }
    public long getExpiration()
    {
        this.getTransactionState();
        long expiration = this.impl.getExpiration();
        this.checkResponse();
        return expiration;
    }
    public String getHeaderField(int paramInt)
    {
        this.getTransactionState();
        String header = this.impl.getHeaderField(paramInt);
        this.checkResponse();
        return header;
    }
    public String getHeaderField(String paramString)
    {
        this.getTransactionState();
        String header = this.impl.getHeaderField(paramString);
        this.checkResponse();
        return header;
    }
    public long getHeaderFieldDate(String paramString, long paramLong)
    {
        paramLong = this.impl.getHeaderFieldDate(paramString, paramLong);
        return paramLong;
    }
    public int getHeaderFieldInt(String paramString, int paramInt)
    {
        this.getTransactionState();
        int header = this.impl.getHeaderFieldInt(paramString, paramInt);
        this.checkResponse();
        return header;
    }
    public String getHeaderFieldKey(int paramInt)
    {
        this.getTransactionState();
        String key = this.impl.getHeaderFieldKey(paramInt);
        this.checkResponse();
        return key;
    }
    public Map> getHeaderFields()
    {
        this.getTransactionState();
        Map fields = this.impl.getHeaderFields();
        this.checkResponse();
        return fields;
    }
    public long getIfModifiedSince()
    {
        this.getTransactionState();
        long ifModifiedSince = this.impl.getIfModifiedSince();
        this.checkResponse();
        return ifModifiedSince;
    }
    public InputStream getInputStream() throws IOException
    {
        final TransactionState transactionState = this.getTransactionState();
        CountingInputStream in;
        try
        {
            in = new CountingInputStream(this.impl.getInputStream());
            TransactionStateUtil.inspectAndInstrumentResponse(transactionState, this.impl);
        }
        catch (IOException var4)
        {
            this.error(var4);
            throw var4;
        }

        in.addStreamCompleteListener(new StreamCompleteListener()
        {
            public void streamError(StreamCompleteEvent e)
            {
                if(!transactionState.isComplete())
                {
                    transactionState.setBytesReceived(e.getBytes());
                }

                HttpsURLConnectionExtension.this.error(e.getException());
            }
            public void streamComplete(StreamCompleteEvent e)
            {
                if(!transactionState.isComplete())
                {
                    long contentLength = (long)HttpsURLConnectionExtension.this.impl.getContentLength();
                    long numBytes = e.getBytes();
                    if(contentLength >= 0L)
                    {
                        numBytes = contentLength;
                    }

                    transactionState.setBytesReceived(numBytes);
                    HttpsURLConnectionExtension.this.addTransactionAndErrorData(transactionState);
                }

            }
        });
        return in;
    }
    public boolean getInstanceFollowRedirects()
    {
        return this.impl.getInstanceFollowRedirects();
    }
    public long getLastModified()
    {
        this.getTransactionState();
        long lastModified = this.impl.getLastModified();
        this.checkResponse();
        return lastModified;
    }
    public OutputStream getOutputStream() throws IOException
    {
        final TransactionState transactionState = this.getTransactionState();
        CountingOutputStream out;
        try
        {
            out = new CountingOutputStream(this.impl.getOutputStream());
        }
        catch (IOException var4)
        {
            this.error(var4);
            throw var4;
        }
        out.addStreamCompleteListener(new StreamCompleteListener()
        {
            public void streamError(StreamCompleteEvent e)
            {
                if (!transactionState.isComplete())
                {
                    transactionState.setBytesSent(e.getBytes());
                }

                HttpsURLConnectionExtension.this.error(e.getException());
            }

            public void streamComplete(StreamCompleteEvent e) {
                if (!transactionState.isComplete()) {
                    String header = HttpsURLConnectionExtension.this.impl.getRequestProperty("content-length");
                    long numBytes = e.getBytes();
                    if (header != null) {
                        try {
                            numBytes = Long.parseLong(header);
                        } catch (NumberFormatException var6) {

                        }
                    }
                    transactionState.setBytesSent(numBytes);
                    HttpsURLConnectionExtension.this.addTransactionAndErrorData(transactionState);
                }
            }
        });
        return out;
    }
    public Permission getPermission()  throws IOException
    {
        return this.impl.getPermission();
    }
    public int getReadTimeout()
    {
        return this.impl.getReadTimeout();
    }
    public String getRequestMethod()
    {
        return this.impl.getRequestMethod();
    }
    public Map> getRequestProperties()
    {
        return this.impl.getRequestProperties();
    }
    public String getRequestProperty(String paramString)
    {
        return this.impl.getRequestProperty(paramString);
    }
    public int getResponseCode()  throws IOException
    {
        getTransactionState();
        try
        {
            int i = this.impl.getResponseCode();
            checkResponse();
            return i;
        }
        catch (IOException localIOException)
        {
            error(localIOException);
            throw localIOException;
        }
    }
    public String getResponseMessage()  throws IOException
    {
        Log.e("eGAndroidAgent","getResponseMessage");
        getTransactionState();
        try
        {
            String str = this.impl.getResponseMessage();
            checkResponse();
            return str;
        }
        catch (IOException localIOException)
        {
            throw localIOException;
        }
    }
    public URL getURL()
    {
        return this.impl.getURL();
    }
    public boolean getUseCaches()
    {
        return this.impl.getUseCaches();
    }
    public void setAllowUserInteraction(boolean paramBoolean)
    {
        this.impl.setAllowUserInteraction(paramBoolean);
    }
    public void setChunkedStreamingMode(int paramInt)
    {
        this.impl.setChunkedStreamingMode(paramInt);
    }
    public void setConnectTimeout(int paramInt)
    {
        this.impl.setConnectTimeout(paramInt);
    }
    public void setDefaultUseCaches(boolean paramBoolean)
    {
        this.impl.setDefaultUseCaches(paramBoolean);
    }
    public void setDoInput(boolean paramBoolean)
    {
        this.impl.setDoInput(paramBoolean);
    }
    public void setDoOutput(boolean paramBoolean)
    {
        this.impl.setDoOutput(paramBoolean);
    }
    public void setFixedLengthStreamingMode(int paramInt)
    {
        this.impl.setFixedLengthStreamingMode(paramInt);
    }
    public void setIfModifiedSince(long paramLong)
    {
        this.impl.setIfModifiedSince(paramLong);
    }
    public void setInstanceFollowRedirects(boolean paramBoolean)
    {
        this.impl.setInstanceFollowRedirects(paramBoolean);
    }
    public void setReadTimeout(int paramInt)
    {
        this.impl.setReadTimeout(paramInt);
    }
    public void setRequestMethod(String paramString)  throws ProtocolException
    {
        try
        {
            this.impl.setRequestMethod(paramString);
        }
        catch (ProtocolException p)
        {
            throw p;
        }
    }
    public void setRequestProperty(String paramString1, String paramString2)
    {
        this.impl.setRequestProperty(paramString1, paramString2);
    }
    public void setUseCaches(boolean paramBoolean)
    {
        this.impl.setUseCaches(paramBoolean);
    }
    public String toString()
    {
        return this.impl.toString();
    }
    public boolean usingProxy()
    {
        return this.impl.usingProxy();
    }

    public String getCipherSuite()
    {
        return this.impl.getCipherSuite();
    }
    public Certificate[] getLocalCertificates()
    {
        return this.impl.getLocalCertificates();
    }
    public Principal getLocalPrincipal()
    {
        return this.impl.getLocalPrincipal();
    }
    public SSLSocketFactory getSSLSocketFactory()
    {
        return this.impl.getSSLSocketFactory();
    }
    public Certificate[] getServerCertificates()
            throws SSLPeerUnverifiedException
    {
        try
        {
            Certificate[] arrayOfCertificate = this.impl.getServerCertificates();
            return arrayOfCertificate;
        }
        catch (SSLPeerUnverifiedException localSSLPeerUnverifiedException)
        {

            throw localSSLPeerUnverifiedException;
        }
    }





}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy