Search in sources :

Example 1 with JsonPatch

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);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) JsonNode(com.fasterxml.jackson.databind.JsonNode) Method(java.lang.reflect.Method) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JsonPatch(com.github.fge.jsonpatch.JsonPatch) JsonPatchException(com.github.fge.jsonpatch.JsonPatchException) IOException(java.io.IOException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) WebApplicationException(javax.ws.rs.WebApplicationException) JsonPatchException(com.github.fge.jsonpatch.JsonPatchException) ByteArrayInputStream(java.io.ByteArrayInputStream) GET(javax.ws.rs.GET) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with JsonPatch

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();
    }
}
Also used : ListReportProvider(com.github.fge.jsonschema.core.report.ListReportProvider) JsonPatchException(com.github.fge.jsonpatch.JsonPatchException) JsonMergePatch(com.github.fge.jsonpatch.mergepatch.JsonMergePatch) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) JsonPatch(com.github.fge.jsonpatch.JsonPatch) ProcessingException(com.github.fge.jsonschema.core.exceptions.ProcessingException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)2 JsonPatch (com.github.fge.jsonpatch.JsonPatch)2 JsonPatchException (com.github.fge.jsonpatch.JsonPatchException)2 IOException (java.io.IOException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 JsonMergePatch (com.github.fge.jsonpatch.mergepatch.JsonMergePatch)1 ProcessingException (com.github.fge.jsonschema.core.exceptions.ProcessingException)1 ListReportProvider (com.github.fge.jsonschema.core.report.ListReportProvider)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Method (java.lang.reflect.Method)1 GET (javax.ws.rs.GET)1 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 MessageBodyWriter (javax.ws.rs.ext.MessageBodyWriter)1