/*
 * Copyright 2007 - 2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.sf.jailer.ui.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class HttpUtil {
	
	public static String get(final String url) {
		final StringBuilder result = new StringBuilder();
		try {
			get(url, result);
		} catch (Throwable e) {
		    try {
		        Process p = Runtime.getRuntime().exec("java -classpath jailer.jar -Djava.net.useSystemProxies=true " + HttpUtil.class.getName() + " " + url);
	        	BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
	            String line;

	            while ((line = input.readLine()) != null) {
	                result.append(line + "\n");
	            }
	            
	            input.close();
		    } catch (Exception err) {
		       // ignore
		    }
		}
		return result.toString();
	}

	public static void get(final String url, final StringBuilder result) throws MalformedURLException, IOException {
		URL theUrl;
		theUrl = new URL(url);
		URLConnection con = theUrl.openConnection();
		InputStreamReader in = new InputStreamReader(con.getInputStream());
		int c;
		while ((c = in.read()) != -1) {
			result.append((char) c);
		}
	}

	public static void main(String args[]) {
		final StringBuilder result = new StringBuilder();
		try {
			get(args[0] + "&withproxy=1", result);
			System.out.println(result);
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}

}

@@ -753,32 +753,68 @@
 tStreamReader;%0D%0A
+import java.net.HttpURLConnection;%0D%0A
 import java.net.
@@ -1029,24 +1029,28 @@
 %09%09try %7B%0D%0A%09%09%09
+if (
 get(url, res
@@ -1049,25 +1049,71 @@
 url, result)
-;
+ == 200) %7B%0D%0A%09%09%09%09return result.toString();%0D%0A%09%09%09%7D
 %0D%0A%09%09%7D catch 
@@ -1126,24 +1126,48 @@
 able e) %7B%0D%0A%09
+%09%09// fall through%0D%0A%09%09%7D%0D%0A
 %09    try %7B%0D%0A
@@ -1162,25 +1162,24 @@
 %09    try %7B%0D%0A
-%09
 %09        Pro
@@ -1314,25 +1314,24 @@
  %22 + url);%0D%0A
-%09
         %09Buf
@@ -1410,25 +1410,24 @@
 tream()));%0D%0A
-%09
             
@@ -1442,17 +1442,16 @@
 ine;%0D%0A%0D%0A
-%09
         
@@ -1495,25 +1495,24 @@
 != null) %7B%0D%0A
-%09
             
@@ -1544,17 +1544,16 @@
 %22%5Cn%22);%0D%0A
-%09
         
@@ -1559,17 +1559,16 @@
      %7D%0D%0A
-%09
         
@@ -1573,17 +1573,16 @@
       %0D%0A
-%09
         
@@ -1601,17 +1601,16 @@
 ose();%0D%0A
-%09
 %09    %7D c
@@ -1633,17 +1633,16 @@
 err) %7B%0D%0A
-%09
 %09       
@@ -1656,17 +1656,16 @@
 re%0D%0A
-%09
 %09    %7D%0D%0A
 %09%09%7D%0D
@@ -1660,21 +1660,16 @@
 %09    %7D%0D%0A
-%09%09%7D%0D%0A
 %09%09return
@@ -1710,20 +1710,19 @@
  static 
-void
+int
  get(fin
@@ -2044,24 +2044,95 @@
 ) c);%0D%0A%09%09%7D%0D%0A
+%09%09int rc = ((HttpURLConnection) con).getResponseCode();%0D%0A%09%09return rc;%0D%0A
 %09%7D%0D%0A%0D%0A%09publi

check response code