Samuel Gamez
6 months ago
9 changed files with 302 additions and 14 deletions
@ -0,0 +1,26 @@ |
|||
/* |
|||
* 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 xforce.token; |
|||
|
|||
import java.security.SecureRandom; |
|||
|
|||
/** |
|||
* |
|||
* @author Samuel Gamez |
|||
*/ |
|||
public class TokenGenerator { |
|||
|
|||
private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~"; |
|||
private static final int TOKEN_LENGTH = 88; |
|||
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,155 @@ |
|||
/* |
|||
* 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 xforce.servicios; |
|||
|
|||
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; |
|||
import xforce.bl.PaisesBLLocal; |
|||
import xforce.dao.LoginDAO; |
|||
import xforce.modelo.Paises; |
|||
import xforce.modelo.Usuarios; |
|||
import xforce.msg.Mensaje; |
|||
|
|||
/** |
|||
* |
|||
* @author Samuel Gamez |
|||
*/ |
|||
@WebServlet(name = "PaisesEndpoint", urlPatterns = {"/PaisesEndpoint"}) |
|||
public class PaisesEndpoint extends HttpServlet { |
|||
|
|||
@EJB |
|||
private PaisesBLLocal paisesBL; |
|||
|
|||
/** |
|||
* 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
|
|||
LoginDAO loginDAO = new LoginDAO(); |
|||
Usuarios usuario = new Usuarios(); |
|||
usuario.setToken(token); |
|||
String s = loginDAO.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<Paises> listaPaises = paisesBL.buscarValidos(true); |
|||
if (listaPaises != null && !listaPaises.isEmpty()) { |
|||
for (Paises pais : listaPaises) { |
|||
out.println("<p>" + pais.getNombre() + "</p>"); |
|||
} |
|||
} |
|||
|
|||
return; |
|||
} |
|||
|
|||
Paises paises = new Paises(); |
|||
|
|||
String opc = request.getParameter("opc"); |
|||
String id = request.getParameter("id"); |
|||
String nombre = request.getParameter("nombre"); |
|||
Mensaje m = null; |
|||
|
|||
if (opc != null) { |
|||
|
|||
switch (Integer.parseInt(opc)) { |
|||
case 1: |
|||
paises.setNombre(nombre); |
|||
m = paisesBL.agregar(paises); |
|||
break; |
|||
case 2: |
|||
|
|||
paises.setId(Integer.valueOf(id)); |
|||
|
|||
paises.setNombre(nombre); |
|||
|
|||
m = paisesBL.editar(paises); |
|||
break; |
|||
|
|||
case 3: |
|||
|
|||
paises.setId(Integer.valueOf(id)); |
|||
|
|||
m = paisesBL.eliminar(paises); |
|||
|
|||
break; |
|||
default: |
|||
throw new AssertionError(); |
|||
} |
|||
} |
|||
|
|||
List<Paises> listaPaises = paisesBL.buscarValidos(true); |
|||
out.print("<h1>" + m + "</h1>"); |
|||
if (listaPaises != null && !listaPaises.isEmpty()) { |
|||
for (Paises pais : listaPaises) { |
|||
out.println("<p>" + pais.getNombre() + "</p>"); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
// <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