Search in sources :

Example 51 with SynapseException

use of org.apache.synapse.SynapseException in project wso2-synapse by wso2.

the class AnnotatedCommandMediatorFactory method createSpecificMediator.

public Mediator createSpecificMediator(OMElement elem, Properties properties) {
    AnnotatedCommandMediator pojoMediator = new AnnotatedCommandMediator();
    processAuditStatus(pojoMediator, elem);
    // Class name of the Command object should be present
    OMAttribute name = elem.getAttribute(ATT_NAME);
    if (name == null) {
        String msg = "The name of the actual POJO command implementation class" + " is a required attribute";
        log.error(msg);
        throw new SynapseException(msg);
    }
    // load the class for the command object
    try {
        pojoMediator.setCommand(getClass().getClassLoader().loadClass(name.getAttributeValue()));
    } catch (ClassNotFoundException e) {
        handleException("Unable to load the class specified as the command " + name.getAttributeValue(), e);
    }
    // at the mediation time
    for (Iterator it = elem.getChildElements(); it.hasNext(); ) {
        OMElement child = (OMElement) it.next();
        if ("property".equals(child.getLocalName())) {
            String propName = child.getAttribute(ATT_NAME).getAttributeValue();
            if (propName == null) {
                handleException("A POJO command mediator property must specify the name attribute");
            } else {
                if (child.getAttribute(ATT_EXPRN) != null) {
                    SynapseXPath xpath;
                    try {
                        xpath = SynapseXPathFactory.getSynapseXPath(child, ATT_EXPRN);
                        pojoMediator.addMessageSetterProperty(propName, xpath);
                    } catch (JaxenException e) {
                        handleException("Error instantiating XPath expression : " + child.getAttribute(ATT_EXPRN), e);
                    }
                } else {
                    if (child.getAttribute(ATT_VALUE) != null) {
                        pojoMediator.addStaticSetterProperty(propName, child.getAttribute(ATT_VALUE).getAttributeValue());
                    } else {
                        handleException("A POJO mediator property must specify either " + "name and expression attributes, or name and value attributes");
                    }
                }
            }
        }
    }
    return pojoMediator;
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) SynapseException(org.apache.synapse.SynapseException) JaxenException(org.jaxen.JaxenException) Iterator(java.util.Iterator) OMElement(org.apache.axiom.om.OMElement) AnnotatedCommandMediator(org.apache.synapse.mediators.ext.AnnotatedCommandMediator) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 52 with SynapseException

use of org.apache.synapse.SynapseException in project wso2-synapse by wso2.

the class HeaderMediatorFactory method createSpecificMediator.

