Advertisement
Guest User

Untitled

a guest
Jul 25th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 5.57 KB | None | 0 0
  1. package controller;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.security.MessageDigest;
  6. import java.security.NoSuchAlgorithmException;
  7. import java.sql.SQLException;
  8. import java.util.LinkedList;
  9.  
  10. import javax.servlet.RequestDispatcher;
  11. import javax.servlet.ServletException;
  12. import javax.servlet.annotation.WebServlet;
  13. import javax.servlet.http.HttpServlet;
  14. import javax.servlet.http.HttpServletRequest;
  15. import javax.servlet.http.HttpServletResponse;
  16. import javax.servlet.http.HttpSession;
  17.  
  18. import org.json.simple.JSONObject;
  19. import org.json.simple.parser.JSONParser;
  20.  
  21. import model.EventModelDS;
  22. import model.ModelInterface;
  23. import model.User;
  24. import model.UserModelDS;
  25.  
  26. /**
  27.  * Servlet implementation class UserControl
  28.  */
  29. @WebServlet("/UserController")
  30. public class UserController extends HttpServlet {
  31.  
  32.     private static final long serialVersionUID = 1L;
  33.     static ModelInterface<User> model = new UserModelDS();
  34.     private UserModelDS modelDs = (UserModelDS) model;
  35.    
  36.  
  37.     public UserController() {
  38.         super();
  39.     }
  40.  
  41.    
  42.     private void sendError(String error,HttpServletResponse response) throws IOException{
  43.       PrintWriter out = response.getWriter();
  44.           out.println(error);
  45.     }
  46.    
  47.    
  48.      public static String toSHA1(byte[] convertme) {
  49.          MessageDigest md = null;
  50.          try {
  51.              md = MessageDigest.getInstance("SHA-1");
  52.          }
  53.          catch(NoSuchAlgorithmException e) {
  54.              e.printStackTrace();
  55.          }
  56.          return new String(md.digest(convertme));
  57.      }
  58.    
  59.    
  60.    
  61.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  62.             throws ServletException, IOException {
  63.             doPost(request, response);
  64.     }
  65.    
  66.  
  67.     @SuppressWarnings("unchecked")
  68.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  69.             throws ServletException, IOException {
  70.  
  71.  
  72.         String action = request.getParameter("action");
  73.  
  74.         try {
  75.             if (action != null)
  76.             {
  77.                 if (action.equalsIgnoreCase("insert"))
  78.                 {
  79.                     String  user     = request.getParameter("email");
  80.                     String  password = request.getParameter("password");
  81. //                  Integer priority = null;
  82. //                  if (request.getParameter("priority") != null) {
  83. //                      priority = Integer.parseInt(request.getParameter("priority"));
  84. //                  }
  85.                     User bean = new User();
  86.                     bean.setUser(user);
  87.                     bean.setPassword(password);
  88. //                  if (priority != null)   bean.setPriority(priority);
  89.                     UserModelDS modelDs = (UserModelDS) model;
  90.                     if(modelDs.findByName(user) != null )
  91.                     {
  92.                         sendError("Questo account è già esistente", response);
  93.                         return;
  94.                     }
  95.                     if(model.insert(bean) == false)
  96.                     {
  97.                         sendError("Errore di Connessione", response);
  98.                         return;
  99.                     }
  100.                 }
  101.                
  102.                 else if(action.equalsIgnoreCase("update"))
  103.                 {
  104.                     String newPassword = request.getParameter("newPassword");
  105.                     JSONObject obj =  toJson(newPassword);
  106.                     User user = new User();
  107.                    
  108.                     if(obj != null){
  109.                         user.setPassword(newPassword);
  110.                         if(user.getId() == 3){
  111.                             int priority = Integer.parseInt(request.getParameter("priority"));
  112.                             user.setPriority(priority);
  113.                         }
  114.                     }
  115.                     if(!model.update(user))
  116.                             sendError("Errore di Connessione", response);          
  117.                 }
  118.                
  119.                 else if(action.equalsIgnoreCase("login"))
  120.                 {
  121.                       HttpSession session = request.getSession();
  122.                       LinkedList<Integer> eventsPreferred = new LinkedList<Integer>();
  123.                       LinkedList<Integer> eventsLiked = new LinkedList<Integer>();
  124.                      
  125.                       session.setAttribute("eventsPreferred", eventsPreferred);
  126.                       session.setAttribute("eventsLiked",eventsLiked);
  127.                      
  128.                       String email      = (String) request.getParameter("email");
  129.                       String password   = (String) request.getParameter("password");
  130.                       User user = modelDs.findByName(email);
  131.                       if(user == null)
  132.                       {
  133.                           sendError("Utente non esiste.", response);
  134.                           return;
  135.                       }
  136.                       String DbPassword = user.getPassword();
  137.                       String hashed     = toSHA1(password.getBytes());
  138.                       if(DbPassword != null)
  139.                       {
  140.                        if(DbPassword.equals(hashed))
  141.                        {
  142.                            if(user.getPriority() == 3)
  143.                            {
  144.                                RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/admin/Admin.jsp");
  145.                                dispatcher.forward(request,response);
  146.                            }
  147.                           session.setAttribute("userId", user.getId());
  148.                        }
  149.                        else sendError("Credenziali non valide." , response);
  150.                       }
  151.                 }
  152.                 else if(action.equalsIgnoreCase("logout"))
  153.                 {
  154.                         HttpSession session = request.getSession();
  155.                         int idUser = (int) session.getAttribute("userId");
  156.                         LinkedList<Integer> liked =  (LinkedList<Integer>) session.getAttribute("eventsLiked");
  157.                         LinkedList<Integer> preferred =  (LinkedList<Integer>) session.getAttribute("eventsPreferred");
  158.                         EventModelDS es = new EventModelDS();
  159.                         if(liked.size() != 0){
  160.                             es.setLikeToEvent(idUser, liked);
  161.                         }
  162.                         if(preferred.size() != 0){
  163.                             es.setEventAsPreferred(idUser, preferred);
  164.                         }
  165.                      
  166.                 }
  167.                 else if(action.equalsIgnoreCase("remove")){
  168.                     int id = request.getIntHeader("id");
  169.                     if(!model.remove(id))
  170.                         sendError("Errore DataBase", response);
  171.                 }
  172.             }
  173.         } catch (SQLException e) {
  174.             //System.out.println("Error:" + e.getMessage());
  175.         }
  176.  
  177.     }
  178.     private JSONObject toJson(String convertJson){
  179.         JSONParser parser = new JSONParser();
  180.  
  181.         try {
  182.             Object obj = parser.parse(convertJson);
  183.             JSONObject jsonObject = (JSONObject) obj;
  184.             return jsonObject;
  185.         }
  186.         catch(Exception e){
  187.             return null;               
  188.         }
  189.     }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement