Search in sources :

Example 6 with Dispatcher

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

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

the class ServiceDynamicLoadbalanceEndpointFactory method createEndpoint.

protected Endpoint createEndpoint(OMElement epConfig, boolean anonymousEndpoint, Properties properties) {
    OMElement loadbalanceElement = epConfig.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, "serviceDynamicLoadbalance"));
    if (loadbalanceElement == null) {
        return null;
    }
    String configuration = loadbalanceElement.getAttributeValue(new QName(XMLConfigConstants.NULL_NAMESPACE, "configuration"));
    OMElement servicesEle;
    if (configuration != null) {
        if (configuration.startsWith("$system:")) {
            configuration = System.getProperty(configuration.substring("$system:".length()));
        }
        // Load the file
        StAXOMBuilder builder = null;
        try {
            builder = new StAXOMBuilder(new URL(configuration).openStream());
        } catch (Exception e) {
            handleException("Could not load ServiceDynamicLoadbalanceEndpoint configuration file " + configuration);
        }
        servicesEle = builder.getDocumentElement().getFirstChildWithName(SERVICES_QNAME);
    } else {
        OMElement lbConfigEle = loadbalanceElement.getFirstChildWithName(LB_CONFIG_QNAME);
        if (lbConfigEle == null) {
            throw new RuntimeException("loadBalancerConfig element not found as a child of " + "serviceDynamicLoadbalance element");
        }
        servicesEle = lbConfigEle.getFirstChildWithName(SERVICES_QNAME);
    }
    if (servicesEle == null) {
        throw new RuntimeException("services element not found in serviceDynamicLoadbalance configuration");
    }
    Map<String, String> hostDomainMap = new HashMap<String, String>();
    for (Iterator<OMElement> iter = servicesEle.getChildrenWithLocalName("service"); iter.hasNext(); ) {
        OMElement serviceEle = iter.next();
        OMElement hostsEle = serviceEle.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, "hosts"));
        if (hostsEle == null) {
            throw new RuntimeException("hosts element not found as a child of service element");
        }
        List<String> hosts = new ArrayList<String>();
        for (Iterator<OMElement> hostIter = hostsEle.getChildrenWithLocalName("host"); hostIter.hasNext(); ) {
            OMElement hostEle = hostIter.next();
            String host = hostEle.getText();
            if (host.trim().length() == 0) {
                throw new RuntimeException("host cannot be null");
            }
            hosts.add(host);
        }
        OMElement domainEle = serviceEle.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, "domain"));
        if (domainEle == null) {
            throw new RuntimeException("domain element not found in as a child of services");
        }
        String domain = domainEle.getText();
        if (domain.trim().length() == 0) {
            throw new RuntimeException("domain cannot be null");
        }
        for (String host : hosts) {
            if (hostDomainMap.containsKey(host)) {
                throw new RuntimeException("host " + host + " has been already defined for " + "clustering domain " + hostDomainMap.get(host));
            }
            hostDomainMap.put(host, domain);
        }
    }
    if (hostDomainMap.isEmpty()) {
        throw new RuntimeException("No service elements defined under services");
    }
    LoadbalanceAlgorithm algorithm = LoadbalanceAlgorithmFactory.createLoadbalanceAlgorithm(loadbalanceElement, null);
    ServiceDynamicLoadbalanceEndpoint loadbalanceEndpoint = new ServiceDynamicLoadbalanceEndpoint(hostDomainMap, algorithm);
    // set endpoint name
    OMAttribute name = epConfig.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "name"));
    if (name != null) {
        loadbalanceEndpoint.setName(name.getAttributeValue());
    }
    // get the session for this endpoint
    OMElement sessionElement = epConfig.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, "session"));
    if (sessionElement != null) {
        OMElement sessionTimeout = sessionElement.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, "sessionTimeout"));
        if (sessionTimeout != null) {
            try {
                loadbalanceEndpoint.setSessionTimeout(Long.parseLong(sessionTimeout.getText().trim()));
            } catch (NumberFormatException nfe) {
                handleException("Invalid session timeout value : " + sessionTimeout.getText());
            }
        }
        String type = sessionElement.getAttributeValue(new QName("type"));
        if (type.equalsIgnoreCase("soap")) {
            Dispatcher soapDispatcher = new SoapSessionDispatcher();
            loadbalanceEndpoint.setDispatcher(soapDispatcher);
        } else if (type.equalsIgnoreCase("http")) {
            Dispatcher httpDispatcher = new HttpSessionDispatcher();
            loadbalanceEndpoint.setDispatcher(httpDispatcher);
        }
        loadbalanceEndpoint.setSessionAffinity(true);
    }
    loadbalanceEndpoint.setFailover(false);
    return loadbalanceEndpoint;
}
Also used : QName(javax.xml.namespace.QName) OMElement(org.apache.axiom.om.OMElement) SoapSessionDispatcher(org.apache.synapse.endpoints.dispatch.SoapSessionDispatcher) Dispatcher(org.apache.synapse.endpoints.dispatch.Dispatcher) HttpSessionDispatcher(org.apache.synapse.endpoints.dispatch.HttpSessionDispatcher) URL(java.net.URL) ServiceDynamicLoadbalanceEndpoint(org.apache.synapse.endpoints.ServiceDynamicLoadbalanceEndpoint) LoadbalanceAlgorithm(org.apache.synapse.endpoints.algorithms.LoadbalanceAlgorithm) StAXOMBuilder(org.apache.axiom.om.impl.builder.StAXOMBuilder) OMAttribute(org.apache.axiom.om.OMAttribute) HttpSessionDispatcher(org.apache.synapse.endpoints.dispatch.HttpSessionDispatcher) SoapSessionDispatcher(org.apache.synapse.endpoints.dispatch.SoapSessionDispatcher)

