Search in sources :

Example 1 with RestConsumerFactory

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

the class RestEndpoint method createConsumer.

@Override
public Consumer createConsumer(Processor processor) throws Exception {
    RestConsumerFactory factory = null;
    String cname = null;
    if (getComponentName() != null) {
        Object comp = getCamelContext().getRegistry().lookupByName(getComponentName());
        if (comp != null && comp instanceof RestConsumerFactory) {
            factory = (RestConsumerFactory) comp;
        } else {
            comp = getCamelContext().getComponent(getComponentName());
            if (comp != null && comp instanceof RestConsumerFactory) {
                factory = (RestConsumerFactory) comp;
            }
        }
        if (factory == null) {
            if (comp != null) {
                throw new IllegalArgumentException("Component " + getComponentName() + " is not a RestConsumerFactory");
            } else {
                throw new NoSuchBeanException(getComponentName(), RestConsumerFactory.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 RestConsumerFactory) {
                factory = (RestConsumerFactory) comp;
                cname = name;
                break;
            }
        }
    }
    // lookup in registry
    if (factory == null) {
        Set<RestConsumerFactory> factories = getCamelContext().getRegistry().findByType(RestConsumerFactory.class);
        if (factories != null && factories.size() == 1) {
            factory = factories.iterator().next();
        }
    }
    // and there must only be exactly one so we safely can pick this one
    if (factory == null) {
        RestConsumerFactory found = null;
        String foundName = null;
        for (String name : DEFAULT_REST_CONSUMER_COMPONENTS) {
            Object comp = getCamelContext().getComponent(name, true);
            if (comp != null && comp instanceof RestConsumerFactory) {
                if (found == null) {
                    found = (RestConsumerFactory) comp;
                    foundName = name;
                } else {
                    throw new IllegalArgumentException("Multiple RestConsumerFactory found on classpath. Configure explicit which component to use");
                }
            }
        }
        if (found != null) {
            LOG.debug("Auto discovered {} as RestConsumerFactory", foundName);
            factory = found;
        }
    }
    if (factory != null) {
        // if no explicit port/host configured, then use port from rest configuration
        String scheme = "http";
        String host = "";
        int port = 80;
        RestConfiguration config = getCamelContext().getRestConfiguration(cname, true);
        if (config.getScheme() != null) {
            scheme = config.getScheme();
        }
        if (config.getHost() != null) {
            host = config.getHost();
        }
        int num = config.getPort();
        if (num > 0) {
            port = num;
        }
        // if no explicit hostname set then resolve the hostname
        if (ObjectHelper.isEmpty(host)) {
            if (config.getRestHostNameResolver() == RestConfiguration.RestHostNameResolver.allLocalIp) {
                host = "0.0.0.0";
            } else if (config.getRestHostNameResolver() == RestConfiguration.RestHostNameResolver.localHostName) {
                host = HostUtils.getLocalHostName();
            } else if (config.getRestHostNameResolver() == RestConfiguration.RestHostNameResolver.localIp) {
                host = HostUtils.getLocalIp();
            }
        }
        // calculate the url to the rest service
        String path = getPath();
        if (!path.startsWith("/")) {
            path = "/" + path;
        }
        // there may be an optional context path configured to help Camel calculate the correct urls for the REST services
        // this may be needed when using camel-servlet where we cannot get the actual context-path or port number of the servlet engine
        // during init of the servlet
        String contextPath = config.getContextPath();
        if (contextPath != null) {
            if (!contextPath.startsWith("/")) {
                path = "/" + contextPath + path;
            } else {
                path = contextPath + path;
            }
        }
        String baseUrl = scheme + "://" + host + (port != 80 ? ":" + port : "") + path;
        String url = baseUrl;
        if (uriTemplate != null) {
            // make sure to avoid double slashes
            if (uriTemplate.startsWith("/")) {
                url = url + uriTemplate;
            } else {
                url = url + "/" + uriTemplate;
            }
        }
        Consumer consumer = factory.createConsumer(getCamelContext(), processor, getMethod(), getPath(), getUriTemplate(), getConsumes(), getProduces(), config, getParameters());
        configureConsumer(consumer);
        // add to rest registry so we can keep track of them, we will remove from the registry when the consumer is removed
        // the rest registry will automatic keep track when the consumer is removed,
        // and un-register the REST service from the registry
        getCamelContext().getRestRegistry().addRestService(consumer, url, baseUrl, getPath(), getUriTemplate(), getMethod(), getConsumes(), getProduces(), getInType(), getOutType(), getRouteId(), getDescription());
        return consumer;
    } else {
        throw new IllegalStateException("Cannot find RestConsumerFactory in Registry or as a Component to use");
    }
}
Also used : Consumer(org.apache.camel.Consumer) NoSuchBeanException(org.apache.camel.NoSuchBeanException) RestConfiguration(org.apache.camel.spi.RestConfiguration) Component(org.apache.camel.Component) RestConsumerFactory(org.apache.camel.spi.RestConsumerFactory) DefaultEndpoint(org.apache.camel.impl.DefaultEndpoint) UriEndpoint(org.apache.camel.spi.UriEndpoint)

Aggregations

Component (org.apache.camel.Component)1 Consumer (org.apache.camel.Consumer)1 NoSuchBeanException (org.apache.camel.NoSuchBeanException)1 DefaultEndpoint (org.apache.camel.impl.DefaultEndpoint)1 RestConfiguration (org.apache.camel.spi.RestConfiguration)1 RestConsumerFactory (org.apache.camel.spi.RestConsumerFactory)1 UriEndpoint (org.apache.camel.spi.UriEndpoint)1