diff --git a/.idea/artifacts/cURLing_jar.xml b/.idea/artifacts/cURLing_jar.xml
index 9f5722f..a6ea466 100644
--- a/.idea/artifacts/cURLing_jar.xml
+++ b/.idea/artifacts/cURLing_jar.xml
@@ -4,6 +4,8 @@
+
+
diff --git a/.idea/libraries/javax_json_javax_json_api_1_0.xml b/.idea/libraries/javax_json_javax_json_api_1_0.xml
new file mode 100644
index 0000000..a22beb0
--- /dev/null
+++ b/.idea/libraries/javax_json_javax_json_api_1_0.xml
@@ -0,0 +1,10 @@
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/libraries/org_glassfish_javax_json_1_0.xml b/.idea/libraries/org_glassfish_javax_json_1_0.xml
new file mode 100644
index 0000000..79adc19
--- /dev/null
+++ b/.idea/libraries/org_glassfish_javax_json_1_0.xml
@@ -0,0 +1,10 @@
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 5929b0e..21fe182 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -7,16 +7,15 @@
+
+
+
+
+
-
-
-
-
-
-
-
-
-
+
+
+
@@ -38,19 +37,27 @@
+
+
+
+
+
+
+
+
-
+
@@ -104,6 +111,11 @@
+
+
+
+
+
@@ -123,4 +135,7 @@
+
+
+
\ No newline at end of file
diff --git a/cURLing.iml b/cURLing.iml
index c6af685..aa446e9 100644
--- a/cURLing.iml
+++ b/cURLing.iml
@@ -25,5 +25,7 @@
+
+
\ No newline at end of file
diff --git a/src/ovh/alexisdelhaie/curling/web/Client.java b/src/ovh/alexisdelhaie/curling/web/Client.java
deleted file mode 100644
index f0225d8..0000000
--- a/src/ovh/alexisdelhaie/curling/web/Client.java
+++ /dev/null
@@ -1,178 +0,0 @@
-package ovh.alexisdelhaie.curling.web;
-
-
-import javax.net.ssl.*;
-import java.io.IOException;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.net.http.HttpClient;
-import java.net.http.HttpRequest;
-import java.net.http.HttpResponse;
-import java.security.KeyManagementException;
-import java.security.NoSuchAlgorithmException;
-import java.security.cert.X509Certificate;
-import java.time.Duration;
-import java.util.List;
-import java.util.Map;
-import java.util.Properties;
-import java.util.concurrent.CompletableFuture;
-
-public class Client {
-
- public static long TIMEOUT = 10000;
-
- private String body;
- private Map headers;
- private Method method;
- private String url;
- private long timeout;
- private boolean allowInvalidSSL;
- private RequestError error;
-
- private String log;
-
- public String getBody() {
- return body;
- }
-
- public Map getHeaders() {
- return headers;
- }
-
- public Method getMethod() {
- return method;
- }
-
- public String getUrl() {
- return url;
- }
-
- public RequestError getError() {
- return error;
- }
-
- public void setBody(String body) {
- this.body = body;
- }
-
- public void setHeaders(Map headers) {
- this.headers = headers;
- }
-
- public void setMethod(Method method) {
- this.method = method;
- }
-
- public void setUrl(String url) {
- this.url = url;
- }
-
- public Client(String body, Map headers, Method method, String url) {
- this(body, headers, method, url, false, TIMEOUT);
- }
-
- public Client(String body, Map headers, Method method, String url, boolean allowInvalidSSL) {
- this(body, headers, method, url, allowInvalidSSL, TIMEOUT);
- }
-
- public Client(String body, Map headers, Method method, String url, boolean allowInvalidSSL, long timeout) {
- this.body = body;
- this.headers = headers;
- this.method = method;
- this.url = url;
- this.timeout = timeout;
- this.allowInvalidSSL = allowInvalidSSL;
- this.log = "";
- }
-
- public CompletableFuture run() {
- try {
- HttpClient httpClient = getClient();
- HttpRequest request = requestBuilder();
-
- if (request != null) {
- return CompletableFuture.supplyAsync(() -> {
- String result = String.format("%s\n---- HTTP Response ----\n", log);
- try {
- HttpResponse r = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
- result += String.format("> Status : %d\n", r.statusCode());
- for(Map.Entry> header : r.headers().map().entrySet()) {
- result += String.format("> %s: ", header.getKey());
- for(String s : header.getValue()) {
- result += s;
- }
- result += "\n";
- }
- result += String.format("\n---- Body ----\n%s", (String)r.body());
- } catch (IOException e) {
- result += String.format("\n/!\\ %s", e.getMessage());
- error = new RequestError(e.getMessage(), e.getLocalizedMessage(), e);
- } catch (InterruptedException e) {
- result += String.format("\n/!\\ %s", e.getMessage());
- error = new RequestError("Thread interrupted", "The action was stopped (by you maybe)", e);
- }
-
- return result;
- });
-
- } else {
- error = new RequestError("Requete invalide", "La requete est vide");
- }
-
- } catch (URISyntaxException e) {
- error = new RequestError("URL invalide", "Le format de l'URL est invalide", e);
- } catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- } catch (KeyManagementException e) {
- e.printStackTrace();
- }
-
- return null;
- }
-
- private HttpRequest requestBuilder() throws URISyntaxException {
- log += "< ";
- HttpRequest.Builder b;
- b = HttpRequest.newBuilder(new URI(url));
- switch (method) {
- case GET: b.GET(); log += "GET"; break;
- case POST: b.POST(HttpRequest.BodyPublishers.ofString(body)); log += "POST"; break;
- case PUT: b.PUT(HttpRequest.BodyPublishers.ofString(body)); log += "PUT"; break;
- case DELETE: b.DELETE(); log += "DELETE"; break;
- }
- log += String.format(" %s\n", url);
- for (Map.Entry header : headers.entrySet()) {
- b.setHeader(header.getKey(), header.getValue());
- log += String.format("< %s: %s\n", header.getKey(), header.getValue());
- }
- if(method == Method.POST || method == Method.PUT) {
- log += String.format("\n---- Body ----\n%s", body);
- }
- b.timeout(Duration.ofMillis(timeout));
- return b.build();
- }
-
- private HttpClient getClient() throws KeyManagementException, NoSuchAlgorithmException {
- HttpClient.Builder b = HttpClient.newBuilder();
- b.followRedirects(HttpClient.Redirect.NEVER);
- b.version(HttpClient.Version.HTTP_2);
- if(allowInvalidSSL) {
- b.sslContext(getInvalidSSLConfig());
- }
- return b.build();
- }
-
- private SSLContext getInvalidSSLConfig() throws NoSuchAlgorithmException, KeyManagementException {
- TrustManager[] trustAllCerts = new TrustManager[]{
- new X509TrustManager() {
- public java.security.cert.X509Certificate[] getAcceptedIssuers() {return null;}
- public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType){}
- public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType){}
- }
- };
-
- SSLContext sc = SSLContext.getInstance("SSL");
- sc.init(null, trustAllCerts, new java.security.SecureRandom());
- return sc;
- }
-}
diff --git a/src/ovh/alexisdelhaie/curling/web/HttpClient.java b/src/ovh/alexisdelhaie/curling/web/HttpClient.java
new file mode 100644
index 0000000..cdfbe2b
--- /dev/null
+++ b/src/ovh/alexisdelhaie/curling/web/HttpClient.java
@@ -0,0 +1,169 @@
+package ovh.alexisdelhaie.curling.web;
+
+import java.io.*;
+import java.net.*;
+import java.util.Map;
+
+public class HttpClient {
+
+ public static String CRLF = "\r\n";
+ public static int DEFAULT_PORT = 80;
+ public static int DEFAULT_TIMEOUT = 10000;
+
+ protected InetAddress addr;
+ protected URL url;
+ protected boolean valid;
+ protected Method method;
+ protected Map headers;
+ protected String body;
+
+ public HttpClient(String u, Method method, String body, Map headers) {
+ try {
+ URL url = new URL(u);
+ addr = resolve(url.getHost());
+ this.url = url;
+ this.headers = headers;
+ this.method = method;
+ this.body = body;
+ valid = true;
+ } catch (UnknownHostException | MalformedURLException e) {
+ valid = false;
+ }
+ }
+
+ public InetAddress getAddr() {
+ return addr;
+ }
+
+ public URL getUrl() {
+ return url;
+ }
+
+ public Method getMethod() {
+ return method;
+ }
+
+ public Map getHeaders() {
+ return headers;
+ }
+
+ public String getBody() {
+ return body;
+ }
+
+ public boolean isDomain() {
+ if(valid) {
+ String comparable = url.getHost();
+ if(comparable.contains(":")) {
+ comparable = comparable.split(":")[0];
+ }
+ return !(comparable.equals(addr.getHostAddress()));
+ }
+ return false;
+ }
+
+ public String run() {
+ String result = "";
+ Socket s = null;
+ if (valid) {
+ String request = String.format("%s%s%s", buildHeader(), CRLF, body);
+ result += String.format("--- Request ---\n%s", request);
+ BufferedOutputStream bos = null;
+ BufferedInputStream bis = null;
+ try {
+ s = new Socket(addr, getPort());
+ s.setKeepAlive(false);
+ s.setSoTimeout(DEFAULT_TIMEOUT);
+ if (s.isConnected()) {
+ bos = new BufferedOutputStream(s.getOutputStream());
+ bos.write(request.getBytes());
+ bos.flush();
+ bis = new BufferedInputStream(s.getInputStream());
+ byte[] b = bis.readAllBytes();
+ result += String.format("\n\n--- Response ---\n%s", new String(b, "UTF-8"));
+ } else {
+ result += "/!\\ Not connected";
+ }
+ } catch (IOException io) {
+ result += String.format("\n\n /!\\ %s", io.getMessage());
+ } finally {
+ try {
+ if(bos != null) {
+ bos.close();
+ }
+ if(bis != null) {
+ bis.close();
+ }
+ if(s != null && s.isConnected()) {
+ s.close();
+ }
+ } catch (IOException e) {
+ result += String.format("\n\n /!\\ %s", e.getMessage());
+ }
+ }
+ }
+ return result;
+ }
+
+ private InetAddress resolve(String host) throws UnknownHostException {
+ InetAddress[] addrs = InetAddress.getAllByName(host);
+ return addrs[0];
+ }
+
+ protected String buildHeader() {
+ String result = String.format("%s %s HTTP/1.1%s", method.toString(), getPath(), CRLF);
+ result += getHostHeader();
+ result += String.format("Accept: */*%s", CRLF);
+ result += String.format("Connection: close%s", CRLF);
+ if(method == Method.POST || method == Method.PUT) {
+ result += String.format("Content-Length: %d%s", body.length(), CRLF);
+ }
+ if(!headers.containsKey("User-Agent")) {
+ result += String.format("User-Agent: cURLing/2.0 %s%s", System.getProperty("os.name"), CRLF);
+ }
+ if(headers != null) {
+ for (Map.Entry entry : headers.entrySet()) {
+ if (isAuthorizedHeader(entry.getKey())) {
+ result += String.format("%s: %s%s", entry.getKey(), entry.getValue(), CRLF);
+ }
+ }
+ }
+ return result;
+ }
+
+ private boolean isAuthorizedHeader(String key) {
+ boolean result = true;
+ if (key.toUpperCase().equals("ACCEPT") || key.toUpperCase().equals("HOST") || key.toUpperCase().equals("CONNECTION")) {
+ result = false;
+ }
+ return result;
+ }
+
+ public boolean isValid() {
+ return valid;
+ }
+
+ private String getPath() {
+ if(url.getPath().isBlank()) {
+ return "/";
+ }
+ return url.getPath();
+ }
+
+ private String getHostHeader() {
+ String result;
+ if (url.getPort() != -1) {
+ result = String.format("Host: %s:%d%s", url.getHost(), url.getPort(), CRLF);
+ } else {
+ result = String.format("Host: %s%s", url.getHost(), CRLF);
+ }
+ return result;
+ }
+
+ protected int getPort() {
+ if(url.getPort() != -1) {
+ return url.getPort();
+ }
+ return DEFAULT_PORT;
+ }
+}
diff --git a/src/ovh/alexisdelhaie/curling/web/HttpsClient.java b/src/ovh/alexisdelhaie/curling/web/HttpsClient.java
new file mode 100644
index 0000000..6bd07ec
--- /dev/null
+++ b/src/ovh/alexisdelhaie/curling/web/HttpsClient.java
@@ -0,0 +1,107 @@
+package ovh.alexisdelhaie.curling.web;
+
+import javax.net.ssl.*;
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.Socket;
+import java.security.*;
+import java.security.cert.CertificateException;
+import java.util.Map;
+
+public class HttpsClient extends HttpClient {
+
+ public static int DEFAULT_PORT = 443;
+
+ private boolean badSSL;
+
+ public HttpsClient(String u, Method method, String body, Map headers, boolean allowBadSSL) {
+ super(u, method, body, headers);
+ badSSL = allowBadSSL;
+ }
+
+ @Override
+ public String run() {
+ String result = "";
+ SSLSocket s = null;
+ if (valid) {
+ String request = String.format("%s%s%s", buildHeader(), CRLF, body);
+ result += String.format("--- Request ---\n%s", request);
+ BufferedOutputStream bos = null;
+ BufferedInputStream bis = null;
+ try {
+ s = getSocket(addr, getPort());
+ s.setKeepAlive(false);
+ s.setSoTimeout(DEFAULT_TIMEOUT);
+ if (s.isConnected()) {
+ bos = new BufferedOutputStream(s.getOutputStream());
+ bos.write(request.getBytes());
+ bos.flush();
+ bis = new BufferedInputStream(s.getInputStream());
+ byte[] b = bis.readAllBytes();
+ result += String.format("\n\n--- Response ---\n%s", new String(b, "UTF-8"));
+ } else {
+ result += "/!\\ Not connected";
+ }
+ } catch (IOException io) {
+ result += String.format("\n\n /!\\ %s", io.getMessage());
+ } catch (NoSuchAlgorithmException e) {
+ result += String.format("\n\n /!\\ %s", e.getMessage());
+ } catch (KeyManagementException e) {
+ result += String.format("\n\n /!\\ %s", e.getMessage());
+ } finally {
+ try {
+ if(bos != null) {
+ bos.close();
+ }
+ if(bis != null) {
+ bis.close();
+ }
+ if(s != null && s.isConnected()) {
+ s.close();
+ }
+ } catch (IOException e) {
+ result += String.format("\n\n /!\\ %s", e.getMessage());
+ }
+ }
+ }
+ return result;
+ }
+
+ private SSLSocket getSocket(InetAddress addr, int port) throws IOException, KeyManagementException, NoSuchAlgorithmException {
+ SSLSocketFactory factory = null;
+ if(badSSL) {
+ factory = getInvalidSSLConfig().getSocketFactory();
+ } else {
+ factory = (SSLSocketFactory)SSLSocketFactory.getDefault();
+ }
+ SSLSocket s = (SSLSocket) factory.createSocket(addr, port);
+ s.startHandshake();
+ return s;
+ }
+
+ private SSLContext getInvalidSSLConfig() throws NoSuchAlgorithmException, KeyManagementException {
+ TrustManager[] trustAllCerts = new TrustManager[]{
+ new X509TrustManager() {
+ public java.security.cert.X509Certificate[] getAcceptedIssuers() {return null;}
+ public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType){}
+ public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType){}
+ }
+ };
+
+ SSLContext sc = SSLContext.getInstance("SSL");
+ sc.init(null, trustAllCerts, new java.security.SecureRandom());
+ return sc;
+ }
+
+ @Override
+ protected int getPort() {
+ int port = super.getPort();
+ if (port == super.DEFAULT_PORT) {
+ return this.DEFAULT_PORT;
+ }
+ return port;
+ }
+}
diff --git a/src/ovh/alexisdelhaie/curling/web/RequestError.java b/src/ovh/alexisdelhaie/curling/web/RequestError.java
deleted file mode 100644
index e172076..0000000
--- a/src/ovh/alexisdelhaie/curling/web/RequestError.java
+++ /dev/null
@@ -1,34 +0,0 @@
-package ovh.alexisdelhaie.curling.web;
-
-public class RequestError {
-
- private String title;
- private String message;
- private Exception e;
-
- public RequestError(String title, String message, Exception exception) {
- this.title = title;
- this.message = message;
- this.e = exception;
- }
-
- public RequestError(String title, String message) {
- this(title, message, null);
- }
-
- public String getTitle() {
- return title;
- }
-
- public String getMessage() {
- return message;
- }
-
- public Exception getException() {
- return e;
- }
-
- public boolean hasException() {
- return (e != null);
- }
-}
diff --git a/src/ovh/alexisdelhaie/curling/windows/MainWindow.form b/src/ovh/alexisdelhaie/curling/windows/MainWindow.form
index d9e92ae..387cec2 100644
--- a/src/ovh/alexisdelhaie/curling/windows/MainWindow.form
+++ b/src/ovh/alexisdelhaie/curling/windows/MainWindow.form
@@ -110,7 +110,7 @@
-
+
@@ -130,13 +130,29 @@
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
@@ -155,8 +171,8 @@
-
-
+
+
@@ -164,6 +180,14 @@
+
+
+
+
+
+
+
+
diff --git a/src/ovh/alexisdelhaie/curling/windows/MainWindow.java b/src/ovh/alexisdelhaie/curling/windows/MainWindow.java
index 3287f41..57ae94e 100644
--- a/src/ovh/alexisdelhaie/curling/windows/MainWindow.java
+++ b/src/ovh/alexisdelhaie/curling/windows/MainWindow.java
@@ -1,19 +1,18 @@
package ovh.alexisdelhaie.curling.windows;
import ovh.alexisdelhaie.curling.dataSet.HeaderModel;
-import ovh.alexisdelhaie.curling.web.Client;
+import ovh.alexisdelhaie.curling.web.HttpClient;
+import ovh.alexisdelhaie.curling.web.HttpsClient;
import ovh.alexisdelhaie.curling.web.Method;
+import javax.json.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
-import java.awt.event.ItemListener;
-import java.net.http.HttpClient;
+import java.io.*;
import java.util.HashMap;
import java.util.Map;
-import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.ExecutionException;
public class MainWindow extends JFrame {
@@ -36,10 +35,12 @@ public class MainWindow extends JFrame {
private JButton addAuthButton;
private JButton runButton;
private JCheckBox allowInvalidSSLField;
+ private JButton clearButton;
+ private JButton saveButton;
+ private JButton openButton;
private DefaultListModel model;
private Map headers;
- private Map params;
private boolean allowInvalidSSL;
public MainWindow() {
@@ -93,6 +94,26 @@ public class MainWindow extends JFrame {
}
});
+ clearButton.addActionListener((event) -> {
+ clear();
+ });
+
+ saveButton.addActionListener((event) -> {
+ try {
+ save();
+ } catch (IOException e) {
+ JOptionPane.showMessageDialog(this, e.getMessage());
+ }
+ });
+
+ openButton.addActionListener((event) -> {
+ try {
+ open();
+ } catch (IOException e) {
+ JOptionPane.showMessageDialog(this, e.getMessage());
+ }
+ });
+
runButton.addActionListener(this::start);
}
@@ -111,24 +132,39 @@ public class MainWindow extends JFrame {
return;
}
startLoading(true);
- Client c = new Client(bodyArea.getText(), headers, getMethod(), urlBuilder(), getAllowInvalidSSL());
- CompletableFuture f = c.run();
- new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- httpResponseArea.setText(f.get());
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (ExecutionException e) {
- e.printStackTrace();
- } finally {
- startLoading(false);
+ new Thread(() -> {
+ try{
+ String result = "";
+ if(isHTTPS()) {
+ HttpsClient c = new HttpsClient(urlBuilder(), getMethod(), bodyArea.getText(), headers, getAllowInvalidSSL());
+ if (c.isValid()) {
+ if(c.isDomain()) {
+ result += String.format("Resolved %s : %s\n", c.getUrl().getHost(), c.getAddr().getHostAddress());
+ }
+ result += c.run();
+ }
+ } else {
+ HttpClient c = new HttpClient(urlBuilder(), getMethod(), bodyArea.getText(), headers);
+ if (c.isValid()) {
+ if(c.isDomain()) {
+ result += String.format("Resolved %s : %s\n", c.getUrl().getHost(), c.getAddr().getHostAddress());
+ }
+ result += c.run();
+ }
}
+ httpResponseArea.setText(result);
+ } catch (Exception e) {
+ e.printStackTrace();
+ } finally {
+ startLoading(false);
}
}).start();
}
+ private boolean isHTTPS() {
+ return httpTypeBox.getSelectedItem().equals("HTTPS");
+ }
+
private String urlBuilder() {
return String.format("%s://%s", ((String) httpTypeBox.getSelectedItem()).toLowerCase(), urlField.getText());
}
@@ -146,6 +182,20 @@ public class MainWindow extends JFrame {
return Method.GET;
}
+ private boolean methodExist(String s) {
+ boolean result = false;
+ for(Method m : Method.values()) {
+ if(s.equals(m.toString())) {
+ result = true;
+ }
+ }
+ return result;
+ }
+
+ private boolean httpExist(String s) {
+ return (s.equals("HTTP") || s.equals("HTTPS"));
+ }
+
private void setDisabledComponents(boolean b) {
urlField.setEnabled(!b);
httpTypeBox.setEnabled(!b);
@@ -160,7 +210,7 @@ public class MainWindow extends JFrame {
}
private void setEnableAllowInvalidSSLField(boolean b) {
- if(httpTypeBox.getSelectedItem().equals("HTTPS")) {
+ if(isHTTPS()) {
allowInvalidSSLField.setEnabled(b);
} else {
allowInvalidSSLField.setEnabled(false);
@@ -168,7 +218,7 @@ public class MainWindow extends JFrame {
}
private boolean getAllowInvalidSSL() {
- if(httpTypeBox.getSelectedItem().equals("HTTPS")) {
+ if(isHTTPS()) {
return allowInvalidSSL;
}
@@ -176,8 +226,12 @@ public class MainWindow extends JFrame {
}
private void startLoading(boolean b) {
- setDisabledComponents(b);
- progressBar1.setIndeterminate(b);
+ try{
+ setDisabledComponents(b);
+ progressBar1.setIndeterminate(b);
+ } catch (Exception e) {
+ System.out.println("Trop rapide mec !");
+ }
}
public void showFrame() {
@@ -192,4 +246,69 @@ public class MainWindow extends JFrame {
int x = (int)( screen.getWidth() / 2 ) - this.getWidth() / 2;
this.setLocation(x, y);
}
+
+ private void save() throws IOException {
+ JFileChooser fileChooser = new JFileChooser();
+ fileChooser.setDialogTitle("Specify a file to save");
+ int userSelection = fileChooser.showSaveDialog(this);
+ if (userSelection == JFileChooser.APPROVE_OPTION) {
+ File fileToSave = fileChooser.getSelectedFile();
+ JsonArrayBuilder a = Json.createArrayBuilder();
+ for(Map.Entry entry : headers.entrySet()) {
+ a.add(Json.createObjectBuilder()
+ .add("key", entry.getKey())
+ .add("value", entry.getValue())
+ .build());
+ }
+ JsonObject o = Json.createObjectBuilder()
+ .add("url", urlField.getText())
+ .add("method", getMethod().toString())
+ .add("http", (String)httpTypeBox.getSelectedItem())
+ .add("body", bodyArea.getText())
+ .add("headers", a.build())
+ .build();
+ FileWriter fw = new FileWriter(fileToSave);
+ fw.write(o.toString());
+ fw.close();
+ }
+ }
+
+ private void clear() {
+ headers.clear();
+ updateHeaderList();
+ bodyArea.setText("");
+ urlField.setText("");
+ httpResponseArea.setText("");
+ }
+
+ private void open() throws IOException {
+ JFileChooser fileChooser = new JFileChooser();
+ fileChooser.setDialogTitle("Specify a file to open");
+ int userSelection = fileChooser.showSaveDialog(this);
+ if (userSelection == JFileChooser.APPROVE_OPTION) {
+ File fileToRead = fileChooser.getSelectedFile();
+ FileInputStream fis = new FileInputStream(fileToRead);
+ byte[] data = new byte[(int) fileToRead.length()];
+ fis.read(data);
+ fis.close();
+ String json = new String(data, "UTF-8");
+ JsonReader reader = Json.createReader(new StringReader(json));
+ JsonObject o = reader.readObject();
+ clear();
+ urlField.setText(o.getString("url"));
+ if(methodExist(o.getString("method"))) {
+ methodBox.setSelectedItem(o.getString("method"));
+ }
+ if(httpExist(o.getString("http"))) {
+ httpTypeBox.setSelectedItem(o.getString("http"));
+ }
+ bodyArea.setText(o.getString("body"));
+ JsonArray a = o.getJsonArray("headers");
+ for (JsonValue v : a) {
+ JsonObject vo = (JsonObject) v;
+ headers.put(vo.getString("key"), vo.getString("value"));
+ }
+ updateHeaderList();
+ }
+ }
}