diff --git a/.idea/artifacts/cURLing_jar.xml b/.idea/artifacts/cURLing_jar.xml new file mode 100644 index 0000000..9f5722f --- /dev/null +++ b/.idea/artifacts/cURLing_jar.xml @@ -0,0 +1,10 @@ + + + $PROJECT_DIR$/out/artifacts/cURLing_jar + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 1763e15..df60b67 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 507f6c3..87ce4fe 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,19 +1,60 @@ + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/bin/META-INF/MANIFEST.MF b/bin/META-INF/MANIFEST.MF new file mode 100644 index 0000000..bd61d69 --- /dev/null +++ b/bin/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: ovh.alexisdelhaie.curling.Runtime + diff --git a/bin/com/intellij/uiDesigner/core/AbstractLayout.class b/bin/com/intellij/uiDesigner/core/AbstractLayout.class new file mode 100644 index 0000000..474957a Binary files /dev/null and b/bin/com/intellij/uiDesigner/core/AbstractLayout.class differ diff --git a/bin/com/intellij/uiDesigner/core/DimensionInfo.class b/bin/com/intellij/uiDesigner/core/DimensionInfo.class new file mode 100644 index 0000000..0db2f7d Binary files /dev/null and b/bin/com/intellij/uiDesigner/core/DimensionInfo.class differ diff --git a/bin/com/intellij/uiDesigner/core/GridConstraints.class b/bin/com/intellij/uiDesigner/core/GridConstraints.class new file mode 100644 index 0000000..2e1afcc Binary files /dev/null and b/bin/com/intellij/uiDesigner/core/GridConstraints.class differ diff --git a/bin/com/intellij/uiDesigner/core/GridLayoutManager.class b/bin/com/intellij/uiDesigner/core/GridLayoutManager.class new file mode 100644 index 0000000..94e3f41 Binary files /dev/null and b/bin/com/intellij/uiDesigner/core/GridLayoutManager.class differ diff --git a/bin/com/intellij/uiDesigner/core/HorizontalInfo.class b/bin/com/intellij/uiDesigner/core/HorizontalInfo.class new file mode 100644 index 0000000..bdc1fe3 Binary files /dev/null and b/bin/com/intellij/uiDesigner/core/HorizontalInfo.class differ diff --git a/bin/com/intellij/uiDesigner/core/LayoutState.class b/bin/com/intellij/uiDesigner/core/LayoutState.class new file mode 100644 index 0000000..61494df Binary files /dev/null and b/bin/com/intellij/uiDesigner/core/LayoutState.class differ diff --git a/bin/com/intellij/uiDesigner/core/Spacer.class b/bin/com/intellij/uiDesigner/core/Spacer.class new file mode 100644 index 0000000..e534880 Binary files /dev/null and b/bin/com/intellij/uiDesigner/core/Spacer.class differ diff --git a/bin/com/intellij/uiDesigner/core/SupportCode$TextWithMnemonic.class b/bin/com/intellij/uiDesigner/core/SupportCode$TextWithMnemonic.class new file mode 100644 index 0000000..67909c5 Binary files /dev/null and b/bin/com/intellij/uiDesigner/core/SupportCode$TextWithMnemonic.class differ diff --git a/bin/com/intellij/uiDesigner/core/SupportCode.class b/bin/com/intellij/uiDesigner/core/SupportCode.class new file mode 100644 index 0000000..4950c35 Binary files /dev/null and b/bin/com/intellij/uiDesigner/core/SupportCode.class differ diff --git a/bin/com/intellij/uiDesigner/core/Util.class b/bin/com/intellij/uiDesigner/core/Util.class new file mode 100644 index 0000000..4866582 Binary files /dev/null and b/bin/com/intellij/uiDesigner/core/Util.class differ diff --git a/bin/com/intellij/uiDesigner/core/VerticalInfo.class b/bin/com/intellij/uiDesigner/core/VerticalInfo.class new file mode 100644 index 0000000..049e702 Binary files /dev/null and b/bin/com/intellij/uiDesigner/core/VerticalInfo.class differ diff --git a/bin/ovh/alexisdelhaie/curling/Runtime.class b/bin/ovh/alexisdelhaie/curling/Runtime.class new file mode 100644 index 0000000..ea379e7 Binary files /dev/null and b/bin/ovh/alexisdelhaie/curling/Runtime.class differ diff --git a/bin/ovh/alexisdelhaie/curling/dataSet/HeaderModel.class b/bin/ovh/alexisdelhaie/curling/dataSet/HeaderModel.class new file mode 100644 index 0000000..d8a3811 Binary files /dev/null and b/bin/ovh/alexisdelhaie/curling/dataSet/HeaderModel.class differ diff --git a/bin/ovh/alexisdelhaie/curling/web/Client$1.class b/bin/ovh/alexisdelhaie/curling/web/Client$1.class new file mode 100644 index 0000000..20a65e2 Binary files /dev/null and b/bin/ovh/alexisdelhaie/curling/web/Client$1.class differ diff --git a/bin/ovh/alexisdelhaie/curling/web/Client.class b/bin/ovh/alexisdelhaie/curling/web/Client.class new file mode 100644 index 0000000..29e17c1 Binary files /dev/null and b/bin/ovh/alexisdelhaie/curling/web/Client.class differ diff --git a/bin/ovh/alexisdelhaie/curling/web/Method.class b/bin/ovh/alexisdelhaie/curling/web/Method.class new file mode 100644 index 0000000..97df8fe Binary files /dev/null and b/bin/ovh/alexisdelhaie/curling/web/Method.class differ diff --git a/bin/ovh/alexisdelhaie/curling/web/RequestError.class b/bin/ovh/alexisdelhaie/curling/web/RequestError.class new file mode 100644 index 0000000..375a8e2 Binary files /dev/null and b/bin/ovh/alexisdelhaie/curling/web/RequestError.class differ diff --git a/bin/ovh/alexisdelhaie/curling/windows/AddHeader$1.class b/bin/ovh/alexisdelhaie/curling/windows/AddHeader$1.class new file mode 100644 index 0000000..8fabaed Binary files /dev/null and b/bin/ovh/alexisdelhaie/curling/windows/AddHeader$1.class differ diff --git a/bin/ovh/alexisdelhaie/curling/windows/AddHeader$2.class b/bin/ovh/alexisdelhaie/curling/windows/AddHeader$2.class new file mode 100644 index 0000000..bfd0478 Binary files /dev/null and b/bin/ovh/alexisdelhaie/curling/windows/AddHeader$2.class differ diff --git a/bin/ovh/alexisdelhaie/curling/windows/AddHeader$3.class b/bin/ovh/alexisdelhaie/curling/windows/AddHeader$3.class new file mode 100644 index 0000000..a85a13e Binary files /dev/null and b/bin/ovh/alexisdelhaie/curling/windows/AddHeader$3.class differ diff --git a/bin/ovh/alexisdelhaie/curling/windows/AddHeader$4.class b/bin/ovh/alexisdelhaie/curling/windows/AddHeader$4.class new file mode 100644 index 0000000..dc6ad9a Binary files /dev/null and b/bin/ovh/alexisdelhaie/curling/windows/AddHeader$4.class differ diff --git a/bin/ovh/alexisdelhaie/curling/windows/AddHeader.class b/bin/ovh/alexisdelhaie/curling/windows/AddHeader.class new file mode 100644 index 0000000..a017709 Binary files /dev/null and b/bin/ovh/alexisdelhaie/curling/windows/AddHeader.class differ diff --git a/bin/ovh/alexisdelhaie/curling/windows/BasicAuthenticationDialog$1.class b/bin/ovh/alexisdelhaie/curling/windows/BasicAuthenticationDialog$1.class new file mode 100644 index 0000000..a1665f2 Binary files /dev/null and b/bin/ovh/alexisdelhaie/curling/windows/BasicAuthenticationDialog$1.class differ diff --git a/bin/ovh/alexisdelhaie/curling/windows/BasicAuthenticationDialog$2.class b/bin/ovh/alexisdelhaie/curling/windows/BasicAuthenticationDialog$2.class new file mode 100644 index 0000000..6432b5d Binary files /dev/null and b/bin/ovh/alexisdelhaie/curling/windows/BasicAuthenticationDialog$2.class differ diff --git a/bin/ovh/alexisdelhaie/curling/windows/BasicAuthenticationDialog$3.class b/bin/ovh/alexisdelhaie/curling/windows/BasicAuthenticationDialog$3.class new file mode 100644 index 0000000..741dba1 Binary files /dev/null and b/bin/ovh/alexisdelhaie/curling/windows/BasicAuthenticationDialog$3.class differ diff --git a/bin/ovh/alexisdelhaie/curling/windows/BasicAuthenticationDialog$4.class b/bin/ovh/alexisdelhaie/curling/windows/BasicAuthenticationDialog$4.class new file mode 100644 index 0000000..91f6a4d Binary files /dev/null and b/bin/ovh/alexisdelhaie/curling/windows/BasicAuthenticationDialog$4.class differ diff --git a/bin/ovh/alexisdelhaie/curling/windows/BasicAuthenticationDialog.class b/bin/ovh/alexisdelhaie/curling/windows/BasicAuthenticationDialog.class new file mode 100644 index 0000000..ec41454 Binary files /dev/null and b/bin/ovh/alexisdelhaie/curling/windows/BasicAuthenticationDialog.class differ diff --git a/bin/ovh/alexisdelhaie/curling/windows/FormEncodedDialog$1.class b/bin/ovh/alexisdelhaie/curling/windows/FormEncodedDialog$1.class new file mode 100644 index 0000000..8cd9283 Binary files /dev/null and b/bin/ovh/alexisdelhaie/curling/windows/FormEncodedDialog$1.class differ diff --git a/bin/ovh/alexisdelhaie/curling/windows/FormEncodedDialog$2.class b/bin/ovh/alexisdelhaie/curling/windows/FormEncodedDialog$2.class new file mode 100644 index 0000000..efb171d Binary files /dev/null and b/bin/ovh/alexisdelhaie/curling/windows/FormEncodedDialog$2.class differ diff --git a/bin/ovh/alexisdelhaie/curling/windows/FormEncodedDialog$3.class b/bin/ovh/alexisdelhaie/curling/windows/FormEncodedDialog$3.class new file mode 100644 index 0000000..eec62a7 Binary files /dev/null and b/bin/ovh/alexisdelhaie/curling/windows/FormEncodedDialog$3.class differ diff --git a/bin/ovh/alexisdelhaie/curling/windows/FormEncodedDialog$4.class b/bin/ovh/alexisdelhaie/curling/windows/FormEncodedDialog$4.class new file mode 100644 index 0000000..388dc88 Binary files /dev/null and b/bin/ovh/alexisdelhaie/curling/windows/FormEncodedDialog$4.class differ diff --git a/bin/ovh/alexisdelhaie/curling/windows/FormEncodedDialog.class b/bin/ovh/alexisdelhaie/curling/windows/FormEncodedDialog.class new file mode 100644 index 0000000..6af67a7 Binary files /dev/null and b/bin/ovh/alexisdelhaie/curling/windows/FormEncodedDialog.class differ diff --git a/bin/ovh/alexisdelhaie/curling/windows/MainWindow$1.class b/bin/ovh/alexisdelhaie/curling/windows/MainWindow$1.class new file mode 100644 index 0000000..3f41009 Binary files /dev/null and b/bin/ovh/alexisdelhaie/curling/windows/MainWindow$1.class differ diff --git a/bin/ovh/alexisdelhaie/curling/windows/MainWindow.class b/bin/ovh/alexisdelhaie/curling/windows/MainWindow.class new file mode 100644 index 0000000..5653f4f Binary files /dev/null and b/bin/ovh/alexisdelhaie/curling/windows/MainWindow.class differ diff --git a/cURLing.iml b/cURLing.iml index 9c5aa9b..c6af685 100644 --- a/cURLing.iml +++ b/cURLing.iml @@ -25,137 +25,5 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/META-INF/MANIFEST.MF b/src/META-INF/MANIFEST.MF new file mode 100644 index 0000000..bd61d69 --- /dev/null +++ b/src/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: ovh.alexisdelhaie.curling.Runtime + diff --git a/src/ovh/alexisdelhaie/curling/dataSet/HeaderModel.java b/src/ovh/alexisdelhaie/curling/dataSet/HeaderModel.java new file mode 100644 index 0000000..04d2ac6 --- /dev/null +++ b/src/ovh/alexisdelhaie/curling/dataSet/HeaderModel.java @@ -0,0 +1,20 @@ +package ovh.alexisdelhaie.curling.dataSet; + +public class HeaderModel { + + private String key; + private String value; + + public HeaderModel(String key, String value) { + this.key = key; + this.value = value; + } + + public String getKey() { + return key; + } + + public String getValue() { + return value; + } +} diff --git a/src/ovh/alexisdelhaie/curling/web/Client.java b/src/ovh/alexisdelhaie/curling/web/Client.java new file mode 100644 index 0000000..76932ec --- /dev/null +++ b/src/ovh/alexisdelhaie/curling/web/Client.java @@ -0,0 +1,128 @@ +package ovh.alexisdelhaie.curling.web; + + +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.util.List; +import java.util.Map; +import java.util.concurrent.CompletableFuture; + +public class Client { + + private String body; + private Map headers; + private Method method; + private String url; + 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 = body; + this.headers = headers; + this.method = method; + this.url = url; + this.log = ""; + } + + public CompletableFuture run() { + + HttpClient httpClient = HttpClient.newBuilder().build(); + try { + HttpRequest request = requestBuilder(); + + if (request != null) { + return CompletableFuture.supplyAsync(() -> { + String result = String.format("%s\n\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%s", (String)r.body()); + } catch (IOException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + 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); + } + + 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", header.getKey(), header.getValue()); + } + if(method == Method.POST || method == Method.PUT) { + log += String.format("%s", body); + } + return b.build(); + } +} diff --git a/src/ovh/alexisdelhaie/curling/web/Method.java b/src/ovh/alexisdelhaie/curling/web/Method.java new file mode 100644 index 0000000..8532217 --- /dev/null +++ b/src/ovh/alexisdelhaie/curling/web/Method.java @@ -0,0 +1,8 @@ +package ovh.alexisdelhaie.curling.web; + +public enum Method { + GET, + POST, + PUT, + DELETE +} diff --git a/src/ovh/alexisdelhaie/curling/web/RequestError.java b/src/ovh/alexisdelhaie/curling/web/RequestError.java new file mode 100644 index 0000000..e172076 --- /dev/null +++ b/src/ovh/alexisdelhaie/curling/web/RequestError.java @@ -0,0 +1,34 @@ +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/AddHeader.java b/src/ovh/alexisdelhaie/curling/windows/AddHeader.java index 78e37d9..c085bdc 100644 --- a/src/ovh/alexisdelhaie/curling/windows/AddHeader.java +++ b/src/ovh/alexisdelhaie/curling/windows/AddHeader.java @@ -1,5 +1,7 @@ package ovh.alexisdelhaie.curling.windows; +import ovh.alexisdelhaie.curling.dataSet.HeaderModel; + import javax.swing.*; import java.awt.event.*; @@ -13,10 +15,9 @@ public class AddHeader extends JDialog { private JTextField keyField; private JTextField valueField; - private String value; + private HeaderModel result; public AddHeader() { - this.value = ""; setContentPane(contentPane); setModal(true); setTitle(WINDOW_TITLE); @@ -51,11 +52,8 @@ public class AddHeader extends JDialog { } private void onOK() { - if(!keyField.getText().isBlank()) { - value = keyField.getText(); - if(!valueField.getText().isBlank()) { - value += String.format(": %s", valueField.getText()); - } + if(!keyField.getText().isBlank() && !valueField.getText().isBlank()) { + result = new HeaderModel(keyField.getText(), valueField.getText()); } dispose(); } @@ -64,9 +62,9 @@ public class AddHeader extends JDialog { dispose(); } - public String getValue() { + public HeaderModel getValue() { pack(); setVisible(true); - return value; + return result; } } diff --git a/src/ovh/alexisdelhaie/curling/windows/BasicAuthenticationDialog.java b/src/ovh/alexisdelhaie/curling/windows/BasicAuthenticationDialog.java index a57e7ea..1f6c703 100644 --- a/src/ovh/alexisdelhaie/curling/windows/BasicAuthenticationDialog.java +++ b/src/ovh/alexisdelhaie/curling/windows/BasicAuthenticationDialog.java @@ -1,5 +1,7 @@ package ovh.alexisdelhaie.curling.windows; +import ovh.alexisdelhaie.curling.dataSet.HeaderModel; + import javax.swing.*; import java.awt.event.*; import java.io.UnsupportedEncodingException; @@ -7,7 +9,7 @@ import java.util.Base64; public class BasicAuthenticationDialog extends JDialog { - public static String HTTP_AUTH_BASIC_PATTERN = "Authorization: Basic %s"; + public static String HTTP_AUTH_HEADER_KEY = "Authorization"; public static String WINDOW_TITLE = "Authentification HTTP"; private JPanel contentPane; @@ -16,7 +18,7 @@ public class BasicAuthenticationDialog extends JDialog { private JTextField textField1; private JPasswordField passwordField1; - private String value = ""; + private HeaderModel result; public BasicAuthenticationDialog() { setContentPane(contentPane); @@ -60,7 +62,7 @@ public class BasicAuthenticationDialog extends JDialog { .encode(String.format("%s:%s", user, password).getBytes()); try { String authEncoded = new String(authEncodedBytes, "UTF-8"); - value = String.format(HTTP_AUTH_BASIC_PATTERN, authEncoded); + result = new HeaderModel(HTTP_AUTH_HEADER_KEY,String.format("Basic %s", authEncoded)); dispose(); } catch (UnsupportedEncodingException ex) { JOptionPane.showMessageDialog(this, "Le format UTF-8 n'est pas supporté"); @@ -82,9 +84,9 @@ public class BasicAuthenticationDialog extends JDialog { dispose(); } - public String showDialog() { + public HeaderModel showDialog() { pack(); setVisible(true); - return value; + return result; } } diff --git a/src/ovh/alexisdelhaie/curling/windows/FormEncodedDialog.java b/src/ovh/alexisdelhaie/curling/windows/FormEncodedDialog.java index 716ee36..e993686 100644 --- a/src/ovh/alexisdelhaie/curling/windows/FormEncodedDialog.java +++ b/src/ovh/alexisdelhaie/curling/windows/FormEncodedDialog.java @@ -8,6 +8,8 @@ import java.util.Base64; public class FormEncodedDialog extends JDialog { public static String WINDOW_TITLE = "Formulaire de données"; + public static String[] UNENCODED_CHAR = {"!", "*", "'", "(", ")", ";", ":", "@", "&", "=", "+", "$", ",", "/", "?", "#", "[", "]"}; + public static String[] ENCODED_CHAR = {"%21", "%2A", "%27", "%28", "%29", "%3B", "%3A", "%40", "%26", "%3D", "%2B", "%24", "%2C", "%2F", "%3F", "%23", "%5B", "%5D"}; private JPanel contentPane; private JButton buttonOK; @@ -20,8 +22,6 @@ public class FormEncodedDialog extends JDialog { private String resultValue = ""; private DefaultListModel model; - public static String[] UNENCODED_CHAR = {"!", "*", "'", "(", ")", ";", ":", "@", "&", "=", "+", "$", ",", "/", "?", "#", "[", "]"}; - public static String[] ENCODED_CHAR = {"%21", "%2A", "%27", "%28", "%29", "%3B", "%3A", "%40", "%26", "%3D", "%2B", "%24", "%2C", "%2F", "%3F", "%23", "%5B", "%5D"}; public FormEncodedDialog() { setContentPane(contentPane); diff --git a/src/ovh/alexisdelhaie/curling/windows/MainWindow.form b/src/ovh/alexisdelhaie/curling/windows/MainWindow.form index 6e2c697..73e0fbd 100644 --- a/src/ovh/alexisdelhaie/curling/windows/MainWindow.form +++ b/src/ovh/alexisdelhaie/curling/windows/MainWindow.form @@ -8,18 +8,7 @@ - - - - - - - - - - - - + @@ -30,7 +19,7 @@ - + @@ -43,7 +32,7 @@ - + @@ -131,11 +120,27 @@ - + + + + + + + + + + + + + + + + + diff --git a/src/ovh/alexisdelhaie/curling/windows/MainWindow.java b/src/ovh/alexisdelhaie/curling/windows/MainWindow.java index e4f2118..069076a 100644 --- a/src/ovh/alexisdelhaie/curling/windows/MainWindow.java +++ b/src/ovh/alexisdelhaie/curling/windows/MainWindow.java @@ -1,17 +1,31 @@ package ovh.alexisdelhaie.curling.windows; +import ovh.alexisdelhaie.curling.dataSet.HeaderModel; +import ovh.alexisdelhaie.curling.web.Client; +import ovh.alexisdelhaie.curling.web.Method; + import javax.swing.*; import java.awt.*; +import java.awt.event.ActionEvent; +import java.net.http.HttpClient; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; public class MainWindow extends JDialog { - private DefaultListModel model; + public static int WINDOW_HEIGHT = 700; + public static int WINDOW_WIDTH = 1100; + public static String WINDOW_TITLE = "cURLing"; + public static String CONTENT_TYPE = "content-type"; + public static String WWW_FORM_URLENCODED = "application/x-www-form-urlencoded"; private JPanel contentPane; - private JTextField textField1; - private JTextArea textArea1; - private JComboBox comboBox1; - private JComboBox comboBox2; + private JTextField urlField; + private JTextArea httpResponseArea; + private JComboBox httpTypeBox; + private JComboBox methodBox; private JButton xFormEncodedButton; private JTextArea bodyArea; private JProgressBar progressBar1; @@ -20,13 +34,14 @@ public class MainWindow extends JDialog { private JButton addAuthButton; private JButton runButton; - public static int WINDOW_HEIGHT = 700; - public static int WINDOW_WIDTH = 1100; - public static String WINDOW_TITLE = "cURLing"; + private DefaultListModel model; + private Map headers; + private Map params; public MainWindow() { super((Dialog) null); setTitle(WINDOW_TITLE); + headers = new HashMap<>(); model = new DefaultListModel<>(); list1.setModel(model); setContentPane(contentPane); @@ -36,19 +51,21 @@ public class MainWindow extends JDialog { newHeaderButton.addActionListener((event) -> { AddHeader ah = new AddHeader(); - String header = ah.getValue(); - model.addElement(header); - list1.updateUI(); + HeaderModel h = ah.getValue(); + if(h != null) { + headers.put(h.getKey(), h.getValue()); + updateHeaderList(); + } }); addAuthButton.addActionListener((event) -> { if(addAuthButton.isEnabled()) { BasicAuthenticationDialog bad = new BasicAuthenticationDialog(); - String auth = bad.showDialog(); - if(!auth.isBlank()) { + HeaderModel h = bad.showDialog(); + if(h != null) { + headers.put(h.getKey(), h.getValue()); + updateHeaderList(); addAuthButton.setEnabled(false); - model.addElement(auth); - list1.updateUI(); } } }); @@ -57,7 +74,74 @@ public class MainWindow extends JDialog { FormEncodedDialog fe = new FormEncodedDialog(); String datas = fe.showDialog(); bodyArea.setText(datas); + headers.put(CONTENT_TYPE, WWW_FORM_URLENCODED); + updateHeaderList(); }); + + runButton.addActionListener(this::start); + } + + private void updateHeaderList() { + model.clear(); + for(Map.Entry entry : headers.entrySet()) { + String header = String.format("%s: %s", entry.getKey(), entry.getValue()); + model.addElement(header); + } + list1.updateUI(); + } + + private void start(ActionEvent ev) { + startLoading(true); + Client c = new Client(bodyArea.getText(), headers, getMethod(), urlBuilder()); + 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); + } + } + }).start(); + } + + private String urlBuilder() { + return String.format("%s://%s", ((String) httpTypeBox.getSelectedItem()).toLowerCase(), urlField.getText()); + } + + private Method getMethod() { + if (methodBox.getSelectedItem().equals("GET")) { + return Method.GET; + } else if (methodBox.getSelectedItem().equals("POST")) { + return Method.POST; + } else if (methodBox.getSelectedItem().equals("PUT")) { + return Method.PUT; + } else if (methodBox.getSelectedItem().equals("DELETE")) { + return Method.DELETE; + } + return Method.GET; + } + + private void setDisabledComponents(boolean b) { + urlField.setEnabled(!b); + httpTypeBox.setEnabled(!b); + methodBox.setEnabled(!b); + xFormEncodedButton.setEnabled(!b); + bodyArea.setEnabled(!b); + list1.setEnabled(!b); + newHeaderButton.setEnabled(!b); + addAuthButton.setEnabled(!b); + runButton.setEnabled(!b); + } + + private void startLoading(boolean b) { + setDisabledComponents(b); + progressBar1.setIndeterminate(b); } public void showFrame() {