Created
March 13, 2014 02:52
-
-
Save darrikmazey/9521134 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.darmasoft.parental_informant; | |
import java.io.IOException; | |
import java.net.InetAddress; | |
import java.net.InetSocketAddress; | |
import java.net.Socket; | |
import java.net.UnknownHostException; | |
import javax.net.ssl.SSLContext; | |
import javax.net.ssl.SSLSocket; | |
import javax.net.ssl.TrustManager; | |
import org.apache.http.conn.ConnectTimeoutException; | |
import org.apache.http.conn.scheme.LayeredSocketFactory; | |
import org.apache.http.conn.scheme.SocketFactory; | |
import org.apache.http.params.HttpConnectionParams; | |
import org.apache.http.params.HttpParams; | |
public class EasySSLSocketFactory implements SocketFactory, | |
LayeredSocketFactory { | |
private SSLContext m_ssl_context = null; | |
private static SSLContext createEasySSLContext() throws IOException { | |
try { | |
SSLContext context = SSLContext.getInstance("TLS"); | |
context.init(null, new TrustManager[] { new EasyX509TrustManager(null) }, null); | |
return(context); | |
} catch (Exception e) { | |
throw new IOException(e.getMessage()); | |
} | |
} | |
private SSLContext getSSLContext() throws IOException { | |
if (this.m_ssl_context == null) { | |
this.m_ssl_context = createEasySSLContext(); | |
} | |
return(this.m_ssl_context); | |
} | |
public EasySSLSocketFactory() { | |
// TODO Auto-generated constructor stub | |
} | |
@Override | |
public Socket connectSocket(Socket sock, String host, int port, | |
InetAddress localAddress, int localPort, HttpParams params) throws IOException, | |
UnknownHostException, ConnectTimeoutException { | |
int connTimeout = HttpConnectionParams.getConnectionTimeout(params); | |
int soTimeout = HttpConnectionParams.getSoTimeout(params); | |
InetSocketAddress remoteAddress = new InetSocketAddress(host, port); | |
SSLSocket sslsock = (SSLSocket) ((sock != null) ? sock : createSocket()); | |
if ((localAddress != null) || (localPort > 0)) { | |
// we need to bind explicitly | |
if (localPort < 0) { | |
localPort = 0; // indicates "any" | |
} | |
InetSocketAddress isa = new InetSocketAddress(localAddress, localPort); | |
sslsock.bind(isa); | |
} | |
sslsock.connect(remoteAddress, connTimeout); | |
sslsock.setSoTimeout(soTimeout); | |
return sslsock; | |
} | |
@Override | |
public Socket createSocket() throws IOException { | |
return getSSLContext().getSocketFactory().createSocket(); | |
} | |
@Override | |
public boolean isSecure(Socket sock) throws IllegalArgumentException { | |
return true; | |
} | |
@Override | |
public Socket createSocket(Socket socket, String host, int port, | |
boolean autoClose) throws IOException, UnknownHostException { | |
return(getSSLContext().getSocketFactory().createSocket(socket, host, port, autoClose)); | |
} | |
public boolean equals(Object obj) { | |
return((obj != null) && obj.getClass().equals(EasySSLSocketFactory.class)); | |
} | |
public int hashCode() { | |
return (EasySSLSocketFactory.class.hashCode()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.darmasoft.parental_informant; | |
import java.security.KeyStore; | |
import java.security.KeyStoreException; | |
import java.security.NoSuchAlgorithmException; | |
import java.security.cert.CertificateException; | |
import java.security.cert.X509Certificate; | |
import javax.net.ssl.TrustManager; | |
import javax.net.ssl.TrustManagerFactory; | |
import javax.net.ssl.X509TrustManager; | |
public class EasyX509TrustManager implements X509TrustManager { | |
private X509TrustManager standardTrustManager = null; | |
public EasyX509TrustManager(KeyStore keystore) throws NoSuchAlgorithmException, KeyStoreException { | |
super(); | |
TrustManagerFactory factory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); | |
factory.init(keystore); | |
TrustManager[] trustmanagers = factory.getTrustManagers(); | |
if (trustmanagers.length == 0) { | |
throw new NoSuchAlgorithmException("no trust manager found"); | |
} | |
this.standardTrustManager = (X509TrustManager) trustmanagers[0]; | |
} | |
@Override | |
public void checkClientTrusted(X509Certificate[] chain, String authType) | |
throws CertificateException { | |
standardTrustManager.checkClientTrusted(chain, authType); | |
} | |
@Override | |
public void checkServerTrusted(X509Certificate[] chain, String authType) | |
throws CertificateException { | |
if ((chain != null) && (chain.length == 1)) { | |
chain[0].checkValidity(); | |
} else { | |
standardTrustManager.checkServerTrusted(chain, authType); | |
} | |
} | |
@Override | |
public X509Certificate[] getAcceptedIssuers() { | |
return(this.standardTrustManager.getAcceptedIssuers()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.darmasoft.parental_informant; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.io.Reader; | |
import java.nio.charset.Charset; | |
import org.apache.http.HttpEntity; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.entity.BufferedHttpEntity; | |
import android.util.Log; | |
public class HttpHelper { | |
public static final String TAG = "Locker:HttpHelper"; | |
public static String readBody(HttpResponse res) { | |
Log.d(TAG, res.getStatusLine().toString()); | |
int sc = res.getStatusLine().getStatusCode(); | |
StringBuffer buffer = new StringBuffer(); | |
try { | |
HttpEntity entity = res.getEntity(); | |
InputStreamReader isr = new InputStreamReader(entity.getContent(), Charset.forName("ISO-8859-1")); | |
Reader in = new BufferedReader(isr); | |
char[] tmp_buffer = new char[1024]; | |
int bytes_read = 0; | |
while ((bytes_read = in.read(tmp_buffer, 0, 1024)) > -1) { | |
buffer.append(tmp_buffer, 0, bytes_read); | |
tmp_buffer = new char[1024]; | |
} | |
} catch(IOException e) { | |
e.printStackTrace(); | |
} | |
if (sc == 200) { | |
Log.d(TAG, "200 code"); | |
return(buffer.toString()); | |
} else if (sc == 404) { | |
// url not found | |
Log.d(TAG, "404"); | |
} else if (sc == 401) { | |
// auth error | |
Log.d(TAG, "401"); | |
} else { | |
Log.d(TAG, String.format("bad status: %d", sc)); | |
} | |
return(""); | |
} | |
public static InputStream getStream(HttpResponse res) { | |
Log.d(TAG, res.getStatusLine().toString()); | |
int sc = res.getStatusLine().getStatusCode(); | |
HttpEntity entity = res.getEntity(); | |
BufferedHttpEntity buf_entity; | |
try { | |
buf_entity = new BufferedHttpEntity(entity); | |
return(buf_entity.getContent()); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment