KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > web > servlets > GenericController


1 /*
2  * This software was designed and created by Jason Carroll.
3  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4  * The author can be reached at jcarroll@cowsultants.com
5  * ITracker website: http://www.cowsultants.com
6  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it only under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  */

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 JavaDoc 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 JavaDoc requestPath = request.getRequestURI();
83             if(! requestPath.endsWith("/login.jsp")) {
84                 String JavaDoc 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 JavaDoc 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 JavaDoc url, HttpServletRequest request, HttpServletResponse response)
103       throws IOException, ServletException {
104
105         String JavaDoc baseURL = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() +
106                      request.getContextPath();
107
108         response.sendRedirect(baseURL + url);
109     }
110
111 }
112
Popular Tags