Search in sources :

Example 41 with SequenceMediator

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

the class FilterMediator method init.

@Override
public void init(SynapseEnvironment se) {
    super.init(se);
    synapseEnv = se;
    if (elseMediator != null) {
        elseMediator.init(se);
    } else if (elseKey != null) {
        SequenceMediator elseSequence = (SequenceMediator) se.getSynapseConfiguration().getSequence(elseKey);
        if (elseSequence == null || elseSequence.isDynamic()) {
            se.addUnavailableArtifactRef(elseKey);
        }
    }
    if (thenKey != null) {
        SequenceMediator thenSequence = (SequenceMediator) se.getSynapseConfiguration().getSequence(thenKey);
        if (thenSequence == null || thenSequence.isDynamic()) {
            se.addUnavailableArtifactRef(thenKey);
        }
    }
}
Also used : SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator)

Example 42 with SequenceMediator

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

the class MessageInjector method execute.

/**
 * This will be invoked by the scheduler to inject the message
 * in to the SynapseEnvironment
 */
public void execute() {
    if (log.isDebugEnabled()) {
        log.debug("execute");
    }
    if (synapseEnvironment == null) {
        handleError("Synapse Environment not set");
        return;
    }
    if (synapseEnvironment.getTaskManager() != null && !synapseEnvironment.getTaskManager().isInitialized()) {
        log.warn("Task Manager not initialized. Not executing the cycle");
        return;
    }
    if (message == null && registryKey == null) {
        handleError("message or registry-key not set");
        return;
    }
    if (INJECT_TO_PROXY.equalsIgnoreCase(injectTo)) {
        if (proxyName == null || proxyName.equals("")) {
            handleError("Proxy service name not specified");
        }
        // Prepare axis2 message context
        org.apache.axis2.context.MessageContext axis2MsgCtx = new org.apache.axis2.context.MessageContext();
        ConfigurationContext configurationContext = ((Axis2SynapseEnvironment) synapseEnvironment).getAxis2ConfigurationContext();
        axis2MsgCtx.setConfigurationContext(configurationContext);
        axis2MsgCtx.setIncomingTransportName(Constants.TRANSPORT_LOCAL);
        axis2MsgCtx.setServerSide(true);
        axis2MsgCtx.setMessageID(UIDGenerator.generateURNString());
        try {
            AxisService axisService = configurationContext.getAxisConfiguration().getService(proxyName);
            if (axisService == null) {
                handleError("Proxy Service: " + proxyName + " not found");
            }
            axis2MsgCtx.setAxisService(axisService);
        } catch (AxisFault axisFault) {
            handleError("Error occurred while attempting to find the Proxy Service");
        }
        if (to != null) {
            axis2MsgCtx.setTo(new EndpointReference(to));
        }
        SOAPEnvelope envelope = null;
        if (format == null) {
            envelope = OMAbstractFactory.getSOAP12Factory().createSOAPEnvelope();
        } else if (SOAP11_FORMAT.equalsIgnoreCase(format)) {
            envelope = OMAbstractFactory.getSOAP11Factory().createSOAPEnvelope();
        } else if (SOAP12_FORMAT.equalsIgnoreCase(format)) {
            envelope = OMAbstractFactory.getSOAP12Factory().createSOAPEnvelope();
        } else if (POX_FORMAT.equalsIgnoreCase(format)) {
            envelope = OMAbstractFactory.getSOAP12Factory().createSOAPEnvelope();
            axis2MsgCtx.setDoingREST(true);
        } else if (GET_FORMAT.equalsIgnoreCase(format)) {
            envelope = OMAbstractFactory.getSOAP12Factory().createSOAPEnvelope();
            axis2MsgCtx.setDoingREST(true);
            axis2MsgCtx.setProperty(Constants.Configuration.HTTP_METHOD, Constants.Configuration.HTTP_METHOD_GET);
        } else {
            handleError("incorrect format specified");
        }
        try {
            PayloadHelper.setXMLPayload(envelope, message.cloneOMElement());
            axis2MsgCtx.setEnvelope(envelope);
        } catch (AxisFault axisFault) {
            handleError("Error in setting the message payload : " + message);
        }
        if (soapAction != null) {
            axis2MsgCtx.setSoapAction(soapAction);
        }
        try {
            if (log.isDebugEnabled()) {
                log.debug("injecting message to proxy service : " + proxyName);
            }
            AxisEngine.receive(axis2MsgCtx);
        } catch (AxisFault axisFault) {
            handleError("Error occurred while invoking proxy service : " + proxyName);
        }
    } else {
        MessageContext mc = synapseEnvironment.createMessageContext();
        mc.setMessageID(UIDGenerator.generateURNString());
        mc.pushFaultHandler(new MediatorFaultHandler(mc.getFaultSequence()));
        if (to != null) {
            mc.setTo(new EndpointReference(to));
        }
        if (registryKey == null) {
            if (format == null) {
                PayloadHelper.setXMLPayload(mc, message.cloneOMElement());
            } else {
                try {
                    if (SOAP11_FORMAT.equalsIgnoreCase(format)) {
                        mc.setEnvelope(OMAbstractFactory.getSOAP11Factory().createSOAPEnvelope());
                    } else if (SOAP12_FORMAT.equalsIgnoreCase(format)) {
                        mc.setEnvelope(OMAbstractFactory.getSOAP12Factory().createSOAPEnvelope());
                    } else if (POX_FORMAT.equalsIgnoreCase(format)) {
                        mc.setDoingPOX(true);
                    } else if (GET_FORMAT.equalsIgnoreCase(format)) {
                        mc.setDoingGET(true);
                    }
                    PayloadHelper.setXMLPayload(mc, message.cloneOMElement());
                } catch (AxisFault axisFault) {
                    handleError("Error in setting the message payload : " + message);
                }
            }
        } else {
            Object entry = mc.getEntry(registryKey);
            if (entry == null) {
                handleError("Key " + registryKey + " not found ");
            }
            String text = "";
            if (entry instanceof OMElement) {
                OMElement e = (OMElement) entry;
                removeIndentations(e);
                text = e.toString();
            } else if (entry instanceof OMText) {
                text = ((OMText) entry).getText();
            } else if (entry instanceof String) {
                text = (String) entry;
            }
            OMElement omXML = null;
            try {
                omXML = AXIOMUtil.stringToOM(text);
                if (format == null) {
                    PayloadHelper.setXMLPayload(mc, omXML);
                } else {
                    if (SOAP11_FORMAT.equalsIgnoreCase(format)) {
                        mc.setEnvelope(OMAbstractFactory.getSOAP11Factory().createSOAPEnvelope());
                    } else if (SOAP12_FORMAT.equalsIgnoreCase(format)) {
                        mc.setEnvelope(OMAbstractFactory.getSOAP12Factory().createSOAPEnvelope());
                    } else if (POX_FORMAT.equalsIgnoreCase(format)) {
                        mc.setDoingPOX(true);
                    } else if (GET_FORMAT.equalsIgnoreCase(format)) {
                        mc.setDoingGET(true);
                    }
                    PayloadHelper.setXMLPayload(mc, omXML);
                }
            } catch (XMLStreamException e) {
                handleError("Error parsing XML for JSON conversion, please check your property values return valid XML");
            } catch (AxisFault axisFault) {
                handleError("Error in setting the message payload : " + omXML);
            }
        }
        if (soapAction != null) {
            mc.setSoapAction(soapAction);
        }
        // Adding runtime properties to SynapseMessageContext, if exists
        if (runtimeProperties != null && runtimeProperties.size() > 0) {
            for (Map.Entry<String, Object> entry : runtimeProperties.entrySet()) {
                mc.setProperty(entry.getKey(), entry.getValue());
            }
        }
        if (INJECT_TO_SEQUENCE.equalsIgnoreCase(injectTo)) {
            if (sequenceName == null || sequenceName.equals("")) {
                handleError("Sequence name not specified");
            }
            SequenceMediator seq = (SequenceMediator) synapseEnvironment.getSynapseConfiguration().getSequence(sequenceName);
            if (seq != null) {
                if (log.isDebugEnabled()) {
                    log.debug("injecting message to sequence : " + sequenceName);
                }
                mc.pushFaultHandler(new MediatorFaultHandler(mc.getFaultSequence()));
                synapseEnvironment.injectAsync(mc, seq);
            } else {
                handleError("Sequence: " + sequenceName + " not found");
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("injecting message to main sequence");
            }
            synapseEnvironment.injectMessage(mc);
        }
    }
}
Also used : AxisFault(org.apache.axis2.AxisFault) ConfigurationContext(org.apache.axis2.context.ConfigurationContext) MediatorFaultHandler(org.apache.synapse.mediators.MediatorFaultHandler) AxisService(org.apache.axis2.description.AxisService) OMElement(org.apache.axiom.om.OMElement) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) EndpointReference(org.apache.axis2.addressing.EndpointReference) Axis2SynapseEnvironment(org.apache.synapse.core.axis2.Axis2SynapseEnvironment) XMLStreamException(javax.xml.stream.XMLStreamException) OMText(org.apache.axiom.om.OMText) MessageContext(org.apache.synapse.MessageContext) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) HashMap(java.util.HashMap) Map(java.util.Map)