public Mediator createSpecificMediator(OMElement elem, Properties properties) {
    HeaderMediator headerMediator = new HeaderMediator();
    OMAttribute name = elem.getAttribute(ATT_NAME);
    OMAttribute value = elem.getAttribute(ATT_VALUE);
    OMAttribute exprn = elem.getAttribute(ATT_EXPRN);
    OMAttribute action = elem.getAttribute(ATT_ACTION);
    OMAttribute scope = elem.getAttribute(ATT_SCOPE);
    if (name == null || name.getAttributeValue() == null) {
        if (elem.getChildElements() == null || !elem.getChildElements().hasNext()) {
            String msg = "A valid name attribute is required for the header mediator";
            log.error(msg);
            throw new SynapseException(msg);
        }
    } else {
        if (scope == null || scope.getAttributeValue().equals(XMLConfigConstants.SCOPE_DEFAULT)) {
            String nameAtt = name.getAttributeValue();
            int colonPos = nameAtt.indexOf(":");
            if (colonPos != -1) {
                // has a NS prefix.. find it and the NS it maps into
                String prefix = nameAtt.substring(0, colonPos);
                String namespaceURI = OMElementUtils.getNameSpaceWithPrefix(prefix, elem);
                if (namespaceURI == null) {
                    handleException("Invalid namespace prefix '" + prefix + "' in name attribute");
                } else {
                    headerMediator.setQName(new QName(namespaceURI, nameAtt.substring(colonPos + 1), prefix));
                }
            } else {
                // no prefix
                if (SynapseConstants.HEADER_TO.equals(nameAtt) || SynapseConstants.HEADER_FROM.equals(nameAtt) || SynapseConstants.HEADER_ACTION.equals(nameAtt) || SynapseConstants.HEADER_FAULT.equals(nameAtt) || SynapseConstants.HEADER_REPLY_TO.equals(nameAtt) || SynapseConstants.HEADER_RELATES_TO.equals(nameAtt)) {
                    headerMediator.setQName(new QName(nameAtt));
                } else {
                    handleException("Invalid SOAP header: " + nameAtt + " specified at the " + "header mediator. All SOAP headers must be namespace qualified.");
                }
            }
        } else {
            headerMediator.setQName(new QName(name.getAttributeValue()));
        }
    }
    if (scope != null) {
        String valueStr = scope.getAttributeValue();
        if (!XMLConfigConstants.SCOPE_TRANSPORT.equals(valueStr) && !XMLConfigConstants.SCOPE_DEFAULT.equals(valueStr)) {
            String msg = "Only '" + XMLConfigConstants.SCOPE_TRANSPORT + "' or '" + XMLConfigConstants.SCOPE_DEFAULT + "' values are allowed for attribute scope for a header mediator" + ", Unsupported scope " + valueStr;
            log.error(msg);
            throw new SynapseException(msg);
        }
        headerMediator.setScope(valueStr);
    }
    // after successfully creating the mediator
    // set its common attributes such as tracing etc
    processAuditStatus(headerMediator, elem);
    // header mediator will act as a header remove mediator
    if (action != null && "remove".equals(action.getAttributeValue())) {
        headerMediator.setAction(HeaderMediator.ACTION_REMOVE);
    }
    if (headerMediator.getAction() == HeaderMediator.ACTION_SET && value == null && exprn == null && !headerMediator.isImplicit()) {
        handleException("A 'value' or 'expression' attribute is required for a [set] " + "header mediator");
    }
    if (value != null && value.getAttributeValue() != null) {
        headerMediator.setValue(value.getAttributeValue());
    } else if (exprn != null && exprn.getAttributeValue() != null) {
        try {
            headerMediator.setExpression(SynapseXPathFactory.getSynapseXPath(elem, ATT_EXPRN));
        } catch (JaxenException je) {
            handleException("Invalid XPath expression : " + exprn.getAttributeValue());
        }
    } else if (headerMediator.isImplicit()) {
        // we have an implicit, non standard header
        Iterator i = elem.getChildElements();
        if (i == null) {
            handleException("A non standard header with both value and expression are null must " + "contain an embedded XML definition.");
        }
        for (; i.hasNext(); ) {
            headerMediator.addEmbeddedXml((OMElement) i.next());
        }
    }
    return headerMediator;
}
Also used : HeaderMediator(org.apache.synapse.mediators.transform.HeaderMediator) SynapseException(org.apache.synapse.SynapseException) QName(javax.xml.namespace.QName) JaxenException(org.jaxen.JaxenException) Iterator(java.util.Iterator) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 53 with SynapseException

use of org.apache.synapse.SynapseException in project wso2-synapse by wso2.

the class EnrichMediatorFactory method populateTarget.

private void populateTarget(Target target, OMElement sourceEle) {
    // type attribute
    OMAttribute typeAttr = sourceEle.getAttribute(ATT_TYPE);
    OMAttribute actionAttr = sourceEle.getAttribute(ATT_ACTION);
    if (actionAttr != null && actionAttr.getAttributeValue() != null) {
        target.setAction(actionAttr.getAttributeValue());
    } else {
        target.setAction("replace");
    }
    if (typeAttr != null && typeAttr.getAttributeValue() != null) {
        int type = convertTypeToInt(typeAttr.getAttributeValue());
        if (type >= 0) {
            target.setTargetType(type);
            if (type == 1) {
                if (!target.getAction().equals("replace")) {
                    throw new SynapseException("Invalid target action");
                }
            }
        } else {
            handleException("Un-expected type : " + typeAttr.getAttributeValue());
        }
    }
    if (target.getTargetType() == EnrichMediator.CUSTOM) {
        OMAttribute xpathAttr = sourceEle.getAttribute(ATT_XPATH);
        if (xpathAttr != null && xpathAttr.getAttributeValue() != null) {
            try {
                target.setXpath(SynapseXPathFactory.getSynapseXPath(sourceEle, ATT_XPATH));
            } catch (JaxenException e) {
                handleException("Invalid XPath expression: " + xpathAttr);
            }
        } else {
            handleException("xpath attribute is required for CUSTOM type");
        }
    } else if (target.getTargetType() == EnrichMediator.PROPERTY) {
        OMAttribute propertyAttr = sourceEle.getAttribute(ATT_PROPERTY);
        if (propertyAttr != null && propertyAttr.getAttributeValue() != null) {
            target.setProperty(propertyAttr.getAttributeValue());
        } else {
            handleException("xpath attribute is required for CUSTOM type");
        }
    }
}
Also used : SynapseException(org.apache.synapse.SynapseException) JaxenException(org.jaxen.JaxenException) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 54 with SynapseException

