Search in sources :

Example 1 with VersionStrategy

use of org.apache.synapse.rest.version.VersionStrategy in project wso2-synapse by wso2.

the class APIFactory method createAPI.

public static API createAPI(OMElement apiElt, Properties properties) {
    OMAttribute nameAtt = apiElt.getAttribute(new QName("name"));
    if (nameAtt == null || "".equals(nameAtt.getAttributeValue())) {
        handleException("Attribute 'name' is required for an API definition");
    }
    OMAttribute contextAtt = apiElt.getAttribute(new QName("context"));
    if (contextAtt == null || "".equals(contextAtt.getAttributeValue())) {
        handleException("Attribute 'context' is required for an API definition");
    }
    API api = new API(nameAtt.getAttributeValue(), contextAtt.getAttributeValue());
    OMAttribute hostAtt = apiElt.getAttribute(new QName("hostname"));
    if (hostAtt != null && !"".equals(hostAtt.getAttributeValue())) {
        api.setHost(hostAtt.getAttributeValue());
    }
    VersionStrategy vStrategy = VersionStrategyFactory.createVersioningStrategy(api, apiElt);
    api.setVersionStrategy(vStrategy);
    OMAttribute portAtt = apiElt.getAttribute(new QName("port"));
    if (portAtt != null && !"".equals(portAtt.getAttributeValue())) {
        api.setPort(Integer.parseInt(portAtt.getAttributeValue()));
    }
    Iterator resources = apiElt.getChildrenWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "resource"));
    boolean noResources = true;
    while (resources.hasNext()) {
        OMElement resourceElt = (OMElement) resources.next();
        api.addResource(ResourceFactory.createResource(resourceElt, properties));
        noResources = false;
    }
    if (noResources) {
        handleException("An API must contain at least one resource definition");
    }
    OMElement handlersElt = apiElt.getFirstChildWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "handlers"));
    if (handlersElt != null) {
        Iterator handlers = handlersElt.getChildrenWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "handler"));
        while (handlers.hasNext()) {
            OMElement handlerElt = (OMElement) handlers.next();
            defineHandler(api, handlerElt);
        }
    }
    OMAttribute trans = apiElt.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "transports"));
    if (trans != null) {
        String transports = trans.getAttributeValue();
        if (!"".equals(transports)) {
            if (Constants.TRANSPORT_HTTP.equals(transports)) {
                api.setProtocol(RESTConstants.PROTOCOL_HTTP_ONLY);
            } else if (Constants.TRANSPORT_HTTPS.equals(transports)) {
                api.setProtocol(RESTConstants.PROTOCOL_HTTPS_ONLY);
            } else {
                handleException("Invalid protocol name: " + transports);
            }
        }
    }
    String nameString = api.getName();
    if (nameString == null || "".equals(nameString)) {
        nameString = SynapseConstants.ANONYMOUS_API;
    }
    AspectConfiguration aspectConfiguration = new AspectConfiguration(nameString);
    api.configure(aspectConfiguration);
    OMAttribute statistics = apiElt.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 = apiElt.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();
            }
        }
    }
    return api;
}
Also used : QName(javax.xml.namespace.QName) VersionStrategy(org.apache.synapse.rest.version.VersionStrategy) Iterator(java.util.Iterator) API(org.apache.synapse.rest.API) OMElement(org.apache.axiom.om.OMElement) AspectConfiguration(org.apache.synapse.aspects.AspectConfiguration) OMAttribute(org.apache.axiom.om.OMAttribute)

Aggregations

Iterator (java.util.Iterator)1 QName (javax.xml.namespace.QName)1 OMAttribute (org.apache.axiom.om.OMAttribute)1 OMElement (org.apache.axiom.om.OMElement)1 AspectConfiguration (org.apache.synapse.aspects.AspectConfiguration)1 API (org.apache.synapse.rest.API)1 VersionStrategy (org.apache.synapse.rest.version.VersionStrategy)1