|
| 1 | +package com.revature.controllers; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | + |
| 6 | +import javax.servlet.http.HttpServletRequest; |
| 7 | +import javax.servlet.http.HttpServletResponse; |
| 8 | +import javax.servlet.http.HttpSession; |
| 9 | + |
| 10 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 11 | +import com.revature.models.LoginDTO; |
| 12 | +import com.revature.services.LoginService; |
| 13 | + |
| 14 | +public class LoginController { |
| 15 | + |
| 16 | + private static final LoginService ls = new LoginService(); |
| 17 | + private static final ObjectMapper om = new ObjectMapper(); |
| 18 | + |
| 19 | + public void login(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| 20 | + |
| 21 | + if(req.getMethod().equals("POST")) { |
| 22 | + BufferedReader reader = req.getReader(); |
| 23 | + |
| 24 | + StringBuilder s = new StringBuilder(); |
| 25 | + |
| 26 | + String line = reader.readLine(); |
| 27 | + |
| 28 | + while(line != null) { |
| 29 | + s.append(line); |
| 30 | + line=reader.readLine(); |
| 31 | + } |
| 32 | + |
| 33 | + String body = new String(s); |
| 34 | + |
| 35 | + System.out.println(body); |
| 36 | + |
| 37 | + LoginDTO l = om.readValue(body, LoginDTO.class); |
| 38 | + |
| 39 | +// res.getWriter().println(l.username+" "+l.password); |
| 40 | + |
| 41 | + |
| 42 | + if(ls.login(l).yn) { |
| 43 | + //Note - the no args getSession method will create a new session if one does not already exist for this client. |
| 44 | + HttpSession ses = req.getSession(); |
| 45 | + ses.setAttribute("user", l); |
| 46 | + ses.setAttribute("loggedin", true); |
| 47 | + res.setStatus(200); |
| 48 | + res.getWriter().println("Login Successful!"); |
| 49 | + } else { |
| 50 | + //will only return a session if one is already associated with the request, will not create a new one. |
| 51 | + HttpSession ses = req.getSession(false); |
| 52 | + if(ses!=null) { |
| 53 | + //This will throw out the session, the client will no longer have a session associated with their cookie. |
| 54 | + ses.invalidate(); |
| 55 | + } |
| 56 | + res.setStatus(401); |
| 57 | + res.getWriter().println("Login Failed"); |
| 58 | + } |
| 59 | + } else if (req.getMethod().equals("GET") |
| 60 | + &&(req.getParameterMap().containsKey("username") |
| 61 | + &&(req.getParameterMap().containsKey("password")))) { |
| 62 | + |
| 63 | + LoginDTO l = new LoginDTO(); |
| 64 | + l.username = req.getParameter("username"); |
| 65 | + l.password = req.getParameter("password"); |
| 66 | + |
| 67 | + if(ls.login(l).yn) { |
| 68 | + //Note - the no args getSession method will create a new session if one does not already exist for this client. |
| 69 | + HttpSession ses = req.getSession(); |
| 70 | + ses.setAttribute("user", l); |
| 71 | + ses.setAttribute("loggedin", true); |
| 72 | + res.setStatus(200); |
| 73 | + res.getWriter().println("Login Successful!"); |
| 74 | + } else { |
| 75 | + //will only return a session if one is already associated with the request, will not create a new one. |
| 76 | + HttpSession ses = req.getSession(false); |
| 77 | + if(ses!=null) { |
| 78 | + //This will throw out the session, the client will no longer have a session associated with their cookie. |
| 79 | + ses.invalidate(); |
| 80 | + } |
| 81 | + res.setStatus(401); |
| 82 | + res.getWriter().println("Login Failed"); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + public void logout(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| 88 | + HttpSession ses = req.getSession(false); |
| 89 | + |
| 90 | + if(ses!=null) { |
| 91 | + LoginDTO l = (LoginDTO) ses.getAttribute("user"); |
| 92 | + ses.invalidate(); |
| 93 | + res.setStatus(200); |
| 94 | + res.getWriter().println(l.username+" you logged out."); |
| 95 | + } else { |
| 96 | + res.setStatus(400); |
| 97 | + res.getWriter().println("You must be logged in to log out."); |
| 98 | + } |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments