Search in sources :

Example 1 with XSLTMediator

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

the class XSLTMediatorFactory method createSpecificMediator.

public Mediator createSpecificMediator(OMElement elem, Properties properties) {
    XSLTMediator transformMediator = new XSLTMediator();
    OMAttribute attXslt = elem.getAttribute(ATT_KEY);
    OMAttribute attSource = elem.getAttribute(ATT_SOURCE);
    OMAttribute attTarget = elem.getAttribute(ATT_TARGET);
    OMAttribute attUseCache = elem.getAttribute(ATT_USECACHE);
    if (attXslt != null) {
        // ValueFactory for creating dynamic or static Value
        ValueFactory keyFac = new ValueFactory();
        // create dynamic or static key based on OMElement
        Value generatedKey = keyFac.createValue(XMLConfigConstants.KEY, elem);
        // set generated key as the Value
        transformMediator.setXsltKey(generatedKey);
    } else {
        handleException("The '" + XMLConfigConstants.KEY + "' " + "attribute is required for the XSLT mediator");
    }
    if (attSource != null) {
        try {
            transformMediator.setSourceXPathString(attSource.getAttributeValue());
            transformMediator.setSource(SynapseXPathFactory.getSynapseXPath(elem, ATT_SOURCE));
        } catch (JaxenException e) {
            handleException("Invalid XPath specified for the source attribute : " + attSource.getAttributeValue());
        }
    }
    if (attTarget != null) {
        transformMediator.setTargetPropertyName(attTarget.getAttributeValue());
    }
    if (attUseCache != null) {
        transformMediator.setUseCache(Boolean.parseBoolean(attUseCache.getAttributeValue()));
    }
    // after successfully creating the mediator
    // set its common attributes such as tracing etc
    processAuditStatus(transformMediator, elem);
    // set the features
    for (Map.Entry<String, String> entry : collectNameValuePairs(elem, FEATURE_Q).entrySet()) {
        String value = entry.getValue();
        boolean isFeatureEnabled;
        if ("true".equals(value)) {
            isFeatureEnabled = true;
        } else if ("false".equals(value)) {
            isFeatureEnabled = false;
        } else {
            handleException("The feature must have value true or false");
            break;
        }
        transformMediator.addFeature(entry.getKey(), isFeatureEnabled);
    }
    for (Map.Entry<String, String> entry : collectNameValuePairs(elem, ATTRIBUTE_Q).entrySet()) {
        transformMediator.addAttribute(entry.getKey(), entry.getValue());
    }
    transformMediator.addAllProperties(MediatorPropertyFactory.getMediatorProperties(elem));
    transformMediator.setResourceMap(ResourceMapFactory.createResourceMap(elem));
    return transformMediator;
}
Also used : XSLTMediator(org.apache.synapse.mediators.transform.XSLTMediator) JaxenException(org.jaxen.JaxenException) Value(org.apache.synapse.mediators.Value) OMAttribute(org.apache.axiom.om.OMAttribute) Map(java.util.Map)

Example 2 with XSLTMediator

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

the class XSLTMediatorSerializer method serializeSpecificMediator.

public OMElement serializeSpecificMediator(Mediator m) {
    if (!(m instanceof XSLTMediator)) {
        handleException("Unsupported mediator passed in for serialization : " + m.getType());
    }
    XSLTMediator mediator = (XSLTMediator) m;
    OMElement xslt = fac.createOMElement("xslt", synNS);
    if (mediator.getXsltKey() != null) {
        // Serialize Value using ValueSerializer
        ValueSerializer keySerializer = new ValueSerializer();
        keySerializer.serializeValue(mediator.getXsltKey(), XMLConfigConstants.KEY, xslt);
    } else {
        handleException("Invalid XSLT mediator. XSLT registry key is required");
    }
    saveTracingState(xslt, mediator);
    if (mediator.getSource() != null) {
        SynapseXPathSerializer.serializeXPath(mediator.getSource(), xslt, "source");
    }
    if (mediator.getTargetPropertyName() != null) {
        xslt.addAttribute(fac.createOMAttribute("target", nullNS, mediator.getTargetPropertyName()));
    }
    serializeProperties(xslt, mediator.getProperties());
    List<MediatorProperty> features = mediator.getFeatures();
    if (!features.isEmpty()) {
        for (MediatorProperty mp : features) {
            OMElement prop = fac.createOMElement("feature", synNS, xslt);
            if (mp.getName() != null) {
                prop.addAttribute(fac.createOMAttribute("name", nullNS, mp.getName()));
            } else {
                handleException("The Feature name is missing");
            }
            if (mp.getValue() != null) {
                prop.addAttribute(fac.createOMAttribute("value", nullNS, mp.getValue()));
            } else {
                handleException("The Feature value is missing");
            }
        }
    }
    serializeMediatorProperties(xslt, mediator.getAttributes(), ATTRIBUTE_Q);
    ResourceMapSerializer.serializeResourceMap(xslt, mediator.getResourceMap());
    return xslt;
}
Also used : MediatorProperty(org.apache.synapse.mediators.MediatorProperty) XSLTMediator(org.apache.synapse.mediators.transform.XSLTMediator) OMElement(org.apache.axiom.om.OMElement)

Example 3 with XSLTMediator

use of org.apache.synapse.mediators.transform.XSLTMediator 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

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