Search in sources :

Example 11 with RestDefinition

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;
}
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 12 with RestDefinition

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;
}
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 13 with RestDefinition

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);
    }
}
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 14 with RestDefinition

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

Example 15 with RestDefinition

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();
}
Also used : SwaggerConfig(com.wordnik.swagger.config.SwaggerConfig) RestDefinition(org.apache.camel.model.rest.RestDefinition) ApiListing(com.wordnik.swagger.model.ApiListing) Test(org.junit.Test)

Aggregations

RestDefinition (org.apache.camel.model.rest.RestDefinition)38 ToDefinition (org.apache.camel.model.ToDefinition)10 Test (org.junit.Test)10 RestsDefinition (org.apache.camel.model.rest.RestsDefinition)5 RouteDefinition (org.apache.camel.model.RouteDefinition)4 Swagger (io.swagger.models.Swagger)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Unmarshaller (javax.xml.bind.Unmarshaller)2 FromDefinition (org.apache.camel.model.FromDefinition)2 InterceptFromDefinition (org.apache.camel.model.InterceptFromDefinition)2 GetVerbDefinition (org.apache.camel.model.rest.GetVerbDefinition)2 RestContainer (org.apache.camel.model.rest.RestContainer)2 VerbDefinition (org.apache.camel.model.rest.VerbDefinition)2 RestConfiguration (org.apache.camel.spi.RestConfiguration)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1