Search in sources :

Example 1 with RestConfigurationDefinition

use of org.apache.camel.model.rest.RestConfigurationDefinition in project camel by apache.

the class RouteBuilder method restConfiguration.

/**
     * Configures the REST service for the given component
     *
     * @return the builder
     */
public RestConfigurationDefinition restConfiguration(String component) {
    if (restConfigurations == null) {
        restConfigurations = new HashMap<String, RestConfigurationDefinition>();
    }
    RestConfigurationDefinition restConfiguration = restConfigurations.get(component);
    if (restConfiguration == null) {
        restConfiguration = new RestConfigurationDefinition();
        if (!component.isEmpty()) {
            restConfiguration.component(component);
        }
        restConfigurations.put(component, restConfiguration);
    }
    return restConfiguration;
}
Also used : RestConfigurationDefinition(org.apache.camel.model.rest.RestConfigurationDefinition)

Example 2 with RestConfigurationDefinition

use of org.apache.camel.model.rest.RestConfigurationDefinition in project camel by apache.

the class RouteBuilder method populateRests.

protected void populateRests() throws Exception {
    ModelCamelContext camelContext = getContext();
    if (camelContext == null) {
        throw new IllegalArgumentException("CamelContext has not been injected!");
    }
    getRestCollection().setCamelContext(camelContext);
    // setup rest configuration before adding the rests
    if (getRestConfigurations() != null) {
        for (Map.Entry<String, RestConfigurationDefinition> entry : getRestConfigurations().entrySet()) {
            RestConfiguration config = entry.getValue().asRestConfiguration(getContext());
            if ("".equals(entry.getKey())) {
                camelContext.setRestConfiguration(config);
            } else {
                camelContext.addRestConfiguration(config);
            }
        }
    }
    camelContext.addRestDefinitions(getRestCollection().getRests());
    // convert rests into routes so we they are routes for runtime
    List<RouteDefinition> routes = new ArrayList<RouteDefinition>();
    for (RestDefinition rest : getRestCollection().getRests()) {
        List<RouteDefinition> list = rest.asRouteDefinition(getContext());
        routes.addAll(list);
    }
    // convert rests api-doc into routes so they are routes for runtime
    for (RestConfiguration config : camelContext.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 : camelContext.getRouteDefinitions()) {
                FromDefinition from = route.getInputs().get(0);
                if (from.getUri() != null && from.getUri().startsWith("rest-api:")) {
                    hasRestApi = true;
                }
            }
            if (!hasRestApi) {
                RouteDefinition route = RestDefinition.asRouteApiDefinition(camelContext, config);
                log.debug("Adding routeId: {} as rest-api route", route.getId());
                routes.add(route);
            }
        }
    }
    // add the rest routes
    for (RouteDefinition route : routes) {
        getRouteCollection().route(route);
    }
}
Also used : RestDefinition(org.apache.camel.model.rest.RestDefinition) FromDefinition(org.apache.camel.model.FromDefinition) InterceptFromDefinition(org.apache.camel.model.InterceptFromDefinition) ArrayList(java.util.ArrayList) ModelCamelContext(org.apache.camel.model.ModelCamelContext) RestConfigurationDefinition(org.apache.camel.model.rest.RestConfigurationDefinition) RouteDefinition(org.apache.camel.model.RouteDefinition) RestConfiguration(org.apache.camel.spi.RestConfiguration) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

RestConfigurationDefinition (org.apache.camel.model.rest.RestConfigurationDefinition)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 FromDefinition (org.apache.camel.model.FromDefinition)1 InterceptFromDefinition (org.apache.camel.model.InterceptFromDefinition)1 ModelCamelContext (org.apache.camel.model.ModelCamelContext)1 RouteDefinition (org.apache.camel.model.RouteDefinition)1 RestDefinition (org.apache.camel.model.rest.RestDefinition)1 RestConfiguration (org.apache.camel.spi.RestConfiguration)1