use of org.apache.synapse.SynapseException in project wso2-synapse by wso2.

the class ClassMediatorFactory method createSpecificMediator.

public Mediator createSpecificMediator(OMElement elem, Properties properties) {
    ClassMediator classMediator = new ClassMediator();
    OMAttribute name = elem.getAttribute(ATT_NAME);
    if (name == null) {
        String msg = "The name of the actual mediator class is a required attribute";
        log.error(msg);
        throw new SynapseException(msg);
    }
    Class clazz = null;
    Mediator mediator;
    if (properties != null) {
        // load from synapse libs or dynamic class mediators
        ClassLoader libLoader = (ClassLoader) properties.get(SynapseConstants.SYNAPSE_LIB_LOADER);
        if (libLoader != null) {
            // load from synapse lib
            try {
                clazz = libLoader.loadClass(name.getAttributeValue());
            } catch (ClassNotFoundException e) {
                String msg = "Error loading class : " + name.getAttributeValue() + " from Synapse library";
                log.error(msg, e);
                throw new SynapseException(msg, e);
            }
        } else {
            // load from dynamic class mediators
            Map<String, ClassLoader> dynamicClassMediatorLoaderMap = (Map<String, ClassLoader>) properties.get(SynapseConstants.CLASS_MEDIATOR_LOADERS);
            if (dynamicClassMediatorLoaderMap != null) {
                // Has registered dynamic class mediator loaders in the deployment store.
                // Try to load class from them.
                Iterator<ClassLoader> dynamicClassMediatorLoaders = dynamicClassMediatorLoaderMap.values().iterator();
                while (dynamicClassMediatorLoaders.hasNext()) {
                    try {
                        clazz = dynamicClassMediatorLoaders.next().loadClass(name.getAttributeValue());
                        break;
                    } catch (Exception ignore) {
                    }
                }
            }
        }
    }
    if (clazz == null) {
        try {
            clazz = getClass().getClassLoader().loadClass(name.getAttributeValue());
        } catch (ClassNotFoundException e) {
            String msg = "Error loading class : " + name.getAttributeValue() + " - Class not found";
            log.error(msg, e);
            throw new SynapseException(msg, e);
        }
    }
    try {
        mediator = (Mediator) clazz.newInstance();
    } catch (Throwable e) {
        String msg = "Error in instantiating class : " + name.getAttributeValue();
        log.error(msg, e);
        throw new SynapseException(msg, e);
    }
    classMediator.addAllProperties(MediatorPropertyFactory.getMediatorProperties(elem));
    // after successfully creating the mediator
    // set its common attributes such as tracing etc
    classMediator.setMediator(mediator);
    processAuditStatus(classMediator, elem);
    return classMediator;
}
Also used : ClassMediator(org.apache.synapse.mediators.ext.ClassMediator) SynapseException(org.apache.synapse.SynapseException) SynapseException(org.apache.synapse.SynapseException) Mediator(org.apache.synapse.Mediator) ClassMediator(org.apache.synapse.mediators.ext.ClassMediator) OMAttribute(org.apache.axiom.om.OMAttribute) Map(java.util.Map)

Example 55 with SynapseException

