Search in sources :

Example 1 with PayloadFactoryMediator

use of org.apache.synapse.mediators.transform.PayloadFactoryMediator in project wso2-synapse by wso2.

the class PayloadFactoryMediatorSerializerTest method testSerializeSpecificMediator2.

/**
 * Test SerializeSpecificMediator method with format added for payloadFactory mediator
 * and assert the format added in OmElement.
 */
@Test
public void testSerializeSpecificMediator2() {
    PayloadFactoryMediatorSerializer serializer = new PayloadFactoryMediatorSerializer();
    PayloadFactoryMediator payloadFactoryMediator = new PayloadFactoryMediator();
    payloadFactoryMediator.setFormat(format);
    OMElement element = serializer.serializeSpecificMediator(payloadFactoryMediator);
    Assert.assertNotNull(element);
    Assert.assertTrue("Format set for Mediator is not serialized", element.getChildren().next().toString().contains("<p:echoInt xmlns:p=\"" + "http://echo.services.core.carbon.wso2.org\">"));
}
Also used : PayloadFactoryMediator(org.apache.synapse.mediators.transform.PayloadFactoryMediator) OMElement(org.apache.axiom.om.OMElement) Test(org.junit.Test)

Example 2 with PayloadFactoryMediator

use of org.apache.synapse.mediators.transform.PayloadFactoryMediator in project wso2-synapse by wso2.

the class PayloadFactoryMediatorSerializerTest method testSerializeSpecificMediator4.

/**
 * Test SerializeSpecificMediator method with PathArgument added for payloadFactory mediator
 * and assert that argument is added.
 */
@Test
public void testSerializeSpecificMediator4() throws JaxenException {
    PayloadFactoryMediatorSerializer serializer = new PayloadFactoryMediatorSerializer();
    PayloadFactoryMediator payloadFactoryMediator = new PayloadFactoryMediator();
    Argument argument = new Argument();
    argument.setValue("TestArgument1");
    payloadFactoryMediator.addPathArgument(argument);
    payloadFactoryMediator.setFormat(format);
    OMElement element = serializer.serializeSpecificMediator(payloadFactoryMediator);
    MediatorFactory mediatorFactory = new PayloadFactoryMediatorFactory();
    Mediator mediator = mediatorFactory.createMediator(element, null);
    Assert.assertNotNull(element);
    Assert.assertEquals("Path argument added is not serialized", "TestArgument1", ((PayloadFactoryMediator) mediator).getPathArgumentList().get(0).getValue());
}
Also used : Argument(org.apache.synapse.mediators.transform.Argument) PayloadFactoryMediator(org.apache.synapse.mediators.transform.PayloadFactoryMediator) OMElement(org.apache.axiom.om.OMElement) PayloadFactoryMediator(org.apache.synapse.mediators.transform.PayloadFactoryMediator) Mediator(org.apache.synapse.Mediator) Test(org.junit.Test)

Example 3 with PayloadFactoryMediator

use of org.apache.synapse.mediators.transform.PayloadFactoryMediator in project wso2-synapse by wso2.

the class PayloadFactoryMediatorSerializerTest method testSerializeSpecificMediator3.

/**
 * Test SerializeSpecificMediator method with Dynamic format added for payloadFactory mediator
 * and assert that dynamic format is set.
 */
@Test
public void testSerializeSpecificMediator3() {
    PayloadFactoryMediatorSerializer serializer = new PayloadFactoryMediatorSerializer();
    PayloadFactoryMediator payloadFactoryMediator = new PayloadFactoryMediator();
    payloadFactoryMediator.setFormat(format);
    payloadFactoryMediator.setFormatDynamic(true);
    payloadFactoryMediator.setFormatKey(new Value("testKey"));
    OMElement element = serializer.serializeSpecificMediator(payloadFactoryMediator);
    Assert.assertNotNull(element);
    MediatorFactory mediatorFactory = new PayloadFactoryMediatorFactory();
    Mediator mediator = mediatorFactory.createMediator(element, null);
    Assert.assertTrue("Dynamic Format set for Mediator is not serialized", ((PayloadFactoryMediator) mediator).isFormatDynamic());
}
Also used : PayloadFactoryMediator(org.apache.synapse.mediators.transform.PayloadFactoryMediator) Value(org.apache.synapse.mediators.Value) OMElement(org.apache.axiom.om.OMElement) PayloadFactoryMediator(org.apache.synapse.mediators.transform.PayloadFactoryMediator) Mediator(org.apache.synapse.Mediator) Test(org.junit.Test)

Example 4 with PayloadFactoryMediator

use of org.apache.synapse.mediators.transform.PayloadFactoryMediator in project wso2-synapse by wso2.

