Search in sources :

Example 1 with CalloutMediator

use of org.apache.synapse.mediators.builtin.CalloutMediator in project wso2-synapse by wso2.

the class CalloutMediatorSerializer method serializeSpecificMediator.

public OMElement serializeSpecificMediator(Mediator m) {
    if (!(m instanceof CalloutMediator)) {
        handleException("Unsupported mediator passed in for serialization : " + m.getType());
    }
    CalloutMediator mediator = (CalloutMediator) m;
    OMElement callout = fac.createOMElement("callout", synNS);
    saveTracingState(callout, mediator);
    if (mediator.getServiceURL() != null) {
        callout.addAttribute(fac.createOMAttribute("serviceURL", nullNS, mediator.getServiceURL()));
    } else if (mediator.getEndpointKey() != null) {
        callout.addAttribute(fac.createOMAttribute("endpointKey", nullNS, mediator.getEndpointKey()));
    }
    Endpoint endpoint = mediator.getEndpoint();
    if (endpoint != null) {
        callout.addChild(EndpointSerializer.getElementFromEndpoint(endpoint));
    }
    if (mediator.getAction() != null) {
        callout.addAttribute(fac.createOMAttribute("action", nullNS, mediator.getAction()));
    }
    if (mediator.getUseServerConfig() != null) {
        callout.addAttribute(fac.createOMAttribute("useServerConfig", nullNS, mediator.getUseServerConfig()));
    }
    if (!mediator.getInitClientOptions()) {
        callout.addAttribute(fac.createOMAttribute("initAxis2ClientOptions", nullNS, Boolean.toString(mediator.getInitClientOptions())));
    }
    if (mediator.getClientRepository() != null || mediator.getAxis2xml() != null) {
        OMElement config = fac.createOMElement("configuration", synNS);
        if (mediator.getClientRepository() != null) {
            config.addAttribute(fac.createOMAttribute("repository", nullNS, mediator.getClientRepository()));
        }
        if (mediator.getAxis2xml() != null) {
            config.addAttribute(fac.createOMAttribute("axis2xml", nullNS, mediator.getAxis2xml()));
        }
        callout.addChild(config);
    }
    if (mediator.isUseEnvelopeAsSource()) {
        OMElement source = fac.createOMElement("source", synNS, callout);
        source.addAttribute(fac.createOMAttribute("type", nullNS, "envelope"));
    } else if (mediator.getRequestXPath() != null) {
        OMElement source = fac.createOMElement("source", synNS, callout);
        SynapseXPathSerializer.serializeXPath(mediator.getRequestXPath(), source, "xpath");
    } else if (mediator.getRequestKey() != null) {
        OMElement source = fac.createOMElement("source", synNS, callout);
        source.addAttribute(fac.createOMAttribute("key", nullNS, mediator.getRequestKey()));
    }
    if (mediator.getTargetXPath() != null) {
        OMElement target = fac.createOMElement("target", synNS, callout);
        SynapseXPathSerializer.serializeXPath(mediator.getTargetXPath(), target, "xpath");
    } else if (mediator.getTargetKey() != null) {
        OMElement target = fac.createOMElement("target", synNS, callout);
        target.addAttribute(fac.createOMAttribute("key", nullNS, mediator.getTargetKey()));
    }
    if (mediator.isSecurityOn()) {
        OMElement security = fac.createOMElement("enableSec", synNS);
        if (mediator.getWsSecPolicyKey() != null) {
            security.addAttribute(fac.createOMAttribute("policy", nullNS, mediator.getWsSecPolicyKey()));
            callout.addChild(security);
        } else if (mediator.getOutboundWsSecPolicyKey() != null || mediator.getInboundWsSecPolicyKey() != null) {
            if (mediator.getOutboundWsSecPolicyKey() != null) {
                security.addAttribute(fac.createOMAttribute("outboundPolicy", nullNS, mediator.getOutboundWsSecPolicyKey()));
            }
            if (mediator.getInboundWsSecPolicyKey() != null) {
                security.addAttribute(fac.createOMAttribute("inboundPolicy", nullNS, mediator.getInboundWsSecPolicyKey()));
            }
            callout.addChild(security);
        }
    }
    return callout;
}
Also used : Endpoint(org.apache.synapse.endpoints.Endpoint) CalloutMediator(org.apache.synapse.mediators.builtin.CalloutMediator) OMElement(org.apache.axiom.om.OMElement)

Example 2 with CalloutMediator

use of org.apache.synapse.mediators.builtin.CalloutMediator in project wso2-synapse by wso2.

the class CalloutMediatorFactory method createSpecificMediator.

