use of org.apache.camel.model.FromDefinition 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);
}
}
use of org.apache.camel.model.FromDefinition in project camel by apache.
the class RouteIdFactory method extractId.
/**
* Extract id from routes
*/
private Optional<String> extractId(RouteDefinition routeDefinition) {
if (routeDefinition.getRestDefinition() != null) {
return Optional.empty();
}
List<FromDefinition> inputs = routeDefinition.getInputs();
if (inputs == null || inputs.isEmpty()) {
return Optional.empty();
}
FromDefinition from = inputs.get(0);
String uri = from.getUri();
// we want to use the context-path of the route
int colon = uri.indexOf(':');
if (colon > 0) {
String name = uri.substring(colon + 1);
int questionMark = name.indexOf("?");
if (questionMark > 0) {
return Optional.of(name.substring(0, questionMark));
} else {
return Optional.of(name);
}
}
return Optional.empty();
}
use of org.apache.camel.model.FromDefinition 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);
}
}
use of org.apache.camel.model.FromDefinition in project ddf by codice.
the class ContentDirectoryMonitorTest method verifyRoute.
private void verifyRoute(RouteDefinition routeDefinition, String monitoredDirectory, String processingMechanism) {
List<FromDefinition> fromDefinitions = routeDefinition.getInputs();
assertThat(fromDefinitions.size(), is(1));
String uri = fromDefinitions.get(0).getUri();
String expectedUri = "file:" + monitoredDirectory + "?recursive=true&moveFailed=.errors&readLockMinLength=1&readLock=changed&readLockTimeout=2000&readLockCheckInterval=1000";
if (ContentDirectoryMonitor.DELETE.equals(processingMechanism)) {
expectedUri += "&delete=true";
} else if (ContentDirectoryMonitor.MOVE.equals(processingMechanism)) {
expectedUri += "&move=.ingested";
} else if (ContentDirectoryMonitor.IN_PLACE.equals(processingMechanism)) {
expectedUri = "durable:" + monitoredDirectory;
}
assertThat(uri, equalTo(expectedUri));
List<ProcessorDefinition<?>> processorDefinitions = routeDefinition.getOutputs();
if (ContentDirectoryMonitor.IN_PLACE.equals(processingMechanism)) {
assertThat(processorDefinitions.size(), is(2));
} else {
assertThat(processorDefinitions.size(), is(1));
}
}
use of org.apache.camel.model.FromDefinition in project ddf by codice.
the class ContentDirectoryMonitor method dumpCamelContext.
private void dumpCamelContext(String msg) {
LOGGER.debug("\n\n*************** START: {} *****************", msg);
List<RouteDefinition> routeDefinitions = camelContext.getRouteDefinitions();
if (routeDefinitions != null) {
LOGGER.debug("Number of routes = {}", routeDefinitions.size());
for (RouteDefinition routeDef : routeDefinitions) {
String routeId = routeDef.getId();
LOGGER.debug("route ID = {}", routeId);
List<FromDefinition> routeInputs = routeDef.getInputs();
if (routeInputs.isEmpty()) {
LOGGER.debug("routeInputs are EMPTY");
} else {
for (FromDefinition fromDef : routeInputs) {
LOGGER.debug("route input's URI = {}", fromDef.getUri());
}
}
ServiceStatus routeStatus = camelContext.getRouteStatus(routeId);
if (routeStatus != null) {
LOGGER.debug("Route ID {} is started = {}", routeId, routeStatus.isStarted());
} else {
LOGGER.debug("routeStatus is NULL for routeId = {}", routeId);
}
}
}
LOGGER.debug("*************** END: {} *****************\n\n", msg);
}
Aggregations