use of org.apache.synapse.SynapseException in project wso2-synapse by wso2.

the class SynapseOutHandler method invoke.

/**
 * This method will inject the message into Synapse after creating the SynapseMC from the
 * Axis2MC and after the mediation if synapse lets the message to flow through this will let
 * the message to flow and if not aborts the message
 *
 * @param messageContext - Axis2MC to be mediated using Synapse
 * @return InvocationResponse.CONTINUE if Synapse lets the message to flow and
 *  InvocationResponse.ABORT if not
 * @throws AxisFault - incase of a failure in mediation of initiation of the mediation
 */
public InvocationResponse invoke(MessageContext messageContext) throws AxisFault {
    HandlerUtil.doHandlerLogging(log, messageContext);
    org.apache.synapse.MessageContext synCtx = MessageContextCreatorForAxis2.getSynapseMessageContext(messageContext);
    // handles the incoming and outgoing behaviours in client and server sides
    if (messageContext.isServerSide()) {
        synCtx.setProperty(SynapseConstants.RESPONSE, Boolean.TRUE);
        synCtx.setResponse(true);
        synCtx.setProperty(SynapseConstants.RESPONSE, Boolean.TRUE);
        synCtx.setResponse(true);
        try {
            // if synapse says ok let the message to flow through
            if (HandlerUtil.mediateOutMessage(log, messageContext, synCtx)) {
                return InvocationResponse.CONTINUE;
            } else {
                // if not abort the further processings
                log.debug("Synapse has decided to abort the message:\n" + synCtx);
                return InvocationResponse.ABORT;
            }
        } catch (SynapseException syne) {
        // todo : invoke the fault sequence
        }
    } else {
        synCtx.setProperty(SynapseConstants.RESPONSE, Boolean.FALSE);
        synCtx.setResponse(false);
        try {
            // if synapse says ok let the message to flow through
            if (HandlerUtil.mediateInMessage(log, messageContext, synCtx)) {
                return InvocationResponse.CONTINUE;
            } else {
                // if not abort the further processings
                log.debug("Synapse has decided to abort the message:\n" + synCtx);
                return InvocationResponse.ABORT;
            }
        } catch (SynapseException syne) {
        // todo : invoke the fault sequence
        }
    }
    try {
        // if synapse says ok let the message to flow through
        if (synCtx.getEnvironment().injectMessage(synCtx)) {
            return InvocationResponse.CONTINUE;
        } else {
            // if not abort the further processings
            log.debug("Synapse has decided to abort the message:\n" + synCtx.getEnvelope());
            return InvocationResponse.ABORT;
        }
    } catch (SynapseException syne) {
        if (!synCtx.getFaultStack().isEmpty()) {
            ((FaultHandler) synCtx.getFaultStack().pop()).handleFault(synCtx, syne);
        } else {
            log.error("Synapse encountered an exception, " + "No error handlers found.\n" + syne.getMessage());
            throw new AxisFault("Synapse encountered an error." + syne);
        }
    }
    // general case should let the message flow
    return InvocationResponse.CONTINUE;
}
Also used : AxisFault(org.apache.axis2.AxisFault) SynapseException(org.apache.synapse.SynapseException)

Aggregations

SynapseException (org.apache.synapse.SynapseException)136 OMElement (org.apache.axiom.om.OMElement)31 OMAttribute (org.apache.axiom.om.OMAttribute)23 MessageContext (org.apache.synapse.MessageContext)20 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)16 QName (javax.xml.namespace.QName)15 Iterator (java.util.Iterator)14 JaxenException (org.jaxen.JaxenException)14 XMLStreamException (javax.xml.stream.XMLStreamException)13 AxisFault (org.apache.axis2.AxisFault)13 Map (java.util.Map)12 Endpoint (org.apache.synapse.endpoints.Endpoint)12 ArrayList (java.util.ArrayList)11 HashMap (java.util.HashMap)10 IOException (java.io.IOException)8 MalformedURLException (java.net.MalformedURLException)8 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)8 OMNode (org.apache.axiom.om.OMNode)7 Mediator (org.apache.synapse.Mediator)7 MediatorProperty (org.apache.synapse.mediators.MediatorProperty)7