From 968e63a87f581c8576153192e1dc376501efe5c5 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Tue, 10 Sep 2013 18:49:14 -0400
Subject: [PATCH] Added TPS config resource.

A new REST service and clients have been added to read and update the
TPS configuration in CS.cfg. When the configuration is updated, the
previous configuration will be stored as a backup.

Ticket #652
---
 .../netscape/certsrv/base/ISourceConfigStore.java  |   1 +
 .../src/com/netscape/certsrv/tps/TPSClient.java    |   2 +
 .../{TPSClient.java => config/ConfigClient.java}   |  43 ++++----
 .../netscape/certsrv/tps/config/ConfigData.java    | 115 +++++++++++++++++++++
 .../certsrv/tps/config/ConfigResource.java         |  46 +++++++++
 .../com/netscape/cmscore/base/PropConfigStore.java |   5 +
 .../src/com/netscape/cmstools/cli/TPSCLI.java      |   2 +
 .../netscape/cmstools/tps/config/ConfigCLI.java    |  80 ++++++++++++++
 .../cmstools/tps/config/ConfigModifyCLI.java       | 115 +++++++++++++++++++++
 .../cmstools/tps/config/ConfigShowCLI.java         |  85 +++++++++++++++
 .../org/dogtagpki/server/tps/TPSApplication.java   |   4 +
 .../dogtagpki/server/tps/config/ConfigService.java | 102 ++++++++++++++++++
 12 files changed, 581 insertions(+), 19 deletions(-)
 copy base/common/src/com/netscape/certsrv/tps/{TPSClient.java => config/ConfigClient.java} (50%)
 create mode 100644 base/common/src/com/netscape/certsrv/tps/config/ConfigData.java
 create mode 100644 base/common/src/com/netscape/certsrv/tps/config/ConfigResource.java
 create mode 100644 base/java-tools/src/com/netscape/cmstools/tps/config/ConfigCLI.java
 create mode 100644 base/java-tools/src/com/netscape/cmstools/tps/config/ConfigModifyCLI.java
 create mode 100644 base/java-tools/src/com/netscape/cmstools/tps/config/ConfigShowCLI.java
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/config/ConfigService.java

diff --git a/base/common/src/com/netscape/certsrv/base/ISourceConfigStore.java b/base/common/src/com/netscape/certsrv/base/ISourceConfigStore.java
index 42637c258258472d8469fd8c691a826e3bcb1b3e..7271c2f745870ce9a2396a73045dea8f6e238e68 100644
--- a/base/common/src/com/netscape/certsrv/base/ISourceConfigStore.java
+++ b/base/common/src/com/netscape/certsrv/base/ISourceConfigStore.java
@@ -78,4 +78,5 @@ public interface ISourceConfigStore extends Serializable {
      */
     public void save(OutputStream out, String header);
 
+    public void clear();
 }
diff --git a/base/common/src/com/netscape/certsrv/tps/TPSClient.java b/base/common/src/com/netscape/certsrv/tps/TPSClient.java
index 8381b1ac2454e779d33b582d5755e7c60630b67c..e30858bfec342f2e43856125c0752dd9aa8579e8 100644
--- a/base/common/src/com/netscape/certsrv/tps/TPSClient.java
+++ b/base/common/src/com/netscape/certsrv/tps/TPSClient.java
@@ -25,6 +25,7 @@ import com.netscape.certsrv.group.GroupClient;
 import com.netscape.certsrv.logging.ActivityClient;
 import com.netscape.certsrv.tps.authenticator.AuthenticatorClient;
 import com.netscape.certsrv.tps.cert.TPSCertClient;
+import com.netscape.certsrv.tps.config.ConfigClient;
 import com.netscape.certsrv.tps.connection.ConnectionClient;
 import com.netscape.certsrv.tps.token.TokenClient;
 import com.netscape.certsrv.user.UserClient;
