|
1 | 1 | package org.zalando.logbook.json;
|
2 | 2 |
|
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import com.fasterxml.jackson.databind.node.BooleanNode; |
| 5 | +import com.fasterxml.jackson.databind.node.DoubleNode; |
| 6 | +import com.fasterxml.jackson.databind.node.TextNode; |
3 | 7 | import com.jayway.jsonpath.Configuration;
|
4 | 8 | import com.jayway.jsonpath.DocumentContext;
|
5 | 9 | import com.jayway.jsonpath.JsonPath;
|
6 |
| -import com.jayway.jsonpath.Option; |
7 |
| -import lombok.AccessLevel; |
| 10 | +import com.jayway.jsonpath.ParseContext; |
| 11 | +import com.jayway.jsonpath.spi.json.JacksonJsonNodeJsonProvider; |
| 12 | +import lombok.AllArgsConstructor; |
8 | 13 | import lombok.NoArgsConstructor;
|
9 | 14 | import lombok.RequiredArgsConstructor;
|
10 | 15 | import org.apiguardian.api.API;
|
11 | 16 | import org.zalando.logbook.BodyFilter;
|
12 | 17 |
|
13 | 18 | import javax.annotation.Nullable;
|
14 |
| -import java.util.List; |
15 |
| -import java.util.Optional; |
16 |
| -import java.util.function.Function; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.Collection; |
17 | 21 | import java.util.regex.Matcher;
|
18 | 22 | import java.util.regex.Pattern;
|
19 | 23 |
|
20 |
| -@API(status = API.Status.EXPERIMENTAL) |
21 |
| -@NoArgsConstructor(access = AccessLevel.PRIVATE) |
22 |
| -public final class JsonPathBodyFilters { |
| 24 | +import static com.jayway.jsonpath.JsonPath.compile; |
| 25 | +import static lombok.AccessLevel.PRIVATE; |
| 26 | +import static org.apiguardian.api.API.Status.EXPERIMENTAL; |
| 27 | +import static org.zalando.logbook.json.JsonMediaType.JSON; |
23 | 28 |
|
24 |
| - private static final Configuration conf = Configuration.builder().options(Option.AS_PATH_LIST).build(); |
| 29 | +@API(status = EXPERIMENTAL) |
| 30 | +@NoArgsConstructor(access = PRIVATE) |
| 31 | +public final class JsonPathBodyFilters { |
25 | 32 |
|
26 |
| - @RequiredArgsConstructor(access = AccessLevel.PRIVATE) |
| 33 | + @RequiredArgsConstructor(access = PRIVATE) |
27 | 34 | public static final class JsonPathBodyFilterBuilder {
|
28 |
| - private final JsonPath jsonPath; |
| 35 | + |
| 36 | + private final JsonPath path; |
29 | 37 |
|
30 | 38 | public BodyFilter delete() {
|
31 |
| - return (JsonBodyFilter) documentContext -> documentContext.delete(jsonPath).jsonString(); |
| 39 | + return filter(context -> context.delete(path)); |
| 40 | + } |
| 41 | + |
| 42 | + public BodyFilter replace(final String replacement) { |
| 43 | + return replace(new TextNode(replacement)); |
32 | 44 | }
|
33 | 45 |
|
34 |
| - public BodyFilter replace(String replacement) { |
35 |
| - return replaceInternal(replacement); |
| 46 | + public BodyFilter replace(final Boolean replacement) { |
| 47 | + return replace(BooleanNode.valueOf(replacement)); |
36 | 48 | }
|
37 | 49 |
|
38 |
| - public BodyFilter replace(Number replacement) { |
39 |
| - return replaceInternal(replacement); |
| 50 | + public BodyFilter replace(final Double replacement) { |
| 51 | + return replace(new DoubleNode(replacement)); |
40 | 52 | }
|
41 | 53 |
|
42 |
| - public BodyFilter replace(Boolean replacement) { |
43 |
| - return replaceInternal(replacement); |
| 54 | + public BodyFilter replace(final JsonNode replacement) { |
| 55 | + return filter(context -> context.set(path, replacement)); |
44 | 56 | }
|
45 | 57 |
|
46 |
| - public BodyFilter replace(String matchRegex, String replacementRegex) { |
47 |
| - Pattern pattern = Pattern.compile(matchRegex); |
48 |
| - return (JsonBodyFilter) documentContext -> { |
49 |
| - final List<String> paths = documentContext.read(jsonPath); |
50 |
| - final Object document = documentContext.json(); |
51 |
| - paths.forEach(path -> { |
52 |
| - final Object node = JsonPath.compile(path).read(document); |
53 |
| - replaceWithRegex(node, pattern, replacementRegex) |
54 |
| - .ifPresent(s -> documentContext.set(path, s)); |
55 |
| - }); |
56 |
| - return documentContext.jsonString(); |
57 |
| - }; |
| 58 | + public BodyFilter replace( |
| 59 | + final Pattern pattern, final String replacement) { |
| 60 | + |
| 61 | + return filter(context -> context.map(path, (node, config) -> { |
| 62 | + final Matcher matcher = pattern.matcher(node.toString()); |
| 63 | + |
| 64 | + if (matcher.find()) { |
| 65 | + return new TextNode(matcher.replaceAll(replacement)); |
| 66 | + } else { |
| 67 | + return node; |
| 68 | + } |
| 69 | + })); |
58 | 70 | }
|
59 | 71 |
|
60 |
| - private BodyFilter replaceInternal(Object replacement) { |
61 |
| - return (JsonBodyFilter) documentContext -> documentContext.set(jsonPath, replacement).jsonString(); |
| 72 | + } |
| 73 | + |
| 74 | + private static JsonPathBodyFilter filter(final Operation operation) { |
| 75 | + return new JsonPathBodyFilter(operation); |
| 76 | + } |
| 77 | + |
| 78 | + @AllArgsConstructor |
| 79 | + private static class JsonPathBodyFilter implements BodyFilter { |
| 80 | + |
| 81 | + private static final ParseContext CONTEXT = JsonPath.using( |
| 82 | + Configuration.builder() |
| 83 | + .jsonProvider(new JacksonJsonNodeJsonProvider()) |
| 84 | + .build()); |
| 85 | + |
| 86 | + private final Operation operation; |
| 87 | + |
| 88 | + @Override |
| 89 | + public String filter( |
| 90 | + @Nullable final String contentType, final String body) { |
| 91 | + |
| 92 | + if (JSON.test(contentType)) { |
| 93 | + final DocumentContext original = CONTEXT.parse(body); |
| 94 | + return operation.filter(original).jsonString(); |
| 95 | + } |
| 96 | + |
| 97 | + return body; |
62 | 98 | }
|
63 | 99 |
|
64 |
| - private Optional<String> replaceWithRegex(Object node, Pattern pattern, String replacementRegex) { |
65 |
| - String stringRepresentation = node.toString(); |
66 |
| - Matcher matcher = pattern.matcher(stringRepresentation); |
67 |
| - if (matcher.find()) { |
68 |
| - return Optional.of(matcher.replaceAll(replacementRegex)); |
| 100 | + @Nullable |
| 101 | + @Override |
| 102 | + public BodyFilter tryMerge(final BodyFilter next) { |
| 103 | + if (next instanceof JsonPathBodyFilter) { |
| 104 | + final JsonPathBodyFilter filter = (JsonPathBodyFilter) next; |
| 105 | + return new JsonPathBodyFilter( |
| 106 | + Operation.composite(operation, filter.operation)); |
69 | 107 | }
|
70 |
| - return Optional.empty(); |
| 108 | + return BodyFilter.super.tryMerge(next); |
| 109 | + } |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | + @FunctionalInterface |
| 114 | + private interface Operation { |
| 115 | + DocumentContext filter(DocumentContext context); |
| 116 | + |
| 117 | + static Operation composite(final Operation... operations) { |
| 118 | + return composite(Arrays.asList(operations)); |
71 | 119 | }
|
72 | 120 |
|
| 121 | + static Operation composite(final Collection<Operation> operations) { |
| 122 | + return new CompositeOperation(operations); |
| 123 | + } |
73 | 124 | }
|
74 | 125 |
|
75 |
| - private interface JsonBodyFilter extends BodyFilter, Function<DocumentContext, String> { |
| 126 | + @AllArgsConstructor |
| 127 | + private static final class CompositeOperation implements Operation { |
| 128 | + |
| 129 | + private final Collection<Operation> operations; |
| 130 | + |
76 | 131 | @Override
|
77 |
| - default String filter(@Nullable String contentType, String body) { |
78 |
| - if (JsonMediaType.JSON.test(contentType)) { |
79 |
| - return apply(JsonPath.using(conf).parse(body)); |
| 132 | + public DocumentContext filter(final DocumentContext context) { |
| 133 | + DocumentContext result = context; |
| 134 | + |
| 135 | + for (final Operation operation : operations) { |
| 136 | + result = operation.filter(result); |
80 | 137 | }
|
81 |
| - return body; |
| 138 | + |
| 139 | + return result; |
82 | 140 | }
|
83 | 141 | }
|
84 | 142 |
|
85 |
| - public static JsonPathBodyFilterBuilder jsonPath(String jsonPath) { |
86 |
| - return new JsonPathBodyFilterBuilder(JsonPath.compile(jsonPath)); |
| 143 | + public static JsonPathBodyFilterBuilder jsonPath(final String jsonPath) { |
| 144 | + return new JsonPathBodyFilterBuilder(compile(jsonPath)); |
87 | 145 | }
|
| 146 | + |
88 | 147 | }
|
0 commit comments