private static void handleUnsupportedVersion(
     final UpgradeRequest request, UpgradeResponse response) {
   response.setStatus(426);
   response
       .getHeaders()
       .put(
           UpgradeRequest.SEC_WEBSOCKET_VERSION,
           Arrays.asList(Version.getSupportedWireProtocolVersions()));
 }
  @Override
  public UpgradeInfo upgrade(final UpgradeRequest request, final UpgradeResponse response) {

    try {
      final TyrusEndpoint endpoint = getEndpoint(request);
      if (endpoint != null) {
        final ProtocolHandler protocolHandler = loadHandler(request);
        if (protocolHandler == null) {
          handleUnsupportedVersion(request, response);
          return HANDSHAKE_FAILED_UPGRADE_INFO;
        }

        final ExtendedExtension.ExtensionContext extensionContext =
            new ExtendedExtension.ExtensionContext() {

              private final Map<String, Object> properties = new HashMap<String, Object>();

              @Override
              public Map<String, Object> getProperties() {
                return properties;
              }
            };

        protocolHandler.handshake(endpoint, request, response, extensionContext);
        return new SuccessfulUpgradeInfo(
            endpoint, protocolHandler, incomingBufferSize, request, extensionContext);
      }
    } catch (HandshakeException e) {
      LOGGER.log(Level.SEVERE, e.getMessage(), e);
      response.setStatus(e.getCode());
      return HANDSHAKE_FAILED_UPGRADE_INFO;
    }

    response.setStatus(500);
    return NOT_APPLICABLE_UPGRADE_INFO;
  }
예제 #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;
  }