Search in sources :

Example 26 with RestConfiguration

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

the class NettyHttpComponent method doCreateConsumer.

Consumer doCreateConsumer(CamelContext camelContext, Processor processor, String verb, String basePath, String uriTemplate, String consumes, String produces, RestConfiguration configuration, Map<String, Object> parameters, boolean api) throws Exception {
    String path = basePath;
    if (uriTemplate != null) {
        // make sure to avoid double slashes
        if (uriTemplate.startsWith("/")) {
            path = path + uriTemplate;
        } else {
            path = path + "/" + uriTemplate;
        }
    }
    path = FileUtil.stripLeadingSeparator(path);
    String scheme = "http";
    String host = "";
    int port = 0;
    // if no explicit port/host configured, then use port from rest configuration
    RestConfiguration config = configuration;
    if (config == null) {
        config = camelContext.getRestConfiguration("netty-http", true);
    }
    if (config.getScheme() != null) {
        scheme = config.getScheme();
    }
    if (config.getHost() != null) {
        host = config.getHost();
    }
    int num = config.getPort();
    if (num > 0) {
        port = num;
    }
    // prefix path with context-path if configured in rest-dsl configuration
    String contextPath = config.getContextPath();
    if (ObjectHelper.isNotEmpty(contextPath)) {
        contextPath = FileUtil.stripTrailingSeparator(contextPath);
        contextPath = FileUtil.stripLeadingSeparator(contextPath);
        if (ObjectHelper.isNotEmpty(contextPath)) {
            path = contextPath + "/" + path;
        }
    }
    // 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();
        }
    }
    Map<String, Object> map = new HashMap<String, Object>();
    // build query string, and append any endpoint configuration properties
    if (config.getComponent() == null || config.getComponent().equals("netty-http")) {
        // setup endpoint options
        if (config.getEndpointProperties() != null && !config.getEndpointProperties().isEmpty()) {
            map.putAll(config.getEndpointProperties());
        }
    }
    boolean cors = config.isEnableCORS();
    if (cors) {
        // allow HTTP Options as we want to handle CORS in rest-dsl
        map.put("optionsEnabled", "true");
    }
    String query = URISupport.createQueryString(map);
    String url;
    if (api) {
        url = "netty-http:%s://%s:%s/%s?matchOnUriPrefix=true&httpMethodRestrict=%s";
    } else {
        url = "netty-http:%s://%s:%s/%s?httpMethodRestrict=%s";
    }
    // must use upper case for restrict
    String restrict = verb.toUpperCase(Locale.US);
    if (cors) {
        restrict += ",OPTIONS";
    }
    // get the endpoint
    url = String.format(url, scheme, host, port, path, restrict);
    if (!query.isEmpty()) {
        url = url + "&" + query;
    }
    NettyHttpEndpoint endpoint = camelContext.getEndpoint(url, NettyHttpEndpoint.class);
    setProperties(camelContext, endpoint, parameters);
    // configure consumer properties
    Consumer consumer = endpoint.createConsumer(processor);
    if (config.getConsumerProperties() != null && !config.getConsumerProperties().isEmpty()) {
        setProperties(camelContext, consumer, config.getConsumerProperties());
    }
    return consumer;
}
Also used : Consumer(org.apache.camel.Consumer) HashMap(java.util.HashMap) RestConfiguration(org.apache.camel.spi.RestConfiguration) Endpoint(org.apache.camel.Endpoint)

Example 27 with RestConfiguration

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

the class ServletComponent method doCreateConsumer.

