Search in sources :

Example 1 with PropertyMediator

use of org.apache.synapse.mediators.builtin.PropertyMediator in project wso2-synapse by wso2.

the class RESTMediationTestCase method getTestSequence.

protected SequenceMediator getTestSequence(String name, String value) {
    SequenceMediator seq = new SequenceMediator();
    PropertyMediator prop = new PropertyMediator();
    prop.setName(name);
    prop.setValue(value);
    seq.addChild(prop);
    return seq;
}
Also used : PropertyMediator(org.apache.synapse.mediators.builtin.PropertyMediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator)

Example 2 with PropertyMediator

use of org.apache.synapse.mediators.builtin.PropertyMediator in project wso2-synapse by wso2.

the class CloneMediatorTest method testDeepClonedOMTypeProperty.

public void testDeepClonedOMTypeProperty() throws InterruptedException {
    String propName = "testProp";
    Mediator clone = fac.createMediator(createOMElement("<clone " + "xmlns=\"http://ws.apache.org/ns/synapse\"><target soapAction=\"urn:clone\" " + "sequence=\"seqRef\"/><target to=\"http://test\"><sequence><sequence " + "key=\"seqRef\"/></sequence></target></clone>"), new Properties());
    PropertyMediator propertyMediator = new PropertyMediator();
    propertyMediator.setName(propName);
    propertyMediator.setValueElement(TestUtils.createOMElement("<a><b>asdf</b></a>"));
    propertyMediator.setScope("default");
    propertyMediator.mediate(testCtx);
    clone.mediate(testCtx);
    while (helperMediator.getMediatedContext(1) == null) {
        Thread.sleep(100);
    }
    MessageContext mediatedCtx = helperMediator.getMediatedContext(0);
    Object obj = mediatedCtx.getProperty(propName);
    Assert.assertNotNull(obj);
    Assert.assertTrue(obj instanceof OMElement);
    OMElement omElement = (OMElement) obj;
    String cloneStr = omElement.getParent().toString();
    OMElement omElement1 = (OMElement) testCtx.getProperty(propName);
    String parentID = omElement1.getParent().toString();
    Assert.assertTrue(!cloneStr.equals(parentID));
}
Also used : PropertyMediator(org.apache.synapse.mediators.builtin.PropertyMediator) Mediator(org.apache.synapse.Mediator) PropertyMediator(org.apache.synapse.mediators.builtin.PropertyMediator) OMElement(org.apache.axiom.om.OMElement) MessageContext(org.apache.synapse.MessageContext) Properties(java.util.Properties)

Example 3 with PropertyMediator

use of org.apache.synapse.mediators.builtin.PropertyMediator in project wso2-synapse by wso2.

the class PropertyMediatorFactory method createSpecificMediator.

