Search in sources :

Example 41 with Endpoint

use of org.apache.synapse.endpoints.Endpoint in project wso2-synapse by wso2.

the class ForwardingService method dispatch.

/**
 * Sends the message to a given endpoint.
 *
 * @param messageContext synapse {@link MessageContext} to be sent
 */
public void dispatch(MessageContext messageContext) {
    if (log.isDebugEnabled()) {
        log.debug("Sending the message to client with message processor [" + messageProcessor.getName() + "]");
    }
    // The below code is just for keeping the backward compatibility with the old code.
    if (targetEndpoint == null) {
        targetEndpoint = (String) messageContext.getProperty(ForwardingProcessorConstants.TARGET_ENDPOINT);
    }
    if (targetEndpoint != null) {
        Endpoint endpoint = messageContext.getEndpoint(targetEndpoint);
        if (endpoint == null) {
            log.error("Endpoint does not exists. Deactivating the message processor");
            deactivateMessageProcessor(messageContext);
            return;
        }
        AbstractEndpoint abstractEndpoint = (AbstractEndpoint) endpoint;
        EndpointDefinition endpointDefinition = abstractEndpoint.getDefinition();
        String endpointReferenceValue;
        if (endpointDefinition.getAddress() != null) {
            endpointReferenceValue = endpointDefinition.getAddress();
            // we only validate response for certain protocols (i.e HTTP/HTTPS)
            isResponseValidationNotRequired = !isResponseValidationRequiredEndpoint(endpointReferenceValue);
        }
        try {
            // Send message to the client
            while (!isSuccessful && !isTerminated) {
                tryToDispatchToEndpoint(messageContext, endpoint);
                isTerminated = messageProcessor.isDeactivated();
                if (!isSuccessful) {
                    prepareToRetry(messageContext);
                }
            }
        } catch (Exception e) {
            log.error("Message processor [" + messageProcessor.getName() + "] failed to send the message to" + " client", e);
        }
    } else {
        /*
			 * No Target Endpoint defined for the Message So we do not have a
			 * place to deliver.
			 * Here we log a warning and remove the message todo: we can improve
			 * this by implementing a target inferring
			 * mechanism.
			 */
        log.error("Neither targetEndpoint defined in the MessageProcessor configuration nor " + "Property " + ForwardingProcessorConstants.TARGET_ENDPOINT + " is found in the message context, hence deactivating the MessageProcessor");
        deactivateMessageProcessor(messageContext);
    }
}
Also used : AbstractEndpoint(org.apache.synapse.endpoints.AbstractEndpoint) Endpoint(org.apache.synapse.endpoints.Endpoint) AbstractEndpoint(org.apache.synapse.endpoints.AbstractEndpoint) EndpointDefinition(org.apache.synapse.endpoints.EndpointDefinition) StoreForwardException(org.apache.synapse.message.StoreForwardException) SynapseException(org.apache.synapse.SynapseException) OMException(org.apache.axiom.om.OMException)

Example 42 with Endpoint

use of org.apache.synapse.endpoints.Endpoint 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 43 with Endpoint

use of org.apache.synapse.endpoints.Endpoint 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)

Example 44 with Endpoint

use of org.apache.synapse.endpoints.Endpoint in project wso2-synapse by wso2.

the class SALoadbalanceEndpointSerializer method serializeEndpoint.

