Search in sources :

Example 1 with RestsDefinition

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);
}
Also used : CamelContext(org.apache.camel.CamelContext) RestDefinition(org.apache.camel.model.rest.RestDefinition) RestsDefinition(org.apache.camel.model.rest.RestsDefinition)

Example 2 with RestsDefinition

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);
    }
}
Also used : RestDefinition(org.apache.camel.model.rest.RestDefinition) RouteDefinition(org.apache.camel.model.RouteDefinition) Resource(org.springframework.core.io.Resource) RestsDefinition(org.apache.camel.model.rest.RestsDefinition) FileNotFoundException(java.io.FileNotFoundException) FileNotFoundException(java.io.FileNotFoundException)

Example 3 with RestsDefinition

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;
}
Also used : RestDefinition(org.apache.camel.model.rest.RestDefinition) RestsDefinition(org.apache.camel.model.rest.RestsDefinition) IOException(java.io.IOException) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 4 with RestsDefinition

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;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RestDefinition(org.apache.camel.model.rest.RestDefinition) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) RestsDefinition(org.apache.camel.model.rest.RestsDefinition) Document(org.w3c.dom.Document) XmlLineNumberParser(org.apache.camel.util.XmlLineNumberParser) IOException(java.io.IOException)

Example 5 with RestsDefinition

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;
}
Also used : RestsDefinition(org.apache.camel.model.rest.RestsDefinition) ObjectName(javax.management.ObjectName) MBeanServer(javax.management.MBeanServer)

Aggregations

RestsDefinition (org.apache.camel.model.rest.RestsDefinition)6 RestDefinition (org.apache.camel.model.rest.RestDefinition)5 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 MBeanServer (javax.management.MBeanServer)1 ObjectName (javax.management.ObjectName)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 CamelContext (org.apache.camel.CamelContext)1 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)1 RouteDefinition (org.apache.camel.model.RouteDefinition)1 XmlLineNumberParser (org.apache.camel.util.XmlLineNumberParser)1 Resource (org.springframework.core.io.Resource)1 Document (org.w3c.dom.Document)1