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;
}
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);
}
}
Aggregations