the class PayloadFactoryMediatorSerializer method serializeSpecificMediator.

public OMElement serializeSpecificMediator(Mediator m) {
    if (!(m instanceof PayloadFactoryMediator)) {
        handleException("Unsupported mediator passed in for serialization : " + m.getType());
        return null;
    }
    PayloadFactoryMediator mediator = (PayloadFactoryMediator) m;
    OMElement payloadFactoryElem = fac.createOMElement(PAYLOAD_FACTORY, synNS);
    if (mediator.getType() != null) {
        payloadFactoryElem.addAttribute(fac.createOMAttribute(MEDIA_TYPE, null, mediator.getType()));
    }
    saveTracingState(payloadFactoryElem, mediator);
    if (!mediator.isFormatDynamic()) {
        if (mediator.getFormat() != null) {
            try {
                OMElement formatElem = fac.createOMElement(FORMAT, synNS);
                String type = mediator.getType();
                if (type != null && (type.contains(JSON_TYPE) || type.contains(TEXT))) {
                    formatElem.setText(mediator.getFormat());
                } else {
                    formatElem.addChild(AXIOMUtil.stringToOM(mediator.getFormat()));
                }
                payloadFactoryElem.addChild(formatElem);
            } catch (XMLStreamException e) {
                handleException("Error while serializing payloadFactory mediator", e);
            }
        } else {
            handleException("Invalid payloadFactory mediator, format is required");
        }
    } else {
        // Serialize Value using ValueSerializer
        OMElement formatElem = fac.createOMElement(FORMAT, synNS);
        formatElem.addAttribute(fac.createOMAttribute("key", nullNS, mediator.getFormatKey().getKeyValue()));
        ValueSerializer keySerializer = new ValueSerializer();
        keySerializer.serializeValue(mediator.getFormatKey(), XMLConfigConstants.KEY, formatElem);
        payloadFactoryElem.addChild(formatElem);
    }
    OMElement argumentsElem = fac.createOMElement(ARGS, synNS);
    List<Argument> pathArgList = mediator.getPathArgumentList();
    if (null != pathArgList && pathArgList.size() > 0) {
        for (Argument arg : pathArgList) {
            if (arg.getExpression() == null && arg.getValue() == null) {
                continue;
            }
            OMElement argElem = fac.createOMElement(ARG, synNS);
            if (arg.isLiteral()) {
                // add literal attribute only it is true. (because default value is false)
                argElem.addAttribute(fac.createOMAttribute(LITERAL, nullNS, "true"));
            }
            if (null != arg.getExpression() && null != arg.getExpression().getPathType()) {
                argElem.addAttribute(fac.createOMAttribute(EVALUATOR, nullNS, getEvaluator(arg.getExpression().getPathType())));
            } else if (null == arg.getExpression() && arg.getValue() != null) {
                argElem.addAttribute(fac.createOMAttribute(VALUE, nullNS, arg.getValue()));
            } else {
                argElem.addAttribute(fac.createOMAttribute(EVALUATOR, nullNS, getEvaluator(SynapsePath.X_PATH)));
            }
            if (null != arg.getExpression()) {
                SynapsePathSerializer.serializePath(arg.getExpression(), argElem, EXPRESSION);
                // We don't want the "json-eval(" prefix in PayloadFactory, for backward compatibility and since
                // PF has evaluator attribute.
                QName EXPR_Q = new QName(EXPRESSION);
                String strExpr = argElem.getAttribute(EXPR_Q).getAttributeValue();
                if (strExpr.startsWith("json-eval(")) {
                    argElem.getAttribute(EXPR_Q).setAttributeValue(strExpr.substring(10, strExpr.length() - 1));
                }
            }
            argumentsElem.addChild(argElem);
        }
    }
    payloadFactoryElem.addChild(argumentsElem);
    return payloadFactoryElem;
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) Argument(org.apache.synapse.mediators.transform.Argument) PayloadFactoryMediator(org.apache.synapse.mediators.transform.PayloadFactoryMediator) QName(javax.xml.namespace.QName) OMElement(org.apache.axiom.om.OMElement)

Example 5 with PayloadFactoryMediator

use of org.apache.synapse.mediators.transform.PayloadFactoryMediator in project wso2-synapse by wso2.

the class PayloadFactoryMediatorFactory method createSpecificMediator.

