view src/main/java/de/mpiwg/gazetteer/utils/HTTPUtils.java @ 4:c30ff4f27ec3

adding developer guide documentation
author "jurzua <jurzua@mpiwg-berlin.mpg.de>"
date Mon, 18 May 2015 15:55:01 +0200
parents 3e62083dbcbf
children 3b3e2963c8f7
line wrap: on
line source

package de.mpiwg.gazetteer.utils;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringReader;
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;
        }
    } };	
	

    
    /**
     * This method returns a table that is generated by the extraction interface from a file id.
     * 
     * @TODO: Currently, the extraction interface runs over HTTP, when it changes to HTTPS, this methods should be adapted to this. 
     * 
     * @param fileId
     * @return
     * @throws Exception some HTTP code different higher than 400
     */
    public static String getTableOfFile(Long fileId) throws Exception{
		
		
		String link = PropertiesUtils.getPropValue("extraction_interface") + "/Extractapp/ExportTable";
		
		System.out.println(link);
		
		URL url = new URL(link);
		HttpURLConnection httpConn = (HttpURLConnection)url.openConnection();
		
		httpConn.setReadTimeout(10000);
		httpConn.setConnectTimeout(15000);
		httpConn.setRequestMethod("POST");
		httpConn.setDoInput(true);
		httpConn.setDoOutput(true);

		//Send request
		DataOutputStream wr = new DataOutputStream (httpConn.getOutputStream ());
	    wr.writeBytes ("fileId=" + fileId);
	    wr.flush ();
	    wr.close ();
		
        BufferedReader in = null;
        if (httpConn.getResponseCode() >= 400) {
        	in = new BufferedReader(
                    new InputStreamReader(
                    		httpConn.getErrorStream()));
        	
        	String inputLine;
            StringBuilder sb = new StringBuilder();
            while ((inputLine = in.readLine()) != null) 
            	sb.append(inputLine + "\n");
        	
        	throw new Exception("HTTP Error, code " + httpConn.getResponseCode());
        	
        } 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 sb.toString();
	}
    
    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;
		}
	}	
	
}