client.ui.Container.java Source code

Java tutorial

Introduction

Here is the source code for client.ui.Container.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package client.ui;

import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Protocol;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Request.Builder;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;

import java.io.IOException;
import java.net.ConnectException;
import java.net.MalformedURLException;
import java.net.URL;

import java.security.cert.Certificate;
import java.security.cert.CertificateExpiredException;
import java.security.cert.CertificateNotYetValidException;
import java.security.cert.X509Certificate;
import java.util.Arrays;

import javax.net.SocketFactory;
import javax.net.ssl.*;

import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.text.DefaultCaret;

import org.json.*;

/**
 *
 * @author Jos Fernando
 */
public class Container extends javax.swing.JFrame {

    private static final String ENDPOINT = "https://ametrics.it.uc3m.es/start/jar/";

    private OkHttpClient httpClient;
    private OkHttpClient http2Client;

    /**
     * Creates new form Container
     */
    public Container() {
        initComponents();
        urlInput.setText(ENDPOINT);
        vcodeOutput.setEnabled(false);

        initClients();
    }

    private void initClients() {
        http2Client = new OkHttpClient();
        httpClient = http2Client.clone();

        httpClient.setProtocols(Arrays.asList(Protocol.HTTP_1_1));
        http2Client.setProtocols(Arrays.asList(Protocol.HTTP_2));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        campaignLabel = new javax.swing.JLabel();
        progress = new javax.swing.JProgressBar();
        button = new javax.swing.JButton();
        campaignInput = new javax.swing.JTextField();
        workerlLabel = new javax.swing.JLabel();
        workerInput = new javax.swing.JTextField();
        urlInput = new javax.swing.JTextField();
        urlLabel = new javax.swing.JLabel();
        vcodeOutput = new javax.swing.JTextField();
        vcodeLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMaximumSize(new java.awt.Dimension(460, 240));
        setMinimumSize(new java.awt.Dimension(460, 240));
        setPreferredSize(new java.awt.Dimension(460, 240));
        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        campaignLabel.setText("Campaign ID:");
        getContentPane().add(campaignLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 80, 20));
        getContentPane().add(progress, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 70, 430, 20));

        button.setText("Start");
        button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                buttonMouseReleased(evt);
            }
        });
        button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonActionPerformed(evt);
            }
        });
        getContentPane().add(button, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 160, 70, 30));

        campaignInput.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campaignInputActionPerformed(evt);
            }
        });
        getContentPane().add(campaignInput, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 10, 120, -1));

        workerlLabel.setText("Worker ID:");
        getContentPane().add(workerlLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 10, 60, 20));

        workerInput.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                workerInputActionPerformed(evt);
            }
        });
        getContentPane().add(workerInput, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 10, 110, -1));

        urlInput.setPreferredSize(new java.awt.Dimension(200, 20));
        urlInput.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                urlInputActionPerformed(evt);
            }
        });
        getContentPane().add(urlInput, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 40, 350, 20));

        urlLabel.setText("URL endpoint:");
        getContentPane().add(urlLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40, 80, 20));

        vcodeOutput.setPreferredSize(new java.awt.Dimension(200, 20));
        vcodeOutput.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                vcodeOutputActionPerformed(evt);
            }
        });
        getContentPane().add(vcodeOutput, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 130, 430, 20));
        getContentPane().add(vcodeLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 100, 130, 20));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_buttonActionPerformed

    private void buttonMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_buttonMouseReleased
        String campaignId = campaignInput.getText().trim();
        String workerId = workerInput.getText().trim();

        if (campaignId.length() < 1) {
            JOptionPane.showMessageDialog(this, "Please introduce the Campaign ID", "Error",
                    JOptionPane.WARNING_MESSAGE);
            return;

        } else if (workerId.length() < 1) {
            JOptionPane.showMessageDialog(this, "Please introduce the Worker ID", "Error",
                    JOptionPane.WARNING_MESSAGE);
            return;
        }

        campaignInput.setEnabled(false);
        workerInput.setEnabled(false);
        urlInput.setEnabled(false);
        button.setEnabled(false);

        progress.setValue(0);
        progress.setIndeterminate(true);

        try {
            String url = urlInput.getText();
            if (url.endsWith("/") == false) {
                url += "/";
            }
            test(url + campaignId + "/" + workerId);

            /**/
        } catch (Exception e) {
            campaignInput.setEnabled(true);
            workerInput.setEnabled(true);
            urlInput.setEnabled(true);

            progress.setIndeterminate(false);
            progress.setValue(0);

            JOptionPane.showMessageDialog(this, "Invalid url: " + e.getMessage(), "Error",
                    JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_buttonMouseReleased

    private void campaignInputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campaignInputActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_campaignInputActionPerformed

    private void workerInputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_workerInputActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_workerInputActionPerformed

    private void urlInputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_urlInputActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_urlInputActionPerformed

    private void vcodeOutputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_vcodeOutputActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_vcodeOutputActionPerformed

    public void test(String URL) throws IOException, MalformedURLException {

        Thread worker = new Thread() {
            public void run() {
                String error = null;

                try {
                    int http = 0;
                    int h2 = 0;

                    URL entry = new URL(URL);
                    Request request = new Request.Builder().url(entry.toString()).build();

                    Response response = httpClient.newCall(request).execute();
                    log("Getting urls to test from: " + entry.toString());

                    JSONObject json = new JSONObject(response.body().string());

                    JSONArray urls = json.getJSONArray("urls");
                    progress.setMaximum(urls.length());
                    progress.setIndeterminate(false);

                    SocketFactory factory = SSLSocketFactory.getDefault();
                    JSONArray certs = new JSONArray();

                    for (int i = 0; i < urls.length(); ++i) {
                        URL url = new URL(urls.getString(i));

                        if ("https".equals(url.getProtocol())) {
                            certs.put(getCert(factory, url));
                        }

                        response = request(httpClient, url);
                        if (response != null) {
                            http++;
                        }

                        response = request(http2Client, url);
                        if (response != null) {
                            h2++;
                        }
                        progress.setValue(progress.getValue() + 1);
                    }

                    URL finish = new URL(entry.getProtocol(), entry.getHost(), entry.getPort(),
                            json.getString("finish"));

                    json = new JSONObject();
                    json.put("certs", certs);

                    response = request(httpClient, finish, json.toString());
                    json = new JSONObject(response.body().string());

                    String vcode = json.getString("vcode");
                    if (vcode.length() > 0) {
                        vcodeLabel.setText("Please, copy the VCODE:");
                        vcodeOutput.setText(vcode);
                        vcodeOutput.setEnabled(true);
                    }

                    JOptionPane.showMessageDialog(Container.this,
                            "Successful requests: " + "\nHTTP/1.1: " + Integer.toString(http) + "/"
                                    + Integer.toString(urls.length()) + "\nHTTP/2:    " + Integer.toString(h2) + "/"
                                    + Integer.toString(urls.length()),
                            "Completed!", JOptionPane.INFORMATION_MESSAGE);

                } catch (Exception e) {
                    error = e.getMessage();
                }

                if (error != null) {
                    System.out.println(error);
                    JOptionPane.showMessageDialog(Container.this, "Error: " + error, "Error",
                            JOptionPane.ERROR_MESSAGE);

                    progress.setIndeterminate(false);
                    progress.setValue(0);
                }

                campaignInput.setEnabled(true);
                workerInput.setEnabled(true);
                urlInput.setEnabled(true);
                button.setEnabled(true);
            }
        };

        worker.start();
    }

    private Response request(OkHttpClient client, URL url) {
        return request(client, url, "");
    }

    private Response request(OkHttpClient client, URL url, String json) {
        Response response = null;

        try {
            log("Requesting: " + client.getProtocols().get(0) + " => " + url.toString());

            Builder builder = new Request.Builder().url(url.toString());
            if (!"".equals(json)) {
                builder.post(RequestBody.create(MediaType.parse("application/json"), json));
            }
            Request request = builder.build();

            response = client.newCall(request).execute();

            log("Completed: " + response.code());

        } catch (ConnectException e) {
            log("\n" + "Failed: " + e.getMessage());
        } catch (IOException e) {
            log("Failed: " + e.getMessage());
        }

        return response;
    }

    private JSONObject getCert(SocketFactory factory, URL url) {
        JSONObject json = new JSONObject();
        json.put("host", url.getHost());
        json.put("port", url.getPort());

        try {
            log("Get Certs: " + url.getHost() + ":" + url.getPort());

            SSLSocket socket = (SSLSocket) factory.createSocket(url.getHost(), url.getPort());
            socket.startHandshake();

            Certificate[] certs = socket.getSession().getPeerCertificates();
            String result = "";

            for (Certificate cert : certs) {

                if (cert instanceof X509Certificate) {
                    try {
                        ((X509Certificate) cert).checkValidity();
                        result += "OK ";

                    } catch (CertificateExpiredException cee) {
                        result += "Expired ";
                    } catch (CertificateNotYetValidException ex) {
                        result += "NotYetValid ";
                    }
                }
            }

            log("Result: " + result.trim());
            json.put("result", result.trim());

        } catch (SSLException se) {
            log("Error: SSLException (" + se.getMessage() + ")");
            json.put("result", "SSLException: " + se.getMessage());
        } catch (ConnectException ce) {
            log("Error: ConnectException (" + ce.getMessage() + ")");
            json.put("result", "ConnectException: " + ce.getMessage());
        } catch (IOException ioe) {
            log("Error: IOException (" + ioe.getMessage() + ")");
            json.put("result", "IOException: " + ioe.getMessage());
        }

        return json;
    }

    private void log(String log) {
        System.out.println(log);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Container.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Container.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Container.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Container.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Container().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton button;
    private javax.swing.JTextField campaignInput;
    private javax.swing.JLabel campaignLabel;
    private javax.swing.JProgressBar progress;
    private javax.swing.JTextField urlInput;
    private javax.swing.JLabel urlLabel;
    private javax.swing.JLabel vcodeLabel;
    private javax.swing.JTextField vcodeOutput;
    private javax.swing.JTextField workerInput;
    private javax.swing.JLabel workerlLabel;
    // End of variables declaration//GEN-END:variables
}