Re: [cometd-users] Cometd Java Request Headers

142 views
Skip to first unread message

Simone Bordet

unread,
May 23, 2012, 3:08:48 AM5/23/12
to cometd...@googlegroups.com
Hi,

On Wed, May 23, 2012 at 5:33 AM, Jeff Nuss <jeff...@gmail.com> wrote:
> I've looked all over and haven't been able to find a clear answer to this
> question. Using the Java cometd library (not javascript), how can I add a
> request header to each Bayuex message that gets sent? In the javascript
> library, you can use cometd.config and pass it the configuration params as
> seen here: http://cometd.org/documentation/cometd-javascript/configuration
> Also, this post mentions overriding the customize method on
> LongPollingTransport:
> https://groups.google.com/forum/?fromgroups#!topic/cometd-users/-Mvdu1wOFq4
> However, it doesn't seem apparent to me how overriding this method helps.
>
> protected void customize(ContentExchange arg0) {
>
> arg0.addRequestHeader(Constants.MSB_HEADER_SESSIONID, sessionId);
>
> // super.customize(arg0);
>
> // }

That is exactly the way to do it.
You have to call super.customize() so that cookies are handled
properly, then you can add the headers you want.

Simon
--
http://cometd.org
http://intalio.com
http://bordet.blogspot.com
----
Finally, no matter how good the architecture and design are,
to deliver bug-free software with optimal performance and reliability,
the implementation technique must be flawless.   Victoria Livschitz

Jeff Nuss

unread,
May 23, 2012, 8:33:25 PM5/23/12
to cometd...@googlegroups.com
So here's the class that I create:

private static class CustomTransport extends LongPollingTransport {
   public CustomTransport(Map<String, Object> options, HttpClient httpClient) {
      super(options, httpClient);
      customize(new ContentExchange()); 
   }

   @Override
   protected void customize(ContentExchange arg0) {
      super.customize(arg0);
      arg0.addRequestHeader(HEADER_SESSIONID, sessionId);
   }
}

Then I call:

client = new BayeuxClient(cometdServerAddress, CustomTransport.create(null));

Then when I call: 
client.handshake()

Or: 
client.getChannel(playerSubscriptionChannel).publish(statusData);

I sniff the packets and I'm not seeing the header anywhere. I see the requests (for the handshake and the long poll, for example): 

POST /cometd/handshake HTTP/1.1
Content-Type: application/json;charset=UTF-8
Host: www. cometdserver.com
Content-Length: 152

POST /cometd/connect HTTP/1.1
Content-Type: application/json;charset=UTF-8
Content-Length: 148

But no custom header.

Am I missing a step? Do you have any sample code where you've added a custom header? Thanks.

Austyn Mahoney

unread,
May 24, 2012, 12:29:32 AM5/24/12
to cometd...@googlegroups.com
I found a solution to this problem so I figured I'd share it.

Instead of using the factory create method on the static class, I created a jetty HttpClient and then used that when creating a LongPollingTransport object. That object was then used to initialize a BayeuxClient. The header then showed up in our request.

HttpClient httpClient = new HttpClient();
        httpClient.setConnectTimeout(TIMEOUT);
        httpClient.setTimeout(TIMEOUT);
        try {
            httpClient.start();
        } catch (Exception e) {
            logger.e(e, "Unable to start httpClient");
        }
        LongPollingTransport transport = new LongPollingTransport(null, httpClient) {

            @Override
            protected void customize(ContentExchange exchange) {

                super.customize(exchange);

                exchange.addRequestHeader(HEADER_KEY, HEADER_VALUE);

            }

        };

client = new BayeuxClient(serverAddress, transport);

Simone Bordet

unread,
May 24, 2012, 2:41:43 AM5/24/12
to cometd...@googlegroups.com
Hi,

On Thu, May 24, 2012 at 2:33 AM, Jeff Nuss <jeff...@gmail.com> wrote:
> So here's the class that I create:
>
> private static class CustomTransport extends LongPollingTransport {
>    public CustomTransport(Map<String, Object> options, HttpClient
> httpClient) {
>       super(options, httpClient);
>       customize(new ContentExchange());
>    }
>
>    @Override
>    protected void customize(ContentExchange arg0) {
>       super.customize(arg0);
>       arg0.addRequestHeader(HEADER_SESSIONID, sessionId);
>    }
> }

So far so good.

> Then I call:
>
> client = new BayeuxClient(cometdServerAddress,
> CustomTransport.create(null));

Ugh, no.
Calling the static method will create - of course - an instance of
LongPollingTransport, not of your subclass.
Please have a look at
http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

You should do this:

Map<String, Object> options = new HashMap<>();
HttpClient httpClient = new HttpClient();
BayeuxClient client = new BayeuxClient(serverAddress, new
CustomTransport(options, httpClient));
Reply all
Reply to author
Forward
Message has been deleted
0 new messages