-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JSON Patch processor; update -examples version dependency
- Loading branch information
Showing
22 changed files
with
1,138 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/com/github/fge/jsonschema/load/JsonPatchLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.github.fge.jsonschema.load; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import com.github.fge.jackson.JacksonUtils; | ||
import com.github.fge.jackson.JsonLoader; | ||
import com.google.common.collect.ImmutableList; | ||
|
||
import javax.ws.rs.Path; | ||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Random; | ||
|
||
@Path("/jsonpatch") | ||
public final class JsonPatchLoader | ||
extends SampleLoader | ||
{ | ||
private static final Random RND = new Random(); | ||
private static final List<JsonNode> SAMPLE_DATA; | ||
private static final int SAMPLE_DATA_SIZE; | ||
|
||
static { | ||
try { | ||
final JsonNode node = JsonLoader.fromResource("/jsonpatch.json"); | ||
SAMPLE_DATA = ImmutableList.copyOf(node); | ||
SAMPLE_DATA_SIZE = SAMPLE_DATA.size(); | ||
} catch (IOException e) { | ||
throw new ExceptionInInitializerError(e); | ||
} | ||
} | ||
|
||
@Override | ||
protected JsonNode loadSample() | ||
{ | ||
final int index = RND.nextInt(SAMPLE_DATA_SIZE); | ||
final JsonNode sample = SAMPLE_DATA.get(index); | ||
final ObjectNode ret = FACTORY.objectNode(); | ||
final Map<String, JsonNode> map = JacksonUtils.asMap(sample); | ||
for (final Map.Entry<String, JsonNode> entry: map.entrySet()) | ||
ret.put(entry.getKey(), JacksonUtils.prettyPrint(entry.getValue())); | ||
|
||
return ret; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
src/main/java/com/github/fge/jsonschema/process/JsonPatch.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/* | ||
* Copyright (c) 2013, Francis Galiegue <fgaliegue@gmail.com> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the Lesser GNU General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* Lesser GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.github.fge.jsonschema.process; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.node.ArrayNode; | ||
import com.fasterxml.jackson.databind.node.JsonNodeFactory; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import com.github.fge.jackson.JacksonUtils; | ||
import com.github.fge.jackson.JsonLoader; | ||
import com.github.fge.jsonpatch.JsonPatchInput; | ||
import com.github.fge.jsonpatch.JsonPatchProcessor; | ||
import com.github.fge.jsonschema.constants.ParseError; | ||
import com.github.fge.jsonschema.processing.ProcessingResult; | ||
import com.github.fge.jsonschema.report.ListProcessingReport; | ||
import com.github.fge.jsonschema.report.ProcessingMessage; | ||
import com.github.fge.jsonschema.report.ProcessingReport; | ||
import com.github.fge.jsonschema.util.ValueHolder; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.FormParam; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
import java.io.IOException; | ||
|
||
import static com.github.fge.jsonschema.constants.ResponseFields.*; | ||
|
||
|
||
@Path("/jsonpatch") | ||
@Produces("application/json;charset=utf-8") | ||
public final class JsonPatch | ||
{ | ||
private static final Logger log = LoggerFactory.getLogger(JsonPatch.class); | ||
private static final Response OOPS = Response.status(500).build(); | ||
private static final JsonPatchProcessor PROCESSOR | ||
= new JsonPatchProcessor(); | ||
|
||
@POST | ||
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) | ||
public static Response validate(@FormParam("input") final String patch, | ||
@FormParam("input2") final String data | ||
) | ||
{ | ||
try { | ||
final JsonNode ret = buildResult(patch, data); | ||
return Response.ok().entity(ret.toString()).build(); | ||
} catch (IOException e) { | ||
log.error("I/O error while validating data", e); | ||
return OOPS; | ||
} | ||
} | ||
|
||
|
||
/* | ||
* Build the response. When we arrive here, we are guaranteed that we have | ||
* the needed elements. | ||
*/ | ||
private static JsonNode buildResult(final String rawPatch, | ||
final String rawData) | ||
throws IOException | ||
{ | ||
final ObjectNode ret = JsonNodeFactory.instance.objectNode(); | ||
|
||
final boolean invalidSchema = fillWithData(ret, INPUT, INVALID_INPUT, | ||
rawPatch); | ||
final boolean invalidData = fillWithData(ret, INPUT2, INVALID_INPUT2, | ||
rawData); | ||
|
||
final JsonNode patchNode = ret.remove(INPUT); | ||
final JsonNode data = ret.remove(INPUT2); | ||
|
||
if (invalidSchema || invalidData) | ||
return ret; | ||
|
||
final JsonPatchInput input = new JsonPatchInput(patchNode, data); | ||
|
||
final ProcessingReport report = new ListProcessingReport(); | ||
final ProcessingResult<ValueHolder<JsonNode>> result | ||
= ProcessingResult.uncheckedResult(PROCESSOR, report, input); | ||
|
||
final boolean success = result.isSuccess(); | ||
ret.put(VALID, success); | ||
final JsonNode node = result.isSuccess() ? result.getResult() | ||
.getValue() : buildReport(result.getReport()); | ||
ret.put(RESULTS, JacksonUtils.prettyPrint(node)); | ||
return ret; | ||
} | ||
|
||
/* | ||
* We have to use that since Java is not smart enough to detect that | ||
* sometimes, a variable is initialized in all paths. | ||
* | ||
* This returns true if the data is invalid. | ||
*/ | ||
private static boolean fillWithData(final ObjectNode node, | ||
final String onSuccess, final String onFailure, final String raw) | ||
throws IOException | ||
{ | ||
try { | ||
node.put(onSuccess, JsonLoader.fromString(raw)); | ||
return false; | ||
} catch (JsonProcessingException e) { | ||
node.put(onFailure, ParseError.build(e, raw.contains("\r\n"))); | ||
return true; | ||
} | ||
} | ||
|
||
private static JsonNode buildReport(final ProcessingReport report) | ||
{ | ||
final ArrayNode ret = JacksonUtils.nodeFactory().arrayNode(); | ||
for (final ProcessingMessage message: report) | ||
ret.add(message.asJson()); | ||
return ret; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.