Search in sources :

Example 6 with MediatorProperty

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

the class SynapseConfigUtils method setDefaultFaultSequence.

/**
 * Return the fault sequence if one is not defined. This implementation defaults to
 * a simple sequence :
 * <log level="full">
 *   <property name="MESSAGE" value="Executing default "fault" sequence"/>
 *   <property name="ERROR_CODE" expression="get-property('ERROR_CODE')"/>
 *   <property name="ERROR_MESSAGE" expression="get-property('ERROR_MESSAGE')"/>
 * </log>
 * <drop/>
 *
 * @param config the configuration to be updated
 */
public static void setDefaultFaultSequence(SynapseConfiguration config) {
    SequenceMediator fault = new SequenceMediator();
    fault.setName(org.apache.synapse.SynapseConstants.FAULT_SEQUENCE_KEY);
    LogMediator log = new LogMediator();
    log.setLogLevel(LogMediator.FULL);
    MediatorProperty mp = new MediatorProperty();
    mp.setName("MESSAGE");
    mp.setValue("Executing default \"fault\" sequence");
    log.addProperty(mp);
    mp = new MediatorProperty();
    mp.setName("ERROR_CODE");
    try {
        mp.setExpression(new SynapseXPath("get-property('ERROR_CODE')"));
    } catch (JaxenException ignore) {
    }
    log.addProperty(mp);
    mp = new MediatorProperty();
    mp.setName("ERROR_MESSAGE");
    try {
        mp.setExpression(new SynapseXPath("get-property('ERROR_MESSAGE')"));
    } catch (JaxenException ignore) {
    }
    log.addProperty(mp);
    fault.addChild(log);
    fault.addChild(new DropMediator());
    // set aspect configuration
    AspectConfiguration configuration = new AspectConfiguration(fault.getName());
    fault.configure(configuration);
    config.addSequence(org.apache.synapse.SynapseConstants.FAULT_SEQUENCE_KEY, fault);
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) MediatorProperty(org.apache.synapse.mediators.MediatorProperty) JaxenException(org.jaxen.JaxenException) LogMediator(org.apache.synapse.mediators.builtin.LogMediator) DropMediator(org.apache.synapse.mediators.builtin.DropMediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) AspectConfiguration(org.apache.synapse.aspects.AspectConfiguration)

Example 7 with MediatorProperty

use of org.apache.synapse.mediators.MediatorProperty 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 8 with MediatorProperty

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

the class MediatorPropertyFactory method getMediatorProperties.

public static List<MediatorProperty> getMediatorProperties(OMElement elem) {
    List<MediatorProperty> propertyList = new ArrayList<MediatorProperty>();
    Iterator iter = elem.getChildrenWithName(MediatorProperty.PROPERTY_Q);
    while (iter.hasNext()) {
        OMElement propEle = (OMElement) iter.next();
        OMAttribute attName = propEle.getAttribute(MediatorProperty.ATT_NAME_Q);
        OMAttribute attValue = propEle.getAttribute(MediatorProperty.ATT_VALUE_Q);
        OMAttribute attExpr = propEle.getAttribute(MediatorProperty.ATT_EXPR_Q);
        OMAttribute attScope = propEle.getAttribute(MediatorProperty.ATT_SCOPE_Q);
        MediatorProperty prop = new MediatorProperty();
        if (attName == null || attName.getAttributeValue() == null || attName.getAttributeValue().trim().length() == 0) {
            String msg = "Entry name is a required attribute for a Log property";
            log.error(msg);
            throw new SynapseException(msg);
        } else {
            prop.setName(attName.getAttributeValue());
        }
        // if a value is specified, use it, else look for an expression
        if (attValue != null) {
            if (attValue.getAttributeValue() == null || attValue.getAttributeValue().trim().length() == 0) {
                String msg = "Entry attribute value (if specified) " + "is required for a Log property";
                log.error(msg);
                throw new SynapseException(msg);
            } else {
                prop.setValue(attValue.getAttributeValue());
            }
        } else if (attExpr != null) {
            if (attExpr.getAttributeValue() == null || attExpr.getAttributeValue().trim().length() == 0) {
                String msg = "Entry attribute expression (if specified) " + "is required for a mediator property";
                log.error(msg);
                throw new SynapseException(msg);
            } else {
                try {
                    prop.setExpression(SynapsePathFactory.getSynapsePath(propEle, MediatorProperty.ATT_EXPR_Q));
                } catch (JaxenException e) {
                    String msg = "Invalid XPath expression : " + attExpr.getAttributeValue();
                    log.error(msg);
                    throw new SynapseException(msg, e);
                }
            }
        } else {
            String msg = "Entry attribute value OR expression must " + "be specified for a mediator property";
            log.error(msg);
            throw new SynapseException(msg);
        }
        if (attScope != null) {
            String valueStr = attScope.getAttributeValue();
            if (!XMLConfigConstants.SCOPE_AXIS2.equals(valueStr) && !XMLConfigConstants.SCOPE_TRANSPORT.equals(valueStr) && !XMLConfigConstants.SCOPE_DEFAULT.equals(valueStr) && !XMLConfigConstants.SCOPE_CLIENT.equals(valueStr)) {
                String msg = "Only '" + XMLConfigConstants.SCOPE_AXIS2 + "' or '" + XMLConfigConstants.SCOPE_TRANSPORT + "' or '" + XMLConfigConstants.SCOPE_CLIENT + "' values are allowed for attribute scope for a property" + ", Unsupported scope " + valueStr;
                log.error(msg);
                throw new SynapseException(msg);
            }
            prop.setScope(valueStr);
        }
        propertyList.add(prop);
    }
    return propertyList;
}
Also used : MediatorProperty(org.apache.synapse.mediators.MediatorProperty) SynapseException(org.apache.synapse.SynapseException) JaxenException(org.jaxen.JaxenException) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) OMElement(org.apache.axiom.om.OMElement) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 9 with MediatorProperty

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

