Łukasz
Łukasz

Reputation: 2162

How to add request header to BayeuxClient

  String url = "some_url";
  HttpClient httpClient = new HttpClient();
  httpClient.start();
  Map<String, Object> options = new HashMap<String, Object>();
  LongPollingTransport transport = new LongPollingTransport(options, httpClient);
  BayeuxClient client = new BayeuxClient(url, transport);
  client.getChannel(Channel.META_HANDSHAKE).addListener(new ClientSessionChannel.MessageListener() {
     public void onMessage(ClientSessionChannel channel, Message message) {
         System.out.println(message);
     }
  });
  client.handshake();

Getting

{"failure":{"exception":"org.cometd.common.TransportException: {httpCode=403}","message":{"supportedConnectionTypes":["long-polling"],"channel":"/meta/handshake","id":"2","version":"1.0"},"httpCode":403,"connectionType":"long-polling"},"channel":"/meta/handshake","id":"2","subscription":null,"successful":false}

So my first guess is to add authorization header. How can I do that? Jetty 9 is used for both the server and the client code libraries.

Upvotes: 0

Views: 1216

Answers (2)

Jason Fang
Jason Fang

Reputation: 1

LongPollingTransport was replaced by JettyHttpClientTransport starting with CometD 5. This is how to add authorization in JettyHttpClientTransport in CometD 8:

https://github.com/cometd/cometd/blob/6b24fff9c508a6d2d6ce4302897ad5e45af48e86/cometd-java/cometd-java-client/cometd-java-client-http/cometd-java-client-http-tests/src/test/java/org/cometd/client/http/HandshakeWithAuthenticationTest.java#L79C1-L87C11:

ClientTransport transport = new JettyHttpClientTransport(null, httpClient) {
    @Override
    protected void customize(Request request) {
        String authorization = userName + ":" + password;
        byte[] bytes = Base64.getEncoder().encode(authorization.getBytes(StandardCharsets.UTF_8));
        String encoded = new String(bytes, StandardCharsets.UTF_8);
        request.headers(headers -> headers.put("Authorization", "Basic " + encoded));
    }
};
    BayeuxClient client = new BayeuxClient(cometdURL, transport);

Upvotes: 0

sbordet
sbordet

Reputation: 18477

Please have a look at this test case that shows how to do it.

Upvotes: 1

Related Questions