Example 43 with SequenceMediator

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

the class RegistryResourceFetcher method getResource.

private Object getResource(Entry entry, Properties properties) {
    OMNode omNode;
    RegistryEntry re = registry.getRegistryEntry(entry.getKey());
    omNode = registry.lookup(entry.getKey());
    if (re == null) {
        return null;
    }
    if ((!entry.isCached() || (re.getVersion() == Long.MIN_VALUE || re.getVersion() != entry.getVersion())) || re.getLastModified() >= lastExecutionTime) {
        entry.setEntryProperties(registry.getResourceProperties(entry.getKey()));
        entry.setVersion(re.getVersion());
        // if we get here, we have received the raw omNode from the
        // registry and our previous copy (if we had one) has expired or is not valid
        Object expiredValue = entry.getValue();
        // resource into the appropriate object - e.g. sequence or endpoint
        if (entry.getMapper() != null) {
            entry.setValue(entry.getMapper().getObjectFromOMNode(omNode, properties));
            if (entry.getValue() instanceof SequenceMediator) {
                SequenceMediator seq = (SequenceMediator) entry.getValue();
                seq.setDynamic(true);
                seq.setRegistryKey(entry.getKey());
                seq.init(synapseEnvironment);
            } else if (entry.getValue() instanceof Endpoint) {
                Endpoint ep = (Endpoint) entry.getValue();
                ep.init(synapseEnvironment);
            }
        } else {
            // if the type of the object is known to have a mapper, create the
            // resultant Object using the known mapper, and cache this Object
            // else cache the raw OMNode
            entry.setValue(omNode);
        }
        if (expiredValue != null) {
            // Destroy the old resource so that everything is properly cleaned up
            if (expiredValue instanceof SequenceMediator) {
                ((SequenceMediator) expiredValue).destroy();
            } else if (expiredValue instanceof Endpoint) {
                ((Endpoint) expiredValue).destroy();
            }
        }
        entry.setVersion(re.getVersion());
    }
    // new getRegistryEntry() call
    if (re.getCachableDuration() > 0) {
        entry.setExpiryTime(System.currentTimeMillis() + re.getCachableDuration());
    } else {
        entry.setExpiryTime(-1);
    }
    return entry.getValue();
}
Also used : OMNode(org.apache.axiom.om.OMNode) Endpoint(org.apache.synapse.endpoints.Endpoint) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) RegistryEntry(org.apache.synapse.registry.RegistryEntry)

