Java tutorial
/* * Copyright (C) 2014 jorjoluiso * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ package firmadigital; import firmadigital.util.ArchivoUtils; import java.io.File; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.filechooser.FileNameExtensionFilter; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.PropertiesConfiguration; import recepcion.ws.sri.gob.ec.RespuestaSolicitud; /** * * @author jorjoluiso */ public class Firma extends javax.swing.JFrame { /** * Creates new form Firma */ static String Argumento; public Firma() { 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() { cmdExaminar = new javax.swing.JButton(); cmdFirmar = new javax.swing.JButton(); cmdVerificar = new javax.swing.JButton(); txtUbicacion = new javax.swing.JTextField(); lblNombreArchivo = new javax.swing.JLabel(); lblRutaArchivo = new javax.swing.JLabel(); lblMensaje = new javax.swing.JLabel(); lblIcon = new javax.swing.JLabel(); jMenuBar = new javax.swing.JMenuBar(); jMenuFile = new javax.swing.JMenu(); menParametros = new javax.swing.JMenuItem(); menSalir = new javax.swing.JMenuItem(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Firma Digital"); setBackground(new java.awt.Color(204, 255, 204)); addComponentListener(new java.awt.event.ComponentAdapter() { public void componentShown(java.awt.event.ComponentEvent evt) { formComponentShown(evt); } }); cmdExaminar.setBackground(new java.awt.Color(204, 255, 204)); cmdExaminar.setText("Examinar"); cmdExaminar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cmdExaminarActionPerformed(evt); } }); cmdFirmar.setBackground(new java.awt.Color(0, 255, 204)); cmdFirmar.setText("Firmar"); cmdFirmar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cmdFirmarActionPerformed(evt); } }); cmdVerificar.setBackground(new java.awt.Color(255, 102, 102)); cmdVerificar.setText("Verificar"); cmdVerificar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cmdVerificarActionPerformed(evt); } }); txtUbicacion.setEditable(false); lblIcon.setFont(new java.awt.Font("Brush Script MT", 0, 48)); // NOI18N lblIcon.setIcon(new javax.swing.ImageIcon("X:\\Java\\FirmaDigital1.1\\src\\firmadigital\\emacs.png")); // NOI18N lblIcon.setText("Firma Digital"); jMenuFile.setText("Archivo"); menParametros.setText("Parmetros"); menParametros.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { menParametrosActionPerformed(evt); } }); jMenuFile.add(menParametros); menSalir.setText("Salir"); menSalir.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { menSalirActionPerformed(evt); } }); jMenuFile.add(menSalir); jMenuBar.add(jMenuFile); setJMenuBar(jMenuBar); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(lblMensaje, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(lblRutaArchivo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(lblNombreArchivo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(txtUbicacion, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 560, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(cmdExaminar, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(cmdFirmar, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(cmdVerificar, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(lblIcon, javax.swing.GroupLayout.PREFERRED_SIZE, 491, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(0, 0, Short.MAX_VALUE))) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(24, 24, 24) .addComponent(lblIcon, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(txtUbicacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(39, 39, 39) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(cmdFirmar).addComponent(cmdVerificar).addComponent(cmdExaminar)) .addGap(45, 45, 45).addComponent(lblNombreArchivo) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(lblRutaArchivo).addGap(41, 41, 41).addComponent(lblMensaje) .addContainerGap(32, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void cmdExaminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdExaminarActionPerformed JFileChooser fileChooser = new JFileChooser(); txtUbicacion.setText(""); lblNombreArchivo.setText(""); lblRutaArchivo.setText(""); String signFileName = txtUbicacion.getText(); File directory = new File(signFileName).getParentFile(); fileChooser.setCurrentDirectory(directory); FileNameExtensionFilter filter; filter = new FileNameExtensionFilter("XML file", "xml"); fileChooser.setFileFilter(filter); fileChooser.setFileHidingEnabled(true); /*Remove All File option*/ fileChooser.setAcceptAllFileFilterUsed(false); if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { String selectedFile = fileChooser.getSelectedFile().getAbsolutePath(); txtUbicacion.setText(selectedFile); lblNombreArchivo.setText(fileChooser.getSelectedFile().getName()); lblRutaArchivo.setText(fileChooser.getSelectedFile().getParent()); } }//GEN-LAST:event_cmdExaminarActionPerformed private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown //System.out.print(Argumento); if (!Argumento.equals("")) { txtUbicacion.setText(Argumento); File archivo = new File(Argumento); lblNombreArchivo.setText(archivo.getName()); lblRutaArchivo.setText(archivo.getPath().substring(0, archivo.getPath().lastIndexOf(File.separator))); System.out.println( "Ruta " + archivo.getPath().substring(0, archivo.getPath().lastIndexOf(File.separator))); System.out.println("Nombre Archivo" + archivo.getName()); } setEnlace(); }//GEN-LAST:event_formComponentShown private void menSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menSalirActionPerformed System.exit(1); }//GEN-LAST:event_menSalirActionPerformed private void setEnlace() { JLabelLink link = new JLabelLink(); link.setSize(300, 670); link.setText("www.quijotelu.com"); link.setLink("www.quijotelu.com"); link.setTextLink("www.quijotelu.com"); link.setFont(new java.awt.Font("Brush Script MT", 0, 28)); link.setForeground(new java.awt.Color(0, 0, 153)); this.add(link); } private void cmdFirmarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdFirmarActionPerformed try { PropertiesConfiguration config = new PropertiesConfiguration("FirmaDigital.properties"); if (config.getProperty("certificado.almacen") == null) { JOptionPane.showMessageDialog(null, "Primero debe ingrear a la pantalla de parmetros"); return; } } catch (ConfigurationException ex) { Logger.getLogger(Firma.class.getName()).log(Level.SEVERE, null, ex); } File folder = new File("C:\\fd\\Firmados"); folder.mkdirs(); if (!lblRutaArchivo.getText().isEmpty() && !lblNombreArchivo.getText().isEmpty()) { if (XAdESBESSignature.firmar(lblRutaArchivo.getText() + File.separator + lblNombreArchivo.getText(), lblNombreArchivo.getText(), "C:\\fd\\Firmados")) { lblRutaArchivo.setText("C:\\fd\\Firmados"); txtUbicacion.setText(lblRutaArchivo.getText() + File.separator + lblNombreArchivo.getText()); JOptionPane.showMessageDialog(null, "Firmado con xito"); } } else { JOptionPane.showMessageDialog(null, "Tiene que especificar un archivo"); } }//GEN-LAST:event_cmdFirmarActionPerformed private void cmdVerificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdVerificarActionPerformed RespuestaSolicitud respuestaRecepcion; PropertiesConfiguration config; String claveAccesoComprobante = null; try { config = new PropertiesConfiguration("FirmaDigital.properties"); if (config.getProperty("web.web_service") == null) { JOptionPane.showMessageDialog(null, "Primero debe ingrear a la pantalla de parmetros"); return; } if (!lblRutaArchivo.getText().isEmpty() && !lblNombreArchivo.getText().isEmpty()) { File archivo = new File(lblRutaArchivo.getText(), lblNombreArchivo.getText()); claveAccesoComprobante = ArchivoUtils.obtenerValorXML(archivo, "/*/infoTributaria/claveAcceso"); String tipoComprobante = ArchivoUtils.obtenerValorXML(archivo, "/*/infoTributaria/codDoc") .substring(1); String rucEmisor = ArchivoUtils.obtenerValorXML(archivo, "/*/infoTributaria/ruc"); if ((tipoComprobante != null) && (claveAccesoComprobante != null)) { respuestaRecepcion = EnvioComprobantesWs.obtenerRespuestaEnvio(archivo, rucEmisor, tipoComprobante, claveAccesoComprobante, config.getProperty("web.web_service").toString()); JOptionPane.showMessageDialog(null, respuestaRecepcion.getEstado()); } } else { JOptionPane.showMessageDialog(null, "Tiene que especificar un archivo"); } } catch (ConfigurationException ex) { Logger.getLogger(Firma.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_cmdVerificarActionPerformed private void menParametrosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menParametrosActionPerformed new Parametros(this, true).setVisible(true); }//GEN-LAST:event_menParametrosActionPerformed /** * @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(Firma.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Firma.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Firma.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Firma.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> if (args.length > 0) { //System.out.print(args[0]); Argumento = args[0].toString(); } else { Argumento = ""; } System.setProperty("javax.net.ssl.keyStore", "C:\\Archivos de programa\\Java\\jre7\\lib\\security\\cacerts"); System.setProperty("javax.net.ssl.keyStorePassword", "changeit"); System.setProperty("javax.net.ssl.trustStore", "C:\\Archivos de programa\\Java\\jre7\\lib\\security\\cacerts"); System.setProperty("javax.net.ssl.trustStorePassword", "changeit"); /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Firma().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton cmdExaminar; private javax.swing.JButton cmdFirmar; private javax.swing.JButton cmdVerificar; private javax.swing.JMenuBar jMenuBar; private javax.swing.JMenu jMenuFile; private javax.swing.JLabel lblIcon; private javax.swing.JLabel lblMensaje; private javax.swing.JLabel lblNombreArchivo; private javax.swing.JLabel lblRutaArchivo; private javax.swing.JMenuItem menParametros; private javax.swing.JMenuItem menSalir; private javax.swing.JTextField txtUbicacion; // End of variables declaration//GEN-END:variables }