Search in sources :

Example 6 with BeanMediator

use of org.apache.synapse.mediators.bean.BeanMediator in project wso2-synapse by wso2.

the class BeanMediatorFactory method createSpecificMediator.

public Mediator createSpecificMediator(OMElement elem, Properties properties) {
    BeanMediator mediator = new BeanMediator();
    processAuditStatus(mediator, elem);
    String attributeValue;
    attributeValue = elem.getAttributeValue(new QName(BeanConstants.VAR));
    if (attributeValue != null) {
        mediator.setVarName(attributeValue);
    } else {
        handleException("'var' attribute of Bean mediator is required.");
    }
    attributeValue = elem.getAttributeValue(new QName(BeanConstants.ACTION));
    if (attributeValue != null) {
        try {
            switch(BeanMediator.Action.valueOf(attributeValue.toUpperCase())) {
                case CREATE:
                    populateCreateBeanCase(mediator, elem);
                    break;
                case REMOVE:
                    mediator.setAction(BeanMediator.Action.REMOVE);
                    break;
                case SET_PROPERTY:
                    populateSetPropertyCase(mediator, elem);
                    break;
                case GET_PROPERTY:
                    populateGetPropertyCase(mediator, elem);
                    break;
                default:
                    assert false;
            }
        } catch (IllegalArgumentException e) {
            handleException("'action' attribute of bean mediator must be set to 'CREATE', " + "'REMOVE', 'SET_PROPERTY' or 'GET_PROPERTY'.");
        }
    } else {
        handleException("'action' attribute of Bean mediator is required.");
    }
    return mediator;
}
Also used : QName(javax.xml.namespace.QName) BeanMediator(org.apache.synapse.mediators.bean.BeanMediator)

Example 7 with BeanMediator

use of org.apache.synapse.mediators.bean.BeanMediator in project wso2-synapse by wso2.

the class BeanMediatorSerializer method serializeSpecificMediator.

public OMElement serializeSpecificMediator(Mediator m) {
    if (!(m instanceof BeanMediator)) {
        handleException("Unsupported mediator was passed in for serialization: " + m.getType());
        return null;
    }
    BeanMediator mediator = (BeanMediator) m;
    OMElement mediatorElem = fac.createOMElement(BEAN, synNS);
    saveTracingState(mediatorElem, mediator);
    if (mediator.getAction() != null) {
        mediatorElem.addAttribute(fac.createOMAttribute(BeanConstants.ACTION, nullNS, mediator.getAction().toString()));
    } else {
        handleException();
    }
    if (mediator.getVarName() != null) {
        mediatorElem.addAttribute(fac.createOMAttribute(BeanConstants.VAR, nullNS, mediator.getVarName()));
    } else {
        handleException();
    }
    switch(mediator.getAction()) {
        case CREATE:
            serializeCreateBeanCase(mediatorElem, mediator);
            break;
        case SET_PROPERTY:
            serializeSetPropertyCase(mediatorElem, mediator);
            break;
        case GET_PROPERTY:
            serializeGetPropertyCase(mediatorElem, mediator);
            break;
        default:
            assert false;
    }
    return mediatorElem;
}
Also used : OMElement(org.apache.axiom.om.OMElement) BeanMediator(org.apache.synapse.mediators.bean.BeanMediator)

Example 8 with BeanMediator

use of org.apache.synapse.mediators.bean.BeanMediator in project wso2-synapse by wso2.

the class BeanMediatorFactoryTest method testCreateSpecificMediator.

/**
 * Creating a bean mediator and asserting it is created successfully.
 *
 * @throws XMLStreamException - XMLStreamException.
 */
