Search in sources :

Example 31 with SequenceMediator

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

the class ResourceFactory method configureSequences.

private static void configureSequences(Resource resource, OMElement resourceElt, Properties properties) {
    OMAttribute inSequenceKeyAtt = resourceElt.getAttribute(new QName("inSequence"));
    OMElement inSequenceElt = resourceElt.getFirstChildWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "inSequence"));
    if (inSequenceKeyAtt != null && !"".equals(inSequenceKeyAtt.getAttributeValue())) {
        resource.setInSequenceKey(inSequenceKeyAtt.getAttributeValue());
    } else if (inSequenceElt != null) {
        SequenceMediatorFactory fac = new SequenceMediatorFactory();
        SequenceMediator sequence = fac.createAnonymousSequence(inSequenceElt, properties);
        sequence.setSequenceType(SequenceType.API_INSEQ);
        resource.setInSequence(sequence);
    }
    OMAttribute outSequenceKeyAtt = resourceElt.getAttribute(new QName("outSequence"));
    OMElement outSequenceElt = resourceElt.getFirstChildWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "outSequence"));
    if (outSequenceKeyAtt != null && !"".equals(outSequenceKeyAtt.getAttributeValue())) {
        resource.setOutSequenceKey(outSequenceKeyAtt.getAttributeValue());
    } else if (outSequenceElt != null) {
        SequenceMediatorFactory fac = new SequenceMediatorFactory();
        SequenceMediator sequence = fac.createAnonymousSequence(outSequenceElt, properties);
        sequence.setSequenceType(SequenceType.API_OUTSEQ);
        resource.setOutSequence(sequence);
    }
    OMAttribute faultSequenceKeyAtt = resourceElt.getAttribute(new QName("faultSequence"));
    OMElement faultSequenceElt = resourceElt.getFirstChildWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "faultSequence"));
    if (faultSequenceKeyAtt != null && !"".equals(faultSequenceKeyAtt.getAttributeValue())) {
        resource.setFaultSequenceKey(faultSequenceKeyAtt.getAttributeValue());
    } else if (faultSequenceElt != null) {
        SequenceMediatorFactory fac = new SequenceMediatorFactory();
        SequenceMediator sequence = fac.createAnonymousSequence(faultSequenceElt, properties);
        sequence.setSequenceType(SequenceType.API_FAULTSEQ);
        resource.setFaultSequence(sequence);
    }
}
Also used : SequenceMediatorFactory(org.apache.synapse.config.xml.SequenceMediatorFactory) QName(javax.xml.namespace.QName) OMElement(org.apache.axiom.om.OMElement) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 32 with SequenceMediator

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

the class MultiXMLConfigurationBuilder method createSequences.

private static void createSequences(SynapseConfiguration synapseConfig, String rootDirPath, Properties properties) {
    File sequencesDir = new File(rootDirPath, SEQUENCES_DIR);
    if (sequencesDir.exists()) {
        if (log.isDebugEnabled()) {
            log.debug("Loading sequences from : " + sequencesDir.getPath());
        }
        Iterator sequences = FileUtils.iterateFiles(sequencesDir, extensions, false);
        while (sequences.hasNext()) {
            File file = (File) sequences.next();
            try {
                OMElement document = getOMElement(file);
                Mediator seq = SynapseXMLConfigurationFactory.defineSequence(synapseConfig, document, properties);
                if (seq != null && seq instanceof SequenceMediator) {
                    SequenceMediator sequence = (SequenceMediator) seq;
                    sequence.setFileName(file.getName());
                    synapseConfig.getArtifactDeploymentStore().addArtifact(file.getAbsolutePath(), sequence.getName());
                }
            } catch (Exception e) {
                String msg = "Sequence configuration cannot be built from : " + file.getName();
                handleConfigurationError(SynapseConstants.FAIL_SAFE_MODE_SEQUENCES, msg, e);
            }
        }
    }
}
Also used : Iterator(java.util.Iterator) OMElement(org.apache.axiom.om.OMElement) TemplateMediator(org.apache.synapse.mediators.template.TemplateMediator) Mediator(org.apache.synapse.Mediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) XMLStreamException(javax.xml.stream.XMLStreamException) SynapseException(org.apache.synapse.SynapseException)

