View Javadoc
1   // Copyright (c) Microsoft Corporation. All rights reserved.
2   // Licensed under the MIT License.
3   
4   package com.azure.messaging.eventhubs.implementation.handler;
5   
6   import com.azure.core.util.logging.ClientLogger;
7   import com.microsoft.azure.proton.transport.ws.impl.WebSocketImpl;
8   import org.apache.qpid.proton.engine.Event;
9   import org.apache.qpid.proton.engine.impl.TransportInternal;
10  
11  /**
12   * Creates an AMQP connection using web sockets (port 443).
13   */
14  public class WebSocketsConnectionHandler extends ConnectionHandler {
15      static final int HTTPS_PORT = 443;
16  
17      // This is the current limitation of https://github.com/Azure/qpid-proton-j-extensions.
18      // Once this library enables larger frames - this property can be removed.
19      static final int MAX_FRAME_SIZE =  4 * 1024;
20  
21      private static final String SOCKET_PATH = "/$servicebus/websocket";
22      private static final String PROTOCOL = "AMQPWSB10";
23  
24      /**
25       * Creates a handler that handles proton-j's connection events using web sockets.
26       *
27       * @param connectionId Identifier for this connection.
28       * @param hostname Hostname to use for socket creation. If there is a proxy configured, this could be a
29       *         proxy's IP address.
30       */
31      public WebSocketsConnectionHandler(final String connectionId, final String hostname) {
32          super(connectionId, hostname, new ClientLogger(WebSocketsConnectionHandler.class));
33      }
34  
35      @Override
36      protected void addTransportLayers(final Event event, final TransportInternal transport) {
37          final String hostName = event.getConnection().getHostname();
38  
39          final WebSocketImpl webSocket = new WebSocketImpl();
40          webSocket.configure(
41              hostName,
42              SOCKET_PATH,
43              "",
44              0,
45              PROTOCOL,
46              null,
47              null);
48  
49          transport.addTransportLayer(webSocket);
50  
51          logger.verbose("Adding web sockets transport layer for hostname[{}]", hostName);
52  
53          super.addTransportLayers(event, transport);
54      }
55  
56      @Override
57      public int getProtocolPort() {
58          return HTTPS_PORT;
59      }
60  
61      @Override
62      public int getMaxFrameSize() {
63          return MAX_FRAME_SIZE;
64      }
65  }