Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

Commit

Permalink
#319 Tweaking configuration of PreviewInterceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
rjrudin committed Jan 4, 2019
1 parent f8d634a commit c1c4234
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/main/java/com/marklogic/rest/util/PreviewInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.DefaultResponseErrorHandler;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.EnumSet;
Expand Down Expand Up @@ -117,11 +118,11 @@ protected JsonNode buildJsonPatch(ObjectNode existingResource, String payload) t
modifyPayloadBeforePreview(payloadResource);

ObjectNode merged = JsonNodeUtil.mergeObjectNodes(existingResource, payloadResource);
EnumSet<DiffFlags> flags = DiffFlags.dontNormalizeOpIntoMoveAndCopy().clone();
EnumSet<DiffFlags> flags = EnumSet.of(DiffFlags.OMIT_VALUE_ON_REMOVE, DiffFlags.OMIT_MOVE_OPERATION, DiffFlags.OMIT_COPY_OPERATION, DiffFlags.ADD_ORIGINAL_VALUE_ON_REPLACE);
return JsonDiff.asJson(existingResource, merged, flags);
}

protected void includeJsonPatchInReport(HttpRequest request, ObjectNode existingResource, JsonNode jsonPatch) throws IOException {
protected void includeJsonPatchInReport(HttpRequest request, ObjectNode existingResource, JsonNode jsonPatch) {
ObjectMapper mapper = ObjectMapperFactory.getObjectMapper();
if (jsonPatch instanceof ArrayNode && jsonPatch.size() > 0) {
ObjectNode result = mapper.createObjectNode();
Expand Down Expand Up @@ -179,6 +180,15 @@ protected void modifyPayloadBeforePreview(ObjectNode payload) {
protected ClientHttpResponse previewPost(HttpRequest request, byte[] bytes) throws IOException {
ObjectMapper mapper = ObjectMapperFactory.getObjectMapper();
ObjectNode result = mapper.createObjectNode();

if (request.getURI() != null && request.getURI().toString().endsWith("/manage/v3")) {
String message = "Previewing POST calls to /manage/v3 are not yet supported";
result.set("message", new TextNode(message));
results.add(result);
logger.info(message);
return newFakeResponse();
}

result.set("message", new TextNode("Will create new resource at: " + request.getURI()));

String payload = new String(bytes).trim();
Expand Down Expand Up @@ -228,7 +238,8 @@ public void close() {

@Override
public InputStream getBody() {
return null;
// Need to return something besides null, which can cause null pointer exceptions
return new ByteArrayInputStream(new byte[]{});
}

@Override
Expand Down

0 comments on commit c1c4234

Please sign in to comment.