Package com.arrgsocal.servlets

Source Code of com.arrgsocal.servlets.LoginServlet

package com.arrgsocal.servlets;

import java.io.IOException;
import java.util.ArrayList;
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 com.arrgsocal.entities.ArrgError;
import com.arrgsocal.entities.User;
import com.arrgsocal.managers.UserManager;

/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;
  @EJB
  UserManager userManager;

  /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
   *      response)
   */
  protected void doGet(HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException {
    if (request.getRemoteUser() == null) {
      request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request,
          response);
    } else {
      response.sendRedirect("me");
    }
  }

  /**
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
   *      response)
   */
  protected void doPost(HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException {
    try {
      if (request.getRemoteUser() != null) {
        // Handle login
        request.logout();
      }

      // Get the logged in user
      List<User> users = userManager.getUsers();
      User u = null;
      for (User u1 : users) {
        if (u1.getName().equals(request.getParameter("username"))) {
          u = u1;
          break;
        }
      }

      if (u == null) {
        List<ArrgError> errors = getErrorList(request);

        ArrgError e = new ArrgError();
        e.setMessage("No user with that username exists");
        errors.add(e);
        request.setAttribute("errors", errors);

        request.getRequestDispatcher("/WEB-INF/login.jsp").forward(
            request, response);
        return;
      }

      request.login(request.getParameter("username"),
          request.getParameter("password"));

      // Create the session as neccesary and set the current user to the
      // one that just logged in.
      request.getSession(true).setAttribute("currentUser", u);
      response.sendRedirect("me");
    } catch (ServletException e) {
      e.printStackTrace();
      List<ArrgError> errors = getErrorList(request);
      ArrgError error = new ArrgError();
      error.setMessage("Couldn't log you in, please try again");
      errors.add(error);
      request.setAttribute("errors", errors);
      request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request,
          response);
    }
  }

  @SuppressWarnings("unchecked")
  private List<ArrgError> getErrorList(HttpServletRequest request) {
    List<ArrgError> errors;
    if (request.getAttribute("errors") != null) {
      errors = (List<ArrgError>) request.getAttribute("errors");
    } else {
      errors = new ArrayList<>();
    }
    return errors;
  }

}
TOP

Related Classes of com.arrgsocal.servlets.LoginServlet

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.