DIRKRB-711 Implement tool based on REST API for KDC configuration.
diff --git a/has-project/has-client/src/main/java/org/apache/kerby/has/client/HasInitClient.java b/has-project/has-client/src/main/java/org/apache/kerby/has/client/HasInitClient.java
index 104f302..675b1b7 100644
--- a/has-project/has-client/src/main/java/org/apache/kerby/has/client/HasInitClient.java
+++ b/has-project/has-client/src/main/java/org/apache/kerby/has/client/HasInitClient.java
@@ -23,8 +23,10 @@
 import com.sun.jersey.api.client.config.ClientConfig;
 import com.sun.jersey.api.client.config.DefaultClientConfig;
 import com.sun.jersey.client.urlconnection.HTTPSProperties;
+import com.sun.jersey.core.util.MultivaluedMapImpl;
 import org.apache.kerby.has.common.HasConfig;
 import org.apache.kerby.has.common.HasException;
+import org.apache.kerby.kerberos.kerb.KrbException;
 import org.glassfish.jersey.SslConfigurator;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -32,6 +34,7 @@
 import javax.net.ssl.HostnameVerifier;
 import javax.net.ssl.SSLContext;
 import javax.net.ssl.SSLSession;
+import javax.ws.rs.core.MultivaluedMap;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
@@ -137,4 +140,99 @@
         }
         return msg.toString();
     }
+
+    public String getKrb5conf() throws KrbException {
+        WebResource webResource = getWebResource("conf/getkrb5conf");
+        ClientResponse response = webResource.get(ClientResponse.class);
+        String message;
+        try {
+            message = getResponse(response);
+        } catch (HasException e) {
+            throw new KrbException(e.getMessage());
+        }
+        if (response.getStatus() == 200) {
+            return message;
+        } else {
+            throw new KrbException(message);
+        }
+    }
+
+    public String getHasClientConf() throws KrbException {
+        WebResource webResource = getWebResource("conf/gethasclientconf");
+        ClientResponse response = webResource.get(ClientResponse.class);
+        String message;
+        try {
+            message = getResponse(response);
+        } catch (HasException e) {
+            throw new KrbException(e.getMessage());
+        }
+        if (response.getStatus() == 200) {
+            return message;
+        } else {
+            throw new KrbException(message);
+        }
+    }
+
+    public String setPlugin(String plugin) throws KrbException {
+        WebResource webResource = getWebResource("conf/setplugin");
+        MultivaluedMap<String, String> params = new MultivaluedMapImpl();
+        params.add("plugin", plugin);
+        ClientResponse response = webResource.queryParams(params).put(ClientResponse.class);
+        String message;
+        try {
+            message = getResponse(response);
+        } catch (HasException e) {
+            throw new KrbException(e.getMessage());
+        }
+        if (response.getStatus() == 200) {
+            return message;
+        } else {
+            throw new KrbException(message);
+        }
+    }
+
+    public String configKdc(String port, String realm, String host) throws KrbException {
+        WebResource webResource = getWebResource("conf/configkdc");
+        MultivaluedMap<String, String> params = new MultivaluedMapImpl();
+        params.add("port", port);
+        params.add("realm", realm);
+        params.add("host", host);
+        ClientResponse response = webResource.queryParams(params).put(ClientResponse.class);
+        String message;
+        try {
+            message = getResponse(response);
+        } catch (HasException e) {
+            throw new KrbException(e.getMessage());
+        }
+        if (response.getStatus() == 200) {
+            return message;
+        } else {
+            throw new KrbException(message);
+        }
+    }
+    public String configBackend(String backendType, String dir, String url, String user,
+                              String password) throws KrbException {
+        WebResource webResource = getWebResource("conf/configbackend");
+        MultivaluedMap<String, String> params = new MultivaluedMapImpl();
+        params.add("backendType", backendType);
+        if (backendType.equals("json")) {
+            params.add("dir", dir);
+        } else if (backendType.equals("mysql")) {
+            params.add("url", url);
+            params.add("user", user);
+            params.add("password", password);
+        }
+        ClientResponse response = webResource.queryParams(params).put(ClientResponse.class);
+        String message;
+        try {
+            message = getResponse(response);
+        } catch (HasException e) {
+            throw new KrbException(e.getMessage());
+        }
+        if (response.getStatus() == 200) {
+            return message;
+        } else {
+            throw new KrbException(message);
+        }
+    }
 }
