use of org.apache.camel.model.rest.RestDefinition in project camel by apache.
the class DefaultCamelContext method loadRestsDefinition.
public synchronized RestsDefinition loadRestsDefinition(InputStream is) throws Exception {
// load routes using JAXB
if (jaxbContext == null) {
// must use classloader from CamelContext to have JAXB working
jaxbContext = getModelJAXBContextFactory().newJAXBContext();
}
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Object result = unmarshaller.unmarshal(is);
if (result == null) {
throw new IOException("Cannot unmarshal to rests using JAXB from input stream: " + is);
}
// can either be routes or a single route
RestsDefinition answer;
if (result instanceof RestDefinition) {
RestDefinition rest = (RestDefinition) result;
answer = new RestsDefinition();
answer.getRests().add(rest);
} else if (result instanceof RestsDefinition) {
answer = (RestsDefinition) result;
} else {
throw new IllegalArgumentException("Unmarshalled object is an unsupported type: " + ObjectHelper.className(result) + " -> " + result);
}
return answer;
}
use of org.apache.camel.model.rest.RestDefinition in project camel by apache.
the class ManagedCamelContext method dumpRestsAsXml.
@Override
public String dumpRestsAsXml(boolean resolvePlaceholders) throws Exception {
List<RestDefinition> rests = context.getRestDefinitions();
if (rests.isEmpty()) {
return null;
}
// use a routes definition to dump the rests
RestsDefinition def = new RestsDefinition();
def.setRests(rests);
String xml = ModelHelper.dumpModelAsXml(context, def);
// if resolving placeholders we parse the xml, and resolve the property placeholders during parsing
if (resolvePlaceholders) {
final AtomicBoolean changed = new AtomicBoolean();
InputStream is = new ByteArrayInputStream(xml.getBytes());
Document dom = XmlLineNumberParser.parseXml(is, new XmlLineNumberParser.XmlTextTransformer() {
@Override
public String transform(String text) {
try {
String after = getContext().resolvePropertyPlaceholders(text);
if (!changed.get()) {
changed.set(!text.equals(after));
}
return after;
} catch (Exception e) {
// ignore
return text;
}
}
});
// okay there were some property placeholder replaced so re-create the model
if (changed.get()) {
xml = context.getTypeConverter().mandatoryConvertTo(String.class, dom);
RestsDefinition copy = ModelHelper.createModelFromXml(context, xml, RestsDefinition.class);
xml = ModelHelper.dumpModelAsXml(context, copy);
}
}
return xml;
}
use of org.apache.camel.model.rest.RestDefinition in project camel by apache.
the class RoutesCollector method loadXmlRests.
private void loadXmlRests(ApplicationContext applicationContext, CamelContext camelContext, String directory) {
LOG.info("Loading additional Camel XML rests from: {}", directory);
try {
final Resource[] xmlRests = applicationContext.getResources(directory);
for (final Resource xmlRest : xmlRests) {
final RestsDefinition xmlDefinitions = camelContext.loadRestsDefinition(xmlRest.getInputStream());
camelContext.addRestDefinitions(xmlDefinitions.getRests());
for (final RestDefinition xmlDefinition : xmlDefinitions.getRests()) {
final List<RouteDefinition> routeDefinitions = xmlDefinition.asRouteDefinition(camelContext);
camelContext.addRouteDefinitions(routeDefinitions);
}
}
} catch (FileNotFoundException e) {
LOG.debug("No XML rests found in {}. Skipping XML rests detection.", directory);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of org.apache.camel.model.rest.RestDefinition in project camel by apache.
the class DefaultCamelSwaggerServletTest method testServlet.
@Test
public void testServlet() throws Exception {
DefaultCamelSwaggerServlet servlet = new DefaultCamelSwaggerServlet();
Buffer<RestDefinition> list = servlet.getRestDefinitions(null);
assertEquals(1, list.size());
RestDefinition rest = list.iterator().next();
checkRestDefinition(rest);
// get the RestDefinition by using the camel context id
list = servlet.getRestDefinitions(context.getName());
assertEquals(1, list.size());
rest = list.iterator().next();
checkRestDefinition(rest);
RestDefinition rest2 = context.getRestDefinitions().get(0);
checkRestDefinition(rest2);
}
use of org.apache.camel.model.rest.RestDefinition in project camel by apache.
the class RestSwaggerReaderTest method testReaderRead.
@Test
public void testReaderRead() throws Exception {
RestDefinition rest = context.getRestDefinitions().get(0);
assertNotNull(rest);
SwaggerConfig config = new SwaggerConfig();
config.setBasePath("http://localhost:8080/api");
RestSwaggerReader reader = new RestSwaggerReader();
Option<ApiListing> option = reader.read(rest, config);
assertNotNull(option);
ApiListing listing = option.get();
assertNotNull(listing);
String json = JsonSerializer.asJson(listing);
log.info(json);
assertTrue(json.contains("\"basePath\":\"http://localhost:8080/api\""));
assertTrue(json.contains("\"resourcePath\":\"/hello\""));
assertTrue(json.contains("\"method\":\"GET\""));
assertTrue(json.contains("\"nickname\":\"getHelloHi\""));
context.stop();
}
Aggregations