use of com.github.fge.jsonpatch.JsonPatch in project jersey by jersey.
the class PatchingInterceptor method aroundReadFrom.
@SuppressWarnings("unchecked")
@Override
public Object aroundReadFrom(ReaderInterceptorContext readerInterceptorContext) throws IOException, WebApplicationException {
// Get the resource we are being called on, and find the GET method
Object resource = uriInfo.getMatchedResources().get(0);
Method found = null;
for (Method next : resource.getClass().getMethods()) {
if (next.getAnnotation(GET.class) != null) {
found = next;
break;
}
}
if (found == null) {
throw new InternalServerErrorException("No matching GET method on resource");
}
// Invoke the get method to get the state we are trying to patch
Object bean;
try {
bean = found.invoke(resource);
} catch (Exception e) {
throw new WebApplicationException(e);
}
// Convert this object to a an array of bytes
ByteArrayOutputStream baos = new ByteArrayOutputStream();
MessageBodyWriter bodyWriter = workers.getMessageBodyWriter(bean.getClass(), bean.getClass(), new Annotation[0], MediaType.APPLICATION_JSON_TYPE);
bodyWriter.writeTo(bean, bean.getClass(), bean.getClass(), new Annotation[0], MediaType.APPLICATION_JSON_TYPE, new MultivaluedHashMap<String, Object>(), baos);
// Use the Jackson 2.x classes to convert both the incoming patch
// and the current state of the object into a JsonNode / JsonPatch
ObjectMapper mapper = new ObjectMapper();
JsonNode serverState = mapper.readValue(baos.toByteArray(), JsonNode.class);
JsonNode patchAsNode = mapper.readValue(readerInterceptorContext.getInputStream(), JsonNode.class);
JsonPatch patch = JsonPatch.fromJson(patchAsNode);
try {
// Apply the patch
JsonNode result = patch.apply(serverState);
// Stream the result & modify the stream on the readerInterceptor
ByteArrayOutputStream resultAsByteArray = new ByteArrayOutputStream();
mapper.writeValue(resultAsByteArray, result);
readerInterceptorContext.setInputStream(new ByteArrayInputStream(resultAsByteArray.toByteArray()));
// Pass control back to the Jersey code
return readerInterceptorContext.proceed();
} catch (JsonPatchException ex) {
throw new InternalServerErrorException("Error applying patch.", ex);
}
}
use of com.github.fge.jsonpatch.JsonPatch in project ocvn by devgateway.
the class OcdsSchemaValidatorService method init.
/**
* Intializes the JSON schema validator plus the provided patches
*/
public void init() {
try {
ocdsSchemaNode = JsonLoader.fromResource(schemaLocation == null ? OCDS_SCHEMA_LOCATION : schemaLocation);
if (patchResourceNames != null && patchResourceNames.length > 0) {
for (int i = 0; i < patchResourceNames.length; i++) {
JsonNode node = JsonLoader.fromResource(patchResourceNames[i]);
if (patchResourceNames[i].contains("mergepatch")) {
JsonMergePatch patch = JsonMergePatch.fromJson(node);
ocdsSchemaNode = patch.apply(ocdsSchemaNode);
} else {
JsonPatch patch = JsonPatch.fromJson(node);
ocdsSchemaNode = patch.apply(ocdsSchemaNode);
}
}
}
logger.debug(jacksonObjectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(ocdsSchemaNode));
schema = JsonSchemaFactory.newBuilder().setReportProvider(new ListReportProvider(LogLevel.ERROR, LogLevel.FATAL)).freeze().getJsonSchema(ocdsSchemaNode);
} catch (ProcessingException | IOException e) {
logger.error(e.getMessage());
e.printStackTrace();
} catch (JsonPatchException e) {
logger.error(e.getMessage());
e.printStackTrace();
}
}
Aggregations