/**
   * Checks application specific criteria to determine if this application can process the request
   * as a WebSocket connection.
   *
   * @param request the incoming HTTP request.
   * @return <code>true</code> if this application can service this request
   */
  protected boolean isApplicationRequest(UpgradeRequest request) {
    final List<String> protocols = request.getHeaders().get(UpgradeRequest.SEC_WEBSOCKET_PROTOCOL);
    temporaryNegotiatedProtocol = endpoint.getNegotiatedProtocol(protocols);

    final List<Extension> extensions =
        TyrusExtension.fromString(
            request.getHeaders().get(UpgradeRequest.SEC_WEBSOCKET_EXTENSIONS));
    temporaryNegotiatedExtensions = endpoint.getNegotiatedExtensions(extensions);

    return endpoint.checkHandshake(request);
  }
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;
  }
Exemple #3
0
  // server side
  List<Extension> respond(
      UpgradeRequest request, UpgradeResponse response, TyrusEndpointWrapper endpointWrapper
      /*,TyrusUpgradeResponse response*/ ) {
    response.setStatus(101);

    response.getHeaders().put(UpgradeRequest.UPGRADE, Arrays.asList(UpgradeRequest.WEBSOCKET));
    response.getHeaders().put(UpgradeRequest.CONNECTION, Arrays.asList(UpgradeRequest.UPGRADE));
    response.setReasonPhrase(UpgradeRequest.RESPONSE_CODE_MESSAGE);
    response
        .getHeaders()
        .put(HandshakeResponse.SEC_WEBSOCKET_ACCEPT, Arrays.asList(secKey.getSecKey()));

    final List<String> protocols =
        request.getHeaders().get(HandshakeRequest.SEC_WEBSOCKET_PROTOCOL);
    final List<Extension> extensions =
        TyrusExtension.fromString(
            request.getHeaders().get(HandshakeRequest.SEC_WEBSOCKET_EXTENSIONS));

    if (subProtocols != null && !subProtocols.isEmpty()) {
      String protocol = endpointWrapper.getNegotiatedProtocol(protocols);
      if (protocol != null && !protocol.isEmpty()) {
        response.getHeaders().put(HandshakeRequest.SEC_WEBSOCKET_PROTOCOL, Arrays.asList(protocol));
      }
    }

    final List<Extension> negotiatedExtensions =
        endpointWrapper.getNegotiatedExtensions(extensions);
    if (!negotiatedExtensions.isEmpty()) {
      response
          .getHeaders()
          .put(
              HandshakeRequest.SEC_WEBSOCKET_EXTENSIONS,
              Utils.getStringList(
                  negotiatedExtensions,
                  new Utils.Stringifier<Extension>() {
                    @Override
                    String toString(final Extension extension) {
                      if (extension instanceof ExtendedExtension) {
                        return TyrusExtension.toString(
                            new Extension() {
                              @Override
                              public String getName() {
                                return extension.getName();
                              }

                              @Override
                              public List<Parameter> getParameters() {
                                // TODO! XXX FIXME
                                // null is there because extension is wrapped and the
                                // original parameters are stored
                                // in the wrapped instance.
                                return ((ExtendedExtension) extension)
                                    .onExtensionNegotiation(extensionContext, null);
                              }
                            });
                      } else {
                        return TyrusExtension.toString(extension);
                      }
                    }
                  }));
    }
    endpointWrapper.onHandShakeResponse(incomingRequest, response);

    return negotiatedExtensions;
  }