protected OMElement serializeEndpoint(Endpoint endpoint) {
    if (!(endpoint instanceof SALoadbalanceEndpoint)) {
        handleException("Invalid endpoint type for serializing. " + "Expected: SALoadbalanceEndpoint Found: " + endpoint.getClass().getName());
    }
    SALoadbalanceEndpoint loadbalanceEndpoint = (SALoadbalanceEndpoint) endpoint;
    fac = OMAbstractFactory.getOMFactory();
    OMElement endpointElement = fac.createOMElement("endpoint", SynapseConstants.SYNAPSE_OMNAMESPACE);
    // serialize the parameters
    serializeProperties(loadbalanceEndpoint, endpointElement);
    serializeCommonAttributes(endpoint, endpointElement);
    OMElement loadbalanceElement = fac.createOMElement("loadbalance", SynapseConstants.SYNAPSE_OMNAMESPACE);
    endpointElement.addChild(loadbalanceElement);
    Dispatcher dispatcher = loadbalanceEndpoint.getDispatcher();
    if (dispatcher != null) {
        OMElement sessionElement = fac.createOMElement("session", SynapseConstants.SYNAPSE_OMNAMESPACE);
        if (dispatcher instanceof SoapSessionDispatcher) {
            sessionElement.addAttribute("type", "soap", null);
        } else if (dispatcher instanceof HttpSessionDispatcher) {
            sessionElement.addAttribute("type", "http", null);
        } else if (dispatcher instanceof SimpleClientSessionDispatcher) {
            sessionElement.addAttribute("type", "simpleClientSession", null);
        } else {
            handleException("invalid session dispatcher : " + dispatcher.getClass().getName());
        }
        long sessionTimeout = loadbalanceEndpoint.getSessionTimeout();
        if (sessionTimeout != -1) {
            OMElement sessionTimeoutElement = fac.createOMElement("sessionTimeout", SynapseConstants.SYNAPSE_OMNAMESPACE);
            sessionTimeoutElement.setText(String.valueOf(sessionTimeout));
            sessionElement.addChild(sessionTimeoutElement);
        }
        endpointElement.addChild(sessionElement);
    }
    loadbalanceElement.addAttribute(XMLConfigConstants.LOADBALANCE_ALGORITHM, loadbalanceEndpoint.getAlgorithm().getClass().getName(), null);
    if (loadbalanceEndpoint.isBuildMessageAtt()) {
        loadbalanceElement.addAttribute(XMLConfigConstants.BUILD_MESSAGE, Boolean.toString(loadbalanceEndpoint.isBuildMessageAtt()), null);
    }
    for (Endpoint childEndpoint : loadbalanceEndpoint.getChildren()) {
        loadbalanceElement.addChild(EndpointSerializer.getElementFromEndpoint(childEndpoint));
    }
    return endpointElement;
}
Also used : Endpoint(org.apache.synapse.endpoints.Endpoint) SALoadbalanceEndpoint(org.apache.synapse.endpoints.SALoadbalanceEndpoint) SALoadbalanceEndpoint(org.apache.synapse.endpoints.SALoadbalanceEndpoint) OMElement(org.apache.axiom.om.OMElement) HttpSessionDispatcher(org.apache.synapse.endpoints.dispatch.HttpSessionDispatcher) SoapSessionDispatcher(org.apache.synapse.endpoints.dispatch.SoapSessionDispatcher) Dispatcher(org.apache.synapse.endpoints.dispatch.Dispatcher) SimpleClientSessionDispatcher(org.apache.synapse.endpoints.dispatch.SimpleClientSessionDispatcher) SimpleClientSessionDispatcher(org.apache.synapse.endpoints.dispatch.SimpleClientSessionDispatcher) HttpSessionDispatcher(org.apache.synapse.endpoints.dispatch.HttpSessionDispatcher) SoapSessionDispatcher(org.apache.synapse.endpoints.dispatch.SoapSessionDispatcher)

Example 45 with Endpoint

use of org.apache.synapse.endpoints.Endpoint in project wso2-synapse by wso2.

the class SendMediatorSerializer method serializeSpecificMediator.

public OMElement serializeSpecificMediator(Mediator m) {
    if (!(m instanceof SendMediator)) {
        handleException("Unsupported mediator passed in for serialization : " + m.getType());
    }
    SendMediator mediator = (SendMediator) m;
    OMElement send = fac.createOMElement("send", synNS);
    saveTracingState(send, mediator);
    Endpoint activeEndpoint = mediator.getEndpoint();
    if (activeEndpoint != null) {
        send.addChild(EndpointSerializer.getElementFromEndpoint(activeEndpoint));
    }
    Value receive = mediator.getReceivingSequence();
    if (receive != null) {
        ValueSerializer serializer = new ValueSerializer();
        serializer.serializeValue(receive, XMLConfigConstants.RECEIVE, send);
    }
    if (mediator.isBuildMessage()) {
        send.addAttribute(fac.createOMAttribute("buildmessage", nullNS, "true"));
    }
    return send;
}
Also used : Endpoint(org.apache.synapse.endpoints.Endpoint) Value(org.apache.synapse.mediators.Value) OMElement(org.apache.axiom.om.OMElement) SendMediator(org.apache.synapse.mediators.builtin.SendMediator)

Aggregations

Endpoint (org.apache.synapse.endpoints.Endpoint)64 OMElement (org.apache.axiom.om.OMElement)22 SynapseException (org.apache.synapse.SynapseException)13 AddressEndpoint (org.apache.synapse.endpoints.AddressEndpoint)13 SequenceMediator (org.apache.synapse.mediators.base.SequenceMediator)11 ArrayList (java.util.ArrayList)10 MessageContext (org.apache.synapse.MessageContext)9 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)8 SynapseEnvironment (org.apache.synapse.core.SynapseEnvironment)8 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)8 IndirectEndpoint (org.apache.synapse.endpoints.IndirectEndpoint)8 SALoadbalanceEndpoint (org.apache.synapse.endpoints.SALoadbalanceEndpoint)8 InboundEndpoint (org.apache.synapse.inbound.InboundEndpoint)8 OMAttribute (org.apache.axiom.om.OMAttribute)7 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)7 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)7 Axis2SynapseEnvironment (org.apache.synapse.core.axis2.Axis2SynapseEnvironment)7 QName (javax.xml.namespace.QName)6 AbstractEndpoint (org.apache.synapse.endpoints.AbstractEndpoint)6 DynamicLoadbalanceEndpoint (org.apache.synapse.endpoints.DynamicLoadbalanceEndpoint)6