public Mediator createSpecificMediator(OMElement elem, Properties properties) {
    CalloutMediator callout = new CalloutMediator();
    processAuditStatus(callout, elem);
    OMAttribute attServiceURL = elem.getAttribute(ATT_URL);
    OMAttribute attEndpoint = elem.getAttribute(ATT_ENDPOINT);
    OMAttribute attAction = elem.getAttribute(ATT_ACTION);
    OMAttribute attUseServerConfig = elem.getAttribute(ATT_USESERVERCONFIG);
    OMAttribute initAxis2ClientOptions = elem.getAttribute(ATT_INIT_AXI2_CLIENT_OPTIONS);
    OMElement configElt = elem.getFirstChildWithName(Q_CONFIG);
    OMElement epElement = elem.getFirstChildWithName(Q_ENDPOINT);
    OMElement sourceElt = elem.getFirstChildWithName(Q_SOURCE);
    OMElement targetElt = elem.getFirstChildWithName(Q_TARGET);
    OMElement wsSec = elem.getFirstChildWithName(Q_SEC);
    if (attServiceURL != null) {
        callout.setServiceURL(attServiceURL.getAttributeValue());
    } else if (attEndpoint != null) {
        callout.setEndpointKey(attEndpoint.getAttributeValue());
    }
    if (epElement != null) {
        Endpoint endpoint = EndpointFactory.getEndpointFromElement(epElement, true, properties);
        if (endpoint != null) {
            callout.setEndpoint(endpoint);
        }
    }
    if (attAction != null) {
        callout.setAction(attAction.getAttributeValue());
    }
    if (attUseServerConfig != null) {
        callout.setUseServerConfig(attUseServerConfig.getAttributeValue());
    }
    if (initAxis2ClientOptions != null) {
        if ("true".equals(initAxis2ClientOptions.getAttributeValue().toLowerCase())) {
            callout.setInitClientOptions(true);
        } else if ("false".equals(initAxis2ClientOptions.getAttributeValue().toLowerCase())) {
            callout.setInitClientOptions(false);
        } else {
            handleException("The 'initAxis2ClientOptions' attribute only accepts a boolean value.");
        }
    }
    if (configElt != null) {
        OMAttribute axis2xmlAttr = configElt.getAttribute(ATT_AXIS2XML);
        OMAttribute repoAttr = configElt.getAttribute(ATT_REPOSITORY);
        if (axis2xmlAttr != null && axis2xmlAttr.getAttributeValue() != null) {
            File axis2xml = new File(axis2xmlAttr.getAttributeValue());
            if (axis2xml.exists() && axis2xml.isFile()) {
                callout.setAxis2xml(axis2xmlAttr.getAttributeValue());
            } else {
                handleException("Invalid axis2.xml path: " + axis2xmlAttr.getAttributeValue());
            }
        }
        if (repoAttr != null && repoAttr.getAttributeValue() != null) {
            File repo = new File(repoAttr.getAttributeValue());
            if (repo.exists() && repo.isDirectory()) {
                callout.setClientRepository(repoAttr.getAttributeValue());
            } else {
                handleException("Invalid repository path: " + repoAttr.getAttributeValue());
            }
        }
    }
    if (sourceElt != null) {
        OMAttribute sourceType = sourceElt.getAttribute(ATT_SOURCE_TYPE);
        if (sourceType != null && sourceType.getAttributeValue().equals("envelope")) {
            callout.setUseEnvelopeAsSource(true);
        } else if (sourceElt.getAttribute(ATT_XPATH) != null) {
            try {
                callout.setRequestXPath(SynapseXPathFactory.getSynapseXPath(sourceElt, ATT_XPATH));
            } catch (JaxenException e) {
                handleException("Invalid source XPath : " + sourceElt.getAttributeValue(ATT_XPATH));
            }
        } else if (sourceElt.getAttribute(ATT_KEY) != null) {
            callout.setRequestKey(sourceElt.getAttributeValue(ATT_KEY));
        } else {
            handleException("A 'xpath' or 'key' attribute " + "is required for the Callout 'source'");
        }
    } else {
        callout.setUseEnvelopeAsSource(true);
    }
    if (targetElt != null) {
        if (targetElt.getAttribute(ATT_XPATH) != null) {
            try {
                callout.setTargetXPath(SynapseXPathFactory.getSynapseXPath(targetElt, ATT_XPATH));
            } catch (JaxenException e) {
                handleException("Invalid target XPath : " + targetElt.getAttributeValue(ATT_XPATH));
            }
        } else if (targetElt.getAttribute(ATT_KEY) != null) {
            callout.setTargetKey(targetElt.getAttributeValue(ATT_KEY));
        } else {
            handleException("A 'xpath' or 'key' attribute " + "is required for the Callout 'target'");
        }
    }
    if (wsSec != null) {
        callout.setSecurityOn(true);
        OMAttribute policyKey = wsSec.getAttribute(ATT_POLICY);
        OMAttribute outboundPolicyKey = wsSec.getAttribute(ATT_OUTBOUND_SEC_POLICY);
        OMAttribute inboundPolicyKey = wsSec.getAttribute(ATT_INBOUND_SEC_POLICY);
        if (policyKey != null) {
            callout.setWsSecPolicyKey(policyKey.getAttributeValue());
        } else if (outboundPolicyKey != null || inboundPolicyKey != null) {
            if (outboundPolicyKey != null) {
                callout.setOutboundWsSecPolicyKey(outboundPolicyKey.getAttributeValue());
            }
            if (inboundPolicyKey != null) {
                callout.setInboundWsSecPolicyKey(inboundPolicyKey.getAttributeValue());
            }
        } else {
            callout.setSecurityOn(false);
            handleException("A policy key is required to enable security");
        }
    }
    return callout;
}
Also used : Endpoint(org.apache.synapse.endpoints.Endpoint) CalloutMediator(org.apache.synapse.mediators.builtin.CalloutMediator) JaxenException(org.jaxen.JaxenException) OMElement(org.apache.axiom.om.OMElement) OMAttribute(org.apache.axiom.om.OMAttribute) File(java.io.File)

Aggregations

OMElement (org.apache.axiom.om.OMElement)2 Endpoint (org.apache.synapse.endpoints.Endpoint)2 CalloutMediator (org.apache.synapse.mediators.builtin.CalloutMediator)2 File (java.io.File)1 OMAttribute (org.apache.axiom.om.OMAttribute)1 JaxenException (org.jaxen.JaxenException)1