Search in sources :

Example 6 with RestDefinition

use of org.apache.camel.model.rest.RestDefinition in project camel by apache.

the class CamelContextAddRestDefinitionsFromXmlTest method testAddRestDefinitionsFromXml.

public void testAddRestDefinitionsFromXml() throws Exception {
    RestDefinition rest = loadRest("rest1.xml");
    assertNotNull(rest);
    assertEquals("foo", rest.getId());
    assertEquals(0, context.getRestDefinitions().size());
    context.getRestDefinitions().add(rest);
    assertEquals(1, context.getRestDefinitions().size());
    final List<RouteDefinition> routeDefinitions = rest.asRouteDefinition(context);
    for (final RouteDefinition routeDefinition : routeDefinitions) {
        context.addRouteDefinition(routeDefinition);
    }
    assertEquals(2, context.getRoutes().size());
    assertTrue("Route should be started", context.getRouteStatus("route1").isStarted());
    getMockEndpoint("mock:bar").expectedBodiesReceived("Hello World");
    template.sendBody("seda:get-say-hello-bar", "Hello World");
    assertMockEndpointsSatisfied();
}
Also used : RestDefinition(org.apache.camel.model.rest.RestDefinition) RouteDefinition(org.apache.camel.model.RouteDefinition)

Example 7 with RestDefinition

use of org.apache.camel.model.rest.RestDefinition in project camel by apache.

the class RestContextRefDefinitionHelper method cloneRestDefinition.

private static RestDefinition cloneRestDefinition(JAXBContext jaxbContext, RestDefinition def) throws JAXBException {
    Marshaller marshal = jaxbContext.createMarshaller();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    marshal.marshal(def, bos);
    ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    Object clone = unmarshaller.unmarshal(bis);
    if (clone != null && clone instanceof RestDefinition) {
        RestDefinition def2 = (RestDefinition) clone;
        Iterator<VerbDefinition> verbit1 = def.getVerbs().iterator();
        Iterator<VerbDefinition> verbit2 = def2.getVerbs().iterator();
        while (verbit1.hasNext() && verbit2.hasNext()) {
            VerbDefinition verb1 = verbit1.next();
            VerbDefinition verb2 = verbit2.next();
            if (verb1.getToOrRoute() instanceof RouteDefinition && verb2.getToOrRoute() instanceof RouteDefinition) {
                RouteDefinition route1 = (RouteDefinition) verb1.getToOrRoute();
                RouteDefinition route2 = (RouteDefinition) verb2.getToOrRoute();
                // need to clone the namespaces also as they are not JAXB marshalled (as they are transient)
                Iterator<ExpressionNode> it = ProcessorDefinitionHelper.filterTypeInOutputs(route1.getOutputs(), ExpressionNode.class);
                Iterator<ExpressionNode> it2 = ProcessorDefinitionHelper.filterTypeInOutputs(route2.getOutputs(), ExpressionNode.class);
                while (it.hasNext() && it2.hasNext()) {
                    ExpressionNode node = it.next();
                    ExpressionNode node2 = it2.next();
                    NamespaceAwareExpression name = null;
                    NamespaceAwareExpression name2 = null;
                    if (node.getExpression() instanceof NamespaceAwareExpression) {
                        name = (NamespaceAwareExpression) node.getExpression();
                    }
                    if (node2.getExpression() instanceof NamespaceAwareExpression) {
                        name2 = (NamespaceAwareExpression) node2.getExpression();
                    }
                    if (name != null && name2 != null && name.getNamespaces() != null && !name.getNamespaces().isEmpty()) {
                        Map<String, String> map = new HashMap<String, String>();
                        map.putAll(name.getNamespaces());
                        name2.setNamespaces(map);
                    }
                }
            }
        }
        return def2;
    }
    return null;
}
Also used : Marshaller(javax.xml.bind.Marshaller) RestDefinition(org.apache.camel.model.rest.RestDefinition) VerbDefinition(org.apache.camel.model.rest.VerbDefinition) HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Unmarshaller(javax.xml.bind.Unmarshaller) NamespaceAwareExpression(org.apache.camel.model.language.NamespaceAwareExpression)

Example 8 with RestDefinition

use of org.apache.camel.model.rest.RestDefinition in project camel by apache.

the class XmlRestParseTest method assertOneRest.

protected RestDefinition assertOneRest(String uri) throws JAXBException {
    RestContainer context = assertParseRestAsJaxb(uri);
    RestDefinition rest = assertOneElement(context.getRests());
    return rest;
}
Also used : RestDefinition(org.apache.camel.model.rest.RestDefinition) RestContainer(org.apache.camel.model.rest.RestContainer)

Example 9 with RestDefinition

use of org.apache.camel.model.rest.RestDefinition in project camel by apache.

the class XmlRestParseTest method testParseSimpleRestXml.

public void testParseSimpleRestXml() throws Exception {
    RestDefinition rest = assertOneRest("simpleRest.xml");
    assertEquals("/users", rest.getPath());
    assertEquals(1, rest.getVerbs().size());
    GetVerbDefinition get = (GetVerbDefinition) rest.getVerbs().get(0);
    assertEquals("/view/{id}", get.getUri());
    assertEquals("direct:getUser", get.getTo().getUri());
}
Also used : RestDefinition(org.apache.camel.model.rest.RestDefinition) GetVerbDefinition(org.apache.camel.model.rest.GetVerbDefinition)

Example 10 with RestDefinition

use of org.apache.camel.model.rest.RestDefinition in project camel by apache.

the class XmlRestParseToDTest method testParseSimpleRestXml.

public void testParseSimpleRestXml() throws Exception {
    RestDefinition rest = assertOneRest("simpleRestToD.xml");
    assertEquals("/users", rest.getPath());
    assertEquals(1, rest.getVerbs().size());
    GetVerbDefinition get = (GetVerbDefinition) rest.getVerbs().get(0);
    assertEquals("/view/{id}", get.getUri());
    assertEquals("bean:getUser?id=${header.id}", get.getToD().getUri());
}
Also used : RestDefinition(org.apache.camel.model.rest.RestDefinition) GetVerbDefinition(org.apache.camel.model.rest.GetVerbDefinition)

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