forked from comixed/comixed
/
AuditLogController.java
150 lines (137 loc) · 5.52 KB
/
AuditLogController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* ComiXed - A digital comic book library management application.
* Copyright (C) 2020, The ComiXed Project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses>
*/
package org.comixedproject.controller.core;
import com.fasterxml.jackson.annotation.JsonView;
import java.util.Date;
import java.util.List;
import lombok.extern.log4j.Log4j2;
import org.comixedproject.auditlog.AuditableEndpoint;
import org.comixedproject.controller.ComiXedControllerException;
import org.comixedproject.model.auditlog.RestAuditLogEntry;
import org.comixedproject.model.net.ApiResponse;
import org.comixedproject.model.net.GetRestAuditLogResponse;
import org.comixedproject.model.net.GetTaskAuditLogResponse;
import org.comixedproject.model.tasks.TaskAuditLogEntry;
import org.comixedproject.service.ComiXedServiceException;
import org.comixedproject.service.auditlog.RestAuditLogService;
import org.comixedproject.service.task.TaskService;
import org.comixedproject.views.View;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/**
* <code>AuditLogController</code> provides REST APIs for interacting with the tasks system.
*
* @author Darryl L. Pierce
*/
@RestController
@Log4j2
public class AuditLogController {
@Autowired private TaskService taskService;
@Autowired private RestAuditLogService restAuditLogService;
/**
* Retrieve the list of log entries after the cutoff time.
*
* @param cutoff the cutoff
* @return the log entries
*/
@GetMapping(value = "/api/tasks/entries/{cutoff}", produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasRole('ADMIN')")
@JsonView(View.AuditLogEntryList.class)
@AuditableEndpoint
public ApiResponse<GetTaskAuditLogResponse> getAllTaskEntriesAfterDate(
@PathVariable("cutoff") final Long timestamp) throws ComiXedControllerException {
ApiResponse<GetTaskAuditLogResponse> response = new ApiResponse<>();
final Date cutoff = new Date(timestamp);
log.debug("Getting all task audit log entries after: {}", cutoff);
try {
final List<TaskAuditLogEntry> entries = this.taskService.getAuditLogEntriesAfter(cutoff);
response.setResult(new GetTaskAuditLogResponse());
response.getResult().setEntries(entries);
if (!entries.isEmpty())
response.getResult().setLatest(entries.get(entries.size() - 1).getStartTime());
response.setSuccess(true);
} catch (ComiXedServiceException error) {
log.error("Failed to load task audit log entries", error);
response.setSuccess(false);
response.setError(error.getMessage());
response.setThrowable(error);
}
return response;
}
/**
* Endpoint for clearing the task log.
*
* @return the success of the operation
*/
@DeleteMapping(value = "/api/tasks/entries", produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasRole('ADMIN')")
@JsonView(View.ApiResponse.class)
@AuditableEndpoint
public ApiResponse<Void> clearTaskAuditLog() {
log.debug("Clearing task audit log");
final ApiResponse<Void> response = new ApiResponse<>();
try {
this.taskService.clearTaskAuditLog();
response.setSuccess(true);
} catch (Exception error) {
log.error("Failed to clear audit log", error);
response.setSuccess(false);
response.setError(error.getMessage());
response.setThrowable(error);
}
return response;
}
/**
* Returns the list of REST audit log entries that ended after the given date.
*
* <p><b>NOTE:</b> this endpoint is <em>NOT</em> audited since it would create an infinite
* feedback loop.
*
* @param cutoff the cutoff timestamp
* @return the list of entries
*/
@GetMapping(
value = "/api/auditing/rest/entries/{cutoff}",
produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasRole('ADMIN')")
@JsonView(View.AuditLogEntryList.class)
public ApiResponse<GetRestAuditLogResponse> getAllRestEntriesAfterDate(
@PathVariable("cutoff") final Long cutoff) {
log.info("Getting all REST audit entries since {}", cutoff);
ApiResponse<GetRestAuditLogResponse> response = new ApiResponse<>();
try {
final List<RestAuditLogEntry> result = this.restAuditLogService.getEntriesAfterDate(cutoff);
response.setSuccess(true);
response.setResult(new GetRestAuditLogResponse());
response.getResult().setEntries(result);
response
.getResult()
.setLatest(result.isEmpty() ? new Date() : result.get(result.size() - 1).getEndTime());
} catch (Exception error) {
response.setSuccess(false);
response.setError(error.getMessage());
response.setThrowable(error);
}
return response;
}
}