Search in sources :

Example 1 with Handler

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

the class APIFactory method defineHandler.

private static void defineHandler(API api, OMElement handlerElt) {
    String handlerClass = handlerElt.getAttributeValue(new QName("class"));
    if (handlerClass == null || "".equals(handlerClass)) {
        handleException("A handler element must have a class attribute");
    }
    try {
        Class clazz = APIFactory.class.getClassLoader().loadClass(handlerClass);
        Handler handler = (Handler) clazz.newInstance();
        api.addHandler(handler);
        for (Iterator it = handlerElt.getChildrenWithName(PROP_Q); it.hasNext(); ) {
            OMElement child = (OMElement) it.next();
            String propName = child.getAttribute(ATT_NAME).getAttributeValue();
            if (propName == null) {
                handleException("A Class mediator property must specify the name attribute");
            } else {
                if (child.getAttribute(ATT_VALUE) != null) {
                    String value = child.getAttribute(ATT_VALUE).getAttributeValue();
                    handler.addProperty(propName, value);
                    PropertyHelper.setInstanceProperty(propName, value, handler);
                } else {
                    OMNode omElt = child.getFirstElement();
                    if (omElt != null) {
                        handler.addProperty(propName, omElt);
                        PropertyHelper.setInstanceProperty(propName, omElt, handler);
                    } else {
                        handleException("A Class mediator property must specify " + "name and value attributes, or a name and a child XML fragment");
                    }
                }
            }
        }
    } catch (Exception e) {
        handleException("Error initializing API handler: " + handlerClass, e);
    }
}
Also used : OMNode(org.apache.axiom.om.OMNode) QName(javax.xml.namespace.QName) Iterator(java.util.Iterator) Handler(org.apache.synapse.rest.Handler) OMElement(org.apache.axiom.om.OMElement) SynapseException(org.apache.synapse.SynapseException)

Example 2 with Handler

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

the class APISerializer method serializeAPI.

public static OMElement serializeAPI(API api) {
    OMElement apiElt = fac.createOMElement("api", SynapseConstants.SYNAPSE_OMNAMESPACE);
    apiElt.addAttribute("name", api.getAPIName(), null);
    apiElt.addAttribute("context", api.getContext(), null);
    VersionStrategySerializer.serializeVersioningStrategy(api.getVersionStrategy(), apiElt);
    if (api.getHost() != null) {
        apiElt.addAttribute("hostname", api.getHost(), null);
    }
    if (api.getPort() != -1) {
        apiElt.addAttribute("port", String.valueOf(api.getPort()), null);
    }
    StatisticsConfigurable statisticsConfigurable = api.getAspectConfiguration();
    if (statisticsConfigurable != null && statisticsConfigurable.isStatisticsEnable()) {
        apiElt.addAttribute(XMLConfigConstants.STATISTICS_ATTRIB_NAME, XMLConfigConstants.STATISTICS_ENABLE, null);
    }
    if (statisticsConfigurable != null && statisticsConfigurable.isTracingEnabled()) {
        apiElt.addAttribute(XMLConfigConstants.TRACE_ATTRIB_NAME, XMLConfigConstants.TRACE_ENABLE, null);
    }
    Resource[] resources = api.getResources();
    for (Resource r : resources) {
        OMElement resourceElt = ResourceSerializer.serializeResource(r);
        apiElt.addChild(resourceElt);
    }
    Handler[] handlers = api.getHandlers();
    if (handlers.length > 0) {
        OMElement handlersElt = fac.createOMElement("handlers", SynapseConstants.SYNAPSE_OMNAMESPACE);
        for (Handler handler : handlers) {
            OMElement handlerElt = fac.createOMElement("handler", SynapseConstants.SYNAPSE_OMNAMESPACE);
            handlerElt.addAttribute("class", handler.getClass().getName(), null);
            handlersElt.addChild(handlerElt);
            if (handler.getProperties() != null) {
                Map propertyMap = handler.getProperties();
                if (propertyMap != null) {
                    Iterator itr = propertyMap.keySet().iterator();
                    while (itr.hasNext()) {
                        String propName = (String) itr.next();
                        Object o = handler.getProperties().get(propName);
                        OMElement prop = fac.createOMElement(APIFactory.PROP_Q, handlerElt);
                        prop.addAttribute(fac.createOMAttribute(APIFactory.ATT_NAME.getLocalPart(), nullNS, propName));
                        if (o instanceof String) {
                            prop.addAttribute(fac.createOMAttribute(APIFactory.ATT_VALUE.getLocalPart(), nullNS, (String) o));
                        } else {
                            prop.addChild((OMNode) o);
                        }
                        handlerElt.addChild(prop);
                    }
                }
            }
        }
        apiElt.addChild(handlersElt);
    }
    if (api.getProtocol() == RESTConstants.PROTOCOL_HTTP_ONLY) {
        apiElt.addAttribute("transports", Constants.TRANSPORT_HTTP, null);
    } else if (api.getProtocol() == RESTConstants.PROTOCOL_HTTPS_ONLY) {
        apiElt.addAttribute("transports", Constants.TRANSPORT_HTTPS, null);
    }
    return apiElt;
}
Also used : Resource(org.apache.synapse.rest.Resource) Iterator(java.util.Iterator) Handler(org.apache.synapse.rest.Handler) StatisticsConfigurable(org.apache.synapse.aspects.statistics.StatisticsConfigurable) Map(java.util.Map)

Aggregations

Iterator (java.util.Iterator)2 Handler (org.apache.synapse.rest.Handler)2 Map (java.util.Map)1 QName (javax.xml.namespace.QName)1 OMElement (org.apache.axiom.om.OMElement)1 OMNode (org.apache.axiom.om.OMNode)1 SynapseException (org.apache.synapse.SynapseException)1 StatisticsConfigurable (org.apache.synapse.aspects.statistics.StatisticsConfigurable)1 Resource (org.apache.synapse.rest.Resource)1