/
TaskController.java
104 lines (94 loc) · 3.72 KB
/
TaskController.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
/*
* 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.tasks;
import com.fasterxml.jackson.annotation.JsonView;
import java.util.Date;
import java.util.List;
import lombok.extern.log4j.Log4j2;
import org.comixedproject.controller.ComiXedControllerException;
import org.comixedproject.model.tasks.TaskAuditLogEntry;
import org.comixedproject.net.ApiResponse;
import org.comixedproject.net.GetTaskAuditLogResponse;
import org.comixedproject.repositories.tasks.TaskAuditLogRepository;
import org.comixedproject.service.ComiXedServiceException;
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.*;
/**
* <code>TaskController</code> provides REST APIs for interacting with the tasks system.
*
* @author Darryl L. Pierce
*/
@RestController
@RequestMapping(value = "/api/tasks")
@Log4j2
public class TaskController {
@Autowired private TaskAuditLogRepository taskAuditLogRepository;
@Autowired private TaskService taskService;
/**
* Retrieve the list of log entries after the cutoff time.
*
* @param cutoff the cutoff
* @return the log entries
*/
@GetMapping(value = "/entries/{cutoff}", produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasRole('ADMIN')")
@JsonView(View.TaskAuditLogEntryList.class)
public ApiResponse<GetTaskAuditLogResponse> getAllAfterDate(
@PathVariable("cutoff") final Long timestamp) throws ComiXedControllerException {
ApiResponse<GetTaskAuditLogResponse> result = 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);
result.setResult(new GetTaskAuditLogResponse());
result.getResult().setEntries(entries);
result.getResult().setLatest(entries.get(entries.size() - 1).getStartTime());
result.setSuccess(true);
} catch (ComiXedServiceException error) {
log.error("Failed to load task audit log entries", error);
result.setSuccess(false);
result.setError(error.getMessage());
}
return result;
}
/**
* Endpoint for clearing the task log.
*
* @return the success of the operation
*/
@DeleteMapping(value = "/entries", produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasRole('ADMIN')")
@JsonView(View.ApiResponse.class)
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());
}
return response;
}
}