diff --git a/kerby-tool/has-tool/src/main/java/org/apache/kerby/kerberos/tool/init/HasInitTool.java b/kerby-tool/has-tool/src/main/java/org/apache/kerby/kerberos/tool/init/HasInitTool.java
index 977d16d..d8abb3e 100644
--- a/kerby-tool/has-tool/src/main/java/org/apache/kerby/kerberos/tool/init/HasInitTool.java
+++ b/kerby-tool/has-tool/src/main/java/org/apache/kerby/kerberos/tool/init/HasInitTool.java
@@ -24,8 +24,13 @@
 import org.apache.kerby.has.common.HasException;
 import org.apache.kerby.has.common.util.HasUtil;
 import org.apache.kerby.kerberos.kerb.KrbException;
+import org.apache.kerby.kerberos.tool.init.cmd.ConfigBackendCmd;
+import org.apache.kerby.kerberos.tool.init.cmd.ConfigKdcCmd;
+import org.apache.kerby.kerberos.tool.init.cmd.GetHasClientConfCmd;
+import org.apache.kerby.kerberos.tool.init.cmd.GetKrb5ConfCmd;
 import org.apache.kerby.kerberos.tool.init.cmd.InitCmd;
 import org.apache.kerby.kerberos.tool.init.cmd.InitKdcCmd;
+import org.apache.kerby.kerberos.tool.init.cmd.SetPluginCmd;
 import org.apache.kerby.kerberos.tool.init.cmd.StartKdcCmd;
 import org.apache.kerby.util.OSUtil;
 
@@ -45,6 +50,16 @@
 
     private static final String LEGAL_COMMANDS = "Available commands are: "
             + "\n"
+            + "get_krb5conf, getkrb5\n"
+            + "                         Get krb5.conf\n"
+            + "get_hasConf, gethas\n"
+            + "                         Get has-client.conf\n"
+            + "set_plugin, setplugin\n"
+            + "                         Set plugin\n"
+            + "config_kdcBackend, confbackend\n"
+            + "                         Config kdc backend\n"
+            + "config_kdc, confkdc\n"
+            + "                         Config kdc\n"
             + "start_kdc, start\n"
             + "                         Start kdc\n"
             + "init_kdc, init\n"
@@ -93,7 +108,22 @@
         String cmd = items[0];
 
         InitCmd executor;
-        if (cmd.equals("start_kdc")
+        if (cmd.equals("get_krb5conf")
+            || cmd.equals("getkrb5")) {
+            executor = new GetKrb5ConfCmd(hasInitClient);
+        } else if (cmd.equals("get_hasConf")
+            || cmd.equals("gethas")) {
+            executor = new GetHasClientConfCmd(hasInitClient);
+        } else if (cmd.equals("set_plugin")
+            || cmd.equals("setplugin")) {
+            executor = new SetPluginCmd(hasInitClient);
+        } else if (cmd.equals("config_backend")
+            || cmd.equals("confbackend")) {
+            executor = new ConfigBackendCmd(hasInitClient);
+        } else if (cmd.equals("config_kdc")
+            || cmd.equals("confkdc")) {
+            executor = new ConfigKdcCmd(hasInitClient);
+        } else if (cmd.equals("start_kdc")
             || cmd.equals("start")) {
             executor = new StartKdcCmd(hasInitClient);
         } else if (cmd.equals("init_kdc")
@@ -103,6 +133,7 @@
             System.out.println(LEGAL_COMMANDS);
             return;
         }
+
         executor.execute(items);
     }
 }
