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;
}
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;
}
Aggregations