Java tutorial
import java.util.logging.Level; import java.util.logging.Logger; import java.security.KeyStore; import org.bouncycastle.cms.CMSSignedDataGenerator; import org.bouncycastle.util.encoders.Base64; /* * 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. */ /** * * @author robsoncardozo */ public class buttonsFrame extends javax.swing.JFrame { /** * Creates new form buttonsFrame */ public buttonsFrame() { initComponents(); } /** * 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() { buttonSolicitar = new javax.swing.JButton(); validateDesafio = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); textDesafio = new javax.swing.JTextArea(); textResposta = new javax.swing.JTextField(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); buttonSolicitar.setText("SOLICITAR DESAFIO"); buttonSolicitar.setToolTipText(""); buttonSolicitar.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { buttonSolicitarMouseClicked(evt); } }); buttonSolicitar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonSolicitarActionPerformed(evt); } }); validateDesafio.setText("ASSINAR DESAFIO E VALIDAR"); validateDesafio.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { validateDesafioMouseClicked(evt); } }); textDesafio.setColumns(20); textDesafio.setRows(5); jScrollPane1.setViewportView(textDesafio); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(111, 111, 111).addComponent(validateDesafio) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addComponent( textResposta, javax.swing.GroupLayout.PREFERRED_SIZE, 455, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup().addGap(115, 115, 115).addComponent( buttonSolicitar, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(0, 0, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap().addComponent(jScrollPane1))) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addComponent(buttonSolicitar, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE) .addComponent(validateDesafio, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(textResposta, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(25, 25, 25))); pack(); }// </editor-fold>//GEN-END:initComponents //AO DO BOTO "SOLICITAR DESAFIO" private void buttonSolicitarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_buttonSolicitarMouseClicked try { //COLOCA O TEXTO NA TELA DE ACORDO COM O RETORNO DADO EM sb.toString(); Conexao conexao = new Conexao(); textDesafio.setText(conexao.requisicaoGet("https://api-prova.lab.ca.inf.br:9445/desafio")); } catch (Exception ex) { Logger.getLogger(buttonsFrame.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_buttonSolicitarMouseClicked private void validateDesafioMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_validateDesafioMouseClicked try { //INSTANCIA A CLASSE PARA ASSINATURA PKCS7 assinaBc signer = new assinaBc(); KeyStore keyStore = signer.loadKeyStore(); CMSSignedDataGenerator signatureGenerator = signer.setUpProvider(keyStore); Conexao conexao = new Conexao(); byte[] signedBytes = signer.signPkcs7(Conexao.convertStringToByteArray(), signatureGenerator);//ASSINA O BYTE ARRAY CONVERTIDO DE separaJson String recebeCrip = new String(Base64.encode(signedBytes));//Atribui a uma string o byte array assinado String assinaturaFormat = recebeCrip.replaceAll("(.{64})", "$1\n");//quebra automatica a cada 64 caracteres String resposta = ("-----BEGIN PKCS7-----\n" + assinaturaFormat + "\n-----END PKCS7-----");//Add o cabealho PKCS7 na assinatura //System.out.println(resposta); TransformaJson<Resposta> tJson = new TransformaJson<Resposta>(); Resposta resp = new Resposta(resposta); org.json.JSONObject json = tJson.converteJSON(resp); String reposta = conexao.requisicaoPost("https://api-prova.lab.ca.inf.br:9445/desafio", json); textResposta.setText(conexao.imprimeResultado()); } catch (Exception ex) { Logger.getLogger(buttonsFrame.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_validateDesafioMouseClicked private void buttonSolicitarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSolicitarActionPerformed }//GEN-LAST:event_buttonSolicitarActionPerformed /** * @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 ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(buttonsFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(buttonsFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(buttonsFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(buttonsFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { @Override public void run() { } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton buttonSolicitar; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextArea textDesafio; private javax.swing.JTextField textResposta; private javax.swing.JButton validateDesafio; // End of variables declaration//GEN-END:variables }