Search code examples
apache-cameljax-rsjbossfuseblueprint-osgi

What is the secret to successfully calling a REST/POST operation using the "to" method in the RouteBuilder class?


I have a JAX-RS rest service whose POST method looks like this:

...
@Path("/post1/")
@POST
@Produces({MediaType.TEXT_PLAIN})
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response post(@Context javax.servlet.http.HttpServletRequest request, ContainerListType containerListType) throws Exception, Throwable, RuntimeException {

    Response response = null;
    try {
        List<ContainerType> containerList = containerListType.getContainer();  <=== null value:  "containerListType" is null!
        ContainerType ct = containerList.get(0);
        URI uri = UriBuilder.fromUri(uriInfo.getRequestUri()).path(ct.getFielda()).path(ct.getFieldb()).path(ct.getFieldc()).build();
        response = Response.created(uri).build();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return response;
}
...
    
    

(Note: Testing the above POST operation using a tool -i.e., like the FireFox plugin: "HttpRequestor" - works fine for posting -both- JSON and XML messages.)

I'm attempting a POST operation using the "to." method as shown below (this is in the RouteBuilder class).

...
    from("direct:thingH")
            .process(new Processor() {
                @Override
                public void process(Exchange exchange) throws Exception {
                    exchange.getIn().setBody("{'container':[{'fielda': '...this is fielda value...','fieldb': '...this is fielda value...','fieldc': '...this is fielda value...'}]}");
                }
            })
            .log("---------------------- (from(\"direct:thingH\")) ----------------------> process yields...:" + body().toString())                
            .to("restlet:http://localhost:7001/jaxrsRestService/service/post1/?restletMethod=POST&exchangePattern=InOut");
...

NOTE: the log entry seems to indicate that the "body" is NOT empty:

2017-08-24 11:17:19,488 | INFO  | stlet-1261331139 | route42                          | 232 - org.apache.camel.camel-core - 2.17.0.redhat-630187 | ---------------------- (from("direct:thingH")) ----------------------> process yields...:simple{{'container':[{'fielda': '...this is fielda value...','fieldb': '...this is fielda value...','fieldc': '...this is fielda value...'}]}}

Yet executing the post method yields a java.lang.NullPointerException - i.e., the "containerListType" parameter in the REST service's POST method is null. Why is this?

...
java.lang.NullPointerException
    at aaa.bbb.ccc.JaxrsRestService.post(JaxrsRestService.java:51)
    at sun.reflect.GeneratedMethodAccessor949.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161)
    at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:160)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
    at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:326)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
    at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:471)
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:425)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:383)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:336)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:223)
    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:286)
    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:260)
    at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:137)
    at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:350)
    at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:247)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3679)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3649)
    at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:326)
    at weblogic.security.service.SecurityManager.runAsForUserCode(SecurityManager.java:197)
    at weblogic.servlet.provider.WlsSecurityProvider.runAsForUserCode(WlsSecurityProvider.java:203)
    at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:71)
    at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2433)
    at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2281)
    at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2259)
    at weblogic.servlet.internal.ServletRequestImpl.runInternal(ServletRequestImpl.java:1691)
    at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1651)
    at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:270)
    at weblogic.invocation.ComponentInvocationContextManager._runAs(ComponentInvocationContextManager.java:348)
    at weblogic.invocation.ComponentInvocationContextManager.runAs(ComponentInvocationContextManager.java:333)
    at weblogic.work.LivePartitionUtility.doRunWorkUnderContext(LivePartitionUtility.java:54)
    at weblogic.work.PartitionUtility.runWorkUnderContext(PartitionUtility.java:41)
    at weblogic.work.SelfTuningWorkManagerImpl.runWorkUnderContext(SelfTuningWorkManagerImpl.java:640)
    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:406)
    at weblogic.work.ExecuteThread.run(ExecuteThread.java:346)
...     

The NullPointerException occurs because when the "post1" method is invoked, the POST'd "ContainerListType" value is null: why is is null?

Is calling the REST/POST operation this way - i.e., using the "to()" method improper/incorrect in any way?:

.to("restlet:http://localhost:7001/jaxrsRestService/service/post1/?restletMethod=POST&exchangePattern=InOut");

Or, do I need to perform the client REST call from inside a Processor?

Why is the posted object null? FWIW: I've the check the body() prior to the post - using logging - and it is not null.

Other environment info:

jdk1.8.0_131
jboss-fuse-6.3.0.redhat-187
WebLogic 12.2.1  (running the rest service)

