use of com.fasterxml.jackson.core.JsonParseException in project dcos-commons by mesosphere.
the class DefaultServiceSpecTest method invalidTaskName.
@Test
public void invalidTaskName() throws Exception {
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("invalid-task-name.yml").getFile());
try {
DefaultServiceSpec.newGenerator(file, SCHEDULER_CONFIG).build();
Assert.fail("Expected exception");
} catch (JsonMappingException e) {
Assert.assertTrue(e.getCause().toString(), e.getCause() instanceof JsonParseException);
JsonParseException cause = (JsonParseException) e.getCause();
Assert.assertTrue(cause.getMessage(), cause.getMessage().contains("Duplicate field 'meta-data-task'"));
}
}
use of com.fasterxml.jackson.core.JsonParseException in project service-proxy by membrane.
the class JSONBody method write.
@Override
public void write(XMLStreamWriter out) throws XMLStreamException {
out.writeAttribute("type", "json");
try {
final JsonFactory jsonFactory = new JsonFactory();
final JsonParser jp = jsonFactory.createParser(new InputStreamReader(msg.getBodyAsStreamDecoded(), msg.getCharset()));
final List<String> stack = new ArrayList<String>();
String name = "root";
OUTER: while (jp.nextToken() != null) {
switch(jp.getCurrentToken()) {
case START_OBJECT:
if (name != null) {
stack.add(name);
out.writeStartElement(name);
out.writeAttribute("type", "o");
name = null;
}
break;
case END_OBJECT:
out.writeEndElement();
name = stack.remove(stack.size() - 1);
if (stack.isEmpty())
break OUTER;
break;
case FIELD_NAME:
name = jp.getCurrentName();
break;
case START_ARRAY:
if (name != null) {
stack.add(name);
out.writeStartElement(name);
out.writeAttribute("type", "a");
}
name = "item";
break;
case END_ARRAY:
out.writeEndElement();
name = stack.remove(stack.size() - 1);
if (stack.isEmpty())
break OUTER;
break;
case VALUE_TRUE:
case VALUE_FALSE:
out.writeStartElement(name);
out.writeAttribute("type", "b");
out.writeCharacters(Boolean.toString(jp.getBooleanValue()));
out.writeEndElement();
break;
case VALUE_NULL:
out.writeStartElement(name);
out.writeAttribute("type", "n");
out.writeAttribute("isNull", "true");
out.writeEndElement();
break;
case VALUE_STRING:
case VALUE_NUMBER_INT:
case VALUE_NUMBER_FLOAT:
out.writeStartElement(name);
out.writeAttribute("type", jp.getCurrentToken() == JsonToken.VALUE_STRING ? "s" : jp.getCurrentToken() == JsonToken.VALUE_NUMBER_INT ? "i" : "f");
out.writeCharacters(jp.getText());
out.writeEndElement();
break;
case VALUE_EMBEDDED_OBJECT:
case NOT_AVAILABLE:
throw new RuntimeException(jp.getCurrentToken().toString());
}
}
} catch (JsonParseException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of com.fasterxml.jackson.core.JsonParseException in project service-proxy by membrane.
the class JSONValidator method validateMessage.
public Outcome validateMessage(Exchange exc, InputStream body, Charset charset, String source) throws Exception {
List<String> errors;
boolean success = true;
try {
JsonNode node = JsonLoader.fromReader(new InputStreamReader(body, charset));
ProcessingReport report = schema.validateUnchecked(node);
success = report.isSuccess();
errors = new ArrayList<String>();
for (ProcessingMessage message : report) errors.add(message.getMessage());
} catch (JsonParseException e) {
success = false;
errors = new ArrayList<String>();
errors.add(e.getMessage());
}
if (success) {
valid.incrementAndGet();
return Outcome.CONTINUE;
}
if (failureHandler == FailureHandler.VOID) {
StringBuilder message = new StringBuilder();
message.append(source);
message.append(": ");
for (String error : errors) {
message.append(error);
message.append(";");
}
exc.setProperty("error", message.toString());
invalid.incrementAndGet();
return Outcome.ABORT;
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JsonGenerator jg = new JsonFactory().createGenerator(baos);
jg.writeStartObject();
jg.writeStringField("source", source);
jg.writeArrayFieldStart("errors");
for (String message : errors) jg.writeString(message);
jg.close();
if (failureHandler != null) {
failureHandler.handleFailure(new String(baos.toByteArray(), UTF8), exc);
exc.setResponse(Response.badRequest().contentType("application/json;charset=utf-8").body("{\"error\":\"error\"}".getBytes(UTF8)).build());
} else {
exc.setResponse(Response.badRequest().contentType("application/json;charset=utf-8").body(baos.toByteArray()).build());
}
invalid.incrementAndGet();
return Outcome.ABORT;
}
use of com.fasterxml.jackson.core.JsonParseException in project service-proxy by membrane.
the class EtcdResponse method get.
@SuppressWarnings("unchecked")
public String get(String name) {
JsonParser par = getParser(body);
String result = null;
Map<String, Object> respData = null;
try {
respData = new ObjectMapper().readValue(par, Map.class);
} catch (JsonParseException e) {
} catch (JsonMappingException e) {
} catch (IOException e) {
}
if (respData.containsKey("node")) {
LinkedHashMap<String, Object> nodeJson = (LinkedHashMap<String, Object>) respData.get("node");
if (nodeJson.containsKey(name)) {
result = nodeJson.get(name).toString();
}
}
if (result == null) {
throw new RuntimeException();
}
return result;
}
use of com.fasterxml.jackson.core.JsonParseException in project webprotege by protegeproject.
the class FormDataValueDeserializer method deserialize.
@Override
public FormDataValue deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
JsonNode node = p.readValueAsTree();
if (node.isTextual()) {
return FormDataPrimitive.get(node.asText());
} else if (node.isNumber()) {
return FormDataPrimitive.get(node.asDouble());
} else if (node.isBoolean()) {
return FormDataPrimitive.get(node.asBoolean());
} else if (node.isObject()) {
if (node.has("iri")) {
if (node.has("type")) {
IRI iri = IRI.create(node.get("iri").asText());
String type = node.get("type").asText();
switch(type) {
case "Class":
return FormDataPrimitive.get(df.getOWLClass(iri));
case "ObjectProperty":
return FormDataPrimitive.get(df.getOWLObjectProperty(iri));
case "DataProperty":
return FormDataPrimitive.get(df.getOWLDataProperty(iri));
case "AnnotationProperty":
return FormDataPrimitive.get(df.getOWLAnnotationProperty(iri));
case "Datatype":
return FormDataPrimitive.get(df.getOWLDatatype(iri));
case "NamedIndividual":
return FormDataPrimitive.get(df.getOWLDatatype(iri));
}
throw new JsonParseException(p, "Unrecognised entity type: " + type);
} else {
return FormDataPrimitive.get(IRI.create(node.get("iri").asText()));
}
} else if (node.has("literal")) {
String literal = node.get("literal").asText();
String lang = node.get("lang").asText("");
return FormDataPrimitive.get(df.getOWLLiteral(literal, lang));
}
}
throw new JsonParseException(p, "Cannot parse node as primitive value");
}
Aggregations