use of org.apache.synapse.commons.json.jsonprocessor.exceptions.ValidatorException in project wso2-synapse by wso2.
the class JSONTransformMediator method mediate.
@Override
public boolean mediate(MessageContext synCtx) {
if (synCtx.getEnvironment().isDebuggerEnabled()) {
if (super.divertMediationRoute(synCtx)) {
return true;
}
}
SynapseLog synLog = getLog(synCtx);
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug("Start : JSONTransform mediator");
if (synLog.isTraceTraceEnabled()) {
synLog.traceTrace("Message : " + synCtx.getEnvelope());
}
}
if (!propertiesArrayList.isEmpty()) {
try {
// Passing the the custom jsonOutputFactory and converting the Envelope body to JSON
String jsonPayloadFromOMElement = JsonUtil.toJsonString(((Axis2MessageContext) synCtx).getAxis2MessageContext().getEnvelope().getBody().getFirstElement(), jsonOutputFactory).toString();
// Update the jsonstream with the newly build payload
JsonUtil.getNewJsonPayload(((Axis2MessageContext) synCtx).getAxis2MessageContext(), jsonPayloadFromOMElement, true, true);
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug("JSON stream after converting xml to json : " + jsonPayloadFromOMElement);
}
} catch (AxisFault af) {
handleException("Axisfault occured when updating the " + "JSON stream after applying the properties: ", af, synCtx);
}
}
if (schemaKey != null) {
// Derive actual key from message context
String generatedSchemaKey = schemaKey.evaluateValue(synCtx);
Object jsonSchemaObj = synCtx.getEntry(generatedSchemaKey);
if (jsonSchemaObj != null) {
String schema = "";
if (jsonSchemaObj instanceof OMTextImpl) {
try {
// reading the schema with the media-type application/json
schema = JsonLoader.fromReader(new InputStreamReader(((OMTextImpl) jsonSchemaObj).getInputStream())).toString();
} catch (OMException e) {
// reading the schema with the media type "unknown"
schema = ((OMTextImpl) jsonSchemaObj).getText();
} catch (IOException e) {
handleException("Error while reading schema from registry", e, synCtx);
}
} else if (jsonSchemaObj instanceof String) {
schema = (String) jsonSchemaObj;
} else {
handleException("Can not find valid JSON Schema content", synCtx);
}
try {
String jsonPayload;
if (JsonUtil.hasAJsonPayload(((Axis2MessageContext) synCtx).getAxis2MessageContext())) {
jsonPayload = JsonUtil.jsonPayloadToString(((Axis2MessageContext) synCtx).getAxis2MessageContext());
} else {
jsonPayload = JsonUtil.toJsonString(((Axis2MessageContext) synCtx).getAxis2MessageContext().getEnvelope().getBody().getFirstElement()).toString();
}
String result = JsonProcessor.parseJson(jsonPayload, schema);
JsonUtil.getNewJsonPayload(((Axis2MessageContext) synCtx).getAxis2MessageContext(), result, true, true);
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug("JSON stream after applying schema : " + ((result != null) ? result : ""));
}
} catch (ValidatorException | ParserException e) {
handleException(e.getMessage(), e, synCtx);
} catch (AxisFault af) {
handleException("Axisfault fault occured when updating the " + "JSON stream after applying the JSON schema", af, synCtx);
}
} else {
handleException("Schema does not exist in the specified location : " + generatedSchemaKey, synCtx);
}
}
synLog.traceOrDebug("End : JSON Transform mediator");
return true;
}
Aggregations