1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.itracker.web.actions.base;
19
20 import org.apache.log4j.Level;
21 import org.apache.log4j.Logger;
22 import org.apache.struts.action.Action;
23 import org.itracker.model.PermissionType;
24 import org.itracker.services.ITrackerServices;
25 import org.itracker.web.util.LoginUtilities;
26 import org.itracker.web.util.RequestHelper;
27 import org.itracker.web.util.ServletContextUtils;
28
29 import javax.servlet.ServletException;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32 import javax.servlet.http.HttpSession;
33 import java.io.IOException;
34 import java.net.MalformedURLException;
35 import java.net.URL;
36 import java.util.Date;
37 import java.util.Locale;
38 import java.util.Map;
39 import java.util.Set;
40
41
42
43
44
45 public abstract class ItrackerBaseAction extends Action {
46
47 private static final Logger log = Logger
48 .getLogger(ItrackerBaseAction.class);
49
50 public ItrackerBaseAction() {
51 super();
52 }
53
54
55
56
57
58 protected Map<Integer, Set<PermissionType>> getUserPermissions(
59 HttpSession session) {
60 return RequestHelper.getUserPermissions(session);
61 }
62
63
64
65
66 protected boolean hasPermission(PermissionType permissionNeeded,
67 HttpServletRequest request, HttpServletResponse response)
68 throws IOException, ServletException {
69 return LoginUtilities.hasPermission(permissionNeeded, request, response);
70 }
71
72 @Deprecated
73 protected boolean hasPermission(int permissionNeeded,
74 HttpServletRequest request, HttpServletResponse response)
75 throws IOException, ServletException {
76 return LoginUtilities.hasPermission(permissionNeeded, request, response);
77 }
78
79
80
81
82
83
84 public String getBaseURL(HttpServletRequest request) {
85
86 String url = getITrackerServices().getConfigurationService().getSystemBaseURL();
87 if (null == url) {
88 url = new StringBuffer(request.getScheme()).append("://").append(
89 request.getServerName()).append(":").append(
90 request.getServerPort()).append(request.getContextPath())
91 .toString();
92
93 log
94 .warn("getBaseURL: no base-url is configured, determin from request. ("
95 + url + ")");
96 }
97 try {
98 return new URL(url).toExternalForm();
99 } catch (MalformedURLException e) {
100 log.warn("failed to get URL normalized, returning manual url: "
101 + url, e);
102 }
103 return url;
104 }
105
106
107
108
109 protected ITrackerServices getITrackerServices() {
110 ITrackerServices itrackerServices = ServletContextUtils
111 .getItrackerServices();
112 return itrackerServices;
113 }
114
115
116 @Override
117 public Locale getLocale(HttpServletRequest request) {
118 Locale locale = super.getLocale(request);
119 if (null == locale) {
120 locale = LoginUtilities.getCurrentLocale(request);
121 }
122 return locale;
123 }
124
125
126
127
128
129 protected static void logTimeMillies(String message, Date startTime, Logger log,
130 Level level) {
131 if (log.isEnabledFor(level)) {
132 long milliesStart = startTime.getTime();
133 long milliesEnd = System.currentTimeMillis();
134 if (null == log) {
135 log = ItrackerBaseAction.log;
136 }
137 if (null == level) {
138 level = Level.INFO;
139 }
140
141 log.log(level, new StringBuilder().append("logTimeMillies: ").append(
142 message).append(" took ").append(milliesEnd - milliesStart)
143 .append("ms.").toString());
144
145
146 startTime.setTime(System.currentTimeMillis());
147 }
148 }
149
150 }