Exemple #1
0
  /**
   * Client-side handshake.
   *
   * @param webSocketRequest request representation to be modified for use as WebSocket handshake
   *     request.
   * @return handshake instance.
   */
  public static Handshake createClientHandshake(RequestContext webSocketRequest) {
    final Handshake handshake = new Handshake();
    handshake.request = webSocketRequest;
    handshake.secKey = new SecKey();

    return handshake;
  }
Exemple #2
0
  /**
   * Server-side handshake.
   *
   * @param request received handshake request.
   * @param extensionContext extension context.
   * @return created handshake.
   * @throws HandshakeException when there is problem with received {@link UpgradeRequest}.
   */
  static Handshake createServerHandshake(
      UpgradeRequest request, ExtendedExtension.ExtensionContext extensionContext)
      throws HandshakeException {
    final Handshake handshake = new Handshake();

    handshake.incomingRequest = request;
    handshake.extensionContext = extensionContext;
    checkForHeader(request.getHeader(UpgradeRequest.UPGRADE), UpgradeRequest.UPGRADE, "WebSocket");
    checkForHeader(
        request.getHeader(UpgradeRequest.CONNECTION),
        UpgradeRequest.CONNECTION,
        UpgradeRequest.UPGRADE);

    // TODO - trim?
    final String protocolHeader = request.getHeader(HandshakeRequest.SEC_WEBSOCKET_PROTOCOL);
    handshake.subProtocols =
        (protocolHeader == null
            ? Collections.<String>emptyList()
            : Arrays.asList(protocolHeader.split(",")));

    if (request.getHeader(UpgradeRequest.HOST) == null) {
      throw new HandshakeException(LocalizationMessages.HEADERS_MISSING());
    }

    //        final String queryString = request.getQueryString();
    //        if (queryString != null) {
    //            if (!queryString.isEmpty()) {
    //            }
    ////            Parameters queryParameters = new Parameters();
    ////            queryParameters.processParameters(queryString);
    ////            final Set<String> names = queryParameters.getParameterNames();
    ////            for (String name : names) {
    ////                queryParams.put(name, queryParameters.getParameterValues(name));
    ////            }
    //        }

    List<String> value = request.getHeaders().get(HandshakeRequest.SEC_WEBSOCKET_EXTENSIONS);
    if (value != null) {
      handshake.extensions = TyrusExtension.fromHeaders(value);
    }
    handshake.secKey =
        SecKey.generateServerKey(new SecKey(request.getHeader(HandshakeRequest.SEC_WEBSOCKET_KEY)));

    return handshake;
  }