Consumer doCreateConsumer(CamelContext camelContext, Processor processor, String verb, String basePath, String uriTemplate, String consumes, String produces, RestConfiguration configuration, Map<String, Object> parameters, boolean api) throws Exception {
    String path = basePath;
    if (uriTemplate != null) {
        // make sure to avoid double slashes
        if (uriTemplate.startsWith("/")) {
            path = path + uriTemplate;
        } else {
            path = path + "/" + uriTemplate;
        }
    }
    path = FileUtil.stripLeadingSeparator(path);
    // if no explicit port/host configured, then use port from rest configuration
    RestConfiguration config = configuration;
    if (config == null) {
        config = camelContext.getRestConfiguration("servlet", true);
    }
    Map<String, Object> map = new HashMap<String, Object>();
    // build query string, and append any endpoint configuration properties
    if (config.getComponent() == null || config.getComponent().equals("servlet")) {
        // setup endpoint options
        if (config.getEndpointProperties() != null && !config.getEndpointProperties().isEmpty()) {
            map.putAll(config.getEndpointProperties());
        }
    }
    boolean cors = config.isEnableCORS();
    if (cors) {
        // allow HTTP Options as we want to handle CORS in rest-dsl
        map.put("optionsEnabled", "true");
    }
    // do not append with context-path as the servlet path should be without context-path
    String query = URISupport.createQueryString(map);
    String url;
    if (api) {
        url = "servlet:///%s?matchOnUriPrefix=true&httpMethodRestrict=%s";
    } else {
        url = "servlet:///%s?httpMethodRestrict=%s";
    }
    // must use upper case for restrict
    String restrict = verb.toUpperCase(Locale.US);
    if (cors) {
        restrict += ",OPTIONS";
    }
    // get the endpoint
    url = String.format(url, path, restrict);
    if (!query.isEmpty()) {
        url = url + "&" + query;
    }
    ServletEndpoint endpoint = camelContext.getEndpoint(url, ServletEndpoint.class);
    setProperties(camelContext, endpoint, parameters);
    if (!map.containsKey("httpBindingRef")) {
        // use the rest binding, if not using a custom http binding
        HttpBinding binding = new ServletRestHttpBinding();
        binding.setHeaderFilterStrategy(endpoint.getHeaderFilterStrategy());
        binding.setTransferException(endpoint.isTransferException());
        binding.setEagerCheckContentAvailable(endpoint.isEagerCheckContentAvailable());
        endpoint.setHttpBinding(binding);
    }
    // configure consumer properties
    Consumer consumer = endpoint.createConsumer(processor);
    if (config.getConsumerProperties() != null && !config.getConsumerProperties().isEmpty()) {
        setProperties(camelContext, consumer, config.getConsumerProperties());
    }
    return consumer;
}
Also used : Consumer(org.apache.camel.Consumer) HttpConsumer(org.apache.camel.http.common.HttpConsumer) HashMap(java.util.HashMap) HttpBinding(org.apache.camel.http.common.HttpBinding) RestConfiguration(org.apache.camel.spi.RestConfiguration)

Example 28 with RestConfiguration

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

the class CoAPComponent method doStart.

@Override
protected void doStart() throws Exception {
    super.doStart();
    RestConfiguration config = getCamelContext().getRestConfiguration("coap", true);
    // configure additional options on coap configuration
    if (config.getComponentProperties() != null && !config.getComponentProperties().isEmpty()) {
        setProperties(this, config.getComponentProperties());
    }
    defaultServer = getServer(config.getPort());
    for (CoapServer s : servers.values()) {
        s.start();
    }
}
Also used : CoapServer(org.eclipse.californium.core.CoapServer) RestConfiguration(org.apache.camel.spi.RestConfiguration)

Example 29 with RestConfiguration

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

the class CoAPComponent method createConsumer.

@Override
public Consumer createConsumer(CamelContext camelContext, Processor processor, String verb, String basePath, String uriTemplate, String consumes, String produces, RestConfiguration configuration, Map<String, Object> parameters) throws Exception {
    RestConfiguration config = configuration;
    if (config == null) {
        config = getCamelContext().getRestConfiguration("coap", true);
    }
    String host = config.getHost();
    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();
        }
    }
    Map<String, Object> map = new HashMap<String, Object>();
    // setup endpoint options
    if (config.getEndpointProperties() != null && !config.getEndpointProperties().isEmpty()) {
        map.putAll(config.getEndpointProperties());
    }
    // allow HTTP Options as we want to handle CORS in rest-dsl
    boolean cors = config.isEnableCORS();
    String query = URISupport.createQueryString(map);
    String url = (config.getScheme() == null ? "coap" : config.getScheme()) + "://" + host;
    if (config.getPort() != -1) {
        url += ":" + config.getPort();
    }
    String restrict = verb.toUpperCase(Locale.US);
    if (cors) {
        restrict += ",OPTIONS";
    }
    if (uriTemplate == null) {
        uriTemplate = "";
    }
    url += basePath + uriTemplate + "?coapMethod=" + restrict;
    if (!query.isEmpty()) {
        url += "&" + query;
    }
    CoAPEndpoint endpoint = camelContext.getEndpoint(url, CoAPEndpoint.class);
    setProperties(endpoint, parameters);
    // configure consumer properties
    Consumer consumer = endpoint.createConsumer(processor);
    if (config.getConsumerProperties() != null && !config.getConsumerProperties().isEmpty()) {
        setProperties(consumer, config.getConsumerProperties());
    }
    return consumer;
}
Also used : Consumer(org.apache.camel.Consumer) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) RestConfiguration(org.apache.camel.spi.RestConfiguration)

