diff --git a/pom.xml b/pom.xml index 69783ce4..52bcd733 100644 --- a/pom.xml +++ b/pom.xml @@ -35,6 +35,7 @@ e0d44f4 2.10 31.1-jre + 2.1.0 4.2 1.4.5 1.6.6bcv @@ -73,6 +74,11 @@ + + com.konloch + HTTPRequest + ${httprequest.version} + org.jetbrains annotations diff --git a/src/main/java/me/konloch/kontainer/io/HTTPRequest.java b/src/main/java/me/konloch/kontainer/io/HTTPRequest.java deleted file mode 100644 index c3b1798b..00000000 --- a/src/main/java/me/konloch/kontainer/io/HTTPRequest.java +++ /dev/null @@ -1,276 +0,0 @@ -package me.konloch.kontainer.io; - -import java.io.BufferedReader; -import java.io.DataOutputStream; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; -import java.net.Proxy; -import java.net.URL; -import java.util.ArrayList; -import java.util.List; -import java.util.Map.Entry; -import java.util.Set; - -/** - * A wrapper for Java SE classes to write/read an HTTP Request - * - * @author Konloch - */ - -public class HTTPRequest { - - public URL url; - private int timeout = 30000; - private String cookie; - private String referer; - private String postData; - private String useragent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0"; - private Proxy proxy; - private boolean setFollowRedirects = true; - private BufferedReader reader; - private DataOutputStream writer; - private HttpURLConnection connection; - private Set>> lastConnectionHeaders; - private int statusCode; - - /** - * Creates a new HTTPRequest object - * - * @param url - */ - public HTTPRequest(URL url) { - this.url = url; - } - - /** - * Sets a referer to send to the web server - */ - public void setReferer(String referer) { - this.referer = referer; - } - - /** - * Set a cookie string to send to the web server - */ - public void setCookie(String cookie) { - this.cookie = cookie; - } - - /** - * Sets post data to send to the web server - */ - public void setPostData(String postData) { - this.postData = postData; - } - - /** - * Sets a custom useragent, default 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0' - */ - public void setUseragent(String useragent) { - this.useragent = useragent; - } - - /** - * Sets the seconds till timeout, default 30,000 milliseconds - */ - public void setTimeout(int timeout) { - this.timeout = timeout; - } - - /** - * Sets a proxy to connect through - */ - public void setProxy(Proxy proxy) { - this.proxy = proxy; - } - - /** - * Used to get the headers the webserver sent on our last connection - */ - public Set>> getLastConnectionHeaders() { - return lastConnectionHeaders; - } - - public int getStatusCode() - { - return statusCode; - } - - /** - * By default, follow redirects are enabled - */ - public void setFollowRedirects(boolean setFollowRedirects) { - this.setFollowRedirects = setFollowRedirects; - } - - /** - * Used to set up the connection to read the content. - */ - private void setup() throws Exception { - if (proxy != null) - connection = (HttpURLConnection) url.openConnection(proxy); - else - connection = (HttpURLConnection) url.openConnection(); - - if (cookie != null) - connection.setRequestProperty("Cookie", cookie); - if (referer != null) - connection.addRequestProperty("Referer", referer); - - connection.setRequestProperty("User-Agent", useragent); - connection.setReadTimeout(timeout); - connection.setConnectTimeout(timeout); - connection.setUseCaches(false); - HttpURLConnection.setFollowRedirects(setFollowRedirects); - - if (postData != null) { - connection.setRequestMethod("POST"); - connection.setDoOutput(true); - connection.setDoInput(true); - writer = new DataOutputStream(connection.getOutputStream()); - writer.writeBytes(postData); - writer.flush(); - } - - reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); - } - - /** - * Reads the entire page and returns a string array - * - * @return - * @throws Exception - */ - public String[] read() throws Exception { - List st; - - try { - setup(); - - st = new ArrayList<>(); - String s; - while ((s = reader.readLine()) != null) - st.add(s); - - lastConnectionHeaders = connection.getHeaderFields().entrySet(); - statusCode = connection.getResponseCode(); - } catch (Exception e) { - cleanup(); - throw e; - } finally { - cleanup(); - } - - return st.toArray(new String[0]); - } - - /** - * Reads as many lines as expected unless it reaches the end. - * - * @param linesToRead - * @return - * @throws Exception - */ - public String[] read(int linesToRead) throws Exception { - List st; - - try { - setup(); - - st = new ArrayList<>(); - for (int i = 0; i < linesToRead; i++) { - String s = reader.readLine(); - if (s != null) - st.add(s); - } - - lastConnectionHeaders = connection.getHeaderFields().entrySet(); - statusCode = connection.getResponseCode(); - } catch (Exception e) { - cleanup(); - throw e; - } finally { - cleanup(); - } - - return st.toArray(new String[0]); - } - - /** - * Only reads the first line - * - * @return - * @throws Exception - */ - public String readSingle() throws Exception { - String s; - - try { - setup(); - - s = reader.readLine(); - - lastConnectionHeaders = connection.getHeaderFields().entrySet(); - statusCode = connection.getResponseCode(); - } catch (Exception e) { - cleanup(); - throw e; - } finally { - cleanup(); - } - - return s; - } - - /** - * Reads until it reaches the expected line then it returns it. - * - * @param linesToRead - * @return - * @throws Exception - */ - public String readSingle(int linesToRead) throws Exception { - String s; - - try { - setup(); - - for (int i = 0; i < linesToRead - 1; i++) - reader.readLine(); - - s = reader.readLine(); - - lastConnectionHeaders = connection.getHeaderFields().entrySet(); - statusCode = connection.getResponseCode(); - } catch (Exception e) { - cleanup(); - throw e; - } finally { - cleanup(); - } - - return s; - } - - /** - * Used to clean up the connection, closes the connections and nulls the objects - */ - private void cleanup() { - try { - reader.close(); - } catch (Exception ignored) { - } - try { - writer.close(); - } catch (Exception ignored) { - } - try { - connection.disconnect(); - } catch (Exception ignored) { - } - reader = null; - writer = null; - connection = null; - } - -} diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/bootloader/Boot.java b/src/main/java/the/bytecode/club/bytecodeviewer/bootloader/Boot.java index d12676e5..5ea76081 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/bootloader/Boot.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/bootloader/Boot.java @@ -9,7 +9,7 @@ import java.util.List; import java.util.Objects; import javax.swing.JOptionPane; import javax.swing.SwingUtilities; -import me.konloch.kontainer.io.HTTPRequest; +import com.konloch.httprequest.HTTPRequest; import org.apache.commons.io.FileUtils; import the.bytecode.club.bytecodeviewer.BytecodeViewer; import the.bytecode.club.bytecodeviewer.Configuration; @@ -277,7 +277,6 @@ public class Boot { public static void populateUrlList() throws Exception { HTTPRequest req = new HTTPRequest(new URL("https://github.com/Konloch/bytecode-viewer/tree/master/libs")); - req.setTimeout(30000); for (String s : req.read()) if (s.contains("href=\"/Konloch/bytecode-viewer/blob/master/libs/")) { urlList.add("https://github.com" + s.split("href=")[1].split("\"")[1]); diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/bootloader/UpdateCheck.java b/src/main/java/the/bytecode/club/bytecodeviewer/bootloader/UpdateCheck.java index babd9c81..62f8f0ec 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/bootloader/UpdateCheck.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/bootloader/UpdateCheck.java @@ -10,7 +10,7 @@ import java.io.InputStream; import java.net.URI; import java.net.URL; import javax.swing.JFileChooser; -import me.konloch.kontainer.io.HTTPRequest; +import com.konloch.httprequest.HTTPRequest; import the.bytecode.club.bytecodeviewer.BytecodeViewer; import the.bytecode.club.bytecodeviewer.Configuration; import the.bytecode.club.bytecodeviewer.api.BCV; @@ -256,7 +256,7 @@ public class UpdateCheck implements Runnable { HTTPRequest request = new HTTPRequest(new URL(url)); request.readSingle(); - return request.getStatusCode() == 200; + return request.getLastStatusCode() == 200; } private static void download(String url, File saveTo, Runnable onFinish) throws Exception diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/util/PingBack.java b/src/main/java/the/bytecode/club/bytecodeviewer/util/PingBack.java index fe99733f..5176bf8d 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/util/PingBack.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/util/PingBack.java @@ -1,7 +1,7 @@ package the.bytecode.club.bytecodeviewer.util; import java.net.URL; -import me.konloch.kontainer.io.HTTPRequest; +import com.konloch.httprequest.HTTPRequest; import the.bytecode.club.bytecodeviewer.Configuration; /***************************************************************************