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 { 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 urlField; private JTextArea httpResponseArea; private JComboBox httpTypeBox; private JComboBox methodBox; private JButton xFormEncodedButton; private JTextArea bodyArea; private JProgressBar progressBar1; private JList list1; private JButton newHeaderButton; private JButton addAuthButton; private JButton runButton; 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); setMinimumSize(new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT)); setSize(WINDOW_WIDTH, WINDOW_HEIGHT); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); newHeaderButton.addActionListener((event) -> { AddHeader ah = new AddHeader(); HeaderModel h = ah.getValue(); if(h != null) { headers.put(h.getKey(), h.getValue()); updateHeaderList(); } }); addAuthButton.addActionListener((event) -> { if(addAuthButton.isEnabled()) { BasicAuthenticationDialog bad = new BasicAuthenticationDialog(); HeaderModel h = bad.showDialog(); if(h != null) { headers.put(h.getKey(), h.getValue()); updateHeaderList(); addAuthButton.setEnabled(false); } } }); xFormEncodedButton.addActionListener((event) -> { 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() { pack(); setVisible(true); } }