mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 08:27:12 -07:00
Merge branch 'master' of github.com:cbeust/kobalt
This commit is contained in:
commit
ffbe5491f2
2 changed files with 73 additions and 20 deletions
|
@ -0,0 +1,72 @@
|
||||||
|
package com.beust.kobalt.wrapper;
|
||||||
|
|
||||||
|
|
||||||
|
import org.w3c.dom.Document;
|
||||||
|
import org.w3c.dom.Element;
|
||||||
|
import org.w3c.dom.Node;
|
||||||
|
import org.w3c.dom.NodeList;
|
||||||
|
|
||||||
|
import javax.xml.parsers.DocumentBuilder;
|
||||||
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.Proxy;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
|
class Config {
|
||||||
|
|
||||||
|
static Proxy getProxy() {
|
||||||
|
String configFilePath = getConfigFilePath();
|
||||||
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||||
|
try {
|
||||||
|
DocumentBuilder builder = factory.newDocumentBuilder();
|
||||||
|
ByteArrayInputStream input = new ByteArrayInputStream(getConfigFileContent(configFilePath));
|
||||||
|
Document doc = builder.parse(input);
|
||||||
|
NodeList proxies =doc.getElementsByTagName("proxies");
|
||||||
|
for (int temp = 0; temp < proxies.getLength(); temp++) {
|
||||||
|
Node node = proxies.item(temp);
|
||||||
|
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||||
|
Element element = (Element) node;
|
||||||
|
String type = element.getElementsByTagName("type").item(0).getTextContent();
|
||||||
|
if (type.toLowerCase().equals("http")) {
|
||||||
|
String host = element.getElementsByTagName("host").item(0).getTextContent();
|
||||||
|
String portString = element.getElementsByTagName("port").item(0).getTextContent();
|
||||||
|
try {
|
||||||
|
int port = Integer.parseInt(portString);
|
||||||
|
Main.log(2, String.format("Using HTTP proxy: %s:%s", host, port));
|
||||||
|
return new Proxy(java.net.Proxy.Type.HTTP, new InetSocketAddress(host, port));
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
Main.log(1, String.format("Invalid proxy port number: %s in config file: %s", portString, configFilePath));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
Main.log(2, String.format("%s while parsing config file: %s", e.getMessage(), configFilePath));
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Main.log(2, String.format("No HTTP proxy found in config file: %s", configFilePath));
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String getConfigFilePath() {
|
||||||
|
String userHome = System.getProperty("user.home");
|
||||||
|
String fileSeparator = System.getProperty("file.separator");
|
||||||
|
String configDir = ".config";
|
||||||
|
String appName = "kobalt";
|
||||||
|
String configFileName = "settings.xml";
|
||||||
|
return userHome
|
||||||
|
+ fileSeparator + configDir
|
||||||
|
+ fileSeparator + appName
|
||||||
|
+ fileSeparator + configFileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static byte[] getConfigFileContent(String configFilePath) throws IOException {
|
||||||
|
Path path = Paths.get(configFilePath);
|
||||||
|
return Files.readAllBytes(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -2,7 +2,6 @@ package com.beust.kobalt.wrapper;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.InetSocketAddress;
|
|
||||||
import java.net.Proxy;
|
import java.net.Proxy;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
|
@ -366,7 +365,7 @@ public class Main {
|
||||||
log(2, "Downloading " + fileUrl);
|
log(2, "Downloading " + fileUrl);
|
||||||
|
|
||||||
boolean done = false;
|
boolean done = false;
|
||||||
Proxy proxy = getProxy();
|
Proxy proxy = Config.getProxy();
|
||||||
HttpURLConnection httpConn = null;
|
HttpURLConnection httpConn = null;
|
||||||
try {
|
try {
|
||||||
int responseCode = 0;
|
int responseCode = 0;
|
||||||
|
@ -432,24 +431,6 @@ public class Main {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Proxy getProxy() {
|
|
||||||
String httpProxy = System.getenv("http_proxy");
|
|
||||||
try {
|
|
||||||
String host = httpProxy == null || httpProxy.indexOf(':') == -1
|
|
||||||
? "localhost"
|
|
||||||
: httpProxy.substring(0, httpProxy.indexOf(':'));
|
|
||||||
int port = httpProxy == null || httpProxy.indexOf(':') == -1
|
|
||||||
? 0
|
|
||||||
: Integer.parseInt(httpProxy.substring(httpProxy.indexOf(':') + 1));
|
|
||||||
return httpProxy == null
|
|
||||||
? null
|
|
||||||
: new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port));
|
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
log(1, "Warning: invalid proxy port: " + httpProxy);;
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void copyToStreamWithProgress(InputStream inputStream, OutputStream outputStream, long contentLength,
|
private void copyToStreamWithProgress(InputStream inputStream, OutputStream outputStream, long contentLength,
|
||||||
String url) throws IOException {
|
String url) throws IOException {
|
||||||
int bytesRead;
|
int bytesRead;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue