use of org.apache.camel.model.rest.RestsDefinition in project camel by apache.
the class AbstractLocalCamelController method getRestModelAsXml.
public String getRestModelAsXml(String camelContextName) throws Exception {
CamelContext context = this.getLocalCamelContext(camelContextName);
if (context == null) {
return null;
}
List<RestDefinition> rests = context.getRestDefinitions();
if (rests == null || rests.isEmpty()) {
return null;
}
// use a rests definition to dump the rests
RestsDefinition def = new RestsDefinition();
def.setRests(rests);
return ModelHelper.dumpModelAsXml(null, def);
}
use of org.apache.camel.model.rest.RestsDefinition 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.RestsDefinition 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.RestsDefinition 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.RestsDefinition in project camel by apache.
the class RestSwaggerSupport method getRestDefinitions.
public List<RestDefinition> getRestDefinitions(String camelId) throws Exception {
ObjectName found = null;
boolean supportResolvePlaceholder = false;
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
Set<ObjectName> names = server.queryNames(new ObjectName("org.apache.camel:type=context,*"), null);
for (ObjectName on : names) {
String id = on.getKeyProperty("name");
if (id.startsWith("\"") && id.endsWith("\"")) {
id = id.substring(1, id.length() - 1);
}
if (camelId == null || camelId.equals(id)) {
// filter out older Camel versions as this requires Camel 2.15 or better (rest-dsl)
String version = (String) server.getAttribute(on, "CamelVersion");
if (CamelVersionHelper.isGE("2.15.0", version)) {
found = on;
}
if (CamelVersionHelper.isGE("2.15.3", version)) {
supportResolvePlaceholder = true;
}
}
}
if (found != null) {
String xml;
if (supportResolvePlaceholder) {
xml = (String) server.invoke(found, "dumpRestsAsXml", new Object[] { true }, new String[] { "boolean" });
} else {
xml = (String) server.invoke(found, "dumpRestsAsXml", null, null);
}
if (xml != null) {
LOG.debug("DumpRestAsXml:\n{}", xml);
RestsDefinition rests = ModelHelper.createModelFromXml(null, xml, RestsDefinition.class);
if (rests != null) {
return rests.getRests();
}
}
}
return null;
}
Aggregations