@SuppressWarnings("deprecation") 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); }
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(); }
@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; }
public RestsDefinition getRestCollection() { return restCollection; }
public void setRestCollection(RestsDefinition restCollection) { this.restCollection = restCollection; }
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; }
@Override public RestsDefinition loadRestsDefinition(InputStream inputStream) throws Exception { return context.loadRestsDefinition(inputStream); }
@Override public RestsDefinition loadRestsDefinition(InputStream is) throws Exception { return context.loadRestsDefinition(is); }
/** * Loads a collection of rest definitions from the given {@link java.io.InputStream}. * * @param is input stream with the rest(s) definition to add * @return the rest definitions * @throws Exception if the rest definitions could not be loaded for whatever reason */ RestsDefinition loadRestsDefinition(InputStream is) throws Exception;
/** * Loads a collection of rest definitions from the given {@link java.io.InputStream}. * * @param is input stream with the rest(s) definition to add * @throws Exception if the rest definitions could not be loaded for whatever reason * @return the rest definitions */ RestsDefinition loadRestsDefinition(InputStream is) throws Exception;
/** * Configures the rests * * @param context the Camel context * @return the rests configured * @throws Exception can be thrown during configuration */ public RestsDefinition configureRests(ModelCamelContext context) throws Exception { setContext(context); restCollection.setCamelContext(context); return restCollection; }