view src/main/java/de/mpiwg/gazetteer/utils/HTTPUtils.java @ 0:7682c04c63a8

First commit of the source code!
author "jurzua <jurzua@mpiwg-berlin.mpg.de>"
date Tue, 10 Mar 2015 14:50:41 +0100
parents
children
line wrap: on
line source

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 link) throws IOException, KeyManagementException, NoSuchAlgorithmException{
    	if(link.startsWith("https") || link.startsWith("HTTPS"))
    		return getHttpSSLStringResponse(link);
    	return getHttpStringResponse(link);
    			
    }
    
	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(link);
		
		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;
		}
	}	
	
}