use of com.fasterxml.jackson.core.type.TypeReference in project timbuctoo by HuygensING.
the class LocalFileUserAccess method addUser.
@Override
public void addUser(User user) throws AuthenticationUnavailableException {
final List<User> users;
try {
synchronized (usersFile) {
users = objectMapper.readValue(usersFile.toFile(), new TypeReference<List<User>>() {
});
}
users.add(user);
objectMapper.writeValue(usersFile.toFile(), users.toArray(new User[users.size()]));
} catch (IOException e) {
JsonBasedUserStore.LOG.error("Cannot read {}", usersFile.toAbsolutePath());
JsonBasedUserStore.LOG.error("Exception thrown", e);
throw new AuthenticationUnavailableException(e.getMessage());
}
}
use of com.fasterxml.jackson.core.type.TypeReference in project timbuctoo by HuygensING.
the class LocalFileVreAuthorizationAccess method getOrCreateAuthorization.
@Override
public VreAuthorization getOrCreateAuthorization(String vreId, String userId, String userRole) throws AuthorizationUnavailableException {
Optional<VreAuthorization> authOptional = getAuthorization(vreId, userId);
if (authOptional.isPresent()) {
return authOptional.get();
} else {
try {
synchronized (authorizationsFolder) {
File file = getFileOfVre(vreId);
List<VreAuthorization> authorizations = Lists.newArrayList();
if (file.exists()) {
authorizations = objectMapper.readValue(file, new TypeReference<List<VreAuthorization>>() {
});
}
VreAuthorization vreAuthorization = VreAuthorization.create(vreId, userId, userRole);
authorizations.add(vreAuthorization);
objectMapper.writeValue(file, authorizations.toArray(new VreAuthorization[authorizations.size()]));
return vreAuthorization;
}
} catch (IOException e) {
throw new AuthorizationUnavailableException(e.getMessage());
}
}
}
use of com.fasterxml.jackson.core.type.TypeReference in project timbuctoo by HuygensING.
the class CnwPersonSearchDescription method createRef.
@Override
public EntityRef createRef(Vertex vertex) {
final ObjectMapper objectMapper = new ObjectMapper();
EntityRef entityRef = super.createRef(vertex);
Map<String, Object> data = entityRef.getData();
try {
final TypeReference<List<String>> listTypeReference = new TypeReference<List<String>>() {
};
data.put("networkDomains", objectMapper.readValue((String) vertex.value("cnwperson_networkDomains"), listTypeReference));
data.put("characteristics", objectMapper.readValue((String) vertex.value("cnwperson_characteristics"), listTypeReference));
data.put("combinedDomains", objectMapper.readValue((String) vertex.value("cnwperson_combinedDomains"), listTypeReference));
data.put("memberships", objectMapper.readValue((String) vertex.value("cnwperson_memberships"), listTypeReference));
data.put("periodicals", objectMapper.readValue((String) vertex.value("cnwperson_periodicals"), listTypeReference));
} catch (IOException e) {
LOG.error("Cannot read value for cnwperson with tim_id: {}", vertex.value("tim_id"), e);
}
entityRef.setData(data);
return entityRef;
}
use of com.fasterxml.jackson.core.type.TypeReference in project timbuctoo by HuygensING.
the class TinkerPopToEntityMapper method mapEntity.
public ReadEntity mapEntity(GraphTraversal<Vertex, Vertex> entityT, boolean withRelations) {
final List<TimProperty<?>> properties = Lists.newArrayList();
TinkerPopPropertyConverter dbPropertyConverter = new TinkerPopPropertyConverter(collection);
String entityTypeName = collection.getEntityTypeName();
GraphTraversal[] propertyGetters = collection.getReadableProperties().entrySet().stream().map(prop -> prop.getValue().traversalRaw().sideEffect(x -> x.get().onSuccess(value -> {
try {
properties.add(dbPropertyConverter.from(prop.getKey(), value));
} catch (UnknownPropertyException e) {
LOG.error("Unknown property", e);
} catch (IOException e) {
LOG.error(databaseInvariant, "Property '" + prop.getKey() + "' is not encoded correctly", e.getCause());
}
}).onFailure(e -> {
if (e.getCause() instanceof IOException) {
LOG.error(databaseInvariant, "Property '" + prop.getKey() + "' is not encoded correctly", e.getCause());
} else {
LOG.error("Something went wrong while reading the property '" + prop.getKey() + "'.", e.getCause());
}
}))).toArray(GraphTraversal[]::new);
entityT.asAdmin().clone().union(propertyGetters).forEachRemaining(x -> {
// Force side effects to happen
});
ReadEntityImpl entity = new ReadEntityImpl();
entity.setProperties(properties);
Vertex entityVertex = entityT.asAdmin().clone().next();
// TODO make use conversion for the types
entity.setRev(getProp(entityVertex, "rev", Integer.class).orElse(-1));
entity.setDeleted(getProp(entityVertex, "deleted", Boolean.class).orElse(false));
entity.setPid(getProp(entityVertex, "pid", String.class).orElse(null));
URI rdfUri = getProp(entityVertex, RDF_URI_PROP, String.class).map(x -> {
try {
return new URI(x);
} catch (URISyntaxException e) {
return null;
}
}).orElse(null);
entity.setRdfUri(rdfUri);
Property<String[]> rdfAlternativesProp = entityVertex.property(RDF_SYNONYM_PROP);
if (rdfAlternativesProp.isPresent()) {
try {
entity.setRdfAlternatives(Lists.newArrayList(rdfAlternativesProp.value()));
} catch (Exception e) {
LOG.error(databaseInvariant, "Error while reading rdfAlternatives", e);
}
}
Optional<String> typesOptional = getProp(entityVertex, "types", String.class);
if (typesOptional.isPresent()) {
try {
List<String> types = new ObjectMapper().readValue(typesOptional.get(), new TypeReference<List<String>>() {
});
entity.setTypes(types);
} catch (Exception e) {
LOG.error(databaseInvariant, "Error while generating variation refs", e);
entity.setTypes(Lists.newArrayList(entityTypeName));
}
} else {
entity.setTypes(Lists.newArrayList(entityTypeName));
}
Optional<String> modifiedStringOptional = getProp(entityVertex, "modified", String.class);
if (modifiedStringOptional.isPresent()) {
try {
entity.setModified(new ObjectMapper().readValue(modifiedStringOptional.get(), Change.class));
} catch (IOException e) {
LOG.error(databaseInvariant, "Change cannot be converted", e);
entity.setModified(new Change());
}
} else {
entity.setModified(new Change());
}
Optional<String> createdStringOptional = getProp(entityVertex, "created", String.class);
if (createdStringOptional.isPresent()) {
try {
entity.setCreated(new ObjectMapper().readValue(createdStringOptional.get(), Change.class));
} catch (IOException e) {
LOG.error(databaseInvariant, "Change cannot be converted", e);
entity.setCreated(new Change());
}
} else {
entity.setCreated(new Change());
}
entity.setDisplayName(DisplayNameHelper.getDisplayname(traversalSource, entityVertex, collection).orElse(""));
entity.setId(getIdOrDefault(entityVertex));
if (withRelations) {
entity.setRelations(getRelations(entityVertex, traversalSource, collection));
}
customEntityProperties.execute(entity, entityVertex);
return entity;
}
use of com.fasterxml.jackson.core.type.TypeReference in project nzbhydra2 by theotherp.
the class LogContentProvider method getLogsAsJsonLines.
public JsonLogResponse getLogsAsJsonLines(int offset, int limit) throws IOException {
File logfile = getCurrentLogfile(true);
if (logfile == null) {
throw new IOException("Unable to determine log file");
}
if (!logfile.exists()) {
throw new IOException("Determined log file does not exist");
}
List<HashMap<String, Object>> objects = new ArrayList<>();
int count = 0;
ReversedLinesFileReader reversedLinesFileReader = new ReversedLinesFileReader(logfile, Charset.defaultCharset());
String line = reversedLinesFileReader.readLine();
while (offset > 0 && count++ < offset && line != null) {
line = reversedLinesFileReader.readLine();
}
if (count > 0 && line == null) {
return new JsonLogResponse(Collections.emptyList(), false, offset, 0);
}
count = 1;
ObjectMapper objectMapper = new ObjectMapper();
while (line != null && count++ <= limit) {
TypeReference<HashMap<String, Object>> typeRef = new TypeReference<HashMap<String, Object>>() {
};
HashMap<String, Object> o = objectMapper.readValue(line, typeRef);
objects.add(o);
line = reversedLinesFileReader.readLine();
}
return new JsonLogResponse(objects, line != null, offset, objects.size());
}
Aggregations