diff --git a/kerby-tool/has-tool/src/main/java/org/apache/kerby/kerberos/tool/init/cmd/ConfigBackendCmd.java b/kerby-tool/has-tool/src/main/java/org/apache/kerby/kerberos/tool/init/cmd/ConfigBackendCmd.java
new file mode 100644
index 0000000..02c8cf2
--- /dev/null
+++ b/kerby-tool/has-tool/src/main/java/org/apache/kerby/kerberos/tool/init/cmd/ConfigBackendCmd.java
@@ -0,0 +1,65 @@
+/**
+ *  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.
+ *
+ */
+package org.apache.kerby.kerberos.tool.init.cmd;
+
+import org.apache.kerby.has.client.HasInitClient;
+import org.apache.kerby.kerberos.kerb.KrbException;
+
+/**
+ * Remote config kdc cmd
+ */
+public class ConfigBackendCmd extends InitCmd {
+
+    public static final String USAGE = "Usage: config_backend <backendType> [dir] [url] [user]"
+        + " [password]\n"
+        + "\tSupported backendType : json,mysql\n"
+        + "\tExample:\n"
+        + "\t\tconfig_backend json /tmp/has/jsonbackend \n"
+        + "\t\tconfig_backend mysql jdbc:mysql://127.0.0.1:3306/mysqlbackend root passwd\n";
+
+    public ConfigBackendCmd(HasInitClient client) {
+        super(client);
+    }
+
+    @Override
+    public void execute(String[] items) throws KrbException {
+        if (items.length == 1
+            || items.length == 2 && (items[1].startsWith("?") || items[1].startsWith("-help"))) {
+            System.out.println(USAGE);
+            return;
+        }
+        if (items.length < 3) {
+            System.err.println(USAGE);
+            return;
+        }
+
+        HasInitClient client = getClient();
+        if (items.length >= 3 && items[1].equals("json")) {
+            System.out.println(client.configBackend(items[1], items[2],
+                    null, null, null));
+        } else if (items.length >= 5 && items[1].equals("mysql")) {
+            System.out.println(client.configBackend(items[1], null,
+                    items[2], items[3], items[4]));
+        } else {
+            System.err.println(USAGE);
+            return;
+        }
+    }
+}
diff --git a/kerby-tool/has-tool/src/main/java/org/apache/kerby/kerberos/tool/init/cmd/ConfigKdcCmd.java b/kerby-tool/has-tool/src/main/java/org/apache/kerby/kerberos/tool/init/cmd/ConfigKdcCmd.java
new file mode 100644
index 0000000..1f4f2d1
--- /dev/null
+++ b/kerby-tool/has-tool/src/main/java/org/apache/kerby/kerberos/tool/init/cmd/ConfigKdcCmd.java
@@ -0,0 +1,53 @@
+/**
+ *  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.
+ *
+ */
+package org.apache.kerby.kerberos.tool.init.cmd;
+
+import org.apache.kerby.has.client.HasInitClient;
+import org.apache.kerby.kerberos.kerb.KrbException;
+
+/**
+ * Remote config kdc cmd
+ */
+public class ConfigKdcCmd extends InitCmd {
+
+    public static final String USAGE = "Usage: config_kdc <host> <port> <realm>\n"
+        + "\tExample:\n"
+        + "\t\tconfig_kdc localhost 88 HADOOP.COM\n";
+
+    public ConfigKdcCmd(HasInitClient client) {
+        super(client);
+    }
+
+    @Override
+    public void execute(String[] items) throws KrbException {
+        if (items.length == 1
+            || items.length == 2 && (items[1].startsWith("?") || items[1].startsWith("-help"))) {
+            System.out.println(USAGE);
+            return;
+        }
+        if (items.length < 4) {
+            System.err.println(USAGE);
+            return;
+        }
+
+        HasInitClient client = getClient();
+        System.out.println(client.configKdc(items[2], items[3], items[1]));
+    }
+}
diff --git a/kerby-tool/has-tool/src/main/java/org/apache/kerby/kerberos/tool/init/cmd/GetHasClientConfCmd.java b/kerby-tool/has-tool/src/main/java/org/apache/kerby/kerberos/tool/init/cmd/GetHasClientConfCmd.java
new file mode 100644
index 0000000..f195e0e
--- /dev/null
+++ b/kerby-tool/has-tool/src/main/java/org/apache/kerby/kerberos/tool/init/cmd/GetHasClientConfCmd.java
@@ -0,0 +1,72 @@
+/**
+ *  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.
+ *
+ */
+package org.apache.kerby.kerberos.tool.init.cmd;
+
+import org.apache.kerby.has.client.HasInitClient;
+import org.apache.kerby.kerberos.kerb.KrbException;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.PrintStream;
+
+/**
+ * Remote get has-client.conf cmd
+ */
+public class GetHasClientConfCmd extends InitCmd {
+
+    public static final String USAGE = "Usage: get_hasConf [-p] [path]\n"
+        + "\tExample:\n"
+        + "\t\tget_hasConf\n";
+
+    public GetHasClientConfCmd(HasInitClient client) {
+        super(client);
+    }
+
+    @Override
+    public void execute(String[] items) throws KrbException {
+        if (items.length >= 2 && (items[1].startsWith("?") || items[1].startsWith("-help"))) {
+                System.out.println(USAGE);
+            return;
+        }
+        HasInitClient client = getClient();
+        File path = client.getConfDir();
+        if (items.length >= 3 && items[1].startsWith("-p")) {
+            path = new File(items[2]);
+            if (!path.exists() && !path.mkdirs()) {
+                System.err.println("Cannot create file : " + items[2]);
+                return;
+            }
+        }
+        File hasConf = new File(path, "has-client.conf");
+        String content = client.getHasClientConf();
+        if (content == null) {
+            System.err.println("Failed to get has.conf.");
+            return;
+        }
+        try {
+            PrintStream ps = new PrintStream(new FileOutputStream(hasConf));
+            ps.println(content);
+            System.out.println("has-client.conf has saved in : " + hasConf.getAbsolutePath());
+        } catch (FileNotFoundException e) {
+            System.err.println(e.getMessage());
+        }
+    }
+}
diff --git a/kerby-tool/has-tool/src/main/java/org/apache/kerby/kerberos/tool/init/cmd/GetKrb5ConfCmd.java b/kerby-tool/has-tool/src/main/java/org/apache/kerby/kerberos/tool/init/cmd/GetKrb5ConfCmd.java
new file mode 100644
index 0000000..58df209
--- /dev/null
+++ b/kerby-tool/has-tool/src/main/java/org/apache/kerby/kerberos/tool/init/cmd/GetKrb5ConfCmd.java
@@ -0,0 +1,72 @@
+/**
+ *  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.
+ *
+ */
+package org.apache.kerby.kerberos.tool.init.cmd;
+
+import org.apache.kerby.has.client.HasInitClient;
+import org.apache.kerby.kerberos.kerb.KrbException;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.PrintStream;
+
+/**
+ * Remote get krb5.conf cmd
+ */
+public class GetKrb5ConfCmd extends InitCmd {
+
+    public static final String USAGE = "Usage: get_krb5conf [-p] [path]\n"
+        + "\tExample:\n"
+        + "\t\tget_krb5conf -p /tmp/has\n";
+
+    public GetKrb5ConfCmd(HasInitClient client) {
+        super(client);
+    }
+
+    @Override
+    public void execute(String[] items) throws KrbException {
+        if (items.length >= 2 && (items[1].startsWith("?") || items[1].startsWith("-help"))) {
+                System.out.println(USAGE);
+            return;
+        }
+        HasInitClient client = getClient();
+        File path = client.getConfDir();
+        if (items.length >= 3 && items[1].startsWith("-p")) {
+            path = new File(items[2]);
+            if (!path.exists() && !path.mkdirs()) {
+                System.err.println("Cannot create file : " + items[2]);
+                return;
+            }
+        }
+        File krb5Conf = new File(path, "krb5.conf");
+        String content = client.getKrb5conf();
+        if (content == null) {
+            System.err.println("Failed to get krb5.conf.");
+            return;
+        }
+        try {
+            PrintStream ps = new PrintStream(new FileOutputStream(krb5Conf));
+            ps.println(content);
+            System.out.println("krb5.conf has saved in : " + krb5Conf.getAbsolutePath());
+        } catch (FileNotFoundException e) {
+            System.err.println(e.getMessage());
+        }
+    }
+}
diff --git a/kerby-tool/has-tool/src/main/java/org/apache/kerby/kerberos/tool/init/cmd/SetPluginCmd.java b/kerby-tool/has-tool/src/main/java/org/apache/kerby/kerberos/tool/init/cmd/SetPluginCmd.java
new file mode 100644
index 0000000..9974c43
--- /dev/null
+++ b/kerby-tool/has-tool/src/main/java/org/apache/kerby/kerberos/tool/init/cmd/SetPluginCmd.java
@@ -0,0 +1,49 @@
+/**
+ *  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.
+ *
+ */
+package org.apache.kerby.kerberos.tool.init.cmd;
+
+import org.apache.kerby.has.client.HasInitClient;
+import org.apache.kerby.kerberos.kerb.KrbException;
+
+/**
+ * Remote set plugin cmd
+ */
+public class SetPluginCmd extends InitCmd {
+
+    public static final String USAGE = "Usage: set_plugin <plugin>\n"
+        + "\tExample:\n"
+        + "\t\tset_plugin RAM\n";
+
+    public SetPluginCmd(HasInitClient client) {
+        super(client);
+    }
+
+    @Override
+    public void execute(String[] items) throws KrbException {
+        if (items.length == 1
+            || items.length == 2 && (items[1].startsWith("?") || items[1].startsWith("-help"))) {
+            System.out.println(USAGE);
+            return;
+        }
+
+        HasInitClient client = getClient();
+        System.out.println(client.setPlugin(items[1]));
+    }
+}