Example 44 with SequenceMediator

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

the class TemplateMessageExecutor method init.

public void init(SynapseEnvironment se) {
    // Initialize the template and populate the parameters
    synapseEnvironment = se;
    invoker = new InvokeMediator();
    invoker.setTargetTemplate(templateKey);
    buildParameters(templateParams);
    // Remove if there's a sequence already exists
    if (se.getSynapseConfiguration().getSequence(SynapseConstants.PREFIX_HIDDEN_SEQUENCE_KEY + templateKey.hashCode()) != null) {
        se.getSynapseConfiguration().removeSequence(SynapseConstants.PREFIX_HIDDEN_SEQUENCE_KEY + templateKey.hashCode());
    }
    seqMed = new SequenceMediator();
    seqMed.setName(SynapseConstants.PREFIX_HIDDEN_SEQUENCE_KEY + templateKey.hashCode());
    seqMed.addChild(invoker);
    se.getSynapseConfiguration().addSequence(seqMed.getName(), seqMed);
}
Also used : InvokeMediator(org.apache.synapse.mediators.template.InvokeMediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator)

Example 45 with SequenceMediator

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

the class AbstractRegistry method getResource.

/**
 * Get the resource for the given key from this registry
 * @param entry The Enrty instance that contains meta-data
 * @param properties bag of properties with additional information
 * @return the matching resultant object
 */