public Mediator createSpecificMediator(OMElement elem, Properties properties) {
    PayloadFactoryMediator payloadFactoryMediator = new PayloadFactoryMediator();
    processAuditStatus(payloadFactoryMediator, elem);
    String mediaTypeValue = elem.getAttributeValue(TYPE_Q);
    // for the backward compatibility.
    if (mediaTypeValue != null) {
        // set the mediaType for the PF
        payloadFactoryMediator.setType(mediaTypeValue);
    } else {
        payloadFactoryMediator.setType(XML_TYPE);
    }
    OMElement formatElem = elem.getFirstChildWithName(FORMAT_Q);
    if (formatElem != null) {
        OMAttribute n = formatElem.getAttribute(ATT_KEY);
        if (n == null) {
            // OMElement copy = formatElem.getFirstElement().cloneOMElement();
            OMElement copy = formatElem.cloneOMElement();
            removeIndentations(copy);
            if (mediaTypeValue != null && (mediaTypeValue.contains(JSON_TYPE) || mediaTypeValue.contains(TEXT_TYPE))) {
                payloadFactoryMediator.setFormat(copy.getText());
            } else {
                payloadFactoryMediator.setFormat(copy.getFirstElement().toString());
            }
        } else {
            ValueFactory keyFac = new ValueFactory();
            Value generatedKey = keyFac.createValue(XMLConfigConstants.KEY, formatElem);
            payloadFactoryMediator.setFormatKey(generatedKey);
            payloadFactoryMediator.setFormatDynamic(true);
        }
    } else {
        handleException("format element of payloadFactoryMediator is required");
    }
    OMElement argumentsElem = elem.getFirstChildWithName(ARGS_Q);
    if (argumentsElem != null) {
        Iterator itr = argumentsElem.getChildElements();
        while (itr.hasNext()) {
            OMElement argElem = (OMElement) itr.next();
            Argument arg = new Argument();
            String value;
            boolean isLiteral = false;
            String isLiteralString = argElem.getAttributeValue(ATT_LITERAL);
            if (isLiteralString != null) {
                // if literal is 'true' then set literal to true. defaults to false.
                isLiteral = Boolean.parseBoolean(isLiteralString);
            }
            arg.setLiteral(isLiteral);
            if ((value = argElem.getAttributeValue(ATT_VALUE)) != null) {
                arg.setValue(value);
                arg.setExpression(null);
                payloadFactoryMediator.addPathArgument(arg);
            } else if ((value = argElem.getAttributeValue(ATT_EXPRN)) != null) {
                if (value.trim().length() == 0) {
                    handleException("Attribute value for xpath cannot be empty");
                } else {
                    try {
                        // set the evaluator
                        String evaluator = argElem.getAttributeValue(ATT_EVAL);
                        if (evaluator != null && evaluator.equals(JSON_TYPE)) {
                            if (value.startsWith("json-eval(")) {
                                value = value.substring(10, value.length() - 1);
                            }
                            arg.setExpression(SynapseJsonPathFactory.getSynapseJsonPath(value));
                            // we have to explicitly define the path type since we are not going to mark
                            // JSON Path's with prefix "json-eval(".
                            arg.getExpression().setPathType(SynapsePath.JSON_PATH);
                            payloadFactoryMediator.addPathArgument(arg);
                        } else {
                            SynapseXPath sxp = SynapseXPathFactory.getSynapseXPath(argElem, ATT_EXPRN);
                            // we need to disable stream Xpath forcefully
                            sxp.setForceDisableStreamXpath(Boolean.TRUE);
                            arg.setExpression(sxp);
                            arg.getExpression().setPathType(SynapsePath.X_PATH);
                            payloadFactoryMediator.addPathArgument(arg);
                        }
                    } catch (JaxenException e) {
                        handleException("Invalid XPath expression for attribute expression : " + value, e);
                    }
                }
            } else {
                handleException("Unsupported arg type. value or expression attribute required");
            }
        }
    }
    return payloadFactoryMediator;
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) Argument(org.apache.synapse.mediators.transform.Argument) PayloadFactoryMediator(org.apache.synapse.mediators.transform.PayloadFactoryMediator) JaxenException(org.jaxen.JaxenException) Value(org.apache.synapse.mediators.Value) Iterator(java.util.Iterator) OMElement(org.apache.axiom.om.OMElement) OMAttribute(org.apache.axiom.om.OMAttribute)

Aggregations

PayloadFactoryMediator (org.apache.synapse.mediators.transform.PayloadFactoryMediator)9 OMElement (org.apache.axiom.om.OMElement)6 Test (org.junit.Test)5 Argument (org.apache.synapse.mediators.transform.Argument)4 Mediator (org.apache.synapse.Mediator)3 ArrayList (java.util.ArrayList)2 MessageContext (org.apache.synapse.MessageContext)2 Value (org.apache.synapse.mediators.Value)2 SynapseXPath (org.apache.synapse.util.xpath.SynapseXPath)2 Iterator (java.util.Iterator)1 QName (javax.xml.namespace.QName)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 OMAttribute (org.apache.axiom.om.OMAttribute)1 JaxenException (org.jaxen.JaxenException)1