Example 33 with SequenceMediator

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

the class MultiXMLConfigurationSerializer method serializeLocalEntry.

public OMElement serializeLocalEntry(Object o, SynapseConfiguration synapseConfig, OMElement parent) throws Exception {
    if (o instanceof TemplateMediator) {
        return serializeTemplate((TemplateMediator) o, synapseConfig, parent);
    } else if (o instanceof SequenceMediator) {
        return serializeSequence((SequenceMediator) o, synapseConfig, parent);
    } else if (o instanceof Template) {
        return serializeTemplate((Template) o, synapseConfig, parent);
    } else if (o instanceof Endpoint) {
        return serializeEndpoint((Endpoint) o, synapseConfig, parent);
    } else if (o instanceof Entry) {
        Entry entry = (Entry) o;
        if ((SynapseConstants.SERVER_HOST.equals(entry.getKey()) || SynapseConstants.SERVER_IP.equals(entry.getKey())) || entry.getType() == Entry.REMOTE_ENTRY) {
            return null;
        }
        File entriesDir = createDirectory(currentDirectory, MultiXMLConfigurationBuilder.LOCAL_ENTRY_DIR);
        OMElement entryElem = EntrySerializer.serializeEntry(entry, null);
        String fileName = entry.getFileName();
        if (fileName != null) {
            if (currentDirectory == rootDirectory) {
                handleDeployment(entriesDir, fileName, entry.getKey(), synapseConfig.getArtifactDeploymentStore());
            }
            File entryFile = new File(entriesDir, fileName);
            writeToFile(entryElem, entryFile);
        } else if (parent != null) {
            parent.addChild(entryElem);
        }
        return entryElem;
    }
    return null;
}
Also used : Entry(org.apache.synapse.config.Entry) TemplateMediator(org.apache.synapse.mediators.template.TemplateMediator) Endpoint(org.apache.synapse.endpoints.Endpoint) AbstractEndpoint(org.apache.synapse.endpoints.AbstractEndpoint) InboundEndpoint(org.apache.synapse.inbound.InboundEndpoint) OMElement(org.apache.axiom.om.OMElement) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) Template(org.apache.synapse.endpoints.Template)

Example 34 with SequenceMediator

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

the class ProxyServiceFactory method createProxy.

