1 | /*
|
---|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
---|
3 | *
|
---|
4 | * Copyright (c) 2015-2017 Oracle and/or its affiliates. All rights reserved.
|
---|
5 | *
|
---|
6 | * The contents of this file are subject to the terms of either the GNU
|
---|
7 | * General Public License Version 2 only ("GPL") or the Common Development
|
---|
8 | * and Distribution License("CDDL") (collectively, the "License"). You
|
---|
9 | * may not use this file except in compliance with the License. You can
|
---|
10 | * obtain a copy of the License at
|
---|
11 | * https://oss.oracle.com/licenses/CDDL+GPL-1.1
|
---|
12 | * or LICENSE.txt. See the License for the specific
|
---|
13 | * language governing permissions and limitations under the License.
|
---|
14 | *
|
---|
15 | * When distributing the software, include this License Header Notice in each
|
---|
16 | * file and include the License file at LICENSE.txt.
|
---|
17 | *
|
---|
18 | * GPL Classpath Exception:
|
---|
19 | * Oracle designates this particular file as subject to the "Classpath"
|
---|
20 | * exception as provided by Oracle in the GPL Version 2 section of the License
|
---|
21 | * file that accompanied this code.
|
---|
22 | *
|
---|
23 | * Modifications:
|
---|
24 | * If applicable, add the following below the License Header, with the fields
|
---|
25 | * enclosed by brackets [] replaced by your own identifying information:
|
---|
26 | * "Portions Copyright [year] [name of copyright owner]"
|
---|
27 | *
|
---|
28 | * Contributor(s):
|
---|
29 | * If you wish your version of this file to be governed by only the CDDL or
|
---|
30 | * only the GPL Version 2, indicate your decision by adding "[Contributor]
|
---|
31 | * elects to include this software in this distribution under the [CDDL or GPL
|
---|
32 | * Version 2] license." If you don't indicate a single choice of license, a
|
---|
33 | * recipient has the option to distribute your version of this file under
|
---|
34 | * either the CDDL, the GPL Version 2 or to extend the choice of license to
|
---|
35 | * its licensees as provided above. However, if you add GPL Version 2 code
|
---|
36 | * and therefore, elected the GPL Version 2 license, then the option applies
|
---|
37 | * only if the new code is made subject to such option by the copyright
|
---|
38 | * holder.
|
---|
39 | */
|
---|
40 |
|
---|
41 | package org.glassfish.json;
|
---|
42 |
|
---|
43 | import javax.json.Json;
|
---|
44 | import javax.json.JsonArray;
|
---|
45 | import javax.json.JsonArrayBuilder;
|
---|
46 | import javax.json.JsonException;
|
---|
47 | import javax.json.JsonPatch;
|
---|
48 | import javax.json.JsonPatch.Operation;
|
---|
49 | import javax.json.JsonPatchBuilder;
|
---|
50 | import javax.json.JsonStructure;
|
---|
51 | import javax.json.JsonValue;
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * A builder for constructing a JSON Patch by adding
|
---|
55 | * JSON Patch operations incrementally.
|
---|
56 | * <p>
|
---|
57 | * The following illustrates the approach.
|
---|
58 | * <pre>
|
---|
59 | * JsonPatchBuilder builder = Json.createPatchBuilder();
|
---|
60 | * JsonPatch patch = builder.add("/John/phones/office", "1234-567")
|
---|
61 | * .remove("/Amy/age")
|
---|
62 | * .build();
|
---|
63 | * </pre>
|
---|
64 | * The result is equivalent to the following JSON Patch.
|
---|
65 | * <pre>
|
---|
66 | * [
|
---|
67 | * {"op" = "add", "path" = "/John/phones/office", "value" = "1234-567"},
|
---|
68 | * {"op" = "remove", "path" = "/Amy/age"}
|
---|
69 | * ] </pre>
|
---|
70 | *
|
---|
71 | * @since 1.1
|
---|
72 | */
|
---|
73 | public final class JsonPatchBuilderImpl implements JsonPatchBuilder {
|
---|
74 |
|
---|
75 | private final JsonArrayBuilder builder;
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Creates a JsonPatchBuilderImpl, starting with the specified
|
---|
79 | * JSON Patch
|
---|
80 | * @param patch the JSON Patch
|
---|
81 | */
|
---|
82 | public JsonPatchBuilderImpl(JsonArray patch) {
|
---|
83 | builder = Json.createArrayBuilder(patch);
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Creates JsonPatchBuilderImpl with empty JSON Patch
|
---|
88 | */
|
---|
89 | public JsonPatchBuilderImpl() {
|
---|
90 | builder = Json.createArrayBuilder();
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * A convenience method for {@code new JsonPatchImpl(build()).apply(target)}.
|
---|
95 | * The target is not modified by the patch.
|
---|
96 | *
|
---|
97 | * @param <T> the target type, must be a subtype of {@link JsonStructure}
|
---|
98 | * @param target the target to apply the patch operations
|
---|
99 | * @return the transformed target after the patch
|
---|
100 | * @throws JsonException if the supplied JSON Patch is malformed or if
|
---|
101 | * it contains references to non-existing members
|
---|
102 | */
|
---|
103 | public <T extends JsonStructure> T apply(T target) {
|
---|
104 | return build().apply(target);
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Adds an "add" JSON Patch operation.
|
---|
109 | * @param path the "path" member of the operation
|
---|
110 | * @param value the "value" member of the operation
|
---|
111 | * @return this JsonPatchBuilder
|
---|
112 | */
|
---|
113 | @Override
|
---|
114 | public JsonPatchBuilder add(String path, JsonValue value) {
|
---|
115 | builder.add(Json.createObjectBuilder()
|
---|
116 | .add("op", Operation.ADD.operationName())
|
---|
117 | .add("path", path)
|
---|
118 | .add("value", value)
|
---|
119 | );
|
---|
120 | return this;
|
---|
121 | }
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Adds an "add" JSON Patch operation
|
---|
125 | * @param path the "path" member of the operation
|
---|
126 | * @param value the "value" member of the operation
|
---|
127 | * @return this JsonPatchBuilder
|
---|
128 | */
|
---|
129 | @Override
|
---|
130 | public JsonPatchBuilder add(String path, String value) {
|
---|
131 | builder.add(Json.createObjectBuilder()
|
---|
132 | .add("op", Operation.ADD.operationName())
|
---|
133 | .add("path", path)
|
---|
134 | .add("value", value)
|
---|
135 | );
|
---|
136 | return this;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Adds an "add" JSON Patch operation
|
---|
141 | * @param path the "path" member of the operation
|
---|
142 | * @param value the "value" member of the operation
|
---|
143 | * @return this JsonPatchBuilder
|
---|
144 | */
|
---|
145 | @Override
|
---|
146 | public JsonPatchBuilder add(String path, int value) {
|
---|
147 | builder.add(Json.createObjectBuilder()
|
---|
148 | .add("op", Operation.ADD.operationName())
|
---|
149 | .add("path", path)
|
---|
150 | .add("value", value)
|
---|
151 | );
|
---|
152 | return this;
|
---|
153 | }
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Adds an "add" JSON Patch operation
|
---|
157 | * @param path the "path" member of the operation
|
---|
158 | * @param value the "value" member of the operation
|
---|
159 | * @return this JsonPatchBuilder
|
---|
160 | */
|
---|
161 | @Override
|
---|
162 | public JsonPatchBuilder add(String path, boolean value) {
|
---|
163 | builder.add(Json.createObjectBuilder()
|
---|
164 | .add("op", Operation.ADD.operationName())
|
---|
165 | .add("path", path)
|
---|
166 | .add("value", value)
|
---|
167 | );
|
---|
168 | return this;
|
---|
169 | }
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Adds a "remove" JSON Patch operation.
|
---|
173 | * @param path the "path" member of the operation
|
---|
174 | * @return this JsonPatchBuilder
|
---|
175 | */
|
---|
176 | @Override
|
---|
177 | public JsonPatchBuilder remove(String path) {
|
---|
178 | builder.add(Json.createObjectBuilder()
|
---|
179 | .add("op", Operation.REMOVE.operationName())
|
---|
180 | .add("path", path)
|
---|
181 | );
|
---|
182 | return this;
|
---|
183 | }
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * Adds a "replace" JSON Patch operation.
|
---|
187 | * @param path the "path" member of the operation
|
---|
188 | * @param value the "value" member of the operation
|
---|
189 | * @return this JsonPatchBuilder
|
---|
190 | */
|
---|
191 | @Override
|
---|
192 | public JsonPatchBuilder replace(String path, JsonValue value) {
|
---|
193 | builder.add(Json.createObjectBuilder()
|
---|
194 | .add("op", Operation.REPLACE.operationName())
|
---|
195 | .add("path", path)
|
---|
196 | .add("value", value)
|
---|
197 | );
|
---|
198 | return this;
|
---|
199 | }
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * Adds a "replace" JSON Patch operation.
|
---|
203 | * @param path the "path" member of the operation
|
---|
204 | * @param value the "value" member of the operation
|
---|
205 | * @return this JsonPatchBuilder
|
---|
206 | */
|
---|
207 | @Override
|
---|
208 | public JsonPatchBuilder replace(String path, String value) {
|
---|
209 | builder.add(Json.createObjectBuilder()
|
---|
210 | .add("op", Operation.REPLACE.operationName())
|
---|
211 | .add("path", path)
|
---|
212 | .add("value", value)
|
---|
213 | );
|
---|
214 | return this;
|
---|
215 | }
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * Adds a "replace" JSON Patch operation.
|
---|
219 | * @param path the "path" member of the operation
|
---|
220 | * @param value the "value" member of the operation
|
---|
221 | * @return this JsonPatchBuilder
|
---|
222 | */
|
---|
223 | @Override
|
---|
224 | public JsonPatchBuilder replace(String path, int value) {
|
---|
225 | builder.add(Json.createObjectBuilder()
|
---|
226 | .add("op", Operation.REPLACE.operationName())
|
---|
227 | .add("path", path)
|
---|
228 | .add("value", value)
|
---|
229 | );
|
---|
230 | return this;
|
---|
231 | }
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * Adds a "replace" JSON Patch operation.
|
---|
235 | * @param path the "path" member of the operation
|
---|
236 | * @param value the "value" member of the operation
|
---|
237 | * @return this JsonPatchBuilder
|
---|
238 | */
|
---|
239 | @Override
|
---|
240 | public JsonPatchBuilder replace(String path, boolean value) {
|
---|
241 | builder.add(Json.createObjectBuilder()
|
---|
242 | .add("op", Operation.REPLACE.operationName())
|
---|
243 | .add("path", path)
|
---|
244 | .add("value", value)
|
---|
245 | );
|
---|
246 | return this;
|
---|
247 | }
|
---|
248 |
|
---|
249 | /**
|
---|
250 | * Adds a "move" JSON Patch operation.
|
---|
251 | * @param path the "path" member of the operation
|
---|
252 | * @param from the "from" member of the operation
|
---|
253 | * @return this JsonPatchBuilder
|
---|
254 | */
|
---|
255 | @Override
|
---|
256 | public JsonPatchBuilder move(String path, String from) {
|
---|
257 | builder.add(Json.createObjectBuilder()
|
---|
258 | .add("op", Operation.MOVE.operationName())
|
---|
259 | .add("path", path)
|
---|
260 | .add("from", from)
|
---|
261 | );
|
---|
262 | return this;
|
---|
263 | }
|
---|
264 |
|
---|
265 | /**
|
---|
266 | * Adds a "copy" JSON Patch operation.
|
---|
267 | * @param path the "path" member of the operation
|
---|
268 | * @param from the "from" member of the operation
|
---|
269 | * @return this JsonPatchBuilder
|
---|
270 | */
|
---|
271 | @Override
|
---|
272 | public JsonPatchBuilder copy(String path, String from) {
|
---|
273 | builder.add(Json.createObjectBuilder()
|
---|
274 | .add("op", Operation.COPY.operationName())
|
---|
275 | .add("path", path)
|
---|
276 | .add("from", from)
|
---|
277 | );
|
---|
278 | return this;
|
---|
279 | }
|
---|
280 |
|
---|
281 | /**
|
---|
282 | * Adds a "test" JSON Patch operation.
|
---|
283 | * @param path the "path" member of the operation
|
---|
284 | * @param value the "value" member of the operation
|
---|
285 | * @return this JsonPatchBuilder
|
---|
286 | */
|
---|
287 | @Override
|
---|
288 | public JsonPatchBuilder test(String path, JsonValue value) {
|
---|
289 | builder.add(Json.createObjectBuilder()
|
---|
290 | .add("op", Operation.TEST.operationName())
|
---|
291 | .add("path", path)
|
---|
292 | .add("value", value)
|
---|
293 | );
|
---|
294 | return this;
|
---|
295 | }
|
---|
296 |
|
---|
297 | /**
|
---|
298 | * Adds a "test" JSON Patch operation.
|
---|
299 | * @param path the "path" member of the operation
|
---|
300 | * @param value the "value" member of the operation
|
---|
301 | * @return this JsonPatchBuilder
|
---|
302 | */
|
---|
303 | @Override
|
---|
304 | public JsonPatchBuilder test(String path, String value) {
|
---|
305 | builder.add(Json.createObjectBuilder()
|
---|
306 | .add("op", Operation.TEST.operationName())
|
---|
307 | .add("path", path)
|
---|
308 | .add("value", value)
|
---|
309 | );
|
---|
310 | return this;
|
---|
311 | }
|
---|
312 |
|
---|
313 | /**
|
---|
314 | * Adds a "test" JSON Patch operation.
|
---|
315 | * @param path the "path" member of the operation
|
---|
316 | * @param value the "value" member of the operation
|
---|
317 | * @return this JsonPatchBuilder
|
---|
318 | */
|
---|
319 | @Override
|
---|
320 | public JsonPatchBuilder test(String path, int value) {
|
---|
321 | builder.add(Json.createObjectBuilder()
|
---|
322 | .add("op", Operation.TEST.operationName())
|
---|
323 | .add("path", path)
|
---|
324 | .add("value", value)
|
---|
325 | );
|
---|
326 | return this;
|
---|
327 | }
|
---|
328 |
|
---|
329 | /**
|
---|
330 | * Adds a "test" JSON Patch operation.
|
---|
331 | * @param path the "path" member of the operation
|
---|
332 | * @param value the "value" member of the operation
|
---|
333 | * @return this JsonPatchBuilder
|
---|
334 | */
|
---|
335 | @Override
|
---|
336 | public JsonPatchBuilder test(String path, boolean value) {
|
---|
337 | builder.add(Json.createObjectBuilder()
|
---|
338 | .add("op", Operation.TEST.operationName())
|
---|
339 | .add("path", path)
|
---|
340 | .add("value", value)
|
---|
341 | );
|
---|
342 | return this;
|
---|
343 | }
|
---|
344 |
|
---|
345 | /**
|
---|
346 | * Returns the patch operations in a JsonArray
|
---|
347 | * @return the patch operations in a JsonArray
|
---|
348 | */
|
---|
349 | public JsonArray buildAsJsonArray() {
|
---|
350 | return builder.build();
|
---|
351 | }
|
---|
352 |
|
---|
353 | /**
|
---|
354 | * Returns the patch operation in a JsonPatch
|
---|
355 | * @return the patch operation in a JsonPatch
|
---|
356 | */
|
---|
357 | @Override
|
---|
358 | public JsonPatch build() {
|
---|
359 | return new JsonPatchImpl(buildAsJsonArray());
|
---|
360 | }
|
---|
361 | }
|
---|
362 |
|
---|