package hu.sebcsaba.webfeed;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class HtmlLoader {

	private final Config config;

	public HtmlLoader(Config config) {
		this.config = config;
	}

	public Document load(String siteUrl, String baseUrl) throws IOException {
		String httpLoader = config.getHttpLoader();
		if ("wget".equals(httpLoader)) {
			return loadWget(siteUrl, baseUrl);
		}
		else if ("builtin".equals(httpLoader) || httpLoader == null) {
			return loadBuiltin(siteUrl);
		}
		else {
			throw new IOException("Unknown http loader: "+httpLoader);
		}
	}

	private Document loadWget(String siteUrl, String baseUrl) throws IOException {
		Path tempFile = Files.createTempFile("webfeed-", ".html");
		List<String> command = new ArrayList<>();
		command.add("wget");
		command.add("-O");
		command.add(tempFile.toString());
		Map<String, String> headers = config.getHttpHeaders();
		for (String key : headers.keySet()) {
			command.add("--header='"+key+": "+headers.get(key)+"'");
		}
		command.add(siteUrl);
		Runtime rt = Runtime.getRuntime();
		Process p = rt.exec(command.toArray(new String[command.size()]));
		if (p.exitValue() != 0) {
			Scanner scanner = new Scanner(p.getErrorStream());
			try {
				String stderr = scanner.useDelimiter("\\A").next();
				throw new IOException("process returned "+p.exitValue()+", stderr:\n"+stderr);
			} finally {
				scanner.close();
			}
		}
		Document doc = Jsoup.parse(tempFile.toFile(), "UTF-8", baseUrl);
		Files.delete(tempFile);
		return doc;
	}

	private Document loadBuiltin(String siteUrl) throws IOException {
		Connection connection = Jsoup.connect(siteUrl);
		connection.timeout(config.getTimeout());
		Map<String, String> headers = config.getHttpHeaders();
		for (String key : headers.keySet()) {
			connection.header(key, headers.get(key));
		}
		return connection.get();
	}

}

@@ -873,16 +873,25 @@
 ption %7B%0A
+%09%09try %7B%0A%09
 %09%09Path t
@@ -945,16 +945,17 @@
 ml%22);%0A%09%09
+%09
 List%3CStr
@@ -988,16 +988,17 @@
 st%3C%3E();%0A
+%09
 %09%09comman
@@ -1010,24 +1010,25 @@
 (%22wget%22);%0A%09%09
+%09
 command.add(
@@ -1034,16 +1034,17 @@
 (%22-O%22);%0A
+%09
 %09%09comman
@@ -1069,24 +1069,25 @@
 tring());%0A%09%09
+%09
 Map%3CString, 
@@ -1121,32 +1121,33 @@
 tHttpHeaders();%0A
+%09
 %09%09for (String ke
@@ -1169,24 +1169,25 @@
 Set()) %7B%0A%09%09%09
+%09
 command.add(
@@ -1231,22 +1231,24 @@
 )+%22'%22);%0A
+%09
 %09%09%7D%0A%09%09
+%09
 command.
@@ -1267,43 +1267,59 @@
 ;%0A%09%09
-Runtime rt = Runtime.getRuntime(
+%09ProcessBuilder pb = new ProcessBuilder(command
 );%0A
+%09
 %09%09Pr
@@ -1329,84 +1329,79 @@
 ss p
- = rt.exec(command.toArray(new String%5Bcommand.size()%5D)
+rocess = pb.start();%0A%09%09%09int errCode = process.waitFor(
 );%0A
+%09
 %09%09if (
-p.exitValue()
+errCode
  != 
@@ -1405,16 +1405,17 @@
 != 0) %7B%0A
+%09
 %09%09%09Scann
@@ -1440,16 +1440,22 @@
 canner(p
+rocess
 .getErro
@@ -1469,22 +1469,24 @@
 ());%0A%09%09%09
+%09
 try %7B%0A
+%09
 %09%09%09%09Stri
@@ -1537,16 +1537,17 @@
 ();%0A%09%09%09%09
+%09
 throw ne
@@ -1584,21 +1584,15 @@
 d %22+
-p.exitValue()
+errCode
 +%22, 
@@ -1611,16 +1611,17 @@
 tderr);%0A
+%09
 %09%09%09%7D fin
@@ -1631,16 +1631,17 @@
 y %7B%0A%09%09%09%09
+%09
 scanner.
@@ -1653,17 +1653,20 @@
 ();%0A
+%09
 %09%09%09%7D%0A%09%09
+%09
 %7D%0A
+%09
 %09%09Do
@@ -1730,16 +1730,17 @@
 Url);%0A%09%09
+%09
 Files.de
@@ -1757,16 +1757,17 @@
 ile);%0A%09%09
+%09
 return d
@@ -1770,16 +1770,86 @@
 rn doc;%0A
+%09%09%7D catch (InterruptedException e) %7B%0A%09%09%09throw new IOException(e);%0A%09%09%7D%0A
 %09%7D%0A%0A%09pri

use ProcessBuilder