I found the solution!

You can obtain a reference to the CXF CamelMessage context using
@Context org.apache.cxf.jaxrs.ext.MessageContext cxfMessage
As a parameter in your cxfbean.  From there, you can access the 
CamelHttpServletRequest using:
((HttpServletRequest)cxfMessage.get("CamelHttpServletRequest")).getRemoteAddr();

Crazy roundabout way to solve the problem, but I'm happy to have found an 
answer.




Zach Calvert


-----Original Message-----
From: Sergey Beryozkin [mailto:[email protected]] 
Sent: Tuesday, November 06, 2012 9:05 AM
To: [email protected]
Subject: Re: Obtain CXF Request?

On 06/11/12 14:53, Calvert, Zach (Zach)** CTR ** wrote:
> Unfortunately no, I'm stuck in 2.6.0.  Are there any other options exposed 
> for obtaining caller's IP address?
>
> Thank you again, your help is very much appreciated!
>
It's a question to Camel experts at this stage :-)

Cheers, Sergey

>
> Thanks,
> Zach
>
>
> -----Original Message-----
> From: Sergey Beryozkin [mailto:[email protected]]
> Sent: Tuesday, November 06, 2012 8:52 AM
> To: [email protected]
> Subject: Re: Obtain CXF Request?
>
> On 06/11/12 14:40, Calvert, Zach (Zach)** CTR ** wrote:
>> Thank you for your reply Sergey, unfortunately while the context is 
>> populated, I get:
>>
>> java.lang.NullPointerException
>>           at
>> org.apache.cxf.jaxrs.impl.tl.ThreadLocalHttpServletRequest.getRemoteA
>> d
>> dr(ThreadLocalHttpServletRequest.java:223)[119:org.apache.cxf.bundle:
>> 2
>> .3.2]
>> ...
>>
>> So I have a handle to the ThreadLocalHttpServletRequest, but the "get" 
>> function isn't being populated.  I've walked the code and the NPE in 
>> ThreadLocalHttpServletRequest at 223 corresponds to the function
>>       public String getRemoteAddr() {
>>           return get().getRemoteAddr();
>>       }
>>
>> Get is exposed from AbstractThreadLocalProxy:
>> public T get() {
>>           return infos.get();
>>       }
>>
>> Is there something I need to do to force my context to be populated?
>
> I think HttpServletRequest has to be visible to CXF endpoints starting from 
> Camel 2.10.2, as well the as the latest 2.9.x. Are you working with the older 
> version ?
>
> Cheers, Sergey
>
>>
>> Thank you again!
>>
>>
>>
>> Zach Calvert
>>
>>
>> -----Original Message-----
>> From: Sergey Beryozkin [mailto:[email protected]]
>> Sent: Tuesday, November 06, 2012 8:13 AM
>> To: [email protected]
>> Subject: Re: Obtain CXF Request?
>>
>>
>> Hi
>> On 06/11/12 14:02, Calvert, Zach (Zach)** CTR ** wrote:
>>> I am stuck using Camel CXF 2.6.0 (yes, I know I'm behind) and need to get 
>>> the IP address of the remote system making an HTTP POST to my cxfbean:
>>>            <!-- Define the camel route -->
>>>            <camel:route id="restApis">
>>>                <camel:from 
>>> uri="jetty:{{protocol}}://0.0.0.0:{{port}}/rest/?matchOnUriPrefix=true&amp;handlers=securityHandler"/>
>>>                <camel:to 
>>> uri="cxfbean:restfulService?providers=#restExceptionMapper" />
>>>                <camel:log message="done"/>
>>>            </camel:route>
>>>
>>> My restfulService handles the posts.  When I turn on tracing, I see
>>>
>>> 07:10:29,783 | INFO  | qtp26579196-144  | Tracer                           
>>> | 76 - org.apache.camel.camel-core - 2.6.0 | 
>>> ID-centora-45480-1352207331927-4-1>>>    (restApis) 
>>> from(https://0.0.0.0:9091/rest/?matchOnUriPrefix=true&handlers=securityHandler)
>>>  -->    restfulService<<<    Pattern:InOut, Headers:{Host=test.com:9091, 
>>> CamelHttpMethod=POST, CamelHttpServletRequest=[POST 
>>> /rest/domain/feed]@19570394 org.eclipse.jetty.server.Request@12a9eda, 
>>> Accept-Encoding=gzip,deflate, CamelHttpServletResponse=HTTP/1.1 200
>>>
>>> In my bean I have the function
>>>        @POST
>>>        @Path("{domainName}/{domainChild}")
>>>        @Consumes({MediaType.APPLICATION_RESTMS_XML, 
>>> MediaType.APPLICATION_RESTMS_JSON, MediaType.APPLICATION_XML, 
>>> MediaType.APPLICATION_JSON})
>>>        @Produces({MediaType.APPLICATION_RESTMS_XML, 
>>> MediaType.APPLICATION_RESTMS_JSON, MediaType.APPLICATION_XML, 
>>> MediaType.APPLICATION_JSON})
>>>        public Response post(@PathParam("domainName") String domainName,
>>>                @PathParam("domainChild") String domainChildName,
>>>                JAXBElement<RestXml>    restXml) { ...
>>> }
>>>
>>> I have tried to add the parameter Exchange (camel), Exchange (cxf), 
>>> Message, etc all to no avail due to no longer having a mapping for my 
>>> sample test post.  I want to know if there is a way for me to access that 
>>> header "CamelHttpServletRequest" inside of this bean's function?  My end 
>>> goal is simply to get the HttpRequest or ServletRequest or whatever request 
>>> that will have the "getRemoteAddress" function exposed on it.  I've been 
>>> through the jaxrs core annotations and cannot seem to figure out how to get 
>>> a handle to the original incoming request, or any context object that may 
>>> contain the IP address of whoever is making the call to my servlet.  I 
>>> thought maybe UriInfo or Request (from javax.ws.rs.core) contexts would 
>>> have the info I'm looking for, but if they do, I can't figure them out.
>>>
>>> I have also looked at building a custom provider, but I can't figure out 
>>> how to build one that not only gets the IP address, but manages to provide 
>>> it along the route to my cxfbean.  Is there a way I can do something like 
>>> get the IP and then set it as a QueryParam for my bean to consume?
>>>
>>> Any and all help would be greatly appreciated!
>>>
>>
>> Injection "@Context HttpServletRequest" into the bean itself is one 
>> option. Another one is to inject it into a RequestFilter provider and 
>> update (CXF) Message.QUERY_STRING accordingly - this will let you add
>> @QueryParam("remoteip") to the method signature
>>
>> HTH, Sergey
>>
>>>
>>> Thanks,
>>> Zach Calvert
>>>
>>
>>
>> --
>> Sergey Beryozkin
>>
>> Talend Community Coders
>> http://coders.talend.com/
>>
>> Blog: http://sberyozkin.blogspot.com


Reply via email to