dbd730d1353ddcdda34d6c740a3bf5f9a87ff114
[simdecs.git] / src / java / org / ufcspa / simdecs / comunicacao / MetodoAutenticacao.java
1 /*\r
2  * To change this template, choose Tools | Templates\r
3  * and open the template in the editor.\r
4  */\r
5 package org.ufcspa.simdecs.comunicacao;\r
6 \r
7 import java.io.IOException;\r
8 import java.io.PrintWriter;\r
9 import javax.servlet.ServletException;\r
10 import javax.servlet.http.HttpServlet;\r
11 import javax.servlet.http.HttpServletRequest;\r
12 import javax.servlet.http.HttpServletResponse;\r
13 import javax.servlet.*;\r
14 import java.sql.*;\r
15 import javax.persistence.EntityManager;\r
16 import javax.persistence.EntityManagerFactory;\r
17 import javax.persistence.NoResultException;\r
18 import javax.persistence.Persistence;\r
19 import javax.servlet.http.*;\r
20 import org.ufcspa.simdecs.entidade.Log;\r
21 import org.ufcspa.simdecs.entidade.Usuario;\r
22 \r
23 /**\r
24  *\r
25  * @author pbarros\r
26  * testye\r
27  */\r
28 public class MetodoAutenticacao extends HttpServlet {\r
29 \r
30     /**\r
31      * Processes requests for both HTTP\r
32      * <code>GET</code> and\r
33      * <code>POST</code> methods.\r
34      *\r
35      * @param request servlet request\r
36      * @param response servlet response\r
37      * @throws ServletException if a servlet-specific error occurs\r
38      * @throws IOException if an I/O error occurs\r
39      */\r
40     private ServletConfig config;\r
41     public void init(ServletConfig config)\r
42             throws ServletException{\r
43         this.config=config;\r
44     }\r
45     protected void processRequest(HttpServletRequest request, HttpServletResponse response)\r
46             throws ServletException, IOException {\r
47         \r
48         PrintWriter out = response.getWriter();\r
49         // alteracao solicitado mosmmann \r
50         //response.setContentType("text/html;charset=UTF-8");\r
51         \r
52         response.setContentType( "text/xml" );\r
53         response.setHeader( "Cache-Control", "no-cache" );\r
54         ///para devolver arquivo anexo\r
55         //response.addHeader("Content-Disposition", "attachment; filename="+ "teste");\r
56         //response.setContentType("text/html;charset=UTF-8");\r
57         \r
58         //metodo antigo\r
59         //PrintWriter out = response.getWriter();\r
60         EntityManagerFactory emf = Persistence.createEntityManagerFactory("simdecsPU");\r
61         EntityManager em = emf.createEntityManager();\r
62         out.println( "<xml>" );\r
63         \r
64         Usuario user;\r
65         try {\r
66                 user = (Usuario) em.createQuery("select usu from Usuario as usu where usu.login=:login and usu.senha=:senha")\r
67                                                                                 .setParameter("login", request.getParameter("user"))\r
68                                                                                 .setParameter("senha", request.getParameter("pass"))\r
69                                                                                 .getSingleResult();     \r
70                 \r
71                 out.println( "<id>" + String.valueOf(user.getId()) + "</id>" );\r
72 //                out.println("OK "+ user.getId());\r
73 //                out.close();\r
74         } catch (NoResultException ne) {\r
75             \r
76 //              request.setAttribute("id","0");         \r
77 //              out.println("Usuario ou senha Inválido ");\r
78                 out.println( "<id>" + "0" + "</id>" );\r
79                 \r
80         } finally {         \r
81             out.println( "</xml>" );\r
82             out.close();\r
83         }\r
84     }\r
85 \r
86     // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">\r
87     /**\r
88      * Handles the HTTP\r
89      * <code>GET</code> method.\r
90      *\r
91      * @param request servlet request\r
92      * @param response servlet response\r
93      * @throws ServletException if a servlet-specific error occurs\r
94      * @throws IOException if an I/O error occurs\r
95      */\r
96     @Override\r
97     protected void doGet(HttpServletRequest request, HttpServletResponse response)\r
98             throws ServletException, IOException {\r
99         processRequest(request, response);\r
100     }\r
101 \r
102     /**\r
103      * Handles the HTTP\r
104      * <code>POST</code> method.\r
105      *\r
106      * @param request servlet request\r
107      * @param response servlet response\r
108      * @throws ServletException if a servlet-specific error occurs\r
109      * @throws IOException if an I/O error occurs\r
110      */\r
111     @Override\r
112     protected void doPost(HttpServletRequest request, HttpServletResponse response)\r
113             throws ServletException, IOException {\r
114         processRequest(request, response);\r
115     }\r
116 \r
117     /**\r
118      * Returns a short description of the servlet.\r
119      *\r
120      * @return a String containing servlet description\r
121      */\r
122     @Override\r
123     public String getServletInfo() {\r
124         return "Short description";\r
125     }// </editor-fold>\r
126 }\r