Search in sources :

Example 1 with EnrichMediator

use of org.apache.synapse.mediators.elementary.EnrichMediator in project wso2-synapse by wso2.

the class EnrichMediatorFactory method createSpecificMediator.

@Override
protected Mediator createSpecificMediator(OMElement elem, Properties properties) {
    if (!XML_Q.equals(elem.getQName())) {
        handleException("Unable to create the enrich mediator. " + "Unexpected element as the enrich mediator configuration");
    }
    EnrichMediator enrich = new EnrichMediator();
    processAuditStatus(enrich, elem);
    OMElement sourceEle = elem.getFirstChildWithName(SOURCE_Q);
    if (sourceEle == null) {
        handleException("source element is mandatory");
    }
    Source source = new Source();
    enrich.setSource(source);
    OMElement targetEle = elem.getFirstChildWithName(TARGET_Q);
    if (targetEle == null) {
        handleException("target element is mandatory");
    }
    Target target = new Target();
    enrich.setTarget(target);
    validateTypeCombination(sourceEle, targetEle);
    populateSource(source, sourceEle);
    populateTarget(target, targetEle);
    // check whether the inline element of the source is XML
    boolean isInlineSourceXML = false;
    String inlineString = null;
    if (source.getInlineOMNode() != null) {
        if (source.getInlineOMNode() instanceof OMText) {
            inlineString = ((OMTextImpl) source.getInlineOMNode()).getText();
            JsonParser parser = new JsonParser();
            try {
                JsonElement element = parser.parse(inlineString);
                if (!(element instanceof JsonObject || element instanceof JsonArray || element instanceof JsonPrimitive)) {
                    isInlineSourceXML = true;
                }
            } catch (JsonSyntaxException ex) {
                // Therefore, we will check if it is JSON by checking for {}
                if (!(inlineString.trim().startsWith("{") && inlineString.trim().endsWith("}"))) {
                    // not a JSON. Going ahead with XML
                    isInlineSourceXML = true;
                }
            }
        } else if (source.getInlineOMNode() instanceof OMElement) {
            inlineString = ((OMElement) source.getInlineOMNode()).getText();
            isInlineSourceXML = true;
        }
        if (!StringUtils.isEmpty(inlineString)) {
            enrich.setContainsInlineExpressions(InlineExpressionUtil.checkForInlineExpressions(inlineString));
        }
    }
    // Check the enrich mediator configuration to see whether it can support JSON natively
    boolean sourceHasCustom = (source.getSourceType() == EnrichMediator.CUSTOM);
    boolean targetHasCustom = (target.getTargetType() == EnrichMediator.CUSTOM);
    boolean enrichHasCustom = (sourceHasCustom || targetHasCustom);
    boolean sourceHasEnvelope = (source.getSourceType() == EnrichMediator.ENVELOPE);
    boolean targetHasEnvelope = (target.getTargetType() == EnrichMediator.ENVELOPE);
    boolean enrichHasEnvelope = (sourceHasEnvelope || targetHasEnvelope);
    boolean sourceHasACustomJsonPath = false;
    boolean targetHasACustomJsonPath = false;
    if (sourceHasCustom) {
        sourceHasACustomJsonPath = SynapsePath.JSON_PATH.equals(source.getXpath().getPathType());
    }
    if (targetHasCustom) {
        targetHasACustomJsonPath = SynapsePath.JSON_PATH.equals(target.getXpath().getPathType());
    }
    // conditions where native-json-processing is supported
    boolean condition1 = (!enrichHasCustom);
    boolean condition2 = (sourceHasACustomJsonPath && !targetHasCustom);
    boolean condition3 = (!sourceHasCustom && targetHasACustomJsonPath);
    boolean condition4 = (sourceHasACustomJsonPath && targetHasACustomJsonPath);
    boolean condition5 = !enrichHasEnvelope;
    enrich.setNativeJsonSupportEnabled(!isInlineSourceXML && condition5 && (condition1 || condition2 || condition3 || condition4));
    addAllCommentChildrenToList(elem, enrich.getCommentsList());
    return enrich;
}
Also used : EnrichMediator(org.apache.synapse.mediators.elementary.EnrichMediator) JsonPrimitive(com.google.gson.JsonPrimitive) JsonObject(com.google.gson.JsonObject) OMElement(org.apache.axiom.om.OMElement) Source(org.apache.synapse.mediators.elementary.Source) JsonArray(com.google.gson.JsonArray) Target(org.apache.synapse.mediators.elementary.Target) JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonElement(com.google.gson.JsonElement) OMText(org.apache.axiom.om.OMText) JsonParser(com.google.gson.JsonParser)

Example 2 with EnrichMediator

use of org.apache.synapse.mediators.elementary.EnrichMediator in project wso2-synapse by wso2.

the class EnrichMediatorSerializer method serializeSpecificMediator.

@Override
protected OMElement serializeSpecificMediator(Mediator m) {
    assert m != null : "mediator cannot be null";
    assert m instanceof EnrichMediator : "mediator should be of type EnrichMediator";
    EnrichMediator mediator = (EnrichMediator) m;
    OMElement enrichEle = fac.createOMElement("enrich", synNS);
    OMElement sourceEle = serializeSource(mediator.getSource());
    OMElement targetEle = serializeTarget(mediator.getTarget());
    enrichEle.addChild(sourceEle);
    enrichEle.addChild(targetEle);
    EIPUtils.setJsonPathConfiguration();
    serializeComments(enrichEle, mediator.getCommentsList());
    return enrichEle;
}
Also used : EnrichMediator(org.apache.synapse.mediators.elementary.EnrichMediator) OMElement(org.apache.axiom.om.OMElement)

Aggregations

OMElement (org.apache.axiom.om.OMElement)2 EnrichMediator (org.apache.synapse.mediators.elementary.EnrichMediator)2 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 OMText (org.apache.axiom.om.OMText)1 Source (org.apache.synapse.mediators.elementary.Source)1 Target (org.apache.synapse.mediators.elementary.Target)1