public static ProxyService createProxy(OMElement elem, Properties properties) {
    ProxyService proxy = null;
    OMAttribute name = elem.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "name"));
    if (name == null) {
        handleException("The 'name' attribute is required for a Proxy service definition");
    } else {
        proxy = new ProxyService(name.getAttributeValue());
    }
    OMAttribute trans = elem.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "transports"));
    if (trans != null) {
        String transports = trans.getAttributeValue();
        if (transports == null || ProxyService.ALL_TRANSPORTS.equals(transports)) {
        // default to all transports using service name as destination
        } else {
            StringTokenizer st = new StringTokenizer(transports, " ,");
            ArrayList<String> transportList = new ArrayList<String>();
            while (st.hasMoreTokens()) {
                String token = st.nextToken();
                if (token.length() != 0) {
                    transportList.add(token);
                }
            }
            proxy.setTransports(transportList);
        }
    }
    OMAttribute pinnedServers = elem.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "pinnedServers"));
    if (pinnedServers != null) {
        String pinnedServersValue = pinnedServers.getAttributeValue();
        if (pinnedServersValue == null) {
        // default to all servers
        } else {
            StringTokenizer st = new StringTokenizer(pinnedServersValue, " ,");
            List<String> pinnedServersList = new ArrayList<String>();
            while (st.hasMoreTokens()) {
                String token = st.nextToken();
                if (token.length() != 0) {
                    pinnedServersList.add(token);
                }
            }
            proxy.setPinnedServers(pinnedServersList);
        }
    }
    OMAttribute startOnLoad = elem.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "startOnLoad"));
    if (startOnLoad != null) {
        proxy.setStartOnLoad(Boolean.valueOf(startOnLoad.getAttributeValue()));
    } else {
        proxy.setStartOnLoad(true);
    }
    OMAttribute serviceGroup = elem.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "serviceGroup"));
    if (serviceGroup != null) {
        proxy.setServiceGroup(serviceGroup.getAttributeValue());
    }
    // setting the description of the proxy service
    OMElement descriptionElement = elem.getFirstChildWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "description"));
    if (descriptionElement != null) {
        proxy.setDescription(descriptionElement.getText().trim());
    }
    // read definition of the target of this proxy service. The target could be an 'endpoint'
    // or a named sequence. If none of these are specified, the messages would be mediated
    // by the Synapse main mediator
    OMElement target = elem.getFirstChildWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "target"));
    if (target != null) {
        boolean isTargetOk = false;
        SequenceMediatorFactory mediatorFactory = new SequenceMediatorFactory();
        OMAttribute inSequence = target.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "inSequence"));
        if (inSequence != null) {
            proxy.setTargetInSequence(inSequence.getAttributeValue());
            isTargetOk = true;
        } else {
            OMElement inSequenceElement = target.getFirstChildWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "inSequence"));
            if (inSequenceElement != null) {
                SequenceMediator inSequenceMediator = mediatorFactory.createAnonymousSequence(inSequenceElement, properties);
                inSequenceMediator.setSequenceType(SequenceType.PROXY_INSEQ);
                proxy.setTargetInLineInSequence(inSequenceMediator);
                isTargetOk = true;
            }
        }
        OMAttribute outSequence = target.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "outSequence"));
        if (outSequence != null) {
            proxy.setTargetOutSequence(outSequence.getAttributeValue());
        } else {
            OMElement outSequenceElement = target.getFirstChildWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "outSequence"));
            if (outSequenceElement != null) {
                SequenceMediator outSequenceMediator = mediatorFactory.createAnonymousSequence(outSequenceElement, properties);
                outSequenceMediator.setSequenceType(SequenceType.PROXY_OUTSEQ);
                proxy.setTargetInLineOutSequence(outSequenceMediator);
            }
        }
        OMAttribute faultSequence = target.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "faultSequence"));
        if (faultSequence != null) {
            proxy.setTargetFaultSequence(faultSequence.getAttributeValue());
        } else {
            OMElement faultSequenceElement = target.getFirstChildWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "faultSequence"));
            if (faultSequenceElement != null) {
                SequenceMediator faultSequenceMediator = mediatorFactory.createAnonymousSequence(faultSequenceElement, properties);
                faultSequenceMediator.setSequenceType(SequenceType.PROXY_FAULTSEQ);
                proxy.setTargetInLineFaultSequence(faultSequenceMediator);
            }
        }
        OMAttribute tgtEndpt = target.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "endpoint"));
        if (tgtEndpt != null) {
            proxy.setTargetEndpoint(tgtEndpt.getAttributeValue());
            isTargetOk = true;
        } else {
            OMElement endpointElement = target.getFirstChildWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "endpoint"));
            if (endpointElement != null) {
                proxy.setTargetInLineEndpoint(EndpointFactory.getEndpointFromElement(endpointElement, true, properties));
                isTargetOk = true;
            }
        }
        if (!isTargetOk) {
            handleException("Target of the proxy service must declare " + "either an inSequence or endpoint or both");
        }
    } else {
        handleException("Target is required for a Proxy service definition");
    }
    // read the WSDL, Schemas and Policies and set to the proxy service
    OMElement wsdl = elem.getFirstChildWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "publishWSDL"));
    if (wsdl != null) {
        OMAttribute wsdlEndpoint = wsdl.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "endpoint"));
        OMAttribute wsdlKey = wsdl.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "key"));
        OMAttribute preservePolicy = wsdl.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "preservePolicy"));
        if (preservePolicy != null) {
            proxy.setPreservePolicy(preservePolicy.getAttributeValue());
        }
        if (wsdlEndpoint != null) {
            proxy.setPublishWSDLEndpoint(wsdlEndpoint.getAttributeValue());
        } else if (wsdlKey != null) {
            proxy.setWSDLKey(wsdlKey.getAttributeValue());
        } else {
            OMAttribute wsdlURI = wsdl.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "uri"));
            if (wsdlURI != null) {
                try {
                    proxy.setWsdlURI(new URI(wsdlURI.getAttributeValue()));
                } catch (URISyntaxException e) {
                    String msg = "Error creating uri for proxy service wsdl";
                    log.error(msg);
                    handleException(msg, e);
                }
            } else {
                OMElement wsdl11 = wsdl.getFirstChildWithName(new QName(WSDLConstants.WSDL1_1_NAMESPACE, "definitions"));
                if (wsdl11 != null) {
                    proxy.setInLineWSDL(wsdl11);
                } else {
                    OMElement wsdl20 = wsdl.getFirstChildWithName(new QName(WSDL2Constants.WSDL_NAMESPACE, "description"));
                    if (wsdl20 != null) {
                        proxy.setInLineWSDL(wsdl20);
                    }
                }
            }
        }
        proxy.setResourceMap(ResourceMapFactory.createResourceMap(wsdl));
    }
    Iterator policies = elem.getChildrenWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "policy"));
    while (policies.hasNext()) {
        Object o = policies.next();
        if (o instanceof OMElement) {
            OMElement policy = (OMElement) o;
            OMAttribute key = policy.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "key"));
            OMAttribute type = policy.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "type"));
            OMAttribute operationName = policy.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "operationName"));
            OMAttribute operationNS = policy.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "operationNamespace"));
            if (key != null) {
                PolicyInfo pi = new PolicyInfo(key.getAttributeValue());
                if (type != null && type.getAttributeValue() != null) {
                    if ("in".equals(type.getAttributeValue())) {
                        pi.setType(PolicyInfo.MESSAGE_TYPE_IN);
                    } else if ("out".equals(type.getAttributeValue())) {
                        pi.setType(PolicyInfo.MESSAGE_TYPE_OUT);
                    } else {
                        handleException("Undefined policy type for the policy with key : " + key.getAttributeValue());
                    }
                }
                if (operationName != null && operationName.getAttributeValue() != null) {
                    if (operationNS != null && operationNS.getAttributeValue() != null) {
                        pi.setOperation(new QName(operationNS.getAttributeValue(), operationName.getAttributeValue()));
                    } else {
                        pi.setOperation(new QName(operationName.getAttributeValue()));
                    }
                }
                proxy.addPolicyInfo(pi);
            } else {
                handleException("Policy element does not specify the policy key");
            }
        } else {
            handleException("Invalid 'policy' element found under element 'policies'");
        }
    }
    String nameString = proxy.getName();
    if (nameString == null || "".equals(nameString)) {
        nameString = SynapseConstants.ANONYMOUS_PROXYSERVICE;
    }
    AspectConfiguration aspectConfiguration = new AspectConfiguration(nameString);
    proxy.configure(aspectConfiguration);
    OMAttribute statistics = elem.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, XMLConfigConstants.STATISTICS_ATTRIB_NAME));
    if (statistics != null) {
        String statisticsValue = statistics.getAttributeValue();
        if (statisticsValue != null) {
            if (XMLConfigConstants.STATISTICS_ENABLE.equals(statisticsValue)) {
                aspectConfiguration.enableStatistics();
            }
        }
    }
    OMAttribute tracing = elem.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, XMLConfigConstants.TRACE_ATTRIB_NAME));
    if (tracing != null) {
        String tracingValue = tracing.getAttributeValue();
        if (tracingValue != null) {
            if (XMLConfigConstants.TRACE_ENABLE.equals(tracingValue)) {
                aspectConfiguration.enableTracing();
            }
        }
    }
    Iterator props = elem.getChildrenWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "parameter"));
    while (props.hasNext()) {
        Object o = props.next();
        if (o instanceof OMElement) {
            OMElement prop = (OMElement) o;
            OMAttribute pname = prop.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "name"));
            OMElement propertyValue = prop.getFirstElement();
            if (pname != null) {
                if (propertyValue != null) {
                    proxy.addParameter(pname.getAttributeValue(), propertyValue);
                } else {
                    proxy.addParameter(pname.getAttributeValue(), prop.getText().trim());
                }
            } else {
                handleException("Invalid property specified for proxy service : " + name);
            }
        } else {
            handleException("Invalid property specified for proxy service : " + name);
        }
    }
    if (elem.getFirstChildWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "enableAddressing")) != null) {
        proxy.setWsAddrEnabled(true);
    }
    if (elem.getFirstChildWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "enableSec")) != null) {
        proxy.setWsSecEnabled(true);
    }
    return proxy;
}
Also used : QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) PolicyInfo(org.apache.synapse.util.PolicyInfo) OMElement(org.apache.axiom.om.OMElement) URISyntaxException(java.net.URISyntaxException) AspectConfiguration(org.apache.synapse.aspects.AspectConfiguration) URI(java.net.URI) StringTokenizer(java.util.StringTokenizer) ProxyService(org.apache.synapse.core.axis2.ProxyService) Iterator(java.util.Iterator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 35 with SequenceMediator

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

the class ForEachMediatorFactory method createSpecificMediator.

@Override
protected Mediator createSpecificMediator(OMElement elem, Properties properties) {
    ForEachMediator mediator = new ForEachMediator();
    processAuditStatus(mediator, elem);
    OMAttribute id = elem.getAttribute(ID_Q);
    if (id != null) {
        mediator.setId(id.getAttributeValue());
    }
    OMAttribute expression = elem.getAttribute(ATT_EXPRN);
    if (expression != null) {
        try {
            mediator.setExpression(SynapseXPathFactory.getSynapseXPath(elem, ATT_EXPRN));
        } catch (JaxenException e) {
            handleException("Unable to build the ForEach Mediator. " + "Invalid XPath " + expression.getAttributeValue(), e);
        }
    } else {
        handleException("XPath expression is required " + "for an ForEach Mediator under the \"expression\" attribute");
    }
    OMAttribute sequenceAttr = elem.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "sequence"));
    OMElement sequence;
    if (sequenceAttr != null && sequenceAttr.getAttributeValue() != null) {
        mediator.setSequenceRef(sequenceAttr.getAttributeValue());
    } else if ((sequence = elem.getFirstChildWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "sequence"))) != null) {
        SequenceMediatorFactory fac = new SequenceMediatorFactory();
        SequenceMediator sequenceMediator = fac.createAnonymousSequence(sequence, properties);
        if (validateSequence(sequenceMediator)) {
            mediator.setSequence(sequenceMediator);
        } else {
            handleException("Sequence cannot contain Call, Send or CallOut mediators");
        }
    }
    return mediator;
}
Also used : ForEachMediator(org.apache.synapse.mediators.builtin.ForEachMediator) QName(javax.xml.namespace.QName) JaxenException(org.jaxen.JaxenException) OMElement(org.apache.axiom.om.OMElement) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) OMAttribute(org.apache.axiom.om.OMAttribute)

Aggregations

SequenceMediator (org.apache.synapse.mediators.base.SequenceMediator)70 TestMediator (org.apache.synapse.mediators.TestMediator)18 OMElement (org.apache.axiom.om.OMElement)12 ProxyService (org.apache.synapse.core.axis2.ProxyService)12 Endpoint (org.apache.synapse.endpoints.Endpoint)12 Mediator (org.apache.synapse.Mediator)11 AbstractMediator (org.apache.synapse.mediators.AbstractMediator)11 MessageContext (org.apache.synapse.MessageContext)9 TemplateMediator (org.apache.synapse.mediators.template.TemplateMediator)8 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)7 InboundEndpoint (org.apache.synapse.inbound.InboundEndpoint)7 OMAttribute (org.apache.axiom.om.OMAttribute)6 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)6 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)6 File (java.io.File)4 ManagedLifecycle (org.apache.synapse.ManagedLifecycle)4 SynapseException (org.apache.synapse.SynapseException)4 SynapseLog (org.apache.synapse.SynapseLog)4 QName (javax.xml.namespace.QName)3 OMNode (org.apache.axiom.om.OMNode)3