Search in sources :

Example 1 with OsgiLanguageResolver

use of org.apache.camel.core.osgi.OsgiLanguageResolver in project karaf by apache.

the class CamelComponent method activate.

@Activate
public void activate(ComponentContext componentContext) throws Exception {
    BundleContext bundleContext = componentContext.getBundleContext();
    OsgiDefaultCamelContext osgiDefaultCamelContext = new OsgiDefaultCamelContext(bundleContext);
    osgiDefaultCamelContext.setClassResolver(new OsgiClassResolver(camelContext, bundleContext));
    osgiDefaultCamelContext.setDataFormatResolver(new OsgiDataFormatResolver(bundleContext));
    osgiDefaultCamelContext.setLanguageResolver(new OsgiLanguageResolver(bundleContext));
    osgiDefaultCamelContext.setName("context-example");
    camelContext = osgiDefaultCamelContext;
    serviceRegistration = bundleContext.registerService(CamelContext.class, camelContext, null);
    camelContext.start();
    camelContext.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("jetty:http://0.0.0.0:9090/example").id("example-http-inbound").convertBodyTo(String.class).log("[EXAMPLE INBOUND] Received: ${body}").choice().when().simple("${headers.CamelHttpMethod} == 'POST'").setHeader("type").jsonpath("$.notification.type").choice().when().simple("${header.type} == 'email'").log("[EXAMPLE INBOUND] Received email notification").to("direct:email").setHeader("Exchange.HTTP_RESPONSE_CODE", constant(200)).when().simple("${header.type} == 'http'").log("[EXAMPLE INBOUND] Received http notification").to("direct:http").setHeader("Exchange.HTTP_RESPONSE_CODE", constant(200)).otherwise().log("[EXAMPLE INBOUND] Unknown notification").setBody(constant("{ \"status\": \"reject\", \"type\": \"unknown\" }")).setHeader("Exchange.HTTP_RESPONSE_CODE", constant(400)).otherwise().log("[EXAMPLE INBOUND] only POST is accepted (${headers.CamelHttpMethod})").setBody(constant("{ \"error\": \"only POST is accepted\" }")).setHeader("Exchange.HTTP_RESPONSE_CODE", constant(500));
            from("direct:email").id("example-email").log("[EXAMPLE EMAIL] Sending notification email").setHeader("to").jsonpath("$.notification.to").setHeader("subject", constant("Notification")).setHeader("payload").jsonpath("$.notification.message").setBody(simple("{ \"status\": \"email sent\", \"to\": \"${header.to}\", \"subject\": \"${header.subject}\" }"));
            from("direct:http").id("example-http").log("[EXAMPLE HTTP] Sending http notification").setHeader("service").jsonpath("$.notification.service").setBody(simple("{ \"status\": \"http requested\", \"service\": \"${header.service}\" }"));
        }
    });
}
Also used : CamelContext(org.apache.camel.CamelContext) OsgiDefaultCamelContext(org.apache.camel.core.osgi.OsgiDefaultCamelContext) ModelCamelContext(org.apache.camel.model.ModelCamelContext) OsgiDataFormatResolver(org.apache.camel.core.osgi.OsgiDataFormatResolver) OsgiLanguageResolver(org.apache.camel.core.osgi.OsgiLanguageResolver) RouteBuilder(org.apache.camel.builder.RouteBuilder) OsgiDefaultCamelContext(org.apache.camel.core.osgi.OsgiDefaultCamelContext) OsgiClassResolver(org.apache.camel.core.osgi.OsgiClassResolver) BundleContext(org.osgi.framework.BundleContext) Activate(org.osgi.service.component.annotations.Activate)

Aggregations

CamelContext (org.apache.camel.CamelContext)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 OsgiClassResolver (org.apache.camel.core.osgi.OsgiClassResolver)1 OsgiDataFormatResolver (org.apache.camel.core.osgi.OsgiDataFormatResolver)1 OsgiDefaultCamelContext (org.apache.camel.core.osgi.OsgiDefaultCamelContext)1 OsgiLanguageResolver (org.apache.camel.core.osgi.OsgiLanguageResolver)1 ModelCamelContext (org.apache.camel.model.ModelCamelContext)1 BundleContext (org.osgi.framework.BundleContext)1 Activate (org.osgi.service.component.annotations.Activate)1