示例#1
0
  /**
   * 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);
  }
示例#2
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;
  }