Mercurial > hg > LGServices
diff src/main/java/de/mpiwg/gazetteer/utils/HTTPUtils.java @ 0:3e62083dbcbf
First commit. This project comes from LGServer. We removed the framework icefaces. Now, LGServices uses just JSP and jquery.
author | "jurzua <jurzua@mpiwg-berlin.mpg.de>" |
---|---|
date | Thu, 23 Apr 2015 15:46:01 +0200 |
parents | |
children | c30ff4f27ec3 |
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 Thu Apr 23 15:46:01 2015 +0200 @@ -0,0 +1,178 @@ +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 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; + } + } + +}