@Test
public void testCreateSpecificMediator() throws XMLStreamException {
    BeanMediatorFactory beanMediatorFactory = new BeanMediatorFactory();
    String inputXML = "<bean action=\"CREATE\" class=\"org.apache.synapse.util.TestTask\" " + "var=\"loc\"></bean>";
    OMElement element = AXIOMUtil.stringToOM(inputXML);
    Mediator mediator = beanMediatorFactory.createSpecificMediator(element, null);
    Assert.assertTrue("BeanMediator is not created successfully.", mediator instanceof BeanMediator);
    BeanMediator beanMediator = (BeanMediator) mediator;
    Assert.assertEquals("CREATE action is not set", BeanMediator.Action.CREATE, beanMediator.getAction());
}
Also used : OMElement(org.apache.axiom.om.OMElement) Mediator(org.apache.synapse.Mediator) BeanMediator(org.apache.synapse.mediators.bean.BeanMediator) BeanMediator(org.apache.synapse.mediators.bean.BeanMediator) Test(org.junit.Test)

Example 9 with BeanMediator

use of org.apache.synapse.mediators.bean.BeanMediator in project wso2-synapse by wso2.

the class BeanMediatorFactoryTest method testCreateSpecificMediator4.

/**
 * Removing the property of BeanMediator and asserting the action.
 *
 * @throws XMLStreamException - XMLStreamException.
 */
@Test
public void testCreateSpecificMediator4() throws XMLStreamException {
    BeanMediatorFactory beanMediatorFactory = new BeanMediatorFactory();
    String inputXML = "<bean action=\"REMOVE\" property=\"latitude\" value=\"{//latitude}\" var=\"loc\" " + "xmlns:ns3=\"http://org.apache.synapse/xsd\"             " + "xmlns:ns=\"http://org.apache.synapse/xsd\"></bean>\n";
    OMElement element = AXIOMUtil.stringToOM(inputXML);
    Mediator mediator = beanMediatorFactory.createSpecificMediator(element, null);
    Assert.assertTrue("BeanMediator is not created successfully.", mediator instanceof BeanMediator);
    BeanMediator beanMediator = (BeanMediator) mediator;
    Assert.assertEquals("REMOVE action is not set", BeanMediator.Action.REMOVE, beanMediator.getAction());
}
Also used : OMElement(org.apache.axiom.om.OMElement) Mediator(org.apache.synapse.Mediator) BeanMediator(org.apache.synapse.mediators.bean.BeanMediator) BeanMediator(org.apache.synapse.mediators.bean.BeanMediator) Test(org.junit.Test)

Example 10 with BeanMediator

use of org.apache.synapse.mediators.bean.BeanMediator in project wso2-synapse by wso2.

the class BeanMediatorSerializerTest method testSerializeSpecificMediator2.

/**
 * Test serializeSpecificMediator with SET_PROPERTY action and assert the action.
 */
@Test
public void testSerializeSpecificMediator2() {
    BeanMediatorSerializer beanMediatorSerializer = new BeanMediatorSerializer();
    BeanMediator mediator = new BeanMediator();
    mediator.setAction(BeanMediator.Action.SET_PROPERTY);
    mediator.setVarName("loc");
    mediator.setPropertyName("testProperty");
    mediator.setValue(new Value("testValue"));
    OMElement element = beanMediatorSerializer.serializeSpecificMediator(mediator);
    Assert.assertEquals("SET_PROPERTY action is not performed", "SET_PROPERTY", element.getAttributeValue(new QName(BeanConstants.ACTION)));
}
Also used : QName(javax.xml.namespace.QName) Value(org.apache.synapse.mediators.Value) OMElement(org.apache.axiom.om.OMElement) BeanMediator(org.apache.synapse.mediators.bean.BeanMediator) Test(org.junit.Test)

Aggregations

BeanMediator (org.apache.synapse.mediators.bean.BeanMediator)10 OMElement (org.apache.axiom.om.OMElement)8 Test (org.junit.Test)8 QName (javax.xml.namespace.QName)4 Mediator (org.apache.synapse.Mediator)4 Value (org.apache.synapse.mediators.Value)1 Target (org.apache.synapse.mediators.bean.Target)1