public Mediator createSpecificMediator(OMElement elem, Properties properties) {
    PropertyMediator propMediator = new PropertyMediator();
    OMAttribute name = elem.getAttribute(ATT_NAME);
    OMAttribute value = elem.getAttribute(ATT_VALUE);
    OMAttribute expression = elem.getAttribute(ATT_EXPRN);
    OMAttribute scope = elem.getAttribute(ATT_SCOPE);
    OMAttribute action = elem.getAttribute(ATT_ACTION);
    OMAttribute type = elem.getAttribute(ATT_TYPE);
    OMAttribute pattern = elem.getAttribute(ATT_PATTERN);
    OMAttribute group = elem.getAttribute(ATT_GROUP);
    OMElement valueElement = elem.getFirstElement();
    if (name == null) {
        String msg = "The 'name' attribute is required for the configuration of a property mediator";
        log.error(msg);
        throw new SynapseException(msg);
    } else if ((value == null && valueElement == null && expression == null) && !(action != null && "remove".equals(action.getAttributeValue()))) {
        String msg = "Either a child element or one of 'value', 'expression' attributes is " + "required for a property mediator when action is SET";
        log.error(msg);
        throw new SynapseException(msg);
    }
    propMediator.setName(name.getAttributeValue());
    String dataType = null;
    if (type != null) {
        dataType = type.getAttributeValue();
    }
    if (value != null) {
        propMediator.setValue(value.getAttributeValue(), dataType);
    } else if (valueElement != null) {
        // Need to clone to prevent same reference getting modified at the message flow
        propMediator.setValueElement(valueElement.cloneOMElement());
    } else if (expression != null) {
        try {
            propMediator.setExpression(SynapsePathFactory.getSynapsePath(elem, ATT_EXPRN), dataType);
        } catch (JaxenException e) {
            String msg = "Invalid XPath expression for attribute 'expression' : " + expression.getAttributeValue();
            log.error(msg);
            throw new SynapseException(msg);
        }
    }
    if (pattern != null) {
        propMediator.setPattern(Pattern.compile(pattern.getAttributeValue()));
        if (group != null) {
            int groupValue = Integer.parseInt(group.getAttributeValue());
            if (groupValue >= 0) {
                propMediator.setGroup(groupValue);
            } else {
                String msg = "group can have a positive value only";
                log.error(msg);
                throw new SynapseException(msg);
            }
        }
    } else if (group != null) {
        String msg = "group is only allowed when a pattern is specified";
        log.error(msg);
        throw new SynapseException(msg);
    }
    // property mediator will act as a property remove mediator
    if (action != null && "remove".equals(action.getAttributeValue())) {
        propMediator.setAction(PropertyMediator.ACTION_REMOVE);
    }
    if (scope != null) {
        String valueStr = scope.getAttributeValue();
        if (!XMLConfigConstants.SCOPE_AXIS2.equals(valueStr) && !XMLConfigConstants.SCOPE_TRANSPORT.equals(valueStr) && !XMLConfigConstants.SCOPE_OPERATION.equals(valueStr) && !XMLConfigConstants.SCOPE_DEFAULT.equals(valueStr) && !XMLConfigConstants.SCOPE_CLIENT.equals(valueStr) && !XMLConfigConstants.SCOPE_REGISTRY.equals(valueStr)) {
            String msg = "Only '" + XMLConfigConstants.SCOPE_AXIS2 + "' or '" + XMLConfigConstants.SCOPE_TRANSPORT + "' or '" + XMLConfigConstants.SCOPE_CLIENT + "' or '" + XMLConfigConstants.SCOPE_DEFAULT + "' or '" + XMLConfigConstants.SCOPE_OPERATION + "' or '" + XMLConfigConstants.SCOPE_REGISTRY + "' values are allowed for attribute scope for a property mediator" + ", Unsupported scope " + valueStr;
            log.error(msg);
            throw new SynapseException(msg);
        }
        propMediator.setScope(valueStr);
    }
    // after successfully creating the mediator
    // set its common attributes such as tracing etc
    processAuditStatus(propMediator, elem);
    return propMediator;
}
Also used : SynapseException(org.apache.synapse.SynapseException) PropertyMediator(org.apache.synapse.mediators.builtin.PropertyMediator) JaxenException(org.jaxen.JaxenException) OMElement(org.apache.axiom.om.OMElement) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 4 with PropertyMediator

use of org.apache.synapse.mediators.builtin.PropertyMediator in project wso2-synapse by wso2.

the class PropertyMediatorSerializer method serializeSpecificMediator.

public OMElement serializeSpecificMediator(Mediator m) {
    if (!(m instanceof PropertyMediator)) {
        handleException("Unsupported mediator passed in for serialization : " + m.getType());
    }
    PropertyMediator mediator = (PropertyMediator) m;
    OMElement property = fac.createOMElement("property", synNS);
    saveTracingState(property, mediator);
    if (mediator.getName() != null) {
        property.addAttribute(fac.createOMAttribute("name", nullNS, mediator.getName()));
    } else {
        handleException("Invalid property mediator. Name is required");
    }
    if (mediator.getValue() != null) {
        property.addAttribute(fac.createOMAttribute("value", nullNS, mediator.getValue().toString()));
    } else if (mediator.getValueElement() != null) {
        property.addChild(mediator.getValueElement());
    } else if (mediator.getExpression() != null) {
        SynapsePathSerializer.serializePath((SynapsePath) mediator.getExpression(), property, "expression");
    } else if (mediator.getAction() == PropertyMediator.ACTION_SET) {
        handleException("Invalid property mediator. Value or expression is required if " + "action is SET");
    }
    if (mediator.getScope() != null) {
        // if we have already built a mediator with scope, scope should be valid, now save it
        property.addAttribute(fac.createOMAttribute("scope", nullNS, mediator.getScope()));
    }
    if (mediator.getAction() == PropertyMediator.ACTION_REMOVE) {
        property.addAttribute(fac.createOMAttribute("action", nullNS, "remove"));
    } else if (mediator.getType() != null) {
        property.addAttribute(fac.createOMAttribute("type", nullNS, mediator.getType()));
    }
    if (mediator.getPattern() != null) {
        property.addAttribute(fac.createOMAttribute("pattern", nullNS, mediator.getPattern().toString()));
        if (mediator.getGroup() >= 0) {
            property.addAttribute(fac.createOMAttribute("group", nullNS, Integer.toString(mediator.getGroup())));
        }
    }
    return property;
}
Also used : PropertyMediator(org.apache.synapse.mediators.builtin.PropertyMediator) OMElement(org.apache.axiom.om.OMElement)