Solution

  • *Couldn't get "restlet" working... -opted for "netty4-http" which is working *

    src/main/java/aaa/bbb/ccc/CamelRestRoutes.java

    package aaa.bbb.ccc;
    
    import javax.ws.rs.core.MediaType;
    import org.apache.camel.Exchange;
    import org.apache.camel.builder.RouteBuilder;
    import org.apache.camel.cdi.ContextName;
    import org.apache.camel.model.rest.RestBindingMode;
    
    @ContextName("rest-dsl")
    public class CamelRestRoutes extends RouteBuilder {
    
        public CamelRestRoutes() {
        }
    
        private final org.apache.camel.Processor proc1 = new Processor1();
    
        @Override
        public void configure() throws Exception {
    
        restConfiguration().component("netty4-http")
            .host("localhost")
            .port(8182)
            .bindingMode(RestBindingMode.json)
            .enableCORS(true);
    
        rest("/service")
            .get("/getAll").produces(MediaType.APPLICATION_JSON).to("direct:thingC") 
            .post("/post1").produces(MediaType.APPLICATION_JSON).to("direct:thingD");                             
    
        from("direct:thingC")
            .setHeader(Exchange.HTTP_METHOD, constant(org.apache.camel.component.http4.HttpMethods.GET))
            .to("netty4-http:http://localhost:7001/jaxrsRestService/service/getAll")
            .to("direct:thingD");
    
        from("direct:thingD")
            .process(proc1)                    
            .setHeader(Exchange.HTTP_METHOD, constant(org.apache.camel.component.http4.HttpMethods.POST))
            .to("netty4-http:http://localhost:7001/jaxrsRestService/service/post1");
        }
    }
    

    src/main/java/aaa/bbb/ccc/Processor1.java

    package aaa.bbb.ccc;
    
    import org.apache.camel.Message;
    
    public class Processor1 implements org.apache.camel.Processor {
    
        private Message inMsg;
    
        @Override
        public void process(org.apache.camel.Exchange exchange) {
        String s = "{\"container\": [{\"fielda\": \"...this is fielda valueAAA...\",\"fieldb\": \"...this is fielda valueBBB...\",\"fieldc\": \"...this is fielda valueCCC...\"}]}"; //"{'container':[{'fielda': '...this is fielda value...','fieldb': '...this is fielda value...','fieldc': '...this is fielda value...'}]}";
        exchange.getIn().setBody(s);        
        }
    };
    

    src/main/resources/OSGI-INF/blueprint/camel-routes.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"
           xmlns:jaxrs="http://cxf.apache.org/blueprint/jaxrs"
           xmlns:camel="http://camel.apache.org/schema/blueprint"
           xsi:schemaLocation="
             http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
             http://cxf.apache.org/blueprint/jaxrs http://cxf.apache.org/schemas/blueprint/jaxrs.xsd
             http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd
             ">
        <camel:camelContext id="aaa.bbb.ccc.routing.poc" xmlns="http://camel.apache.org/schema/blueprint">
        <packageScan>
            <package>aaa.bbb.ccc</package>
        </packageScan>
        </camel:camelContext>
    
        <bean id="camelRestService" class="aaa.bbb.ccc.CamelRestService"/> 
    
        <bean id="wmqcf" class="com.ibm.mq.jms.MQConnectionFactory">
        <property name="hostName" value="localhost"/>        
        <property name="port" value="1414"/>
        <property name="queueManager" value="QM1"/>     
        <property name="channel" value="DEV.ADMIN.SVRCONN"/>                     
        <property name="transportType" value="1"/>
        </bean>
    
        <bean id="wmqcfw"  class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter">
        <property name="targetConnectionFactory" ref="wmqcf" />
        <property name="username" value="admin" />
        <property name="password" value="passw0rd" />
        </bean>  
    
        <bean id="wmqcfg" class="org.apache.camel.component.jms.JmsConfiguration">
        <property name="connectionFactory" ref="wmqcfw"/>
        <property name="concurrentConsumers" value="10"/>
        </bean>
    
        <bean id="wmq" class="org.apache.camel.component.jms.JmsComponent">
        <property name="configuration" ref="wmqcfg"/>     
        </bean>    
    </blueprint>
    

    jaxrsRestService.java - to provide context, this is what the REST service (POST & GET) looks like...

    package aaa.bbb.ccc;
    
    import aaa.bbb.ccc.generated.ContainerListType;
    import aaa.bbb.ccc.generated.ContainerType;
    import aaa.bbb.ccc.generated.ObjectFactory;
    import java.net.URI;
    import java.util.List;
    import javax.ws.rs.Consumes;
    import javax.ws.rs.GET;
    import javax.ws.rs.POST;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.Context;
    import javax.ws.rs.core.MediaType;
    import javax.ws.rs.core.Response;
    import javax.ws.rs.core.UriBuilder;
    import javax.ws.rs.core.UriInfo;
    
    @Path("/service/")
    public class JaxrsRestService {
    
        @Context
        UriInfo uriInfo;
    
        @Path("/post1/")
        @POST
        @Produces({MediaType.TEXT_PLAIN})
        @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
        public Response post(@Context javax.servlet.http.HttpServletRequest request, ContainerListType containerListType) throws Exception, Throwable, RuntimeException {
        Response response = null;
        try {
            List<ContainerType> containerList = containerListType.getContainer();
            ContainerType ct = containerList.get(0);
            URI uri = UriBuilder.fromUri(uriInfo.getRequestUri()).path(ct.getFielda()).path(ct.getFieldb()).path(ct.getFieldc()).build();
            response = Response.created(uri).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        return response;
        }
    
        @Path("/getAll/")
        @GET
        @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
        public ContainerListType getAll(
            @Context javax.servlet.http.HttpServletRequest request
        ) {
        ObjectFactory of = new ObjectFactory();
        ContainerListType clt = of.createContainerListType();
        ContainerType ct = of.createContainerType();
        ct.setFielda("...this is fielda value...");
        ct.setFieldb("...this is fielda value...");
        ct.setFieldc("...this is fielda value...");
        clt.getContainer().add(ct);
    
        return clt;
        }
    }
    

    snapshot of "installed" camel features...in case it helps

    JBossFuse:karaf@root> features:list | grep "camel-" | grep  "\[installed"
    [installed  ] [2.7.0                ] xml-specs-api                                 camel-2.17.0.redhat-630187
    [installed  ] [2.17.0.redhat-630187 ] camel                                         camel-2.17.0.redhat-630187
    [installed  ] [2.17.0.redhat-630187 ] camel-core                                    camel-2.17.0.redhat-630187
    [installed  ] [2.17.0.redhat-630187 ] camel-blueprint                               camel-2.17.0.redhat-630187
    [installed  ] [2.17.0.redhat-630187 ] camel-spring                                  camel-2.17.0.redhat-630187
    [installed  ] [2.17.0.redhat-630187 ] camel-bindy                                   camel-2.17.0.redhat-630187
    [installed  ] [2.17.0.redhat-630187 ] camel-cdi                                     camel-2.17.0.redhat-630187
    [installed  ] [2.17.0.redhat-630187 ] camel-csv                                     camel-2.17.0.redhat-630187
    [installed  ] [2.17.0.redhat-630187 ] camel-cxf                                     camel-2.17.0.redhat-630187
    [installed  ] [2.17.0.redhat-630187 ] camel-exec                                    camel-2.17.0.redhat-630187
    [installed  ] [2.17.0.redhat-630187 ] camel-ftp                                     camel-2.17.0.redhat-630187
    [installed  ] [2.17.0.redhat-630187 ] camel-http4                                   camel-2.17.0.redhat-630187
    [installed  ] [2.17.0.redhat-630187 ] camel-jackson                                 camel-2.17.0.redhat-630187
    [installed  ] [2.17.0.redhat-630187 ] camel-jacksonxml                              camel-2.17.0.redhat-630187
    [installed  ] [2.17.0.redhat-630187 ] camel-jasypt                                  camel-2.17.0.redhat-630187
    [installed  ] [2.17.0.redhat-630187 ] camel-jaxb                                    camel-2.17.0.redhat-630187
    [installed  ] [2.17.0.redhat-630187 ] camel-jdbc                                    camel-2.17.0.redhat-630187
    [installed  ] [2.17.0.redhat-630187 ] camel-jms                                     camel-2.17.0.redhat-630187
    [installed  ] [2.17.0.redhat-630187 ] camel-jmx                                     camel-2.17.0.redhat-630187
    [installed  ] [2.17.0.redhat-630187 ] camel-mail                                    camel-2.17.0.redhat-630187
    [installed  ] [2.17.0.redhat-630187 ] camel-netty4                                  camel-2.17.0.redhat-630187
    [installed  ] [2.17.0.redhat-630187 ] camel-netty4-http                             camel-2.17.0.redhat-630187
    [installed  ] [2.17.0.redhat-630187 ] camel-ognl                                    camel-2.17.0.redhat-630187
    [installed  ] [2.17.0.redhat-630187 ] camel-paxlogging                              camel-2.17.0.redhat-630187
    [installed  ] [2.17.0.redhat-630187 ] camel-restlet                                 camel-2.17.0.redhat-630187
    [installed  ] [2.17.0.redhat-630187 ] camel-rmi                                     camel-2.17.0.redhat-630187
    [installed  ] [2.17.0.redhat-630187 ] camel-routebox                                camel-2.17.0.redhat-630187
    [installed  ] [2.17.0.redhat-630187 ] camel-saxon                                   camel-2.17.0.redhat-630187
    [installed  ] [2.17.0.redhat-630187 ] camel-script                                  camel-2.17.0.redhat-630187
    [installed  ] [2.17.0.redhat-630187 ] camel-snmp                                    camel-2.17.0.redhat-630187
    [installed  ] [2.17.0.redhat-630187 ] camel-spring-javaconfig                       camel-2.17.0.redhat-630187
    [installed  ] [2.17.0.redhat-630187 ] camel-xstream                                 camel-2.17.0.redhat-630187
    [installed  ] [1.2.0.redhat-630187  ] camel-amq                                     fabric-1.2.0.redhat-630187
    JBossFuse:karaf@root>