]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.auditlogging/src/org/simantics/audit/client/AuditLoggingAPIClient.java
Minor NPE fix for auditlogging
[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     public String getUuid() {
51         return uuid;
52     }
53
54     private void register(String id) throws AuditLoggingException {
55         try {
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);
61             } else {
62                 LOGGER.warn("Invalid response received from {} for register with response payload {}", base.getUri(), payload);
63             }
64         } catch (Exception e) {
65             throw new AuditLoggingException(e);
66         }
67     }
68     
69     private static Path auditLoggingFile() {
70         return Activator.getLogLocation().resolve(".auditlogging");
71     }
72     
73     private static String possibleUUIDFromFile() {
74         Path auditLoggingFile = auditLoggingFile();
75         if (Files.exists(auditLoggingFile)) {
76             try {
77                 String possibleUUID = new String(Files.readAllBytes(auditLoggingFile));
78                 if (possibleUUID != null && !possibleUUID.isEmpty()) {
79                     return possibleUUID;
80                 } else {
81                     LOGGER.warn(".auditlogging file exists but is somehow corrupted");
82                 }
83             } catch (IOException e) {
84                 LOGGER.error("Could not read .auditlogging file and related information", e);
85             }
86         }
87         return null;
88     }
89     
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);
93         uuid = possibleUUID;
94     }
95
96     public void log(Map<String, Object> message) throws AuditLoggingException {
97         try {
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);
101         }
102     }
103     
104     public void error(Map<String, Object> message) throws AuditLoggingException {
105         try {
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);
109         }
110     }
111
112     public void trace(Map<String, Object> message) throws AuditLoggingException {
113         try {
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);
117         }
118     }
119
120 }