Fantastic, thank you Cedric this is exactly what I needed! I was able to
successfully recode this in scala, and have included the stub code below
for anyone else looking for the scala solution in the future as well.
object RemoteAddressConsumer {
def apply(): Props = Props(classOf[RemoteAddressConsumer])
class RemoteAddressProcessor extends Processor {
import javax.servlet.http.HttpServletRequest
override def process(exchange: Exchange) {
val req: HttpServletRequest =
exchange.getIn.getBody(classOf[HttpServletRequest]);
if(req!=null)
exchange.getIn.setHeader("remoteAddr",req.getRemoteAddr());
}
}
val mapper = new Mapper[RouteDefinition, ProcessorDefinition[_]]() {
override def apply(rd: RouteDefinition): ProcessorDefinition[_] =
rd.bean(classOf[RemoteAddressProcessor])
}
}
class RemoteAddressConsumer extends Consumer {
def endpointUri = "jetty:..."
override def getRouteDefinitionHandler = RemoteAddressConsumer.mapper
def receive = {
case msg: CamelMessage => {
println("Message received from
%s".format(msg.headers("remoteAddr")))
}
}
}
On Tuesday, January 7, 2014 12:57:02 AM UTC-8, Cédric Sougné wrote:
>
> You'll have to create a camel Processor that will fill the exchange
> sourceAddr header from httpRequest:
>
> public class SimpleProcessor implements Processor{
>
> @Override
> public void process(Exchange exchange) {
> Message inMsg = exchange.getIn();
>
> // extract the Jetty HttpServletRequest, from there we can extract
> // the remote client address.
> HttpServletRequest req =
> exchange.getIn().getBody(HttpServletRequest.class);
> if (req != null) {
> String remoteAddr = req.getRemoteAddr();
> int remotePort = req.getRemotePort();
> System.out.println("Client called from " + remoteAddr + ":" +
> remotePort);
> exchange.getIn().setHeader("sourceAddr", remoteAddr);
> } else {
> //System.out.println("Could not extract client
> address!</body></html>");
> }
> }
> }
>
>
> this class is from an exemple from
> https://github.com/tmielke/fuse-demos/blob/master/Camel/Camel-Jetty-Client-IP/src/main/java/org/apache/camel/test/SimpleProcessor.java
> but without setting reply body as my consumer actor will handle this.
>
> then akka documentation will help you to intercept route
> construction<http://doc.akka.io/docs/akka/snapshot/java/camel.html#Intercepting_route_construction>
> :
>
> concretely, in your UntypedConsumerActor:
>
>
> @Override
> public Mapper<RouteDefinition, ProcessorDefinition<?>>
> getRouteDefinitionHandler() {
> return mapper; //To change body of generated methods, choose Tools
> | Templates.
>
> }
>
>
> private static Mapper<RouteDefinition, ProcessorDefinition<?>> mapper
> = new Mapper<RouteDefinition, ProcessorDefinition<?>>() {
> @Override
> public ProcessorDefinition<?> apply(RouteDefinition rd) {
> return
> rd.bean(org.acis_group.alec.system.utils.SimpleProcessor.class);
> }
> };
>
>
> then you'll be able to axtract Ip from camel message this way:
>
> camelMessage.getHeaderAs("sourceAddr", String.class, getCamelContext());
>
>
> I hope this will help
>
> Le lundi 6 janvier 2014 20:48:46 UTC+1, Daniel Collins a écrit :
>>
>> Hey guys,
>>
>> Sorry to open this thread again, but I have been trying to accomplish the
>> same thing (Jetty), but haven't had any luck. I have read through this
>> <http://doc.akka.io/docs/akka/snapshot/scala/camel.html>but still feel
>> quite stranded. Almost all documentation outside that page is for
>> direct-java or DSL implementations, whereas with akka you extend the
>> Consumer class, define your endpoint, and you're good to go. I'm afraid to
>> start blindly hacking my way to raw camel objects because I don't know how
>> Akka implements the underlying camel objects and am nervous I will start
>> hardwiring to stuff I don't completely understand or creating duplicate
>> instances or other unintended consequences that will give me more problems
>> than solutions.
>>
>> Up to this point I have handles to my JettyHttpComponent and CamelContext
>> in my Consumer actor constructor. Cedric mentioned a custom Processor, so I
>> am guessing I can get the client IP from the Exchange object somehow. If I
>> were to figure that out, I'm not sure how I would wire that into my
>> consumer actor. Would I define it in my endpoint string which would then
>> point to the actual definition elsewhere, or do I somehow override a
>> consumer method? I haven't found any methods in either the
>> JettyHttpComponent/CamelContext objects that I think would do the trick.
>>
>> Any additional hints or musings would be greatly appreciated!
>>
>> On Thursday, November 14, 2013 2:31:55 AM UTC-8, √ wrote:
>>>
>>> Happy hAkking!
>>>
>>>
>>> On Thu, Nov 14, 2013 at 11:29 AM, Cédric Sougné <[email protected]>wrote:
>>>
>>>> finally got it by adding a Processor to the camel RouteDefinition. the
>>>> Processor add a header to the camelmessage with source address.
>>>>
>>>> Thanks for RTFM reminder :-)
>>>>
>>>> Le mercredi 13 novembre 2013 15:22:35 UTC+1, Cédric Sougné a écrit :
>>>>
>>>>> I see nothing about that on the camel-jetty component page but
>>>>> googling telled me that I should be able to retrieve Jetty's
>>>>> HttpServletRequest object. This contains the complete client address
>>>>> information. The question is how to retrieve this from the consumer actor?
>>>>>
>>>>> Le mercredi 13 novembre 2013 14:13:05 UTC+1, √ a écrit :
>>>>>>
>>>>>> What does the Camel Jetty component documentation say?
>>>>>>
>>>>>>
>>>>>> On Wed, Nov 13, 2013 at 1:36 PM, Cédric Sougné <[email protected]>wrote:
>>>>>>
>>>>>>> Hi,
>>>>>>>
>>>>>>> I'm trying to get client ip address using akka-camel consumer actor
>>>>>>> and the jetty:http: camel component.
>>>>>>>
>>>>>>> Inspecting headers, I've found a _remoteaddr from
>>>>>>> camelHttpServletRequest but value is null.
>>>>>>>
>>>>>>> Thanks for your suggestions.
>>>>>>>
>>>>>>> Best regards,
>>>>>>>
>>>>>>> Cédric
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> --
>>>>>>> >>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>> >>>>>>>>>> Check the FAQ: http://akka.io/faq/
>>>>>>> >>>>>>>>>> Search the archives: https://groups.google.com/
>>>>>>> group/akka-user
>>>>>>> ---
>>>>>>> You received this message because you are subscribed to the Google
>>>>>>> Groups "Akka User List" group.
>>>>>>> To unsubscribe from this group and stop receiving emails from it,
>>>>>>> send an email to [email protected].
>>>>>>> To post to this group, send email to [email protected].
>>>>>>> Visit this group at http://groups.google.com/group/akka-user.
>>>>>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> --
>>>>>> Cheers,
>>>>>> √
>>>>>>
>>>>>> * Viktor Klang*
>>>>>> *Director of Engineering*
>>>>>> Typesafe <http://www.typesafe.com/>
>>>>>>
>>>>>> Twitter: @viktorklang
>>>>>>
>>>>> --
>>>> >>>>>>>>>> Read the docs: http://akka.io/docs/
>>>> >>>>>>>>>> Check the FAQ: http://akka.io/faq/
>>>> >>>>>>>>>> Search the archives:
>>>> https://groups.google.com/group/akka-user
>>>> ---
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Akka User List" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to [email protected].
>>>> To post to this group, send email to [email protected].
>>>> Visit this group at http://groups.google.com/group/akka-user.
>>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>>
>>>
>>>
>>>
>>> --
>>> Cheers,
>>> √
>>>
>>> * Viktor Klang*
>>> *Director of Engineering*
>>> Typesafe <http://www.typesafe.com/>
>>>
>>> Twitter: @viktorklang
>>>
>>
--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://akka.io/faq/
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/groups/opt_out.