Search in sources :

Example 1 with RestApiConsumerFactory

use of org.apache.camel.spi.RestApiConsumerFactory in project camel by apache.

the class RestApiEndpoint method createConsumer.

@Override
public Consumer createConsumer(Processor processor) throws Exception {
    RestApiConsumerFactory factory = null;
    String cname = null;
    // the API then uses the api component (eg usually camel-swagger-java) to build the API
    if (getComponentName() != null) {
        Object comp = getCamelContext().getRegistry().lookupByName(getComponentName());
        if (comp != null && comp instanceof RestApiConsumerFactory) {
            factory = (RestApiConsumerFactory) comp;
        } else {
            comp = getCamelContext().getComponent(getComponentName());
            if (comp != null && comp instanceof RestApiConsumerFactory) {
                factory = (RestApiConsumerFactory) comp;
            }
        }
        if (factory == null) {
            if (comp != null) {
                throw new IllegalArgumentException("Component " + getComponentName() + " is not a RestApiConsumerFactory");
            } else {
                throw new NoSuchBeanException(getComponentName(), RestApiConsumerFactory.class.getName());
            }
        }
        cname = getComponentName();
    }
    // try all components
    if (factory == null) {
        for (String name : getCamelContext().getComponentNames()) {
            Component comp = getCamelContext().getComponent(name);
            if (comp != null && comp instanceof RestApiConsumerFactory) {
                factory = (RestApiConsumerFactory) comp;
                cname = name;
                break;
            }
        }
    }
    // lookup in registry
    if (factory == null) {
        Set<RestApiConsumerFactory> factories = getCamelContext().getRegistry().findByType(RestApiConsumerFactory.class);
        if (factories != null && factories.size() == 1) {
            factory = factories.iterator().next();
        }
    }
    if (factory != null) {
        // calculate the url to the rest API service
        RestConfiguration config = getCamelContext().getRestConfiguration(cname, true);
        // calculate the url to the rest API service
        String path = getPath();
        if (path != null && !path.startsWith("/")) {
            path = "/" + path;
        }
        Consumer consumer = factory.createApiConsumer(getCamelContext(), processor, path, config, getParameters());
        configureConsumer(consumer);
        return consumer;
    } else {
        throw new IllegalStateException("Cannot find RestApiConsumerFactory in Registry or as a Component to use");
    }
}
Also used : Consumer(org.apache.camel.Consumer) RestApiConsumerFactory(org.apache.camel.spi.RestApiConsumerFactory) NoSuchBeanException(org.apache.camel.NoSuchBeanException) RestConfiguration(org.apache.camel.spi.RestConfiguration) Component(org.apache.camel.Component)

Aggregations

Component (org.apache.camel.Component)1 Consumer (org.apache.camel.Consumer)1 NoSuchBeanException (org.apache.camel.NoSuchBeanException)1 RestApiConsumerFactory (org.apache.camel.spi.RestApiConsumerFactory)1 RestConfiguration (org.apache.camel.spi.RestConfiguration)1