Mercurial > hg > LGDataverses
diff src/main/java/de/mpiwg/gazetteer/utils/HTTPUtils.java @ 14:be7787c36e58 default tip
new: nofity LGSercies for deleted files
author | Zoe Hong <zhong@mpiwg-berlin.mpg.de> |
---|---|
date | Mon, 02 Nov 2015 16:41:23 +0100 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/mpiwg/gazetteer/utils/HTTPUtils.java Mon Nov 02 16:41:23 2015 +0100 @@ -0,0 +1,114 @@ +package de.mpiwg.gazetteer.utils; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; + +import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSocketFactory; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; + +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.security.cert.X509Certificate; + +public class HTTPUtils { + + final static TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { + @Override + public void checkClientTrusted( final X509Certificate[] chain, final String authType ) { + } + @Override + public void checkServerTrusted( final X509Certificate[] chain, final String authType ) { + } + @Override + public X509Certificate[] getAcceptedIssuers() { + return null; + } + } }; + + + + + public static HttpStringResponse getStringResponse(String url) throws IOException, KeyManagementException, NoSuchAlgorithmException{ + if(url.startsWith("https") || url.startsWith("HTTPS")) + return getHttpSSLStringResponse(url); + return getHttpStringResponse(url); + } + + private static HttpStringResponse getHttpSSLStringResponse(String link) throws IOException, KeyManagementException, NoSuchAlgorithmException{ + + // Install the all-trusting trust manager + final SSLContext sslContext = SSLContext.getInstance( "SSL" ); + sslContext.init( null, trustAllCerts, new java.security.SecureRandom() ); + // Create an ssl socket factory with our all-trusting manager + final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); + + URL url = new URL(link); + HttpURLConnection httpConn = (HttpURLConnection)url.openConnection(); + + ( (HttpsURLConnection) httpConn ).setSSLSocketFactory( sslSocketFactory ); + + + BufferedReader in = null; + if (httpConn.getResponseCode() >= 400) { + in = new BufferedReader( + new InputStreamReader( + httpConn.getErrorStream())); + } else { + in = new BufferedReader( + new InputStreamReader( + httpConn.getInputStream())); + } + + String inputLine; + StringBuilder sb = new StringBuilder(); + while ((inputLine = in.readLine()) != null) + sb.append(inputLine + "\n"); + in.close(); + + return new HttpStringResponse(httpConn.getResponseCode(), sb.toString()); + } + + private static HttpStringResponse getHttpStringResponse(String link) throws IOException{ + + //System.out.println(url); + + URL url = new URL(link); + HttpURLConnection httpConn = (HttpURLConnection)url.openConnection(); + + BufferedReader in = null; + if (httpConn.getResponseCode() >= 400) { + in = new BufferedReader( + new InputStreamReader( + httpConn.getErrorStream())); + } else { + in = new BufferedReader( + new InputStreamReader( + httpConn.getInputStream())); + } + + + String inputLine; + StringBuilder sb = new StringBuilder(); + while ((inputLine = in.readLine()) != null) + sb.append(inputLine + "\n"); + in.close(); + + return new HttpStringResponse(httpConn.getResponseCode(), sb.toString()); + } + + public static class HttpStringResponse{ + public int code; + public String content; + public HttpStringResponse(int code, String content){ + this.code = code; + this.content = content; + } + } + +}