Skip to content

Commit

Permalink
Add JSON Patch processor; update -examples version dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
fge committed Mar 23, 2013
1 parent d696168 commit e6448bb
Show file tree
Hide file tree
Showing 22 changed files with 1,138 additions and 36 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
<dependency>
<groupId>com.github.fge</groupId>
<artifactId>json-schema-processor-examples</artifactId>
<version>0.4</version>
<version>0.5</version>
</dependency>

<dependency>
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/github/fge/jsonschema/load/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public Set<Class<?>> getClasses()
builder.add(Schema2PojoLoader.class);
builder.add(SyntaxLoader.class);
builder.add(AvroLoader.class);
builder.add(JsonPatchLoader.class);

return builder.build();
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/github/fge/jsonschema/load/AvroLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

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.github.fge.jsonschema.constants.ResponseFields;
import com.github.fge.jsonschema.util.JacksonUtils;
import com.github.fge.jsonschema.util.JsonLoader;
import com.google.common.collect.ImmutableList;

import javax.ws.rs.Path;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/github/fge/jsonschema/load/IndexLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.fge.jsonschema.util.JacksonUtils;
import com.github.fge.jsonschema.util.JsonLoader;
import com.github.fge.jackson.JacksonUtils;
import com.github.fge.jackson.JsonLoader;
import com.google.common.collect.ImmutableList;

import javax.ws.rs.Path;
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/com/github/fge/jsonschema/load/JsonPatchLoader.java
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.github.fge.jsonschema.util.JacksonUtils;
import com.github.fge.jackson.JacksonUtils;

import javax.ws.rs.GET;
import javax.ws.rs.Produces;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

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.github.fge.jsonschema.constants.ResponseFields;
import com.github.fge.jsonschema.util.JacksonUtils;
import com.github.fge.jsonschema.util.JsonLoader;

import javax.ws.rs.Path;
import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

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.github.fge.jsonschema.constants.ResponseFields;
import com.github.fge.jsonschema.util.JacksonUtils;
import com.github.fge.jsonschema.util.JsonLoader;
import com.google.common.collect.ImmutableList;

import javax.ws.rs.Path;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/github/fge/jsonschema/process/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public Set<Class<?>> getClasses()
builder.add(JJSchemaProcessing.class);
builder.add(Schema2PojoProcessing.class);
builder.add(AvroProcessing.class);
builder.add(JsonPatch.class);

return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.fge.avro.Avro2JsonSchemaProcessor;
import com.github.fge.jackson.JacksonUtils;
import com.github.fge.jsonschema.exceptions.ProcessingException;
import com.github.fge.jsonschema.library.DraftV4Library;
import com.github.fge.jsonschema.processing.ProcessingResult;
Expand All @@ -14,9 +15,7 @@
import com.github.fge.jsonschema.tree.JsonTree;
import com.github.fge.jsonschema.tree.SchemaTree;
import com.github.fge.jsonschema.tree.SimpleJsonTree;
import com.github.fge.jsonschema.util.JacksonUtils;
import com.github.fge.jsonschema.util.ValueHolder;
import com.github.fge.util.SimpleValueHolder;

import javax.ws.rs.Path;
import java.io.IOException;
Expand Down Expand Up @@ -53,8 +52,7 @@ protected JsonNode buildResult(final String input)
return ret;

final JsonTree tree = new SimpleJsonTree(schemaNode);
final ValueHolder<JsonTree> holder
= new SimpleValueHolder<JsonTree>(tree);
final ValueHolder<JsonTree> holder = ValueHolder.hold(tree);

final ProcessingReport report = new ListProcessingReport();
final ProcessingResult<ValueHolder<SchemaTree>> result
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/github/fge/jsonschema/process/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
import com.fasterxml.jackson.databind.JsonNode;
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.jsonschema.constants.ParseError;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
import com.github.fge.jsonschema.main.JsonValidator;
import com.github.fge.jsonschema.report.ProcessingReport;
import com.github.fge.jsonschema.util.AsJson;
import com.github.fge.jsonschema.util.JacksonUtils;
import com.github.fge.jsonschema.util.JsonLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -39,6 +39,7 @@
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.IOException;

import static com.github.fge.jsonschema.constants.ResponseFields.*;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.fge.jackson.JacksonUtils;
import com.github.fge.jjschema.JJSchemaFromSource;
import com.github.fge.jjschema.SourceHolder;
import com.github.fge.jsonschema.exceptions.ProcessingException;
import com.github.fge.jsonschema.processing.ProcessingResult;
import com.github.fge.jsonschema.report.ListProcessingReport;
import com.github.fge.jsonschema.report.ProcessingReport;
import com.github.fge.jsonschema.tree.SchemaTree;
import com.github.fge.jsonschema.util.JacksonUtils;
import com.github.fge.jsonschema.util.ValueHolder;

import javax.ws.rs.Path;
Expand All @@ -28,13 +27,12 @@ public final class JJSchemaProcessing
protected JsonNode buildResult(final String input)
throws IOException, ProcessingException
{
final SourceHolder holder = new SourceHolder(input);
final ListProcessingReport report = new ListProcessingReport();
final ValueHolder<String> holder = ValueHolder.hold("source", input);
final ProcessingReport report = new ListProcessingReport();
final ProcessingResult<ValueHolder<SchemaTree>> result
= ProcessingResult.uncheckedResult(PROCESSOR, report, holder);

final ProcessingReport processingReport
= result.getReport();
final ProcessingReport processingReport = result.getReport();

final ObjectNode ret = FACTORY.objectNode();
final boolean success = processingReport.isSuccess();
Expand Down
136 changes: 136 additions & 0 deletions src/main/java/com/github/fge/jsonschema/process/JsonPatch.java
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
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.jsonschema.constants.ParseError;
import com.github.fge.jsonschema.exceptions.ProcessingException;
import com.github.fge.jsonschema.report.ProcessingMessage;
import com.github.fge.jsonschema.report.ProcessingReport;
import com.github.fge.jsonschema.util.JacksonUtils;
import com.github.fge.jsonschema.util.JsonLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.fge.jackson.JacksonUtils;
import com.github.fge.jsonschema.exceptions.ProcessingException;
import com.github.fge.jsonschema.processing.ProcessingResult;
import com.github.fge.jsonschema.report.ListProcessingReport;
import com.github.fge.jsonschema.report.ProcessingReport;
import com.github.fge.jsonschema.tree.CanonicalSchemaTree;
import com.github.fge.jsonschema.tree.SchemaTree;
import com.github.fge.jsonschema.util.JacksonUtils;
import com.github.fge.jsonschema.util.ValueHolder;
import com.github.fge.jsonschema2pojo.JsonSchema2SourceCode;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.fge.jackson.JacksonUtils;
import com.github.fge.jsonschema.cfg.ValidationConfiguration;
import com.github.fge.jsonschema.exceptions.ProcessingException;
import com.github.fge.jsonschema.processors.syntax.SyntaxValidator;
import com.github.fge.jsonschema.report.ProcessingReport;
import com.github.fge.jsonschema.util.JacksonUtils;

import javax.ws.rs.Path;
import java.io.IOException;
Expand Down

0 comments on commit e6448bb

Please sign in to comment.