public Object getResource(Entry entry, Properties properties) {
    OMNode omNode = null;
    RegistryEntry re = null;
    // if we have an unexpired cached copy, return the cached object
    if (entry.isCached() && !entry.isExpired()) {
        return entry.getValue();
    // if we have not cached the referenced object, fetch it and its RegistryEntry
    } else if (!entry.isCached()) {
        try {
            omNode = lookup(entry.getKey());
            entry.setEntryProperties(getResourceProperties(entry.getKey()));
        } catch (OMException e) {
            log.error("Error reading registry resource file : " + entry.getKey(), e);
        }
        if (omNode == null && entry.getEntryProperties() != null && !entry.getEntryProperties().isEmpty()) {
            // Collection
            re = getRegistryEntry(entry.getKey());
            if (re != null) {
                setExpiryTime(entry, re);
                entry.setVersion(re.getVersion());
            }
        }
        if (omNode == null) {
            return null;
        } else {
            re = getRegistryEntry(entry.getKey());
        }
    // if we have cached it before, and now the cache has expired
    // get its *new* registry entry and compare versions and pick new cache duration
    } else if (entry.isExpired()) {
        if (log.isDebugEnabled()) {
            log.debug("Cached object has expired for key : " + entry.getKey());
        }
        re = getRegistryEntry(entry.getKey());
        if (re.getVersion() != Long.MIN_VALUE && re.getVersion() == entry.getVersion()) {
            if (log.isDebugEnabled()) {
                log.debug("Expired version number is same as current version in registry");
            }
            // renew cache lease for another cachable duration (as returned by the
            // new getRegistryEntry() call
            setExpiryTime(entry, re);
            if (log.isDebugEnabled()) {
                log.debug("Renew cache lease for another " + re.getCachableDuration() / 1000 + "s");
            }
            // return cached object
            return entry.getValue();
        } else {
            omNode = lookup(entry.getKey());
            entry.setEntryProperties(getResourceProperties(entry.getKey()));
            if (omNode == null && entry.getEntryProperties() != null && !entry.getEntryProperties().isEmpty()) {
                // Collection
                re = getRegistryEntry(entry.getKey());
                if (re != null) {
                    setExpiryTime(entry, re);
                    entry.setVersion(re.getVersion());
                }
            }
            if (omNode == null) {
                return null;
            }
        }
    }
    // if we get here, we have received the raw omNode from the
    // registry and our previous copy (if we had one) has expired or is not valid
    Object expiredValue = entry.getValue();
    // resource into the appropriate object - e.g. sequence or endpoint
    if (entry.getMapper() != null) {
        entry.setValue(entry.getMapper().getObjectFromOMNode(omNode, properties));
        if (entry.getValue() instanceof SequenceMediator) {
            SequenceMediator seq = (SequenceMediator) entry.getValue();
            seq.setDynamic(true);
            seq.setRegistryKey(entry.getKey());
        } else if (entry.getValue() instanceof Endpoint) {
            Endpoint ep = (Endpoint) entry.getValue();
        } else if (entry.getValue() instanceof TemplateMediator) {
            ((TemplateMediator) entry.getValue()).setDynamic(true);
        }
    } else {
        // else cache the raw OMNode
        if (re != null && re.getType() != null) {
            XMLToObjectMapper mapper = getMapper(re.getType());
            if (mapper != null) {
                entry.setMapper(mapper);
                entry.setValue(mapper.getObjectFromOMNode(omNode, properties));
            } else {
                entry.setValue(omNode);
            }
        }
    }
    if (expiredValue != null) {
        // Destroy the old resource so that everything is properly cleaned up
        if (expiredValue instanceof SequenceMediator) {
            ((SequenceMediator) expiredValue).destroy();
        } else if (expiredValue instanceof Endpoint) {
            ((Endpoint) expiredValue).destroy();
        }
    }
    // increment cache expiry time as specified by the last getRegistryEntry() call
    if (re != null) {
        setExpiryTime(entry, re);
        entry.setVersion(re.getVersion());
    }
    return entry.getValue();
}
Also used : OMNode(org.apache.axiom.om.OMNode) Endpoint(org.apache.synapse.endpoints.Endpoint) TemplateMediator(org.apache.synapse.mediators.template.TemplateMediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) OMException(org.apache.axiom.om.OMException) XMLToObjectMapper(org.apache.synapse.config.XMLToObjectMapper)

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