8 changed files with 246 additions and 11 deletions
			
			
		| @ -0,0 +1,25 @@ | |||
| /* | |||
|  * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 | |||
|  * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 | |||
|  */ | |||
| package hola.token; | |||
| 
 | |||
| import java.security.SecureRandom; | |||
| 
 | |||
| /** | |||
|  * | |||
|  * @author chore | |||
|  */ | |||
| public class TokenGenerator { | |||
|     private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~"; | |||
|     private static final int TOKEN_LENGTH = 50; | |||
|     private static final SecureRandom secureRandom = new SecureRandom(); | |||
| 
 | |||
|     public static String generateToken() { | |||
|         StringBuilder token = new StringBuilder(TOKEN_LENGTH); | |||
|         for (int i = 0; i < TOKEN_LENGTH; i++) { | |||
|             token.append(CHARACTERS.charAt(secureRandom.nextInt(CHARACTERS.length()))); | |||
|         } | |||
|         return token.toString(); | |||
|     } | |||
| } | |||
| @ -0,0 +1,160 @@ | |||
| /* | |||
|  * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 | |||
|  * Click nbfs://nbhost/SystemFileSystem/Templates/JSP_Servlet/Servlet.java to edit this template
 | |||
|  */ | |||
| package hola.servicios; | |||
| 
 | |||
| import hola.BL.TipoBLLocal; | |||
| import hola.dao.UsuarioDAO; | |||
| import hola.modelo.Tipo; | |||
| import hola.modelo.Usuario; | |||
| import hola.msg.Mensaje; | |||
| import java.io.IOException; | |||
| import java.io.PrintWriter; | |||
| import java.util.List; | |||
| import javax.ejb.EJB; | |||
| import javax.servlet.ServletException; | |||
| import javax.servlet.annotation.WebServlet; | |||
| import javax.servlet.http.HttpServlet; | |||
| import javax.servlet.http.HttpServletRequest; | |||
| import javax.servlet.http.HttpServletResponse; | |||
| 
 | |||
| /** | |||
|  * | |||
|  * @author chore | |||
|  */ | |||
| @WebServlet(name = "TipoEndpoint", urlPatterns = {"/TipoEndpoint"}) | |||
| public class TipoEndpoint extends HttpServlet { | |||
| 
 | |||
|     @EJB | |||
|     private TipoBLLocal tipoBL; | |||
| 
 | |||
|     /** | |||
|      * Processes requests for both HTTP <code>GET</code> and <code>POST</code> | |||
|      * methods. | |||
|      * | |||
|      * @param request servlet request | |||
|      * @param response servlet response | |||
|      * @throws ServletException if a servlet-specific error occurs | |||
|      * @throws IOException if an I/O error occurs | |||
|      */ | |||
|      | |||
|      | |||
|     protected void processRequest(HttpServletRequest request, HttpServletResponse response) | |||
|             throws ServletException, IOException { | |||
|         response.setContentType("text/html;charset=UTF-8"); | |||
|         try (PrintWriter out = response.getWriter()) { | |||
|             /* TODO output your page here. You may use following sample code. */ | |||
|              | |||
|             // Obtener el token de la solicitud
 | |||
|             String token = request.getParameter("token"); | |||
| 
 | |||
|             // Verificar si el token es válido
 | |||
|             UsuarioDAO usuarioDAO = new UsuarioDAO(); | |||
|             Usuario usuario = new Usuario(); | |||
|             usuario.setToken(token); | |||
|             String s = usuarioDAO.tokenEnd(token); | |||
|             System.out.println(s); | |||
| 
 | |||
|             if (s == null) { | |||
|                 // Token no válido
 | |||
|                 out.println("<h1>Token no valido</h1>"); | |||
|                 //response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Token no válido");
 | |||
|                 List<Tipo> listaTipo = tipoBL.buscarValidos(true); | |||
|                 if (listaTipo != null && !listaTipo.isEmpty()) { | |||
|                     for (Tipo tipo : listaTipo) { | |||
|                         out.println("<p>" + tipo.getId() + " --- " +  tipo.getNombre() + "</p>"); | |||
|                     } | |||
|                 } | |||
| 
 | |||
|                 return; | |||
|             } | |||
| 
 | |||
|             Tipo tipo = new Tipo(); | |||
| 
 | |||
|             String opc = request.getParameter("op"); | |||
|             String id = request.getParameter("id"); | |||
|             String nombre = request.getParameter("nombre"); | |||
|             | |||
|             Mensaje mensaje = null; | |||
| 
 | |||
|             if (opc != null) { | |||
| 
 | |||
|                 switch (Integer.parseInt(opc)) { | |||
|                     case 1: | |||
|                         tipo.setNombre(nombre); | |||
|                         mensaje = tipoBL.agregar(tipo); | |||
|                          | |||
|                         break; | |||
|                     case 2: | |||
| 
 | |||
|                         tipo.setId(Integer.valueOf(id)); | |||
| 
 | |||
|                         tipo.setNombre(nombre); | |||
| 
 | |||
|                         mensaje = tipoBL.editar(tipo); | |||
|                         break; | |||
| 
 | |||
|                     case 3: | |||
| 
 | |||
|                         tipo.setId(Integer.valueOf(id)); | |||
| 
 | |||
|                         mensaje = tipoBL.eliminarId(tipo); | |||
| 
 | |||
|                         break; | |||
|                     default: | |||
|                         throw new AssertionError(); | |||
|                 } | |||
|             } | |||
| 
 | |||
|             List<Tipo> listaTipo = tipoBL.buscarValidos(true); | |||
|              | |||
|             if (listaTipo != null && !listaTipo.isEmpty()) { | |||
|                 for (Tipo tipos : listaTipo) { | |||
|                     out.println("<p>" + tipos.getId() + " --- " +  tipos.getNombre() + "</p>"); | |||
|                 } | |||
|                 out.println("<h>"+mensaje+"</h>" ); | |||
|             } | |||
|         } | |||
|     } | |||
| 
 | |||
|     // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
 | |||
|     /** | |||
|      * Handles the HTTP <code>GET</code> method. | |||
|      * | |||
|      * @param request servlet request | |||
|      * @param response servlet response | |||
|      * @throws ServletException if a servlet-specific error occurs | |||
|      * @throws IOException if an I/O error occurs | |||
|      */ | |||
|     @Override | |||
|     protected void doGet(HttpServletRequest request, HttpServletResponse response) | |||
|             throws ServletException, IOException { | |||
|         processRequest(request, response); | |||
|     } | |||
| 
 | |||
|     /** | |||
|      * Handles the HTTP <code>POST</code> method. | |||
|      * | |||
|      * @param request servlet request | |||
|      * @param response servlet response | |||
|      * @throws ServletException if a servlet-specific error occurs | |||
|      * @throws IOException if an I/O error occurs | |||
|      */ | |||
|     @Override | |||
|     protected void doPost(HttpServletRequest request, HttpServletResponse response) | |||
|             throws ServletException, IOException { | |||
|         processRequest(request, response); | |||
|     } | |||
| 
 | |||
|     /** | |||
|      * Returns a short description of the servlet. | |||
|      * | |||
|      * @return a String containing servlet description | |||
|      */ | |||
|     @Override | |||
|     public String getServletInfo() { | |||
|         return "Short description"; | |||
|     }// </editor-fold>
 | |||
| 
 | |||
| } | |||
					Loading…
					
					
				
		Reference in new issue