@Override
  public Object createWebSocket(final JettyServerUpgradeRequest request, final JettyServerUpgradeResponse response) {
    try {
      Optional<WebSocketAuthenticator<T>> authenticator = Optional.ofNullable(environment.getAuthenticator());

      final ReusableAuth<T> authenticated;
      if (authenticator.isPresent()) {
        authenticated = authenticator.get().authenticate(request);

        if (authenticated.invalidCredentialsProvided()) {
          response.sendForbidden("Unauthorized");
          return null;
        }
      } else {
        authenticated = ReusableAuth.anonymous();
      }

      return new WebSocketResourceProvider<>(getRemoteAddress(request),
          remoteAddressPropertyName,
          this.jerseyApplicationHandler,
          this.environment.getRequestLog(),
          authenticated,
          this.environment.getMessageFactory(),
          ofNullable(this.environment.getConnectListener()),
          this.environment.getIdleTimeout());
    } catch (AuthenticationException | IOException e) {
      logger.warn("Authentication failure", e);
      try {
        response.sendError(500, "Failure");
      } catch (IOException ignored) {
      }
      return null;
    }
  }
@Test
  void testErrorAuthorization() throws AuthenticationException, IOException {
    when(environment.getAuthenticator()).thenReturn(authenticator);
    when(authenticator.authenticate(eq(request))).thenThrow(new AuthenticationException("database failure"));
    when(environment.jersey()).thenReturn(jerseyEnvironment);

    WebSocketResourceProviderFactory<Account> factory = new WebSocketResourceProviderFactory<>(environment,
        Account.class,
        mock(WebSocketConfiguration.class),
        REMOTE_ADDRESS_PROPERTY_NAME);
    Object connection = factory.createWebSocket(request, response);

    assertNull(connection);
    verify(response).sendError(eq(500), eq("Failure"));
    verify(authenticator).authenticate(eq(request));
  }