package org.apache.helix.rest.server;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.util.Collections;
import java.util.List;
import org.apache.helix.HelixException;
import org.apache.helix.rest.common.ContextPropertyKeys;
import org.apache.helix.rest.server.auditlog.AuditLogger;
import org.apache.helix.rest.server.filters.AuditLogFilter;
import org.apache.helix.rest.server.filters.CORSFilter;
import org.apache.helix.rest.server.resources.AbstractResource;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.servlet.ServletContainer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HelixRestServer extends ResourceConfig {
  private static Logger LOG = LoggerFactory.getLogger(HelixRestServer.class);

  private int _port;
  private String _urlPrefix;
  private Server _server;
  private ServerContext _serverContext;

  public HelixRestServer(String zkAddr, int port, String urlPrefix, List<AuditLogger> auditLoggers) {
    _port = port;
    _urlPrefix = urlPrefix;

    packages(AbstractResource.class.getPackage().getName());

    _serverContext = new ServerContext(zkAddr);
    property(ContextPropertyKeys.SERVER_CONTEXT.name(), _serverContext);

    register(new CORSFilter());
    register(new AuditLogFilter(auditLoggers));

    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
      @Override public void run() {
        shutdown();
      }
    }));
  }

  public HelixRestServer(String zkAddr, int port, String urlPrefix) {
    this(zkAddr, port, urlPrefix, Collections.<AuditLogger>emptyList());
  }

  public void start() throws HelixException, InterruptedException {
    _server = new Server(_port);
    ServletHolder servlet = new ServletHolder(new ServletContainer(this));
    ServletContextHandler contextHandler = new ServletContextHandler(_server, _urlPrefix);
    contextHandler.addServlet(servlet, "/*");

    try {
      _server.start();
    } catch (Exception ex) {
      LOG.error("Failed to start Helix rest server, " + ex);
      throw new HelixException("Failed to start Helix rest server! " + ex);
    }

    LOG.info("Helix rest server started!");
  }

  public void join() {
    if (_server != null) {
      try {
        _server.join();
      } catch (InterruptedException e) {
        LOG.warn("Join on Helix rest server get interrupted!" + e);
      }
    }
  }

  public void shutdown() {
    if (_server != null) {
      try {
        _server.stop();
        LOG.info("Helix rest server stopped!");
      } catch (Exception ex) {
        LOG.error("Failed to stop Helix rest server, " + ex);
      }
    }
    ServerContext serverContext =
        (ServerContext) getProperty(ContextPropertyKeys.SERVER_CONTEXT.name());
    serverContext.close();
  }
}

@@ -1258,21 +1258,59 @@
 tty.
-server.S
+http.HttpVersion;%0Aimport org.eclipse.jetty.s
 erver
+.*
 ;%0Aim
@@ -1407,24 +1407,77 @@
 vletHolder;%0A
+import org.eclipse.jetty.util.ssl.SslContextFactory;%0A
 import org.g
@@ -2024,16 +2024,49 @@
 lPrefix;
+%0A    _server = new Server(_port);
 %0A%0A    pa
@@ -2691,41 +2691,8 @@
 n %7B%0A
-    _server = new Server(_port);%0A
     
@@ -3724,24 +3724,24 @@
 XT.name());%0A
-
     serverCo
@@ -3758,11 +3758,738 @@
 e();%0A  %7D
+%0A%0A  public void setupSslServer(int port, SslContextFactory sslContextFactory) %7B%0A    if (_server != null && port %3E 0) %7B%0A      try %7B%0A        HttpConfiguration https = new HttpConfiguration();%0A        https.addCustomizer(new SecureRequestCustomizer());%0A        ServerConnector sslConnector = new ServerConnector(%0A            _server,%0A            new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()),%0A            new HttpConnectionFactory(https));%0A        sslConnector.setPort(port);%0A%0A        _server.addConnector(sslConnector);%0A%0A        LOG.info(%22Helix SSL rest server is ready to start.%22);%0A      %7D catch (Exception ex) %7B%0A        LOG.error(%22Failed to setup Helix SSL rest server, %22 + ex);%0A      %7D%0A    %7D%0A  %7D
 %0A%7D%0A

Enable Ssl Server