Reputation: 2162
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
Reputation: 1
LongPollingTransport was replaced by JettyHttpClientTransport starting with CometD 5. This is how to add authorization in JettyHttpClientTransport in CometD 8:
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