]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.auditlogging/src/org/simantics/audit/client/AuditLoggingAPIClient.java
Generic HTTP REST Client/Server AuditLogging framework
[simantics/platform.git] / bundles / org.simantics.auditlogging / src / org / simantics / audit / client / AuditLoggingAPIClient.java
1 package org.simantics.audit.client;
2
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;
9 import java.util.Map;
10
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;
17
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;
24
25 public class AuditLoggingAPIClient {
26
27     private static final Logger LOGGER = LoggerFactory.getLogger(AuditLoggingAPIClient.class);
28     
29     private Client httpClient;
30     private WebTarget base;
31     private String uuid;
32
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;
39         }
40         base = httpClient.target(serverAddress);
41         
42         // see if registered already
43         uuid = possibleUUIDFromFile();
44         if (uuid == null) {
45             // register
46             register(id);
47         }
48     }
49
50     private void register(String id) throws AuditLoggingException {
51         try {
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);
57             } else {
58                 LOGGER.warn("Invalid response received from {} for register with response payload {}", base.getUri(), payload);
59             }
60         } catch (Exception e) {
61             throw new AuditLoggingException(e);
62         }
63     }
64     
65     private static Path auditLoggingFile() {
66         return Activator.getLogLocation().resolve(".auditlogging");
67     }
68     
69     private static String possibleUUIDFromFile() {
70         Path auditLoggingFile = auditLoggingFile();
71         if (Files.exists(auditLoggingFile)) {
72             try {
73                 String possibleUUID = new String(Files.readAllBytes(auditLoggingFile));
74                 if (possibleUUID != null && !possibleUUID.isEmpty()) {
75                     return possibleUUID;
76                 } else {
77                     LOGGER.warn(".auditlogging file exists but is somehow corrupted");
78                 }
79             } catch (IOException e) {
80                 LOGGER.error("Could not read .auditlogging file and related information", e);
81             }
82         }
83         return null;
84     }
85     
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);
89         uuid = possibleUUID;
90     }
91
92     public void log(Map<String, Object> message) throws AuditLoggingException {
93         try {
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);
97         }
98     }
99     
100     public void error(Map<String, Object> message) throws AuditLoggingException {
101         try {
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);
105         }
106     }
107
108     public void trace(Map<String, Object> message) throws AuditLoggingException {
109         try {
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);
113         }
114     }
115
116 }