use of org.apache.camel.model.rest.RestDefinition in project camel by apache.
the class RestSwaggerReaderPropertyPlaceholderTest method testReaderRead.
@Test
public void testReaderRead() throws Exception {
BeanConfig config = new BeanConfig();
config.setHost("localhost:8080");
config.setSchemes(new String[] { "http" });
config.setBasePath("/api");
RestSwaggerReader reader = new RestSwaggerReader();
RestSwaggerSupport support = new RestSwaggerSupport();
List<RestDefinition> rests = support.getRestDefinitions(context.getName());
Swagger swagger = reader.read(rests, null, config, context.getName(), new DefaultClassResolver());
assertNotNull(swagger);
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
String json = mapper.writeValueAsString(swagger);
log.info(json);
assertTrue(json.contains("\"host\" : \"localhost:8080\""));
assertTrue(json.contains("\"basePath\" : \"/api\""));
assertTrue(json.contains("\"/hello/bye\""));
assertTrue(json.contains("\"summary\" : \"To update the greeting message\""));
assertTrue(json.contains("\"/hello/bye/{name}\""));
assertTrue(json.contains("\"/hello/hi/{name}\""));
assertFalse(json.contains("{foo}"));
assertFalse(json.contains("{bar}"));
context.stop();
}
use of org.apache.camel.model.rest.RestDefinition in project camel by apache.
the class AbstractCamelContextFactoryBean method initRestRefs.
protected void initRestRefs() throws Exception {
// add rest refs to existing rests
if (getRestRefs() != null) {
for (RestContextRefDefinition ref : getRestRefs()) {
List<RestDefinition> defs = ref.lookupRests(getContext());
for (RestDefinition def : defs) {
LOG.debug("Adding rest from {} -> {}", ref, def);
// add in top as they are most likely to be common/shared
// which you may want to start first
getRests().add(0, def);
}
}
}
}
use of org.apache.camel.model.rest.RestDefinition 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.rest.RestDefinition in project camel by apache.
the class RestContextRefDefinitionHelper method lookupRests.
/**
* Lookup the rests from the {@link org.apache.camel.model.RestContextRefDefinition}.
* <p/>
* This implementation must be used to lookup the rests as it performs a deep clone of the rests
* as a {@link org.apache.camel.model.RestContextRefDefinition} can be re-used with multiple {@link org.apache.camel.model.ModelCamelContext} and each
* context should have their own instances of the routes. This is to ensure no side-effects and sharing
* of instances between the contexts. For example such as property placeholders may be context specific
* so the routes should not use placeholders from another {@link org.apache.camel.model.ModelCamelContext}.
*
* @param camelContext the CamelContext
* @param ref the id of the {@link org.apache.camel.model.RestContextRefDefinition} to lookup and get the routes.
* @return the rests.
*/
@SuppressWarnings("unchecked")
public static synchronized List<RestDefinition> lookupRests(ModelCamelContext camelContext, String ref) {
ObjectHelper.notNull(camelContext, "camelContext");
ObjectHelper.notNull(ref, "ref");
List<RestDefinition> answer = CamelContextHelper.lookup(camelContext, ref, List.class);
if (answer == null) {
throw new IllegalArgumentException("Cannot find RestContext with id " + ref);
}
// must clone the rest definitions as they can be reused with multiple CamelContexts
// and they would need their own instances of the definitions to not have side effects among
// the CamelContext - for example property placeholder resolutions etc.
List<RestDefinition> clones = new ArrayList<RestDefinition>(answer.size());
try {
JAXBContext jaxb = getOrCreateJAXBContext(camelContext);
for (RestDefinition def : answer) {
RestDefinition clone = cloneRestDefinition(jaxb, def);
if (clone != null) {
clones.add(clone);
}
}
} catch (Exception e) {
throw ObjectHelper.wrapRuntimeCamelException(e);
}
return clones;
}
use of org.apache.camel.model.rest.RestDefinition in project camel by apache.
the class LoadRestFromXmlTest method testLoadRestFromXml.
public void testLoadRestFromXml() throws Exception {
assertNotNull("Existing foo route should be there", context.getRoute("foo"));
assertEquals(2, context.getRoutes().size());
// test that existing route works
MockEndpoint foo = getMockEndpoint("mock:foo");
foo.expectedBodiesReceived("Hello World");
template.sendBody("direct:foo", "Hello World");
foo.assertIsSatisfied();
// load rest from XML and add them to the existing camel context
InputStream is = getClass().getResourceAsStream("barRest.xml");
RestsDefinition rests = context.loadRestsDefinition(is);
context.addRestDefinitions(rests.getRests());
for (final RestDefinition restDefinition : rests.getRests()) {
List<RouteDefinition> routeDefinitions = restDefinition.asRouteDefinition(context);
context.addRouteDefinitions(routeDefinitions);
}
assertNotNull("Loaded rest route should be there", context.getRoute("route1"));
assertEquals(3, context.getRoutes().size());
// test that loaded route works
MockEndpoint bar = getMockEndpoint("mock:bar");
bar.expectedBodiesReceived("Bye World");
template.sendBody("seda:get-say-hello-bar", "Bye World");
bar.assertIsSatisfied();
}
Aggregations