Advertisement
Guest User

Untitled

a guest
Nov 28th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. package controller;
  2.  
  3. import DAO.StudentDAO;
  4. import DAO.WebTemplateDao;
  5. import UI.UI;
  6. import DAO.ConnectDB;
  7. import com.sun.net.httpserver.HttpExchange;
  8. import com.sun.net.httpserver.HttpHandler;
  9. import controller.helpers.HashSystem;
  10. import controller.helpers.ParseForm;
  11. import models.Student;
  12.  
  13. import java.io.*;
  14. import java.net.HttpCookie;
  15. import java.net.URLDecoder;
  16. import java.security.NoSuchAlgorithmException;
  17. import java.sql.ResultSet;
  18. import java.sql.SQLException;
  19. import java.util.HashMap;
  20. import java.util.Map;
  21. import java.util.UUID;
  22.  
  23. public class MainController implements HttpHandler {
  24.  
  25. public void handle(HttpExchange httpExchange) throws IOException {
  26. WebTemplateDao webTemplateDao = new WebTemplateDao();
  27. String response = "";
  28. String method = httpExchange.getRequestMethod();
  29.  
  30. if (method.equals("GET")) {
  31. response = webTemplateDao.getSiteTemplate("static/login-page.html");
  32. httpExchange.sendResponseHeaders(200, response.length());
  33. OutputStream os = httpExchange.getResponseBody();
  34. os.write(response.getBytes());
  35. os.close();
  36. }
  37.  
  38. if (method.equals("POST")) {
  39. String user = null;
  40. try {
  41. user = getUserType(httpExchange);
  42. } catch (SQLException e) {
  43. e.printStackTrace();
  44. } catch (NoSuchAlgorithmException e) {
  45. e.printStackTrace();
  46. }
  47. if(user.equals("Admin")){
  48. httpExchange.getResponseHeaders().set("Location", "/admin");
  49. httpExchange.sendResponseHeaders(302, -1);
  50. } else if (user.equals("Mentor")) {
  51. httpExchange.getResponseHeaders().set("Location", "/mentor");
  52. httpExchange.sendResponseHeaders(302, -1);
  53. }
  54. else if (user.equals("Student")) {
  55. httpExchange.getResponseHeaders().set("Location", "/student");
  56. httpExchange.sendResponseHeaders(302, -1);
  57. }
  58.  
  59. }
  60. }
  61.  
  62. public String loginToSystem(String login,String passwordGet) throws SQLException,NoSuchAlgorithmException{
  63.  
  64. String password = HashSystem.getStringFromSHA256(passwordGet);
  65. ConnectDB connectDB = DAO.ConnectDB.getInstance();
  66. String sql = String.format("SELECT * FROM users WHERE email like '%s' and password like '%s'",login,password);
  67. ResultSet result = connectDB.getResult(sql);
  68.  
  69. if (result.next()) {
  70. if (result.getString("role").equals("student")) {
  71. return "Student";
  72. }
  73.  
  74. if (result.getString("role").equals("admin")) {
  75. return "Admin";
  76. }
  77.  
  78. if (result.getString("role").equals("mentor")) {
  79. return "Mentor";
  80. }
  81. }
  82.  
  83. return "backToLogin";
  84. }
  85.  
  86. private String generateSessionID(){
  87. UUID SessionID = UUID.randomUUID();
  88. return String.valueOf(SessionID);
  89. }
  90.  
  91. private String getUserType(HttpExchange httpExchange) throws IOException, SQLException, NoSuchAlgorithmException {
  92. InputStreamReader isr = new InputStreamReader(httpExchange.getRequestBody(),
  93. "utf-8");
  94. BufferedReader br = new BufferedReader(isr);
  95. String formData = br.readLine();
  96. Map<String,String> inputs = ParseForm.parseFormData(formData);
  97. String login = inputs.get("login");
  98. String password = inputs.get("password");
  99.  
  100. String userType = loginToSystem(login,password);
  101. createSessions(httpExchange,login,password,userType);
  102. return userType;
  103. }
  104.  
  105. private void createSessions(HttpExchange httpExchange, String login, String password, String user) throws SQLException, NoSuchAlgorithmException {
  106. String sessionID = generateSessionID();
  107. HttpCookie cookie = new HttpCookie("sessionId", sessionID);
  108. httpExchange.getResponseHeaders().add("Set-Cookie", cookie.toString());
  109. String getId = String.format("SELECT id FROM users WHERE email like '%s' and password like '%s'",login, HashSystem.getStringFromSHA256(password));
  110. String sql = String.format("INSERT INTO sessions values('%s','%s',(%s))",sessionID,user,getId);
  111. ConnectDB connectDB = ConnectDB.getInstance();
  112. connectDB.addRecord(sql);
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement