@@ -34,8 +34,7 @@ public class AccesslogController extends HttpServlet {
3434
3535 @ Override
3636 public void init () throws ServletException {
37- Connection connection = DIContainer .getConnection ();
38- this .accessLogDAO = new AccessLogDAOImpl (connection );
37+ this .accessLogDAO = DIContainer .get (AccessLogDAO .class );
3938 }
4039
4140 @ Override
@@ -57,23 +56,23 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
5756
5857 User user = (User ) userObj ;
5958 String pathInfo = request .getPathInfo ();
60-
59+
6160 // Validate pathInfo
6261 if (pathInfo == null ) {
6362 pathInfo = "/" ;
6463 }
6564 String acceptHeader = request .getHeader ("Accept" );
6665 boolean isJsonRequest = acceptHeader != null && acceptHeader .contains ("application/json" );
67-
66+
6867 // Check if this is a specific endpoint
6968 if (pathInfo != null && !pathInfo .equals ("/" )) {
7069 if (pathInfo .startsWith ("/user/" )) {
7170 // GET /api/accessLog/user/{userId}
7271 String userIdStr = pathInfo .substring (7 );
7372 try {
7473 int userId = Integer .parseInt (userIdStr );
75- boolean isStaff = "staff" .equalsIgnoreCase (user .getRole ()) ||
76- "admin" .equalsIgnoreCase (user .getRole ());
74+ boolean isStaff = "staff" .equalsIgnoreCase (user .getRole ()) ||
75+ "admin" .equalsIgnoreCase (user .getRole ());
7776 if (isStaff || user .getId () == userId ) {
7877 if (isJsonRequest ) {
7978 getUserAccessLogsJson (response , userId );
@@ -106,8 +105,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
106105 LocalDate startDate = null , endDate = null ;
107106 LocalDate today = LocalDate .now ();
108107
109-
110- //date parsing with error handling
108+ // date parsing with error handling
111109 try {
112110 if (startDateStr != null && !startDateStr .isEmpty ()) {
113111 startDate = LocalDate .parse (startDateStr );
@@ -116,26 +114,26 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
116114 endDate = LocalDate .parse (endDateStr );
117115 }
118116 } catch (DateTimeParseException e ) {
119- utils .ErrorAction .handleValidationError (request , response ,
120- "Date format is invalid. Please use YYYY-MM-DD." , "AccesslogController.doPost" );
117+ utils .ErrorAction .handleValidationError (request , response ,
118+ "Date format is invalid. Please use YYYY-MM-DD." , "AccesslogController.doPost" );
121119 return ;
122120 }
123121
124122 // 3. Date validation
125123 if ((startDate != null && startDate .isAfter (today )) || (endDate != null && endDate .isAfter (today ))) {
126- utils .ErrorAction .handleValidationError (request , response ,
127- "You cannot search for access logs in the future." , "AccesslogController.doPost" );
124+ utils .ErrorAction .handleValidationError (request , response ,
125+ "You cannot search for access logs in the future." , "AccesslogController.doPost" );
128126 return ;
129127 }
130128
131- // the start date is null and end date is not null
129+ // the start date is null and end date is not null
132130 if (startDate == null && endDate != null ) {
133- utils .ErrorAction .handleValidationError (request , response ,
134- "Please select a start date when searching with an end date." , "AccesslogController.doPost" );
131+ utils .ErrorAction .handleValidationError (request , response ,
132+ "Please select a start date when searching with an end date." , "AccesslogController.doPost" );
135133 return ;
136134 }
137135
138- //if the start date is null and end date is null
136+ // if the start date is null and end date is null
139137 if (startDate != null && endDate == null ) {
140138 endDate = today ;
141139 }
@@ -172,34 +170,34 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
172170 if (isJsonRequest ) {
173171 response .setContentType ("application/json" );
174172 response .setCharacterEncoding ("UTF-8" );
175-
173+
176174 JsonObject json = new JsonObject ();
177175 json .addProperty ("success" , true );
178176 json .add ("accessLogs" , gson .toJsonTree (accessLogList != null ? accessLogList : Collections .emptyList ()));
179177 json .addProperty ("count" , accessLogList != null ? accessLogList .size () : 0 );
180-
178+
181179 response .getWriter ().write (gson .toJson (json ));
182180 } else {
183181 request .getRequestDispatcher ("/WEB-INF/views/accessLog.jsp" ).forward (request , response );
184182 }
185183 }
186-
184+
187185 // JSON API methods
188- private void getUserAccessLogsJson (HttpServletResponse response , int userId )
186+ private void getUserAccessLogsJson (HttpServletResponse response , int userId )
189187 throws ServletException , IOException {
190-
188+
191189 response .setContentType ("application/json" );
192190 response .setCharacterEncoding ("UTF-8" );
193-
191+
194192 try {
195193 List <AccessLog > accessLogs = accessLogDAO .getAccessLogsByUserId (userId );
196-
194+
197195 JsonObject json = new JsonObject ();
198196 json .addProperty ("success" , true );
199197 json .add ("accessLogs" , gson .toJsonTree (accessLogs ));
200198 json .addProperty ("count" , accessLogs .size ());
201199 json .addProperty ("userId" , userId );
202-
200+
203201 response .getWriter ().write (gson .toJson (json ));
204202 } catch (SQLException e ) {
205203 logger .log (Level .SEVERE , "Failed to retrieve access logs for userId " + userId , e );
@@ -210,48 +208,58 @@ private void getUserAccessLogsJson(HttpServletResponse response, int userId)
210208 response .getWriter ().write (gson .toJson (json ));
211209 }
212210 }
213-
214- private void searchAccessLogsJson (HttpServletRequest request , HttpServletResponse response , User user )
211+
212+ private void searchAccessLogsJson (HttpServletRequest request , HttpServletResponse response , User user )
215213 throws ServletException , IOException {
216-
214+
217215 response .setContentType ("application/json" );
218216 response .setCharacterEncoding ("UTF-8" );
219-
217+
220218 String userIdStr = request .getParameter ("userId" );
221219 String action = request .getParameter ("action" );
222220 String dateFrom = request .getParameter ("dateFrom" );
223221 String dateTo = request .getParameter ("dateTo" );
224222 String ipAddress = request .getParameter ("ipAddress" );
225-
226- boolean isStaff = "staff" .equalsIgnoreCase (user .getRole ()) ||
227- "admin" .equalsIgnoreCase (user .getRole ());
228-
223+
224+ boolean isStaff = "staff" .equalsIgnoreCase (user .getRole ()) ||
225+ "admin" .equalsIgnoreCase (user .getRole ());
226+
229227 try {
230228 List <AccessLog > accessLogs ;
231-
229+
232230 // If staff/admin, can search all logs; otherwise only own logs
233231 if (isStaff && userIdStr != null && !userIdStr .trim ().isEmpty ()) {
234- int searchUserId = Integer .parseInt (userIdStr );
232+ int searchUserId ;
233+ try {
234+ searchUserId = Integer .parseInt (userIdStr );
235+ } catch (NumberFormatException e ) {
236+ response .setStatus (HttpServletResponse .SC_BAD_REQUEST );
237+ JsonObject json = new JsonObject ();
238+ json .addProperty ("success" , false );
239+ json .addProperty ("error" , "Invalid User ID format" );
240+ response .getWriter ().write (gson .toJson (json ));
241+ return ;
242+ }
235243 accessLogs = accessLogDAO .getAccessLogsByUserId (searchUserId );
236244 } else {
237245 accessLogs = accessLogDAO .getAccessLogsByUserId (user .getId ());
238246 }
239-
247+
240248 // Apply additional filters if provided
241249 if (action != null && !action .trim ().isEmpty ()) {
242250 final String actionFilter = action .trim ();
243251 accessLogs = accessLogs .stream ()
244- .filter (log -> actionFilter .equalsIgnoreCase (log .getAction ()))
245- .collect (java .util .stream .Collectors .toList ());
252+ .filter (log -> actionFilter .equalsIgnoreCase (log .getAction ()))
253+ .collect (java .util .stream .Collectors .toList ());
246254 }
247-
255+
248256 if (ipAddress != null && !ipAddress .trim ().isEmpty ()) {
249257 final String ipFilter = ipAddress .trim ();
250258 accessLogs = accessLogs .stream ()
251- .filter (log -> log .getIpAddress () != null && log .getIpAddress ().contains (ipFilter ))
252- .collect (java .util .stream .Collectors .toList ());
259+ .filter (log -> log .getIpAddress () != null && log .getIpAddress ().contains (ipFilter ))
260+ .collect (java .util .stream .Collectors .toList ());
253261 }
254-
262+
255263 // Date range filtering would require additional DAO methods
256264 // For now, filter in memory if dates provided
257265 if (dateFrom != null && dateTo != null ) {
@@ -261,23 +269,24 @@ private void searchAccessLogsJson(HttpServletRequest request, HttpServletRespons
261269 final LocalDate finalStartDate = startDate ;
262270 final LocalDate finalEndDate = endDate ;
263271 accessLogs = accessLogs .stream ()
264- .filter (log -> {
265- if (log .getTimestamp () == null ) return false ;
266- LocalDate logDate = log .getTimestamp ().toLocalDate ();
267- return (logDate .isEqual (finalStartDate ) || logDate .isAfter (finalStartDate )) &&
268- (logDate .isEqual (finalEndDate ) || logDate .isBefore (finalEndDate ));
269- })
270- .collect (java .util .stream .Collectors .toList ());
272+ .filter (log -> {
273+ if (log .getTimestamp () == null )
274+ return false ;
275+ LocalDate logDate = log .getTimestamp ().toLocalDate ();
276+ return (logDate .isEqual (finalStartDate ) || logDate .isAfter (finalStartDate )) &&
277+ (logDate .isEqual (finalEndDate ) || logDate .isBefore (finalEndDate ));
278+ })
279+ .collect (java .util .stream .Collectors .toList ());
271280 } catch (DateTimeParseException e ) {
272281 // Invalid date format, ignore filter
273282 }
274283 }
275-
284+
276285 JsonObject json = new JsonObject ();
277286 json .addProperty ("success" , true );
278287 json .add ("accessLogs" , gson .toJsonTree (accessLogs ));
279288 json .addProperty ("count" , accessLogs .size ());
280-
289+
281290 response .getWriter ().write (gson .toJson (json ));
282291 } catch (SQLException e ) {
283292 logger .log (Level .SEVERE , "Failed to search access logs" , e );
@@ -289,5 +298,6 @@ private void searchAccessLogsJson(HttpServletRequest request, HttpServletRespons
289298 }
290299 }
291300
292- // POST, PUT, DELETE are not implemented (users cannot edit/delete their access logs)
301+ // POST, PUT, DELETE are not implemented (users cannot edit/delete their access
302+ // logs)
293303}
0 commit comments