/** @param securityHandler The {@link SecurityHandler} to set on this context. */
  public void setSecurityHandler(SecurityHandler securityHandler) {
    if (isStarted()) throw new IllegalStateException("STARTED");

    Handler next = null;
    if (_securityHandler != null) {
      next = _securityHandler.getHandler();
      _securityHandler.setHandler(null);
      replaceHandler(_securityHandler, securityHandler);
    }

    _securityHandler = securityHandler;
    if (next != null && _securityHandler.getHandler() == null) _securityHandler.setHandler(next);
    relinkHandlers();
  }
  public boolean declaredRolesContains(String roleName) {
    SecurityHandler security = SecurityHandler.getCurrentSecurityHandler();
    if (security == null) return false;

    if (security instanceof ConstraintAware) {
      Set<String> declaredRoles = ((ConstraintAware) security).getRoles();
      return (declaredRoles != null) && declaredRoles.contains(roleName);
    }

    return false;
  }
  protected void setupTest(ITestContext context) throws Exception {
    Reporter.log(
        String.format(
            "HTTP:%d, HTTPS:%d , HTTPS(Mutual):%d", PLAIN_PORT, SECURE_PORT, MUTUAL_SECURE_PORT),
        true);

    connectorServer = new Server();

    HttpConfiguration httpConfig = new HttpConfiguration();
    httpConfig.setSecureScheme("https");
    httpConfig.setSecurePort(SECURE_PORT);
    httpConfig.setOutputBufferSize(32768);

    // HTTP
    ServerConnector http =
        new ServerConnector(connectorServer, new HttpConnectionFactory(httpConfig));
    http.setPort(PLAIN_PORT);
    http.setHost("127.0.0.1");
    http.setIdleTimeout(30000);

    // HTTPS
    SslContextFactory sslContextFactory = createSsllContextFactory(false);

    // HTTPS Configuration
    HttpConfiguration httpsConfig = new HttpConfiguration(httpConfig);
    httpsConfig.addCustomizer(new SecureRequestCustomizer());

    // HTTPS connector
    ServerConnector https =
        new ServerConnector(
            connectorServer,
            new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()),
            new HttpConnectionFactory(httpsConfig));
    https.setPort(SECURE_PORT);
    http.setHost("127.0.0.1");
    https.setIdleTimeout(500000);

    // Mutual HTTPS connector
    sslContextFactory = createSsllContextFactory(false);
    sslContextFactory.setWantClientAuth(true);
    sslContextFactory.setNeedClientAuth(false);

    ServerConnector mutualHttps =
        new ServerConnector(
            connectorServer,
            new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()),
            new HttpConnectionFactory(httpsConfig));
    mutualHttps.setPort(MUTUAL_SECURE_PORT);
    http.setHost("127.0.0.1");
    mutualHttps.setIdleTimeout(500000);

    connectorServer.setConnectors(new Connector[] {http, https, mutualHttps});

    // Initializing the security handler
    ServletContextHandler handler =
        new ServletContextHandler(
            connectorServer, "/", ServletContextHandler.SESSIONS | ServletContextHandler.SECURITY);

    ServletHolder holder = handler.getServletHandler().newServletHolder(BaseHolder.Source.EMBEDDED);

    serverConnectorFramework = serverConnectorFrameworkFactory.acquire();
    localConnectorFramework = localConnectorFrameworkFactory.acquire();
    holder.setServlet(new OpenICFWebSocketServletBase(serverConnectorFrameworkFactory));
    holder.setInitParameter("maxIdleTime", "300000");
    holder.setInitParameter("maxAsyncWriteTimeout", "60000");

    holder.setInitParameter("maxBinaryMessageSize", "32768");
    holder.setInitParameter("inputBufferSize", "4096");

    handler.addServlet(holder, "/openicf/*");

    SecurityHandler sh = getSecurityHandler();
    sh.setHandler(handler);

    connectorServer.setHandler(sh);
    connectorServer.start();
    Reporter.log("Jetty Server Started", true);

    // Initialise the ConnectorFramework

    serverConnectorFramework
        .get()
        .getLocalManager()
        .addConnectorBundle(TstConnector.class.getProtectionDomain().getCodeSource().getLocation());

    localConnectorFramework
        .get()
        .getLocalManager()
        .addConnectorBundle(TstConnector.class.getProtectionDomain().getCodeSource().getLocation());

    connectorServer.start();
  }