Example 8 with Dispatcher

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

the class Axis2SynapseEnvironment method send.

/**
 * This will be used for sending the message provided, to the endpoint specified by the
 * EndpointDefinition using the axis2 environment.
 *
 * @param endpoint - EndpointDefinition to be used to find the endpoint information
 *                      and the properties of the sending process
 * @param synCtx   - Synapse MessageContext to be sent
 */
public void send(EndpointDefinition endpoint, MessageContext synCtx) {
    // removing rampart engaged property, else outgoing security will not work
    ((Axis2MessageContext) synCtx).getAxis2MessageContext().removeProperty("rampart_engaged");
    if (synCtx.isResponse()) {
        if (endpoint != null) {
            if (isTransportSwitching(synCtx, endpoint)) {
                buildMessage(synCtx);
            }
            Axis2Sender.sendOn(endpoint, synCtx);
        } else {
            String proxyName = (String) synCtx.getProperty(SynapseConstants.PROXY_SERVICE);
            boolean serviceModuleEngaged = false;
            if (proxyName != null) {
                ProxyService proxyService = synapseConfig.getProxyService(proxyName);
                serviceModuleEngaged = proxyService.isModuleEngaged();
            }
            if (serviceModuleEngaged || isTransportSwitching(synCtx, null)) {
                buildMessage(synCtx);
            }
            // Build message in the case of inbound jms dual channel
            Boolean isInboundJMS = (Boolean) synCtx.getProperty(SynapseConstants.INBOUND_JMS_PROTOCOL);
            if (isInboundJMS != null && isInboundJMS) {
                buildMessage(synCtx);
            }
            Axis2Sender.sendBack(synCtx);
        }
    } else {
        // If this request is related to session affinity endpoints - For client initiated session
        Dispatcher dispatcher = (Dispatcher) synCtx.getProperty(SynapseConstants.PROP_SAL_ENDPOINT_CURRENT_DISPATCHER);
        if (dispatcher != null) {
            if (!dispatcher.isServerInitiatedSession()) {
                dispatcher.updateSession(synCtx);
            }
        }
        synCtx.setProperty(SynapseConstants.SENDING_REQUEST, true);
        if (endpoint == null || isTransportSwitching(synCtx, endpoint)) {
            buildMessage(synCtx);
        }
        Axis2Sender.sendOn(endpoint, synCtx);
    }
}
Also used : Dispatcher(org.apache.synapse.endpoints.dispatch.Dispatcher)

Aggregations

Dispatcher (org.apache.synapse.endpoints.dispatch.Dispatcher)8 OMElement (org.apache.axiom.om.OMElement)6 HttpSessionDispatcher (org.apache.synapse.endpoints.dispatch.HttpSessionDispatcher)6 SoapSessionDispatcher (org.apache.synapse.endpoints.dispatch.SoapSessionDispatcher)6 SimpleClientSessionDispatcher (org.apache.synapse.endpoints.dispatch.SimpleClientSessionDispatcher)4 QName (javax.xml.namespace.QName)3 OMAttribute (org.apache.axiom.om.OMAttribute)3 Endpoint (org.apache.synapse.endpoints.Endpoint)3 LoadbalanceAlgorithm (org.apache.synapse.endpoints.algorithms.LoadbalanceAlgorithm)3 Properties (java.util.Properties)2 LoadBalanceMembershipHandler (org.apache.synapse.core.LoadBalanceMembershipHandler)2 DynamicLoadbalanceEndpoint (org.apache.synapse.endpoints.DynamicLoadbalanceEndpoint)2 SALoadbalanceEndpoint (org.apache.synapse.endpoints.SALoadbalanceEndpoint)2 ServiceDynamicLoadbalanceEndpoint (org.apache.synapse.endpoints.ServiceDynamicLoadbalanceEndpoint)2 URL (java.net.URL)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 Stack (java.util.Stack)1 StAXOMBuilder (org.apache.axiom.om.impl.builder.StAXOMBuilder)1 EndpointReference (org.apache.axis2.addressing.EndpointReference)1