1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.itracker.web.servlets;
20
21 import org.apache.commons.lang.StringUtils;
22 import org.apache.log4j.Logger;
23 import org.apache.struts.Globals;
24 import org.apache.struts.action.ActionErrors;
25 import org.itracker.model.PermissionType;
26 import org.itracker.model.User;
27 import org.itracker.services.ITrackerServices;
28 import org.itracker.model.util.UserUtilities;
29 import org.itracker.web.util.Constants;
30 import org.itracker.web.util.LoginUtilities;
31 import org.itracker.web.util.RequestHelper;
32 import org.itracker.web.util.ServletContextUtils;
33
34 import javax.servlet.RequestDispatcher;
35 import javax.servlet.ServletContext;
36 import javax.servlet.ServletException;
37 import javax.servlet.http.HttpServlet;
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpServletResponse;
40 import javax.servlet.http.HttpSession;
41 import java.io.IOException;
42 import java.util.Locale;
43 import java.util.Map;
44 import java.util.Set;
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 public abstract class GenericController extends HttpServlet {
61
62
63
64
65 private static final long serialVersionUID = 1L;
66 private static final Logger logger = Logger.getLogger(GenericController.class);
67
68 public GenericController() {
69 }
70
71 protected static void saveMessages(HttpServletRequest request, ActionErrors errors) {
72
73 if ((errors == null) || errors.isEmpty()) {
74 request.removeAttribute(Globals.ERROR_KEY);
75 return;
76 }
77 request.setAttribute(Globals.ERROR_KEY, errors);
78 }
79
80 protected static void forward(String url, HttpServletRequest request, HttpServletResponse response)
81 throws IOException, ServletException {
82 RequestDispatcher rd = request.getRequestDispatcher(url);
83 if (rd == null) {
84 throw new ServletException("RequestDispatcher is null. URL: " + url);
85 }
86
87 rd.forward(request, response);
88 }
89
90 protected static void redirect(String url, HttpServletRequest request, HttpServletResponse response)
91 throws IOException, ServletException {
92
93 String baseURL = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() +
94 request.getContextPath();
95
96 response.sendRedirect(baseURL + url);
97 }
98
99 public static Locale getLocale(HttpServletRequest request) {
100 return LoginUtilities.getCurrentLocale(request);
101 }
102
103
104 }