Ajout du HttpClient
This commit is contained in:
@@ -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<String> 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<String> model;
|
||||
private Map<String, String> headers;
|
||||
private Map<String, String> 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<String, String> 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<String> 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() {
|
||||
|
||||
Reference in New Issue
Block a user