/*
 * Licensed 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.
 */
package com.teradata.prestomanager.agent;

import com.google.inject.Inject;
import io.airlift.log.Logger;

import javax.ws.rs.ProcessingException;
import javax.ws.rs.client.Client;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;

import java.io.IOException;
import java.nio.file.Path;

import static com.teradata.prestomanager.agent.AgentFileUtils.getFileProperty;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
import static javax.ws.rs.core.MediaType.TEXT_PLAIN;
import static javax.ws.rs.core.UriBuilder.fromUri;

public class PrestoInformer
{
    private static final Logger LOGGER = Logger.get(PrestoInformer.class);

    private final Path configFile;
    private final Client client;

    @Inject
    PrestoInformer(Client client, PrestoConfig config)
    {
        this.client = requireNonNull(client);
        this.configFile = requireNonNull(config.getConfigurationDirectory())
                .resolve("config.properties");
    }

    public int getPrestoPort()
            throws IOException
    {
        return Integer.valueOf(getFileProperty(configFile, "http-server.http.port"));
    }

    /**
     * @return whether Presto is running as a coordinator
     * @throws IOException If Presto port number could not be retrieved
     */
    public boolean isRunningCoordinator()
            throws IOException
    {
        try {
            UriBuilder uriBuilder = fromUri(format("http://localhost:%s", getPrestoPort())).path("/v1/info/coordinator");
            Response isCoordinator = client.target(uriBuilder.build()).request(TEXT_PLAIN).buildGet().invoke();
            return isCoordinator.getStatus() == 200;
        }
        catch (ProcessingException e) {
            LOGGER.warn(e, "Presto is not running");
        }
        return false;
    }
}

@@ -946,47 +946,8 @@
 ty;%0A
-import static java.lang.String.format;%0A
 impo
@@ -1700,15 +1700,15 @@
   * 
-@r
+R
 eturn
+s
  whe
@@ -1747,16 +1747,23 @@
 dinator%0A
+     *%0A
      * @
@@ -1975,15 +1975,8 @@
 Uri(
-format(
 %22htt
@@ -1992,14 +1992,16 @@
 host
-:%25s%22, 
+%22).port(
 getP
@@ -2012,17 +2012,16 @@
 oPort())
-)
 .path(%22/
@@ -2290,22 +2290,67 @@
 e, %22
-Presto is
+Error processing Presto's HTTP response; Presto may
  not 
+be 
 runn
@@ -2393,11 +2393,259 @@
 e;%0A    %7D
+%0A%0A    /**%0A     * Return whether Presto is configured to run as a coordinator%0A     */%0A    public boolean isConfiguredCoordinator()%0A            throws IOException%0A    %7B%0A        return Boolean.valueOf(getFileProperty(configFile, %22coordinator%22));%0A    %7D
 %0A%7D%0A

Add isConfiguedCoordinator to PrestoInformer