/*
 * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
 * (the "License"). You may not use this work except in compliance with the License, which is
 * available at www.apache.org/licenses/LICENSE-2.0
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied, as more fully set forth in the License.
 *
 * See the NOTICE file distributed with this work for information regarding copyright ownership.
 */

package alluxio.proxy;

import alluxio.Configuration;
import alluxio.Constants;
import alluxio.PropertyKey;
import alluxio.util.CommonUtils;
import alluxio.util.network.NetworkAddressUtils;
import alluxio.util.network.NetworkAddressUtils.ServiceType;
import alluxio.web.ProxyWebServer;
import alluxio.web.WebServer;

import com.google.common.base.Function;
import org.apache.commons.httpclient.methods.GetMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

import javax.annotation.concurrent.NotThreadSafe;

/**
 * This class encapsulates the different worker services that are configured to run.
 */
@NotThreadSafe
public final class AlluxioProxyProcess implements ProxyProcess {
  private static final Logger LOG = LoggerFactory.getLogger(AlluxioProxyProcess.class);

  /** The web server. */
  private WebServer mWebServer = null;

  /** Worker start time in milliseconds. */
  private final long mStartTimeMs;

  private final CountDownLatch mLatch;

  /**
   * Creates an instance of {@link AlluxioProxy}.
   */
  AlluxioProxyProcess() {
    mStartTimeMs = System.currentTimeMillis();
    mLatch = new CountDownLatch(1);
  }

  @Override
  public int getWebLocalPort() {
    return mWebServer.getLocalPort();
  }

  @Override
  public long getStartTimeMs() {
    return mStartTimeMs;
  }

  @Override
  public long getUptimeMs() {
    return System.currentTimeMillis() - mStartTimeMs;
  }

  @Override
  public void start() throws Exception {
    mWebServer = new ProxyWebServer(ServiceType.PROXY_WEB.getServiceName(),
        NetworkAddressUtils.getBindAddress(ServiceType.PROXY_WEB), this);
    // reset proxy web port
    Configuration.set(PropertyKey.PROXY_WEB_PORT, Integer.toString(mWebServer.getLocalPort()));
    mWebServer.start();
    mLatch.await();
  }

  @Override
  public void stop() throws Exception {
    if (mWebServer != null) {
      mWebServer.stop();
      mWebServer = null;
    }
    mLatch.countDown();
  }

  @Override
  public void waitForReady() {
    CommonUtils.waitFor(this + " to start", new Function<Void, Boolean>() {
      @Override
      public Boolean apply(Void input) {
        if (mWebServer == null || !mWebServer.getServer().isRunning()) {
          return false;
        }
        GetMethod get = new GetMethod(String
            .format("http://%s:%d%s/%s///%s", mWebServer.getBindHost(), mWebServer.getLocalPort(),
                Constants.REST_API_PREFIX, PathsRestServiceHandler.SERVICE_PREFIX,
                PathsRestServiceHandler.EXISTS));
        try {
          get.getResponseBody();
        } catch (IOException e) {
          LOG.debug("Exception: ", e);
          return false;
        }
        return true;
      }
    });
  }

  @Override
  public String toString() {
    return "Alluxio Proxy";
  }
}

@@ -894,36 +894,316 @@
 ons.
-httpclient.methods.GetMethod
+io.IOUtils;%0Aimport org.apache.http.HttpResponse;%0Aimport org.apache.http.HttpStatus;%0Aimport org.apache.http.StatusLine;%0Aimport org.apache.http.client.HttpClient;%0Aimport org.apache.http.client.methods.HttpGet;%0Aimport org.apache.http.client.methods.HttpPost;%0Aimport org.apache.http.impl.client.HttpClientBuilder
 ;%0Aim
@@ -3107,37 +3107,103 @@
     
-GetMethod get = new GetMethod
+HttpClient client = HttpClientBuilder.create().build();;%0A        HttpPost method = new HttpPost
 (Str
@@ -3466,29 +3466,267 @@
     
-get.getResponseBody()
+HttpResponse response = client.execute(method);%0A          if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) %7B%0A            return true;%0A          %7D%0A          LOG.debug(IOUtils.toString(response.getEntity().getContent()));%0A          return false
 ;%0A  
@@ -3812,32 +3812,32 @@
   return false;%0A
+
         %7D%0A      
@@ -3830,37 +3830,16 @@
       %7D%0A
-        return true;%0A
       %7D%0A

Address review comment.