@@ -42,6 +43,7 @@ public class TPSClient extends SubsystemClient {
     public void init() throws URISyntaxException {
         addClient(new ActivityClient(client, name));
         addClient(new AuthenticatorClient(client, name));
+        addClient(new ConfigClient(client, name));
         addClient(new ConnectionClient(client, name));
         addClient(new GroupClient(client, name));
         addClient(new TokenClient(client, name));
diff --git a/base/common/src/com/netscape/certsrv/tps/TPSClient.java b/base/common/src/com/netscape/certsrv/tps/config/ConfigClient.java
similarity index 50%
copy from base/common/src/com/netscape/certsrv/tps/TPSClient.java
copy to base/common/src/com/netscape/certsrv/tps/config/ConfigClient.java
index 8381b1ac2454e779d33b582d5755e7c60630b67c..9c707e494896182610b391d4bea4981121192f9e 100644
--- a/base/common/src/com/netscape/certsrv/tps/TPSClient.java
+++ b/base/common/src/com/netscape/certsrv/tps/config/ConfigClient.java
@@ -15,37 +15,42 @@
 //(C) 2013 Red Hat, Inc.
 //All rights reserved.
 //--- END COPYRIGHT BLOCK ---
-package com.netscape.certsrv.tps;
+package com.netscape.certsrv.tps.config;
 
 import java.net.URISyntaxException;
 
+import org.jboss.resteasy.client.ClientResponse;
+
+import com.netscape.certsrv.client.Client;
 import com.netscape.certsrv.client.PKIClient;
-import com.netscape.certsrv.client.SubsystemClient;
-import com.netscape.certsrv.group.GroupClient;
-import com.netscape.certsrv.logging.ActivityClient;
-import com.netscape.certsrv.tps.authenticator.AuthenticatorClient;
-import com.netscape.certsrv.tps.cert.TPSCertClient;
-import com.netscape.certsrv.tps.connection.ConnectionClient;
-import com.netscape.certsrv.tps.token.TokenClient;
-import com.netscape.certsrv.user.UserClient;
 
 /**
  * @author Endi S. Dewata
  */
-public class TPSClient extends SubsystemClient {
+public class ConfigClient extends Client {
 
-    public TPSClient(PKIClient client) throws Exception {
-        super(client, "tps");
+    public ConfigResource resource;
+
+    public ConfigClient(PKIClient client) throws URISyntaxException {
+        this(client, client.getSubsystem());
+    }
+
+    public ConfigClient(PKIClient client, String subsystem) throws URISyntaxException {
+        super(client, subsystem, "config");
         init();
     }
 
     public void init() throws URISyntaxException {
-        addClient(new ActivityClient(client, name));
-        addClient(new AuthenticatorClient(client, name));
-        addClient(new ConnectionClient(client, name));
-        addClient(new GroupClient(client, name));
-        addClient(new TokenClient(client, name));
-        addClient(new TPSCertClient(client, name));
-        addClient(new UserClient(client, name));
+        resource = createProxy(ConfigResource.class);
+    }
+
+    public ConfigData getConfig() {
+        return resource.getConfig();
+    }
+
+    public ConfigData updateConfig(ConfigData configData) {
+        @SuppressWarnings("unchecked")
+        ClientResponse<ConfigData> response = (ClientResponse<ConfigData>)resource.updateConfig(configData);
+        return client.getEntity(response);
     }
 }
diff --git a/base/common/src/com/netscape/certsrv/tps/config/ConfigData.java b/base/common/src/com/netscape/certsrv/tps/config/ConfigData.java
new file mode 100644
index 0000000000000000000000000000000000000000..2dda5ae329a99a9ac3c669d467a4f75ea6b06b03
--- /dev/null
+++ b/base/common/src/com/netscape/certsrv/tps/config/ConfigData.java
@@ -0,0 +1,115 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+
+package com.netscape.certsrv.tps.config;
+
+import java.io.StringReader;
+import java.io.StringWriter;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.Unmarshaller;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+
+/**
+ * @author Endi S. Dewata
+ */
+@XmlRootElement(name="Configuration")
+public class ConfigData {
+
+    public static Marshaller marshaller;
+    public static Unmarshaller unmarshaller;
+
+    static {
+        try {
+            marshaller = JAXBContext.newInstance(ConfigData.class).createMarshaller();
+            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
+            unmarshaller = JAXBContext.newInstance(ConfigData.class).createUnmarshaller();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    String contents;
+
+    @XmlElement(name="Contents")
+    public String getContents() {
+        return contents;
+    }
+
+    public void setContents(String contents) {
+        this.contents = contents;
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((contents == null) ? 0 : contents.hashCode());
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj)
+            return true;
+        if (obj == null)
+            return false;
+        if (getClass() != obj.getClass())
+            return false;
+        ConfigData other = (ConfigData) obj;
+        if (contents == null) {
+            if (other.contents != null)
+                return false;
+        } else if (!contents.equals(other.contents))
+            return false;
+        return true;
+    }
+
+    public String toString() {
+        try {
+            StringWriter sw = new StringWriter();
+            marshaller.marshal(this, sw);
+            return sw.toString();
+
+        } catch (Exception e) {
+            return super.toString();
+        }
+    }
+
+    public static ConfigData valueOf(String string) throws Exception {
+        try {
+            return (ConfigData)unmarshaller.unmarshal(new StringReader(string));
+        } catch (Exception e) {
+            return null;
+        }
+    }
+
+    public static void main(String args[]) throws Exception {
+
+        ConfigData before = new ConfigData();
+        before.setContents("param1=value1\nparam2=value2");
+
+        String string = before.toString();
+        System.out.println(string);
+
+        ConfigData after = ConfigData.valueOf(string);
+        System.out.println(before.equals(after));
+    }
+}
diff --git a/base/common/src/com/netscape/certsrv/tps/config/ConfigResource.java b/base/common/src/com/netscape/certsrv/tps/config/ConfigResource.java
new file mode 100644
index 0000000000000000000000000000000000000000..e1076a48e4a0c549b43bb6d550268535e6b37795
--- /dev/null
+++ b/base/common/src/com/netscape/certsrv/tps/config/ConfigResource.java
@@ -0,0 +1,46 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+package com.netscape.certsrv.tps.config;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.GET;
+import javax.ws.rs.PUT;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import org.jboss.resteasy.annotations.ClientResponseType;
+
+
+/**
+ * @author Endi S. Dewata
+ */
+@Path("config")
+public interface ConfigResource {
+
+    @GET
+    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+    public ConfigData getConfig();
+
+    @PUT
+    @ClientResponseType(entityType=ConfigData.class)
+    @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+    public Response updateConfig(ConfigData configData);
+}
diff --git a/base/common/src/com/netscape/cmscore/base/PropConfigStore.java b/base/common/src/com/netscape/cmscore/base/PropConfigStore.java
index 161f549f2148c35bcbf9e985f079394ff545984a..b61bf741a880b4734487336c86c9fc2ebed24e48 100644
--- a/base/common/src/com/netscape/cmscore/base/PropConfigStore.java
+++ b/base/common/src/com/netscape/cmscore/base/PropConfigStore.java
@@ -605,6 +605,11 @@ public class PropConfigStore implements IConfigStore, Cloneable {
         }
     }
 
+    @Override
+    public void clear() {
+        mSource.clear();
+    }
+
     /**
      * Retrieves a sub store. A substore contains a list
      * of properties and substores. For example,
diff --git a/base/java-tools/src/com/netscape/cmstools/cli/TPSCLI.java b/base/java-tools/src/com/netscape/cmstools/cli/TPSCLI.java
index 8adf09edea0a3dbb9bc2f8a005f5cddb5e241994..56d0bff1924d9f7dc222719b1a88c1eb91c68d41 100644
--- a/base/java-tools/src/com/netscape/cmstools/cli/TPSCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cli/TPSCLI.java
@@ -24,6 +24,7 @@ import com.netscape.cmstools.group.GroupCLI;
 import com.netscape.cmstools.logging.ActivityCLI;
 import com.netscape.cmstools.tps.authenticator.AuthenticatorCLI;
 import com.netscape.cmstools.tps.cert.TPSCertCLI;
+import com.netscape.cmstools.tps.config.ConfigCLI;
 import com.netscape.cmstools.tps.connection.ConnectionCLI;
 import com.netscape.cmstools.tps.token.TokenCLI;
 import com.netscape.cmstools.user.UserCLI;
@@ -40,6 +41,7 @@ public class TPSCLI extends SubsystemCLI {
 
         addModule(new ActivityCLI(this));
         addModule(new AuthenticatorCLI(this));
+        addModule(new ConfigCLI(this));
         addModule(new ConnectionCLI(this));
         addModule(new GroupCLI(this));
         addModule(new TokenCLI(this));
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/config/ConfigCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/config/ConfigCLI.java
new file mode 100644
index 0000000000000000000000000000000000000000..343ab9c62e18ca98b8cd8a219003c32ccda34d79
--- /dev/null
+++ b/base/java-tools/src/com/netscape/cmstools/tps/config/ConfigCLI.java
@@ -0,0 +1,80 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+
+package com.netscape.cmstools.tps.config;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.Arrays;
+
+import com.netscape.certsrv.tps.config.ConfigClient;
+import com.netscape.certsrv.tps.config.ConfigData;
+import com.netscape.cmstools.cli.CLI;
+
+/**
+ * @author Endi S. Dewata
+ */
+public class ConfigCLI extends CLI {
+
+    public ConfigClient configClient;
+
+    public ConfigCLI(CLI parent) {
+        super("config", "Config management commands", parent);
+
+        addModule(new ConfigModifyCLI(this));
+        addModule(new ConfigShowCLI(this));
+    }
+
+    public void execute(String[] args) throws Exception {
+
+        client = parent.getClient();
+        configClient = (ConfigClient)parent.getClient("config");
+
+        if (args.length == 0) {
+            printHelp();
+            System.exit(1);
+        }
+
+        String command = args[0];
+        String[] commandArgs = Arrays.copyOfRange(args, 1, args.length);
+
+        if (command == null) {
+            printHelp();
+            System.exit(1);
+        }
+
+        CLI module = getModule(command);
+        if (module != null) {
+            module.execute(commandArgs);
+
+        } else {
+            System.err.println("Error: Invalid command \"" + command + "\"");
+            printHelp();
+            System.exit(1);
+        }
+    }
+
+    public static void printConfigData(ConfigData configData) throws IOException {
+        BufferedReader in = new BufferedReader(new StringReader(configData.getContents()));
+        String line;
+        while ((line = in.readLine()) != null) {
+            System.out.println("  " + line);
+        }
+    }
+}
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/config/ConfigModifyCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/config/ConfigModifyCLI.java
new file mode 100644
index 0000000000000000000000000000000000000000..a18e86c007a5fb272e701ecd6a8ab0411309dd91
--- /dev/null
+++ b/base/java-tools/src/com/netscape/cmstools/tps/config/ConfigModifyCLI.java
@@ -0,0 +1,115 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+
+package com.netscape.cmstools.tps.config;
+
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+
+import com.netscape.certsrv.tps.config.ConfigData;
+import com.netscape.cmstools.cli.CLI;
+import com.netscape.cmstools.cli.MainCLI;
+
+/**
+ * @author Endi S. Dewata
+ */
+public class ConfigModifyCLI extends CLI {
+
+    public ConfigCLI configCLI;
+
+    public ConfigModifyCLI(ConfigCLI configCLI) {
+        super("mod", "Modify config", configCLI);
+        this.configCLI = configCLI;
+    }
+
+    public void printHelp() {
+        formatter.printHelp(getFullName() + " [OPTIONS...]", options);
+    }
+
+    public void execute(String[] args) throws Exception {
+
+        Option option = new Option(null, "input", true, "Input file containing config attributes.");
+        option.setArgName("file");
+        option.setRequired(true);
+        options.addOption(option);
+
+        option = new Option(null, "output", true, "Output file to store updated config attributes.");
+        option.setArgName("file");
+        options.addOption(option);
+
+        CommandLine cmd = null;
+
+        try {
+            cmd = parser.parse(options, args);
+
+        } catch (Exception e) {
+            System.err.println("Error: " + e.getMessage());
+            printHelp();
+            System.exit(1);
+        }
+
+        String[] cmdArgs = cmd.getArgs();
+
+        if (cmdArgs.length != 0) {
+            printHelp();
+            System.exit(1);
+        }
+
+        String input = cmd.getOptionValue("input");
+        String output = cmd.getOptionValue("output");
+
+        if (input == null) {
+            System.err.println("Error: Input file is required.");
+            printHelp();
+            System.exit(1);
+        }
+
+        ConfigData configData = new ConfigData();
+
+        try (BufferedReader in = new BufferedReader(new FileReader(input));
+            StringWriter sw = new StringWriter();
+            PrintWriter out = new PrintWriter(sw, true)) {
+
+            String line;
+            while ((line = in.readLine()) != null) {
+                out.println(line);
+            }
+
+            configData.setContents(sw.toString());
+        }
+
+        configData = configCLI.configClient.updateConfig(configData);
+
+        MainCLI.printMessage("Updated configuration");
+
+        if (output == null) {
+            ConfigCLI.printConfigData(configData);
+
+        } else {
+            try (PrintWriter out = new PrintWriter(new FileWriter(output))) {
+                if (configData.getContents() != null) out.print(configData.getContents());
+            }
+        }
+    }
+}
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/config/ConfigShowCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/config/ConfigShowCLI.java
new file mode 100644
index 0000000000000000000000000000000000000000..45ddc883e059e3313f97d71ec6744e1389c45a18
--- /dev/null
+++ b/base/java-tools/src/com/netscape/cmstools/tps/config/ConfigShowCLI.java
@@ -0,0 +1,85 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+
+package com.netscape.cmstools.tps.config;
+
+import java.io.FileWriter;
+import java.io.PrintWriter;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+
+import com.netscape.certsrv.tps.config.ConfigData;
+import com.netscape.cmstools.cli.CLI;
+import com.netscape.cmstools.cli.MainCLI;
+
+/**
+ * @author Endi S. Dewata
+ */
+public class ConfigShowCLI extends CLI {
+
+    public ConfigCLI configCLI;
+
+    public ConfigShowCLI(ConfigCLI configCLI) {
+        super("show", "Show config", configCLI);
+        this.configCLI = configCLI;
+    }
+
+    public void printHelp() {
+        formatter.printHelp(getFullName(), options);
+    }
+
+    public void execute(String[] args) throws Exception {
+
+        Option option = new Option(null, "output", true, "Output file to store config attributes.");
+        option.setArgName("file");
+        options.addOption(option);
+
+        CommandLine cmd = null;
+
+        try {
+            cmd = parser.parse(options, args);
+
+        } catch (Exception e) {
+            System.err.println("Error: " + e.getMessage());
+            printHelp();
+            System.exit(1);
+        }
+
+        String[] cmdArgs = cmd.getArgs();
+
+        if (cmdArgs.length != 0) {
+            printHelp();
+            System.exit(1);
+        }
+
+        String output = cmd.getOptionValue("output");
+        ConfigData configData = configCLI.configClient.getConfig();
+
+        if (output == null) {
+            MainCLI.printMessage("Configuration");
+            ConfigCLI.printConfigData(configData);
+
+        } else {
+            try (PrintWriter out = new PrintWriter(new FileWriter(output))) {
+                if (configData.getContents() != null) out.print(configData.getContents());
+            }
+            MainCLI.printMessage("Stored configuration into " + output);
+        }
+    }
+}
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSApplication.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSApplication.java
index ff66e6b75f0bc49f8b8591596ebcaeda54731ac5..c3c6195f563b8716e4e100108024122add8a547e 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSApplication.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSApplication.java
@@ -24,6 +24,7 @@ import javax.ws.rs.core.Application;
 
 import org.dogtagpki.server.tps.authenticator.AuthenticatorService;
 import org.dogtagpki.server.tps.cert.TPSCertService;
+import org.dogtagpki.server.tps.config.ConfigService;
 import org.dogtagpki.server.tps.connection.ConnectionService;
 import org.dogtagpki.server.tps.logging.ActivityService;
 import org.dogtagpki.server.tps.token.TokenService;
@@ -72,6 +73,9 @@ public class TPSApplication extends Application {
         // authenticators
         classes.add(AuthenticatorService.class);
 
+        // config
+        classes.add(ConfigService.class);
+
         // connections
         classes.add(ConnectionService.class);
 
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/config/ConfigService.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/config/ConfigService.java
new file mode 100644
index 0000000000000000000000000000000000000000..e6d59ed8970aeeec3be54723bc987ac23fbfda54
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/config/ConfigService.java
@@ -0,0 +1,102 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2013 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+
+package org.dogtagpki.server.tps.config;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import org.dogtagpki.server.tps.TPSSubsystem;
+
+import com.netscape.certsrv.apps.CMS;
+import com.netscape.certsrv.base.IConfigStore;
+import com.netscape.certsrv.base.PKIException;
+import com.netscape.certsrv.tps.config.ConfigData;
+import com.netscape.certsrv.tps.config.ConfigResource;
+import com.netscape.cms.servlet.base.PKIService;
+
+/**
+ * @author Endi S. Dewata
+ */
+public class ConfigService extends PKIService implements ConfigResource {
+
+    public final static int DEFAULT_SIZE = 20;
+
+    public ConfigService() {
+        CMS.debug("ConfigService.<init>()");
+    }
+
+    @Override
+    public ConfigData getConfig() {
+
+        CMS.debug("ConfigService.getConfig()");
+
+        try {
+            TPSSubsystem subsystem = (TPSSubsystem)CMS.getSubsystem(TPSSubsystem.ID);
+            IConfigStore tpsConfigStore = subsystem.getConfigStore();
+            ConfigData configData = new ConfigData();
+
+            try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
+                tpsConfigStore.save(out, null);
+                configData.setContents(out.toString());
+            }
+
+            return configData;
+
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new PKIException(e.getMessage());
+        }
+    }
+
+    @Override
+    public Response updateConfig(ConfigData configData) {
+
+        CMS.debug("ConfigService.updateConfig()");
+
+        try {
+
+            TPSSubsystem subsystem = (TPSSubsystem)CMS.getSubsystem(TPSSubsystem.ID);
+            IConfigStore tpsConfigStore = subsystem.getConfigStore();
+
+            tpsConfigStore.clear();
+
+            byte[] bytes = configData.getContents().getBytes();
+            try (ByteArrayInputStream in = new ByteArrayInputStream(bytes)) {
+                tpsConfigStore.load(in);
+            }
+
+            IConfigStore cs = CMS.getConfigStore();
+            cs.commit(true);
+
+            configData = getConfig();
+
+            return Response
+                    .ok(configData)
+                    .type(MediaType.APPLICATION_XML)
+                    .build();
+
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new PKIException(e.getMessage());
+        }
+    }
+}
-- 
1.8.1.4

