Im developing a RESTful application. One of the application task is to return 10 last business oriented REST calls made to the application with request information and time. What is the best way for doing this? I read about Interceptors and Filters. But Im not sure whether it is good idea... Any other ideas or information how to implemment this?
EDIT: I used Actuator. my solution after your responses:
@Service
public class ActuatorTraceService {
private JsonParser jsonParser = new JsonParser();
private List<String> listWithInformationFromTrace = new ArrayList<>();
private JSONArray jsonArrayFromString;
private JSONArray listWithoutRequestAndTracePath;
public void takeInformationFromActuatorTrace() {
jsonArrayFromString = new JSONArray("[{}]");
String urlAddressForTrace = "http://localhost:8080/trace";
jsonArrayFromString = new JSONArray(jsonParser.takeJsonAsAStringFromUrl(urlAddressForTrace));
}
public String createPathForCheck(int i) {
String pathForCheck = jsonArrayFromString
.getJSONObject(i)
.getJSONObject("info")
.get("path")
.toString();
return pathForCheck;
}
public void createListWithoutRequestAndTracePath() {
listWithInformationFromTrace.clear();
takeInformationFromActuatorTrace();
listWithoutRequestAndTracePath = new JSONArray();
for (int i = 0; i < jsonArrayFromString.length(); i++) {
if (!createPathForCheck(i).equals("/request") &&
!createPathForCheck(i).equals("/trace")) {
listWithoutRequestAndTracePath.put(jsonArrayFromString.get(i));
}
}
}
public List<String> createListWithInformationsFromTrace(){
createListWithoutRequestAndTracePath();
for (int i=0; i<listWithoutRequestAndTracePath.length(); i++){
String timestampAndRequestInformation = takeTimestampFromTrace(i) + "\n" + takeRequestInformationFromTrace(i) + "\n";
listWithInformationFromTrace.add(timestampAndRequestInformation);
}
return listWithInformationFromTrace;
}
public String takeRequestInformationFromTrace(int i) {
return listWithoutRequestAndTracePath
.getJSONObject(i)
.getJSONObject("info")
.getJSONObject("headers")
.get("request")
.toString()
+ "\n";
}
public String takeTimestampFromTrace(int i) {
return jsonArrayFromString.getJSONObject(i).get("timestamp").toString() + "\n";
}
public String printLastTenRequestInformationFromTrace() {
StringBuilder stringToPrint = new StringBuilder();
createListWithInformationsFromTrace();
if (listWithInformationFromTrace.size() > 10) {
for (int i = 0; i < StaticValues.NUMBER_POSITION_TO_PRINT; i++) {
stringToPrint.append(listWithInformationFromTrace.get(i));
}
} else {
for (int i = 0; i < listWithInformationFromTrace.size(); i++) {
stringToPrint.append(listWithInformationFromTrace.get(i));
}
}
return stringToPrint.toString();
}
}
Probably it could be a little bit more readable and pretty print should be implemented at the end, but just for now it works.