Example #1
0
 private IoHandler selectReadHandler(ResourceAddress readAddress) {
   Protocol protocol =
       bridgeServiceFactory.getTransportFactory().getProtocol(readAddress.getResource());
   if (protocol instanceof HttpProtocol) {
     return readHandler;
   }
   throw new RuntimeException("Cannot select read handler for address " + readAddress);
 }
Example #2
0
 private IoHandler selectConnectHandler(ResourceAddress address) {
   Protocol protocol =
       bridgeServiceFactory.getTransportFactory().getProtocol(address.getResource());
   if (protocol instanceof HttpProtocol) {
     return createHandler;
   }
   throw new RuntimeException(
       getClass() + ": Cannot select a connect handler for address " + address);
 }
 private URI locateSecureAcceptURI(HttpAcceptSession session) throws Exception {
   // TODO: same-origin requests must consider cross-origin access control
   //       internal redirect to secure resource should not trigger 403 Forbidden
   ResourceAddress localAddress = session.getLocalAddress();
   URI resource = localAddress.getResource();
   Protocol resourceProtocol = bridgeServiceFactory.getTransportFactory().getProtocol(resource);
   if (WsebProtocol.WSEB_SSL == resourceProtocol || WsProtocol.WSS == resourceProtocol) {
     return resource;
   }
   return null;
 }
Example #4
0
  private <T extends ConnectFuture> IoSessionInitializer<ConnectFuture> createParentInitializer(
      final ResourceAddress connectAddressNext,
      final IoHandler handler,
      final IoSessionInitializer<T> initializer,
      final DefaultConnectFuture wseConnectFuture) {

    final ResourceAddress connectAddress = connectAddressNext.getTransport();
    Protocol protocol =
        bridgeServiceFactory.getTransportFactory().getProtocol(connectAddress.getResource());
    if (!(protocol instanceof HttpProtocol)) {
      final String message =
          format(
              "Cannot create WSEB parent session initializer for address %s", connectAddressNext);
      if (logger.isInfoEnabled()) {
        logger.info(message);
      }
      throw new RuntimeException(message);
    }
    // initialize parent session before connection attempt
    return new IoSessionInitializer<ConnectFuture>() {
      @Override
      public void initializeSession(final IoSession parent, ConnectFuture future) {
        // initializer for bridge session to specify bridge handler,
        // and call user-defined bridge session initializer if present
        final IoSessionInitializer<T> wseSessionInitializer =
            new IoSessionInitializer<T>() {
              @Override
              public void initializeSession(IoSession session, T future) {
                WsebSession wseSession = (WsebSession) session;
                wseSession.setHandler(handler);

                // TODO: add extension filters when we adopt the new webSocket extension SPI
                wseSession.getTransportSession().getFilterChain().fireSessionCreated();
                wseSession.getTransportSession().getFilterChain().fireSessionOpened();

                if (initializer != null) {
                  initializer.initializeSession(session, future);
                }
              }
            };

        final long sequenceNo = 0;

        final HttpSession httpSession = (HttpSession) parent;
        httpSession.setWriteHeader(HEADER_X_ACCEPT_COMMANDS, "ping");
        httpSession.setWriteHeader(HttpHeaders.HEADER_X_SEQUENCE_NO, Long.toString(sequenceNo));
        final IoBufferAllocatorEx<WsBuffer> allocator =
            new WsebBufferAllocator(httpSession.getBufferAllocator());

        // factory to create a new bridge session
        Callable<WsebSession> createSession =
            new Callable<WsebSession>() {
              @Override
              public WsebSession call() throws Exception {

                Callable<WsebSession> wseSessionFactory =
                    new Callable<WsebSession>() {
                      @Override
                      public WsebSession call() throws Exception {
                        WsebSession wsebSession =
                            new WsebSession(
                                httpSession.getIoLayer(),
                                httpSession.getIoThread(),
                                httpSession.getIoExecutor(),
                                WsebConnector.this,
                                getProcessor(),
                                connectAddressNext,
                                connectAddressNext,
                                allocator,
                                null,
                                0,
                                connectAddressNext.getOption(INACTIVITY_TIMEOUT),
                                false, /* no sequence validation */
                                sequenceNo, /* starting sequence no */
                                null);

                        // ability to write will be reactivated when create response returns with
                        // write address
                        wsebSession.suspendWrite();

                        return wsebSession;
                      }
                    };

                return newSession(wseSessionInitializer, wseConnectFuture, wseSessionFactory);
              }
            };

        WSE_SESSION_FACTORY_KEY.set(httpSession, createSession);
        WSE_CONNECT_FUTURE_KEY.set(httpSession, wseConnectFuture);
      }
    };
  }