Search in sources :

Example 11 with ValidatorException

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;
}
Also used : AxisFault(org.apache.axis2.AxisFault) ParserException(org.apache.synapse.commons.json.jsonprocessor.exceptions.ParserException) SynapseLog(org.apache.synapse.SynapseLog) ValidatorException(org.apache.synapse.commons.json.jsonprocessor.exceptions.ValidatorException) InputStreamReader(java.io.InputStreamReader) OMTextImpl(org.apache.axiom.om.impl.llom.OMTextImpl) IOException(java.io.IOException) OMException(org.apache.axiom.om.OMException) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Aggregations

ValidatorException (org.apache.synapse.commons.json.jsonprocessor.exceptions.ValidatorException)11 JsonElement (com.google.gson.JsonElement)8 JsonObject (com.google.gson.JsonObject)6 JsonArray (com.google.gson.JsonArray)5 ParserException (org.apache.synapse.commons.json.jsonprocessor.exceptions.ParserException)4 Map (java.util.Map)2 JsonParser (com.google.gson.JsonParser)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1 OMException (org.apache.axiom.om.OMException)1 OMTextImpl (org.apache.axiom.om.impl.llom.OMTextImpl)1 AxisFault (org.apache.axis2.AxisFault)1 SynapseLog (org.apache.synapse.SynapseLog)1 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)1