/** * Returns whether the request is a PATCH request with a payload of type {@link RestMediaTypes#JSON_PATCH_JSON}. * * @return */ public boolean isJsonPatchRequest() { return isPatchRequest() && RestMediaTypes.JSON_PATCH_JSON.isCompatibleWith(contentType); }
/** * Returns whether the request is a PATCH request with a payload of type {@link RestMediaTypes#MERGE_PATCH_JSON}. * * @return */ public boolean isJsonMergePatchRequest() { return isPatchRequest() && RestMediaTypes.MERGE_PATCH_JSON.isCompatibleWith(contentType); }
/** * Applies the body of the given {@link IncomingRequest} as patch on the given target object. * * @param request must not be {@literal null}. * @param target must not be {@literal null}. * @return * @throws Exception */ public <T> T apply(IncomingRequest request, T target) throws Exception { Assert.notNull(request, "Request must not be null!"); Assert.isTrue(request.isPatchRequest(), "Cannot handle non-PATCH request!"); Assert.notNull(target, "Target must not be null!"); if (request.isJsonPatchRequest()) { return applyPatch(request.getBody(), target); } else { return applyMergePatch(request.getBody(), target); } }
/** * Reads the given {@link ServerHttpRequest} into an object of the type of the given {@link RootResourceInformation}, * potentially applying the content to an object of the given id. * * @param information must not be {@literal null}. * @param request must not be {@literal null}. * @param converter must not be {@literal null}. * @param id must not be {@literal null}. * @return */ private Object read(RootResourceInformation information, IncomingRequest request, HttpMessageConverter<Object> converter, Optional<Object> objectToUpdate) { // JSON + PATCH request if (request.isPatchRequest() && converter instanceof MappingJackson2HttpMessageConverter) { return objectToUpdate.map(it -> { ObjectMapper mapper = ((MappingJackson2HttpMessageConverter) converter).getObjectMapper(); return readPatch(request, mapper, it); }).orElseThrow(() -> new ResourceNotFoundException()); // JSON + PUT request } else if (converter instanceof MappingJackson2HttpMessageConverter) { ObjectMapper mapper = ((MappingJackson2HttpMessageConverter) converter).getObjectMapper(); return objectToUpdate.map(it -> readPutForUpdate(request, mapper, it))// .orElseGet(() -> read(request, converter, information)); } // Catch all return read(request, converter, information); }
/** * Returns whether the request is a PATCH request with a payload of type {@link RestMediaTypes#JSON_PATCH_JSON}. * * @return */ public boolean isJsonPatchRequest() { return isPatchRequest() && RestMediaTypes.JSON_PATCH_JSON.isCompatibleWith(contentType); }
/** * Returns whether the request is a PATCH request with a payload of type {@link RestMediaTypes#MERGE_PATCH_JSON}. * * @return */ public boolean isJsonMergePatchRequest() { return isPatchRequest() && RestMediaTypes.MERGE_PATCH_JSON.isCompatibleWith(contentType); }
/** * Applies the body of the given {@link IncomingRequest} as patch on the given target object. * * @param request must not be {@literal null}. * @param target must not be {@literal null}. * @return * @throws Exception */ public <T> T apply(IncomingRequest request, T target) throws Exception { Assert.notNull(request, "Request must not be null!"); Assert.isTrue(request.isPatchRequest(), "Cannot handle non-PATCH request!"); Assert.notNull(target, "Target must not be null!"); if (request.isJsonPatchRequest()) { return applyPatch(request.getBody(), target); } else { return applyMergePatch(request.getBody(), target); } }
/** * Reads the given {@link ServerHttpRequest} into an object of the type of the given {@link RootResourceInformation}, * potentially applying the content to an object of the given id. * * @param information must not be {@literal null}. * @param request must not be {@literal null}. * @param converter must not be {@literal null}. * @param id must not be {@literal null}. * @return */ private Object read(RootResourceInformation information, IncomingRequest request, HttpMessageConverter<Object> converter, Optional<Object> objectToUpdate) { // JSON + PATCH request if (request.isPatchRequest() && converter instanceof MappingJackson2HttpMessageConverter) { return objectToUpdate.map(it -> { ObjectMapper mapper = ((MappingJackson2HttpMessageConverter) converter).getObjectMapper(); return readPatch(request, mapper, it); }).orElseThrow(() -> new ResourceNotFoundException()); // JSON + PUT request } else if (converter instanceof MappingJackson2HttpMessageConverter) { ObjectMapper mapper = ((MappingJackson2HttpMessageConverter) converter).getObjectMapper(); return objectToUpdate.map(it -> readPutForUpdate(request, mapper, it))// .orElseGet(() -> read(request, converter, information)); } // Catch all return read(request, converter, information); }