com.eg.agent.android.networkinformation.ExtendedHttpsURLConnection Maven / Gradle / Ivy
The newest version!
package com.eg.agent.android.networkinformation;
import android.util.Log;
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.io.CounterInputStream;
import com.eg.agent.android.io.CounterOutputStream;
import com.eg.agent.android.io.StreamEvent;
import com.eg.agent.android.io.StreamListener;
import com.eg.agent.android.Features;
import com.eg.agent.android.MobileAgentUpload;
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;
public class ExtendedHttpsURLConnection extends HttpsURLConnection
{
private HttpsURLConnection impl;
private TransactionData transactionState;
private TransactionData getTransactionState()
{
if (this.transactionState == null)
{
this.transactionState = new TransactionData();
this.transactionState.setStartTime(System.currentTimeMillis());
TransactionUtil.inspectAndInstrument(this.transactionState, this.impl);
}
return this.transactionState;
}
private void error(Exception e)
{
TransactionData transactionState = getTransactionState();
TransactionUtil.setErrorCodeFromException(transactionState, e);
TransactionUtil.inspectAndInstrumentResponse(transactionState, this.impl);
Data transactionData = transactionState.end();
//addTransactionAndErrorData(transactionState);
}
private void addTransactionAndErrorData(TransactionData transactionState)
{
Data 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
{
transactionState.setEndTime(System.currentTimeMillis());
// MobileAgentUpload.sendData(transactionState);
}
catch (Exception e){e.printStackTrace();}
}
}
public ExtendedHttpsURLConnection(HttpsURLConnection impl)
{
super(impl.getURL());
this.impl = impl;
TransactionUtil.setCrossProcessHeader(impl);
if (Features.featureEnabled(Features.DistributedTracing)) {
TransactionUtil.setCatPayload(this.getTransactionState());
try {
this.impl.addRequestProperty("eg", this.transactionState.getCatPayload().asBase64Json());
} catch (NullPointerException var3) {
var3.printStackTrace();
Log.e("Distributed trace payload is null", var3.getMessage());
} catch (IllegalStateException var4) {
Log.e("Connection already opened, unable to add Connectivity header", var4.getMessage());
var4.printStackTrace();
}
}
}
private void checkResponse()
{
if (!this.getTransactionState().isComplete())
{
TransactionUtil.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)
{
TransactionData 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 TransactionData transactionState = this.getTransactionState();
CounterInputStream in;
try
{
in = new CounterInputStream(this.impl.getInputStream());
TransactionUtil.inspectAndInstrumentResponse(transactionState, this.impl);
}
catch (IOException var4)
{
this.error(var4);
throw var4;
}
in.addStreamListener(new StreamListener()
{
public void streamError(StreamEvent e)
{
if(!transactionState.isComplete())
{
transactionState.setBytesReceived(e.getBytes());
}
ExtendedHttpsURLConnection.this.error(e.getException());
}
public void streamComplete(StreamEvent e)
{
if(!transactionState.isComplete())
{
long contentLength = (long)ExtendedHttpsURLConnection.this.impl.getContentLength();
long numBytes = e.getBytes();
if(contentLength >= 0L)
{
numBytes = contentLength;
}
transactionState.setBytesReceived(numBytes);
ExtendedHttpsURLConnection.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 TransactionData transactionState = this.getTransactionState();
CounterOutputStream out;
try
{
out = new CounterOutputStream(this.impl.getOutputStream());
}
catch (IOException var4)
{
this.error(var4);
throw var4;
}
out.addStreamListener(new StreamListener()
{
public void streamError(StreamEvent e)
{
if (!transactionState.isComplete())
{
transactionState.setBytesSent(e.getBytes());
}
ExtendedHttpsURLConnection.this.error(e.getException());
}
public void streamComplete(StreamEvent e) {
if (!transactionState.isComplete()) {
String header = ExtendedHttpsURLConnection.this.impl.getRequestProperty("content-length");
long numBytes = e.getBytes();
if (header != null) {
try {
numBytes = Long.parseLong(header);
} catch (NumberFormatException var6) {
}
}
transactionState.setBytesSent(numBytes);
ExtendedHttpsURLConnection.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
{
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;
}
}
}