the class ClassMediator method mediate.

/**
 * Don't use a new instance... do one instance of the object per instance of
 * this mediator
 *
 * @param synCtx
 *            the message context
 * @return as per standard semantics
 */
public boolean mediate(MessageContext synCtx) {
    if (synCtx.getEnvironment().isDebuggerEnabled()) {
        if (super.divertMediationRoute(synCtx)) {
            return true;
        }
    }
    SynapseLog synLog = getLog(synCtx);
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Start : Class mediator");
        if (synLog.isTraceTraceEnabled()) {
            synLog.traceTrace("Message : " + synCtx.getEnvelope());
        }
    }
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("invoking : " + mediator.getClass() + ".mediate()");
    }
    boolean result;
    try {
        for (MediatorProperty property : properties) {
            String propertyValue = property.getValue() != null ? property.getValue() : property.getEvaluatedExpression(synCtx);
            PropertyHelper.setInstanceProperty(property.getName(), propertyValue, mediator);
        }
        result = mediator.mediate(synCtx);
    } catch (Exception e) {
        // so that the fault handler will be invoked
        throw new SynapseException("Error occured in the mediation of the class mediator", e);
    }
    synLog.traceOrDebug("End : Class mediator");
    return result;
}
Also used : SynapseLog(org.apache.synapse.SynapseLog) MediatorProperty(org.apache.synapse.mediators.MediatorProperty) SynapseException(org.apache.synapse.SynapseException) SynapseException(org.apache.synapse.SynapseException)

Example 10 with MediatorProperty

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

the class XSLTMediator method addFeature.

/**
 * Add a feature to be set on the {@link TransformerFactory} used by this mediator instance.
 * This method can also be used to enable some Synapse specific optimizations and
 * enhancements as described in the documentation of this class.
 *
 * @param featureName The name of the feature
 * @param isFeatureEnable the desired state of the feature
 *
 * @see TransformerFactory#setFeature(String, boolean)
 * @see XSLTMediator
 */
public void addFeature(String featureName, boolean isFeatureEnable) {
    MediatorProperty mp = new MediatorProperty();
    mp.setName(featureName);
    if (isFeatureEnable) {
        mp.setValue("true");
    } else {
        mp.setValue("false");
    }
    transformerFactoryFeatures.add(mp);
    if (USE_DOM_SOURCE_AND_RESULTS.equals(featureName)) {
        if (isFeatureEnable) {
            sourceBuilderFactory = new DOOMSourceBuilderFactory();
            resultBuilderFactory = new DOOMResultBuilderFactory();
        }
    } else {
        try {
            transFact.setFeature(featureName, isFeatureEnable);
        } catch (TransformerConfigurationException e) {
            String msg = "Error occurred when setting features to the TransformerFactory";
            log.error(msg, e);
            throw new SynapseException(msg, e);
        }
    }
}
Also used : MediatorProperty(org.apache.synapse.mediators.MediatorProperty) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) SynapseException(org.apache.synapse.SynapseException) DOOMResultBuilderFactory(org.apache.synapse.util.jaxp.DOOMResultBuilderFactory) DOOMSourceBuilderFactory(org.apache.synapse.util.jaxp.DOOMSourceBuilderFactory)

Aggregations

MediatorProperty (org.apache.synapse.mediators.MediatorProperty)21 OMElement (org.apache.axiom.om.OMElement)8 SynapseException (org.apache.synapse.SynapseException)8 ArrayList (java.util.ArrayList)5 Endpoint (org.apache.synapse.endpoints.Endpoint)4 AddressEndpoint (org.apache.synapse.endpoints.AddressEndpoint)3 SynapseXPath (org.apache.synapse.util.xpath.SynapseXPath)3 Test (org.junit.Test)3 Iterator (java.util.Iterator)2 QName (javax.xml.namespace.QName)2 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)2 OMAttribute (org.apache.axiom.om.OMAttribute)2 PropertyInclude (org.apache.synapse.PropertyInclude)2 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)2 LoadbalanceEndpoint (org.apache.synapse.endpoints.LoadbalanceEndpoint)2 Value (org.apache.synapse.mediators.Value)2 UriTemplate (com.damnhandy.uri.template.UriTemplate)1 ExpressionParseException (com.damnhandy.uri.template.impl.ExpressionParseException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1