1 package org.simantics.audit.client;
3 import java.io.IOException;
4 import java.nio.charset.StandardCharsets;
5 import java.nio.file.Files;
6 import java.nio.file.Path;
7 import java.nio.file.StandardOpenOption;
8 import java.util.Collections;
11 import javax.ws.rs.client.Client;
12 import javax.ws.rs.client.ClientBuilder;
13 import javax.ws.rs.client.Entity;
14 import javax.ws.rs.client.WebTarget;
15 import javax.ws.rs.core.MediaType;
16 import javax.ws.rs.core.Response;
18 import org.glassfish.jersey.client.ClientConfig;
19 import org.glassfish.jersey.jackson.JacksonFeature;
20 import org.simantics.audit.Activator;
21 import org.simantics.audit.AuditLoggingException;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 public class AuditLoggingAPIClient {
27 private static final Logger LOGGER = LoggerFactory.getLogger(AuditLoggingAPIClient.class);
29 private Client httpClient;
30 private WebTarget base;
33 public AuditLoggingAPIClient(String id, String serverAddress) throws AuditLoggingException {
34 ClientConfig configuration = new ClientConfig();
35 configuration.register(JacksonFeature.class);
36 httpClient = ClientBuilder.newClient(configuration);
37 if (!serverAddress.startsWith("http://")) {
38 serverAddress = "http://" + serverAddress;
40 base = httpClient.target(serverAddress);
42 // see if registered already
43 uuid = possibleUUIDFromFile();
50 private void register(String id) throws AuditLoggingException {
52 Response response = base.path("register").request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(Collections.singletonMap("id", id)));
53 Map<String, String> payload = response.readEntity(Map.class);
54 String possibleUUID = payload.get("uuid");
55 if (possibleUUID != null && !possibleUUID.isEmpty()) {
56 persistUUID(possibleUUID);
58 LOGGER.warn("Invalid response received from {} for register with response payload {}", base.getUri(), payload);
60 } catch (Exception e) {
61 throw new AuditLoggingException(e);
65 private static Path auditLoggingFile() {
66 return Activator.getLogLocation().resolve(".auditlogging");
69 private static String possibleUUIDFromFile() {
70 Path auditLoggingFile = auditLoggingFile();
71 if (Files.exists(auditLoggingFile)) {
73 String possibleUUID = new String(Files.readAllBytes(auditLoggingFile));
74 if (possibleUUID != null && !possibleUUID.isEmpty()) {
77 LOGGER.warn(".auditlogging file exists but is somehow corrupted");
79 } catch (IOException e) {
80 LOGGER.error("Could not read .auditlogging file and related information", e);
86 private void persistUUID(String possibleUUID) throws IOException {
87 Path auditLoggingFile = auditLoggingFile();
88 Files.write(auditLoggingFile, possibleUUID.getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
92 public void log(Map<String, Object> message) throws AuditLoggingException {
94 Response response = base.path(uuid).path("log").request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(message));
95 } catch (Exception e) {
96 throw new AuditLoggingException(e);
100 public void error(Map<String, Object> message) throws AuditLoggingException {
102 Response response = base.path(uuid).path("error").request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(message));
103 } catch (Exception e) {
104 throw new AuditLoggingException(e);
108 public void trace(Map<String, Object> message) throws AuditLoggingException {
110 Response response = base.path(uuid).path("trace").request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(message));
111 } catch (Exception e) {
112 throw new AuditLoggingException(e);