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://") && !serverAddress.startsWith("https://")) {
38 serverAddress = "http://" + serverAddress;
40 base = httpClient.target(serverAddress);
42 // see if registered already
43 uuid = possibleUUIDFromFile();
50 public String getUuid() {
54 private void register(String id) throws AuditLoggingException {
56 Response response = base.path("register").request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(Collections.singletonMap("id", id)));
57 Map<String, String> payload = response.readEntity(Map.class);
58 String possibleUUID = payload.get("uuid");
59 if (possibleUUID != null && !possibleUUID.isEmpty()) {
60 persistUUID(possibleUUID);
62 LOGGER.warn("Invalid response received from {} for register with response payload {}", base.getUri(), payload);
64 } catch (Exception e) {
65 throw new AuditLoggingException(e);
69 private static Path auditLoggingFile() {
70 return Activator.getLogLocation().resolve(".auditlogging");
73 private static String possibleUUIDFromFile() {
74 Path auditLoggingFile = auditLoggingFile();
75 if (Files.exists(auditLoggingFile)) {
77 String possibleUUID = new String(Files.readAllBytes(auditLoggingFile));
78 if (possibleUUID != null && !possibleUUID.isEmpty()) {
81 LOGGER.warn(".auditlogging file exists but is somehow corrupted");
83 } catch (IOException e) {
84 LOGGER.error("Could not read .auditlogging file and related information", e);
90 private void persistUUID(String possibleUUID) throws IOException {
91 Path auditLoggingFile = auditLoggingFile();
92 Files.write(auditLoggingFile, possibleUUID.getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
96 public void log(Map<String, Object> message) throws AuditLoggingException {
98 Response response = base.path(uuid).path("log").request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(message));
99 } catch (Exception e) {
100 throw new AuditLoggingException(e);
104 public void error(Map<String, Object> message) throws AuditLoggingException {
106 Response response = base.path(uuid).path("error").request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(message));
107 } catch (Exception e) {
108 throw new AuditLoggingException(e);
112 public void trace(Map<String, Object> message) throws AuditLoggingException {
114 Response response = base.path(uuid).path("trace").request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(message));
115 } catch (Exception e) {
116 throw new AuditLoggingException(e);