Search in sources :

Example 36 with RestConfiguration

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

the class AbstractCamelContextFactoryBean method setupRoutes.

/**
     * Setup all the routes which must be done prior starting {@link CamelContext}.
     */
protected void setupRoutes() throws Exception {
    if (routesSetupDone.compareAndSet(false, true)) {
        LOG.debug("Setting up routes");
        // mark that we are setting up routes
        getContext().setupRoutes(false);
        // must init route refs before we prepare the routes below
        initRouteRefs();
        // must init rest refs before we add the rests
        initRestRefs();
        // and add the rests
        getContext().addRestDefinitions(getRests());
        // convert rests into routes so we reuse routes for runtime
        for (RestDefinition rest : getRests()) {
            List<RouteDefinition> routes = rest.asRouteDefinition(getContext());
            for (RouteDefinition route : routes) {
                getRoutes().add(route);
            }
        }
        // convert rests api-doc into routes so they are routes for runtime
        for (RestConfiguration config : getContext().getRestConfigurations()) {
            if (config.getApiContextPath() != null) {
                // avoid adding rest-api multiple times, in case multiple RouteBuilder classes is added
                // to the CamelContext, as we only want to setup rest-api once
                // so we check all existing routes if they have rest-api route already added
                boolean hasRestApi = false;
                for (RouteDefinition route : getContext().getRouteDefinitions()) {
                    FromDefinition from = route.getInputs().get(0);
                    if (from.getUri() != null && from.getUri().startsWith("rest-api:")) {
                        hasRestApi = true;
                    }
                }
                if (!hasRestApi) {
                    RouteDefinition route = RestDefinition.asRouteApiDefinition(getContext(), config);
                    LOG.debug("Adding routeId: {} as rest-api route", route.getId());
                    getRoutes().add(route);
                }
            }
        }
        // do special preparation for some concepts such as interceptors and policies
        // this is needed as JAXB does not build exactly the same model definition as Spring DSL would do
        // using route builders. So we have here a little custom code to fix the JAXB gaps
        prepareRoutes();
        // and add the routes
        getContext().addRouteDefinitions(getRoutes());
        LOG.debug("Found JAXB created routes: {}", getRoutes());
        findRouteBuilders();
        installRoutes();
        // and we are now finished setting up the routes
        getContext().setupRoutes(true);
    }
}
Also used : RestDefinition(org.apache.camel.model.rest.RestDefinition) FromDefinition(org.apache.camel.model.FromDefinition) InterceptFromDefinition(org.apache.camel.model.InterceptFromDefinition) RouteDefinition(org.apache.camel.model.RouteDefinition) RestConfiguration(org.apache.camel.spi.RestConfiguration)

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