Example 30 with RestConfiguration

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

the class RestApiEndpoint method createProducer.

@Override
public Producer createProducer() throws Exception {
    RestApiProcessorFactory factory = null;
    RestConfiguration config = getCamelContext().getRestConfiguration(componentName, true);
    // lookup in registry
    Set<RestApiProcessorFactory> factories = getCamelContext().getRegistry().findByType(RestApiProcessorFactory.class);
    if (factories != null && factories.size() == 1) {
        factory = factories.iterator().next();
    }
    // lookup on classpath using factory finder to automatic find it (just add camel-swagger-java to classpath etc)
    if (factory == null) {
        String name = apiComponentName != null ? apiComponentName : config.getApiComponent();
        if (name == null) {
            name = DEFAULT_API_COMPONENT_NAME;
        }
        try {
            FactoryFinder finder = getCamelContext().getFactoryFinder(RESOURCE_PATH);
            Object instance = finder.newInstance(name);
            if (instance instanceof RestApiProcessorFactory) {
                factory = (RestApiProcessorFactory) instance;
            }
        } catch (NoFactoryAvailableException e) {
        // ignore
        }
    }
    if (factory != null) {
        // if no explicit port/host configured, then use port from rest configuration
        String host = "";
        int port = 80;
        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();
            }
            // no host was configured so calculate a host to use
            // there should be no schema in the host (but only port)
            String targetHost = host + (port != 80 ? ":" + port : "");
            getParameters().put("host", targetHost);
        }
        // the base path should start with a leading slash
        String path = getPath();
        if (path != null && !path.startsWith("/")) {
            path = "/" + path;
        }
        // whether listing of the context id's is enabled or not
        boolean contextIdListing = config.isApiContextListing();
        Processor processor = factory.createApiProcessor(getCamelContext(), path, getContextIdPattern(), contextIdListing, config, getParameters());
        return new RestApiProducer(this, processor);
    } else {
        throw new IllegalStateException("Cannot find RestApiProcessorFactory in Registry or classpath (such as the camel-swagger-java component)");
    }
}
Also used : Processor(org.apache.camel.Processor) RestApiProcessorFactory(org.apache.camel.spi.RestApiProcessorFactory) NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException) RestConfiguration(org.apache.camel.spi.RestConfiguration) FactoryFinder(org.apache.camel.spi.FactoryFinder) DefaultEndpoint(org.apache.camel.impl.DefaultEndpoint) UriEndpoint(org.apache.camel.spi.UriEndpoint)

Aggregations

RestConfiguration (org.apache.camel.spi.RestConfiguration)36 HashMap (java.util.HashMap)10 Consumer (org.apache.camel.Consumer)10 Endpoint (org.apache.camel.Endpoint)8 CamelContext (org.apache.camel.CamelContext)6 Component (org.apache.camel.Component)3 NoSuchBeanException (org.apache.camel.NoSuchBeanException)3 RouteDefinition (org.apache.camel.model.RouteDefinition)3 Test (org.junit.Test)3 Swagger (io.swagger.models.Swagger)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 NoFactoryAvailableException (org.apache.camel.NoFactoryAvailableException)2 HttpConsumer (org.apache.camel.http.common.HttpConsumer)2 DefaultEndpoint (org.apache.camel.impl.DefaultEndpoint)2 FromDefinition (org.apache.camel.model.FromDefinition)2 InterceptFromDefinition (org.apache.camel.model.InterceptFromDefinition)2 RestDefinition (org.apache.camel.model.rest.RestDefinition)2 FactoryFinder (org.apache.camel.spi.FactoryFinder)2