/
SyncUtils.java
256 lines (217 loc) · 9.45 KB
/
SyncUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package org.openmrs.module.sync2.api.utils;
import org.apache.commons.io.IOUtils;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.ObjectWriter;
import org.codehaus.jackson.type.TypeReference;
import org.codehaus.jackson.util.DefaultPrettyPrinter;
import org.openmrs.api.context.Context;
import org.openmrs.module.atomfeed.api.db.EventAction;
import org.openmrs.module.atomfeed.client.AtomFeedClient;
import org.openmrs.module.sync2.api.SyncConfigurationService;
import org.openmrs.module.sync2.api.exceptions.SyncException;
import org.openmrs.module.sync2.api.model.configuration.SyncConfiguration;
import org.openmrs.module.sync2.api.model.enums.AtomfeedTagContent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.openmrs.module.sync2.SyncConstants.RESOURCE_PREFERRED_CLIENT;
public class SyncUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(SyncUtils.class);
private static final String FHIR = "fhir";
private static final String REST = "rest";
private static final String ATOMFEED_TAG_VALUE_FIELD_NAME="Category.term";
public static String readResourceFile(String file) throws SyncException {
try (InputStream in = SyncUtils.class.getClassLoader().getResourceAsStream(file)) {
if (in == null) {
throw new SyncException("Resource '" + file + "' doesn't exist");
}
return IOUtils.toString(in);
} catch (IOException e) {
throw new SyncException(e);
}
}
public static SyncConfiguration parseJsonFileToSyncConfiguration(String resourcePath) {
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.readValue(readResourceFile(resourcePath), SyncConfiguration.class);
} catch (IOException e) {
throw new SyncException(e);
}
}
public static SyncConfiguration parseJsonStringToSyncConfiguration(String value) {
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.readValue(value, SyncConfiguration.class);
} catch (IOException e) {
throw new SyncException(e);
}
}
public static boolean isValidateJson(String json) throws SyncException {
try {
final JsonParser parser = new ObjectMapper().getJsonFactory().createJsonParser(json);
while (parser.nextToken() != null) {}
} catch (JsonParseException jpe) {
return false;
} catch (IOException e) {
throw new SyncException(e);
}
return true;
}
public static String writeSyncConfigurationToJsonString(SyncConfiguration syncConfiguration) throws SyncException {
ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
try {
return writer.writeValueAsString(syncConfiguration);
} catch (IOException e) {
throw new SyncException(e);
}
}
public static void writeSyncConfigurationToJsonFile(SyncConfiguration syncConfigurations, String file)
throws SyncException {
ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
try {
File resultFile = new File(SyncUtils.class.getClassLoader().getResource("").getPath() + file);
writer.writeValue(resultFile, syncConfigurations);
} catch (IOException e) {
throw new SyncException(e);
}
}
public static boolean resourceFileExists(String path) {
InputStream in = SyncUtils.class.getClassLoader().getResourceAsStream(path);
return in != null;
}
public static Map<String, String> getLinks(String json) {
ObjectMapper mapper = new ObjectMapper();
TypeReference<HashMap<String, String>> typeRef = new TypeReference<HashMap<String, String>>() {};
try {
HashMap<String, String> result = mapper.readValue(json, typeRef);
return result;
} catch (IOException e) {
throw new SyncException(String.format("Invalid resource links JSON Object: %s ", json), e);
}
}
public static String getBaseUrl(String stringUri) {
try {
URI uri = new URI(stringUri);
return uri.getScheme() + "://" + uri.getAuthority() + "/";
} catch(URISyntaxException e) {
throw new SyncException(String.format("Bad Atomfeed URI: %s. ", stringUri), e);
}
}
public static String getValueOfAtomfeedEventTag(List tags, AtomfeedTagContent atomfeedTagContent) {
for (Object tag : tags) {
String tagValue = getTagValue(tag);
boolean isTagEventAction = checkIfParamIsEventAction(tagValue);
if (atomfeedTagContent == AtomfeedTagContent.EVENT_ACTION && isTagEventAction) {
return tagValue;
}
if (atomfeedTagContent == AtomfeedTagContent.CATEGORY && !isTagEventAction) {
return tagValue;
}
}
throw new SyncException(String.format("'%s' enum not found in tag list", atomfeedTagContent.name()));
}
private static String getTagValue(Object tag) {
final HashMap<String, String> map = new HashMap<>();
for (String pair : tag.toString().split("\n")) {
String[] keyValue = pair.split("=");
map.put(keyValue[0], keyValue[1]);
}
return map.get(ATOMFEED_TAG_VALUE_FIELD_NAME);
}
public static String getPreferredUrl(Map<String, String> resourceLinks) {
String preferredClient = getPreferredClient();
String result = resourceLinks.get(preferredClient);
if (result == null && resourceLinks.size() > 0) {
result = getFirstResourceLink(resourceLinks);
}
return result;
}
private static String getFirstResourceLink(Map<String, String> resourceLinks) {
if (resourceLinks.size() > 0) {
return resourceLinks.values().iterator().next();
}
return "";
}
public static String getPreferredClient() {
return Context.getAdministrationService().getGlobalProperty(RESOURCE_PREFERRED_CLIENT);
}
public static String extractUUIDFromResourceLinks(Map<String, String> resourceLinks) {
for (String client : resourceLinks.keySet()) {
switch (client) {
case REST:
return extractUUIDFromRestResource(resourceLinks.get(client));
case FHIR:
return extractUUIDFromFHIRResource(resourceLinks.get(client));
default:
}
}
LOGGER.error("Couldn't find any supported client to extract uuid from.");
return null;
}
public static void readFeedByCategory(String category, AtomFeedClient atomFeedClient,
SyncConfigurationService configurationService, String ws) {
try {
URI uri = new URI(getResourceUrlWithCategory(category, configurationService, ws));
atomFeedClient.setUri(uri);
atomFeedClient.process();
} catch (URISyntaxException e) {
throw new SyncException("Atomfeed URI is not correct. ", e);
} catch (Exception e) {
throw new SyncException(String.format("Error during processing atomfeeds for category %s: ", category), e);
}
}
private static String extractUUIDFromRestResource(String link) {
String[] tokens = link.split("/");
// todo throw custom sync2 exception if tokens.length != 6
return tokens[5].split("\\?")[0];
}
private static String extractUUIDFromFHIRResource(String link) {
String[] tokens = link.split("/");
// todo throw custom sync2 exception if tokens.length != 5
return tokens[4];
}
public static String getPushEndpointFromResourceUrl(String url) {
return url.substring(0, url.lastIndexOf("/"));
}
public static <T> String serializeMapToPrettyJson(Map<T, T> map) {
try {
return new ObjectMapper()
.writerWithDefaultPrettyPrinter()
.writeValueAsString(map);
} catch (IOException ex) {
throw new SyncException("Cannot serialize map", ex);
}
}
public static Map<String, String> deserializeJsonToStringsMap(String json) {
try {
return new ObjectMapper().readValue(json, new TypeReference<Map<String, String>>() {});
} catch (IOException ex) {
throw new SyncException("Cannot deserialize map", ex);
}
}
public static boolean checkIfParamIsEventAction(String actionName) {
try {
EventAction.valueOf(actionName);
} catch (IllegalArgumentException ex) {
return false;
}
return true;
}
private static String getParentUri(SyncConfigurationService configurationService) {
return configurationService.getSyncConfiguration().getGeneral().getParentFeedLocation();
}
private static String getResourceUrlWithCategory(String category, SyncConfigurationService cs, String ws) {
return getParentUri(cs) + ws + category + "/recent";
}
}