×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Dorian Cuentas
Added: Jul 2, 2015 9:09 PM
Views: 11
Tags: no tags
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package integracion.tarjeta.de.coordenadas;
  7.  
  8. import com.senhaforte.action.Utils;
  9. import com.senhaforte.classes.ChallengeRetorno;
  10. import com.senhaforte.classes.DBc;
  11. import com.senhaforte.classes.Grupo;
  12. import com.senhaforte.classes.Retorno;
  13. import com.senhaforte.classes.Ticket;
  14. import com.senhaforte.classes.Usuario;
  15. import com.senhaforte.security.Challenge;
  16. import com.sun.media.sound.ReferenceCountingDevice;
  17. import java.awt.Color;
  18. import java.awt.Graphics2D;
  19. import java.awt.GridBagLayout;
  20. import java.awt.Rectangle;
  21. import java.awt.image.BufferedImage;
  22. import java.awt.image.RenderedImage;
  23. import java.io.File;
  24. import java.io.IOException;
  25. import java.io.OutputStream;
  26. import java.util.Arrays;
  27. import java.util.Scanner;
  28. import java.util.logging.Level;
  29. import java.util.logging.Logger;
  30. import javax.imageio.ImageIO;
  31. import javax.imageio.stream.FileImageOutputStream;
  32. import javax.imageio.stream.ImageOutputStream;
  33. import javax.imageio.stream.MemoryCacheImageOutputStream;
  34. import javax.swing.JEditorPane;
  35. import javax.swing.JLabel;
  36. import javax.swing.JPanel;
  37. import javax.swing.JTextArea;
  38. import javax.swing.SwingUtilities;
  39.  
  40. /**
  41.  *
  42.  * @author user
  43.  */
  44. public class Autenticación {
  45.  
  46.     /**
  47.      *
  48.      * @param gridArray
  49.      * @return styles from http://www.tablesgenerator.com/html_tables
  50.      */
  51.     public static StringBuffer createHtmlGrid(String[] gridCharArray) {
  52.         final StringBuffer htmlStringBuffer = new StringBuffer();
  53.         htmlStringBuffer.append("<html>");
  54.         htmlStringBuffer.append(
  55.                 "<style type=\"text/css\">"
  56.                 + ".table width:700px;height:200px"
  57.                         + ".tg  {height:10px;border-collapse:collapse;border-spacing:0;border-color:#aabcfe;}"
  58.                 + ".tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#aabcfe;color:#669;background-color:#e8edff;}"
  59.                 + ".tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#aabcfe;color:#039;background-color:#b9c9fe;}"
  60.                 + ".tg .tg-s6z2{text-align:center}"
  61.                 + ".tg .tg-igvl{background-color:#b9c9fe;color:#003399;text-align:center}"
  62.                 + "</style>"
  63.                 + "<table class=\"tg\">"
  64.                 + "  <tr>"
  65.                 + "    <th class=\"tg-s6z2\"></th>"
  66.                 + "    <th class=\"tg-s6z2\">A<br></th>"
  67.                 + "    <th class=\"tg-s6z2\">B</th>"
  68.                 + "    <th class=\"tg-s6z2\">C</th>"
  69.                 + "    <th class=\"tg-s6z2\">D</th>"
  70.                 + "    <th class=\"tg-s6z2\">E</th>"
  71.                 + "    <th class=\"tg-s6z2\">F</th>"
  72.                 + "    <th class=\"tg-s6z2\">G</th>"
  73.                 + "    <th class=\"tg-s6z2\">H</th>"
  74.                 + "    <th class=\"tg-s6z2\">I</th>"
  75.                 + "    <th class=\"tg-s6z2\">J</th>"
  76.                 + "  </tr>"
  77.                 + "");
  78.  
  79.         for (int i = 0; i < gridCharArray.length; i++) {
  80.             if (((i) % 10 == 0)) {
  81.                 if (i > 9) {
  82.                     htmlStringBuffer.append("  </tr>");
  83.                 }
  84.                 htmlStringBuffer.append(
  85.                         "  <tr>" + "    <td class=\"tg-igvl\">" + String.valueOf((i / 10) + 1) + "</td>");
  86.             }
  87.             htmlStringBuffer.append("    <td class=\"tg-s6z2\">" + gridCharArray[i] + "</td>");
  88.         }
  89.         htmlStringBuffer.append("</table>");
  90.         htmlStringBuffer.append("</html>");
  91.         return htmlStringBuffer;
  92.     }
  93.  
  94.     /**
  95.      *
  96.      * @param imageOutputStream
  97.      * @param htmlGrid
  98.      */
  99.     public static void createImageGrid(final ImageOutputStream imageOutputStream, String htmlGrid, final int with, final int height) {
  100.         final JPanel containerComponent = new JPanel();
  101.         containerComponent.setBackground(Color.WHITE);
  102.         JLabel htmlTextComponent = new JLabel();
  103.         htmlTextComponent.setBackground(Color.WHITE);
  104.         htmlTextComponent.setText(htmlGrid);
  105.         htmlTextComponent.repaint();
  106.         containerComponent.setLayout(new GridBagLayout());
  107.         containerComponent.add(htmlTextComponent);
  108.         containerComponent.setSize(with, height);
  109.         containerComponent.validate();
  110.  
  111.         //create a new image
  112.         final BufferedImage image = new BufferedImage(containerComponent.getWidth(), containerComponent.getHeight(), BufferedImage.TYPE_INT_ARGB);
  113.  
  114.         SwingUtilities.invokeLater(new Runnable() {
  115.  
  116.             @Override
  117.             public void run() {
  118.                 try {
  119. //                    SwingUtilities.paintComponent(image.createGraphics(), jPanel, new JPanel(), new Rectangle(0, 0, jPanel.getWidth(), jPanel.getHeight()));
  120.                     containerComponent.setSize(with, height);
  121.                     containerComponent.validate();
  122.                     containerComponent.doLayout();
  123.                     Graphics2D g = image.createGraphics();
  124.                     containerComponent.print(g);
  125.                     g.dispose();
  126.  
  127.                     ImageIO.write((RenderedImage) image, "png", imageOutputStream);
  128.                 } catch (IOException ex) {
  129.                     Logger.getLogger(Autenticación.class.getName()).log(Level.SEVERE, null, ex);
  130.                 }
  131.             }
  132.         });
  133.     }
  134.  
  135.     public static void main(String[] args) throws IOException {
  136.         Grupo g = Grupo.BuscaGrupo("NotSel", "NotSelADM", "VdJRYHFK");  //Esta linha adiciona suas credenciais
  137.         DBc c = new DBc(); //Cria conexao com nosso banco de dados
  138.         Usuario user = Usuario.buscaUsuario(g, "dcuentas", c);
  139.  
  140.         String cartaoGrid = user.getCartaoGrid();
  141.         final String[] charArray = cartaoGrid.split(",");
  142.         final StringBuffer stringBuffer = new StringBuffer();
  143.  
  144.         stringBuffer.append("\tA\tB\tC\tD\tE\tF\tG\tH\tI\tJ");
  145. //        System.out.print("1\t");
  146.         for (int i = 0; i < charArray.length; i++) {
  147.             if (((i) % 10 == 0)) {
  148.                 stringBuffer.append("");
  149.                 stringBuffer.append(String.valueOf((i / 10) + 1) + "\t");
  150.             }
  151.             stringBuffer.append(charArray[i] + "\t");
  152.         }
  153.         System.out.println(stringBuffer.toString());
  154.        
  155.         StringBuffer htmlGrid = createHtmlGrid(charArray);
  156.         createImageGrid(new FileImageOutputStream(new File("C:\\Users\\user\\Desktop\\tarjeta.png")), htmlGrid.toString(), 500, 400);
  157.  
  158. //        jPanel.paintComponents(image.createGraphics());
  159.         ChallengeRetorno cr = Challenge.GeraChallenge(g, "dcuentas", c);
  160.         String[] desafio = cr.getDesafio();
  161.         System.out.println("");
  162.         System.out.println("Desafio criado a partir de Grid - " + cr.getSerial_grid());
  163.         System.out.println("Responda ao desafio:");
  164.         System.out.println("");
  165.         System.out.println("ingrese las coordenadas : " + desafio[0] + " - " + desafio[1] + " - " + desafio[2]);
  166.  
  167. //        String geraDesafio = Challenge.geraDesafio();
  168. //        System.out.println("desafio : " + geraDesafio);
  169. //        System.out.println("");
  170. //        final int[][] coords = new int[][]{{1, 2}, {1, 2}, {1, 3}};
  171. //        char[] BuscaValorGrid = Challenge.BuscaValorGrid(coords, user);
  172. //        System.out.println(Arrays.toString(BuscaValorGrid));
  173.         System.out.println("");
  174.         Scanner in = new Scanner(System.in);
  175.         char[] segredos = new char[3];
  176.         segredos[0] = in.nextLine().charAt(0);
  177.         segredos[1] = in.nextLine().charAt(0);
  178.         segredos[2] = in.nextLine().charAt(0);
  179.  
  180.         System.out.println("enviando coordenadas : " + segredos[0] + " - " + segredos[1] + " - " + segredos[2]);
  181.         boolean ret = Challenge.RespostaChallenge(c.CriaConn(), segredos[0], segredos[1], segredos[2], user);
  182.  
  183.         System.out.println("autenticación : " + ret);
  184.     }
  185. }
  186.