Example 5 with PropertyMediator

use of org.apache.synapse.mediators.builtin.PropertyMediator in project wso2-synapse by wso2.

the class ResourceTest method testFaultSequence.

public void testFaultSequence() throws Exception {
    API api = new API("TestAPI", "/test");
    Resource resource = new Resource();
    resource.setDispatcherHelper(new URITemplateHelper("/~{user}"));
    SequenceMediator inSequence = getTestSequence("seq.in", "seq.in.value");
    ((PropertyMediator) inSequence.getChild(0)).setScope("axis2");
    XSLTMediator xsltMediator = new XSLTMediator();
    xsltMediator.setXsltKey(new Value("/bogus/key"));
    inSequence.addChild(xsltMediator);
    resource.setInSequence(inSequence);
    SequenceMediator faultSequence = getTestSequence("seq.fault", "seq.fault.value");
    ((PropertyMediator) faultSequence.getChild(0)).setScope("axis2");
    resource.setFaultSequence(faultSequence);
    api.addResource(resource);
    SynapseConfiguration synapseConfig = new SynapseConfiguration();
    synapseConfig.addAPI(api.getName(), api);
    synapseConfig.addSequence("main", getTestSequence("main.in", "main.value"));
    MessageContext synCtx = getMessageContext(synapseConfig, false, "/test/~foo", "GET");
    MessageContextCreatorForAxis2.setSynConfig(synapseConfig);
    MessageContextCreatorForAxis2.setSynEnv(synCtx.getEnvironment());
    org.apache.axis2.context.MessageContext mc = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
    mc.setConfigurationContext(((Axis2SynapseEnvironment) synCtx.getEnvironment()).getAxis2ConfigurationContext());
    new SynapseMessageReceiver().receive(mc);
    assertEquals("seq.in.value", mc.getProperty("seq.in"));
    assertEquals("seq.fault.value", mc.getProperty("seq.fault"));
}
Also used : SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) URITemplateHelper(org.apache.synapse.rest.dispatch.URITemplateHelper) XSLTMediator(org.apache.synapse.mediators.transform.XSLTMediator) PropertyMediator(org.apache.synapse.mediators.builtin.PropertyMediator) Value(org.apache.synapse.mediators.Value) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) SynapseMessageReceiver(org.apache.synapse.core.axis2.SynapseMessageReceiver) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Aggregations

PropertyMediator (org.apache.synapse.mediators.builtin.PropertyMediator)5 OMElement (org.apache.axiom.om.OMElement)3 MessageContext (org.apache.synapse.MessageContext)2 SequenceMediator (org.apache.synapse.mediators.base.SequenceMediator)2 Properties (java.util.Properties)1 OMAttribute (org.apache.axiom.om.OMAttribute)1 Mediator (org.apache.synapse.Mediator)1 SynapseException (org.apache.synapse.SynapseException)1 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)1 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)1 SynapseMessageReceiver (org.apache.synapse.core.axis2.SynapseMessageReceiver)1 Value (org.apache.synapse.mediators.Value)1 XSLTMediator (org.apache.synapse.mediators.transform.XSLTMediator)1 URITemplateHelper (org.apache.synapse.rest.dispatch.URITemplateHelper)1 JaxenException (org.jaxen.JaxenException)1