1 18 19 package cowsultants.itracker.web.servlets; 20 21 import java.io.*; 22 import java.util.*; 23 import javax.servlet.*; 24 import javax.servlet.http.*; 25 26 import org.apache.struts.Globals; 27 import org.apache.struts.action.ActionErrors; 28 29 import cowsultants.itracker.ejb.client.models.*; 30 import cowsultants.itracker.ejb.client.util.*; 31 import cowsultants.itracker.web.util.*; 32 33 public abstract class GenericController extends HttpServlet { 34 protected void saveErrors(HttpServletRequest request, ActionErrors errors) { 35 36 if ((errors == null) || errors.isEmpty()) { 37 request.removeAttribute(Globals.ERROR_KEY); 38 return; 39 } 40 request.setAttribute(Globals.ERROR_KEY, errors); 41 } 42 43 protected boolean hasPermission(int[] permissionsNeeded, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { 44 if(isLoggedIn(request, response)) { 45 HttpSession session = request.getSession(false); 46 HashMap permissions = (session == null ? null : (HashMap) session.getAttribute("permissions")); 47 if(! UserUtilities.hasPermission(permissions, permissionsNeeded)) { 48 forward("/unauthorized.jsp", request, response); 49 return false; 50 } 51 return true; 52 } 53 return false; 54 } 55 56 protected boolean hasPermission(int permissionNeeded, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { 57 if(isLoggedIn(request, response)) { 58 HttpSession session = request.getSession(false); 59 HashMap permissions = (session == null ? null : (HashMap) session.getAttribute("permissions")); 60 if(! UserUtilities.hasPermission(permissions, permissionNeeded)) { 61 forward("/unauthorized.jsp", request, response); 62 return false; 63 } 64 return true; 65 } 66 return false; 67 } 68 69 protected boolean isLoggedIn(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { 70 HttpSession session = request.getSession(false); 71 UserModel user = (session == null ? null : (UserModel) session.getAttribute("user")); 72 String login = (user == null ? null : user.getLogin()); 73 74 if(login == null || "".equals(login)) { 75 return false; 76 } 77 return true; 78 } 79 80 protected boolean isLoggedInWithRedirect(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { 81 if(! isLoggedIn(request, response)) { 82 String requestPath = request.getRequestURI(); 83 if(! requestPath.endsWith("/login.jsp")) { 84 String redirectURL = request.getRequestURI().substring(request.getContextPath().length()); 85 forward("/login.jsp?" + Constants.AUTH_REDIRECT_KEY + "=" + redirectURL, request, response); 86 } 87 return false; 88 } 89 return true; 90 } 91 92 protected void forward(String url, HttpServletRequest request, HttpServletResponse response) 93 throws IOException, ServletException { 94 RequestDispatcher rd = request.getRequestDispatcher(url); 95 if(rd == null) { 96 throw new ServletException("RequestDispatcher is null. URL: " + url); 97 } 98 99 rd.forward(request, response); 100 } 101 102 protected void redirect(String url, HttpServletRequest request, HttpServletResponse response) 103 throws IOException, ServletException { 104 105 String baseURL = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + 106 request.getContextPath(); 107 108 response.sendRedirect(baseURL + url); 109 } 110 111 } 112 | Popular Tags |