Ejemplo n.º 1
0
 /**
  * Checks protocol specific information can and should be upgraded.
  *
  * <p>The default implementation will check for the presence of the <code>Upgrade</code> header
  * with a value of <code>WebSocket</code>. If present, {@link
  * #isApplicationRequest(org.glassfish.tyrus.spi.UpgradeRequest)} will be invoked to determine if
  * the request is a valid websocket request.
  *
  * @param request TODO
  * @return <code>true</code> if the request should be upgraded to a WebSocket connection
  */
 public final boolean upgrade(UpgradeRequest request) {
   final String upgradeHeader = request.getHeader(UpgradeRequest.UPGRADE);
   return request.getHeaders().get(UpgradeRequest.UPGRADE) != null
       &&
       // RFC 6455, paragraph 4.2.1.3
       UpgradeRequest.WEBSOCKET.equalsIgnoreCase(upgradeHeader)
       && isApplicationRequest(request);
 }
Ejemplo n.º 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;
  }