diff options
author | Endi S. Dewata <edewata@redhat.com> | 2013-08-26 13:45:55 -0400 |
---|---|---|
committer | Endi Sukma Dewata <edewata@redhat.com> | 2013-09-01 01:04:57 -0500 |
commit | 15e029e97aea4a23f065e6bc7fbfa16cdae8c02d (patch) | |
tree | f0b9f036f1ccd26ba4202af30e36957fec520a37 /base/java-tools/src/com/netscape/cmstools | |
parent | a847bcb7c71836f7c0498163e31238f118740339 (diff) | |
download | pki-15e029e97aea4a23f065e6bc7fbfa16cdae8c02d.tar.gz pki-15e029e97aea4a23f065e6bc7fbfa16cdae8c02d.tar.xz pki-15e029e97aea4a23f065e6bc7fbfa16cdae8c02d.zip |
Added TPS connection resource.
A skeleton for TPS connection services and the clients have been added.
The service implementation will be added later.
Ticket #652
Diffstat (limited to 'base/java-tools/src/com/netscape/cmstools')
8 files changed, 561 insertions, 3 deletions
diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertShowCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertShowCLI.java index d1d6132ae..a048ebff3 100644 --- a/base/java-tools/src/com/netscape/cmstools/cert/CertShowCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/cert/CertShowCLI.java @@ -83,9 +83,9 @@ public class CertShowCLI extends CLI { String encoded = certData.getEncoded(); if (encoded != null && file != null) { // store cert to file - PrintWriter out = new PrintWriter(new FileWriter(file)); - out.print(encoded); - out.close(); + try (PrintWriter out = new PrintWriter(new FileWriter(file))) { + out.print(encoded); + } } MainCLI.printMessage("Certificate \"" + certID.toHexString() + "\""); 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 2f28ada7d..7a14b59f6 100644 --- a/base/java-tools/src/com/netscape/cmstools/cli/TPSCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/cli/TPSCLI.java @@ -23,6 +23,7 @@ import com.netscape.certsrv.tps.TPSClient; import com.netscape.cmstools.group.GroupCLI; import com.netscape.cmstools.logging.ActivityCLI; import com.netscape.cmstools.tps.cert.TPSCertCLI; +import com.netscape.cmstools.tps.connection.ConnectionCLI; import com.netscape.cmstools.tps.token.TokenCLI; import com.netscape.cmstools.user.UserCLI; @@ -37,6 +38,7 @@ public class TPSCLI extends SubsystemCLI { super("tps", "TPS management commands", mainCLI); addModule(new ActivityCLI(this)); + addModule(new ConnectionCLI(this)); addModule(new GroupCLI(this)); addModule(new TokenCLI(this)); addModule(new TPSCertCLI(this)); diff --git a/base/java-tools/src/com/netscape/cmstools/tps/connection/ConnectionAddCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/connection/ConnectionAddCLI.java new file mode 100644 index 000000000..bb2beda13 --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/tps/connection/ConnectionAddCLI.java @@ -0,0 +1,107 @@ +// --- 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.connection; + +import java.io.BufferedReader; +import java.io.FileReader; +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.connection.ConnectionData; +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +/** + * @author Endi S. Dewata + */ +public class ConnectionAddCLI extends CLI { + + public ConnectionCLI connectionCLI; + + public ConnectionAddCLI(ConnectionCLI connectionCLI) { + super("add", "Add connection", connectionCLI); + this.connectionCLI = connectionCLI; + } + + public void printHelp() { + formatter.printHelp(getFullName() + " <Connection ID> [OPTIONS...]", options); + } + + public void execute(String[] args) throws Exception { + + Option option = new Option(null, "status", true, "Status: ENABLED, DISABLED."); + option.setArgName("status"); + option.setRequired(true); + options.addOption(option); + + option = new Option(null, "contents", true, "Input file containing connection attributes."); + option.setArgName("file"); + option.setRequired(true); + 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 != 1) { + printHelp(); + System.exit(1); + } + + String connectionID = cmdArgs[0]; + String status = cmd.getOptionValue("status"); + String contents = cmd.getOptionValue("contents"); + + ConnectionData connectionData = new ConnectionData(); + connectionData.setID(connectionID); + connectionData.setStatus(status); + + if (contents != null) { + try (BufferedReader in = new BufferedReader(new FileReader(contents)); + StringWriter sw = new StringWriter(); + PrintWriter out = new PrintWriter(sw, true)) { + + String line; + while ((line = in.readLine()) != null) { + out.println(line); + } + + connectionData.setContents(sw.toString()); + } + } + + connectionData = connectionCLI.connectionClient.addConnection(connectionData); + + MainCLI.printMessage("Added connection \"" + connectionID + "\""); + + ConnectionCLI.printConnectionData(connectionData); + } +} diff --git a/base/java-tools/src/com/netscape/cmstools/tps/connection/ConnectionCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/connection/ConnectionCLI.java new file mode 100644 index 000000000..5371dbe9d --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/tps/connection/ConnectionCLI.java @@ -0,0 +1,108 @@ +// --- 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.connection; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.StringReader; +import java.util.Arrays; + +import org.jboss.resteasy.plugins.providers.atom.Link; + +import com.netscape.certsrv.tps.connection.ConnectionClient; +import com.netscape.certsrv.tps.connection.ConnectionData; +import com.netscape.certsrv.tps.connection.ConnectionInfo; +import com.netscape.cmstools.cli.CLI; + +/** + * @author Endi S. Dewata + */ +public class ConnectionCLI extends CLI { + + public ConnectionClient connectionClient; + + public ConnectionCLI(CLI parent) { + super("connection", "Connection management commands", parent); + + addModule(new ConnectionAddCLI(this)); + addModule(new ConnectionFindCLI(this)); + addModule(new ConnectionModifyCLI(this)); + addModule(new ConnectionRemoveCLI(this)); + addModule(new ConnectionShowCLI(this)); + } + + public void execute(String[] args) throws Exception { + + client = parent.getClient(); + connectionClient = (ConnectionClient)parent.getClient("connection"); + + 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 printConnectionInfo(ConnectionInfo connectionInfo) { + System.out.println(" Connection ID: " + connectionInfo.getID()); + if (connectionInfo.getStatus() != null) System.out.println(" Status: " + connectionInfo.getStatus()); + + Link link = connectionInfo.getLink(); + if (verbose && link != null) { + System.out.println(" Link: " + link.getHref()); + } + } + + public static void printConnectionData(ConnectionData connectionData) throws IOException { + System.out.println(" Connection ID: " + connectionData.getID()); + if (connectionData.getStatus() != null) System.out.println(" Status: " + connectionData.getStatus()); + + System.out.println(" Contents:"); + String contents = connectionData.getContents(); + if (contents != null) { + BufferedReader in = new BufferedReader(new StringReader(contents)); + String line; + while ((line = in.readLine()) != null) { + System.out.println(" " + line); + } + } + + Link link = connectionData.getLink(); + if (verbose && link != null) { + System.out.println(" Link: " + link.getHref()); + } + } +} diff --git a/base/java-tools/src/com/netscape/cmstools/tps/connection/ConnectionFindCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/connection/ConnectionFindCLI.java new file mode 100644 index 000000000..9218edee4 --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/tps/connection/ConnectionFindCLI.java @@ -0,0 +1,94 @@ +// --- 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.connection; + +import java.util.Collection; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Option; + +import com.netscape.certsrv.tps.connection.ConnectionCollection; +import com.netscape.certsrv.tps.connection.ConnectionInfo; +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +/** + * @author Endi S. Dewata + */ +public class ConnectionFindCLI extends CLI { + + public ConnectionCLI connectionCLI; + + public ConnectionFindCLI(ConnectionCLI tokenCLI) { + super("find", "Find connections", tokenCLI); + this.connectionCLI = tokenCLI; + } + + public void printHelp() { + formatter.printHelp(getFullName() + " [OPTIONS...]", options); + } + + public void execute(String[] args) throws Exception { + + Option option = new Option(null, "start", true, "Page start"); + option.setArgName("start"); + options.addOption(option); + + option = new Option(null, "size", true, "Page size"); + option.setArgName("size"); + 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 s = cmd.getOptionValue("start"); + Integer start = s == null ? null : Integer.valueOf(s); + + s = cmd.getOptionValue("size"); + Integer size = s == null ? null : Integer.valueOf(s); + + ConnectionCollection result = connectionCLI.connectionClient.findConnections(start, size); + Collection<ConnectionInfo> connections = result.getEntries(); + + MainCLI.printMessage(connections.size() + " connection(s) matched"); + + boolean first = true; + + for (ConnectionInfo connectionInfo : connections) { + + if (first) { + first = false; + } else { + System.out.println(); + } + + ConnectionCLI.printConnectionInfo(connectionInfo); + } + + MainCLI.printMessage("Number of entries returned " + connections.size()); + } +} diff --git a/base/java-tools/src/com/netscape/cmstools/tps/connection/ConnectionModifyCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/connection/ConnectionModifyCLI.java new file mode 100644 index 000000000..f3e449132 --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/tps/connection/ConnectionModifyCLI.java @@ -0,0 +1,107 @@ +// --- 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.connection; + +import java.io.BufferedReader; +import java.io.FileReader; +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.connection.ConnectionData; +import com.netscape.certsrv.tps.connection.ConnectionModification; +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +/** + * @author Endi S. Dewata + */ +public class ConnectionModifyCLI extends CLI { + + public ConnectionCLI connectionCLI; + + public ConnectionModifyCLI(ConnectionCLI connectionCLI) { + super("mod", "Modify connection", connectionCLI); + this.connectionCLI = connectionCLI; + } + + public void printHelp() { + formatter.printHelp(getFullName() + " <Connection ID> [OPTIONS...]", options); + } + + public void execute(String[] args) throws Exception { + + Option option = new Option(null, "status", true, "Status: ENABLED, DISABLED."); + option.setArgName("status"); + options.addOption(option); + + option = new Option(null, "contents", true, "Input file containing connection 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 != 1) { + printHelp(); + System.exit(1); + } + + String connectionID = cmdArgs[0]; + String status = cmd.getOptionValue("status"); + String contents = cmd.getOptionValue("contents"); + + ConnectionModification connectionModification = new ConnectionModification(); + connectionModification.setID(connectionID); + connectionModification.setStatus(status); + + if (contents != null) { + // load contents from file + StringWriter sw = new StringWriter(); + try (BufferedReader in = new BufferedReader(new FileReader(contents)); + PrintWriter out = new PrintWriter(sw, true)) { + + String line; + while ((line = in.readLine()) != null) { + out.println(line); + } + + connectionModification.setContents(sw.toString()); + } + } + + ConnectionData connectionData = connectionCLI.connectionClient.modifyConnection(connectionID, connectionModification); + + MainCLI.printMessage("Modified connection \"" + connectionID + "\""); + + ConnectionCLI.printConnectionData(connectionData); + } +} diff --git a/base/java-tools/src/com/netscape/cmstools/tps/connection/ConnectionRemoveCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/connection/ConnectionRemoveCLI.java new file mode 100644 index 000000000..5169f23d9 --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/tps/connection/ConnectionRemoveCLI.java @@ -0,0 +1,53 @@ +// --- 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.connection; + +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +/** + * @author Endi S. Dewata + */ +public class ConnectionRemoveCLI extends CLI { + + public ConnectionCLI connectionCLI; + + public ConnectionRemoveCLI(ConnectionCLI connectionCLI) { + super("del", "Remove connection", connectionCLI); + this.connectionCLI = connectionCLI; + } + + public void printHelp() { + formatter.printHelp(getFullName() + " <Connection ID>", options); + } + + public void execute(String[] args) throws Exception { + + if (args.length != 1) { + printHelp(); + System.exit(1); + } + + String connectionID = args[0]; + + connectionCLI.connectionClient.removeConnection(connectionID); + + MainCLI.printMessage("Deleted connection \"" + connectionID + "\""); + } +} diff --git a/base/java-tools/src/com/netscape/cmstools/tps/connection/ConnectionShowCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/connection/ConnectionShowCLI.java new file mode 100644 index 000000000..7e660d394 --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/tps/connection/ConnectionShowCLI.java @@ -0,0 +1,87 @@ +// --- 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.connection; + +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.connection.ConnectionData; +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +/** + * @author Endi S. Dewata + */ +public class ConnectionShowCLI extends CLI { + + public ConnectionCLI connectionCLI; + + public ConnectionShowCLI(ConnectionCLI connectionCLI) { + super("show", "Show connection", connectionCLI); + this.connectionCLI = connectionCLI; + } + + public void printHelp() { + formatter.printHelp(getFullName() + " <Connection ID>", options); + } + + public void execute(String[] args) throws Exception { + + Option option = new Option(null, "contents", true, "Output file to store connection 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 != 1) { + printHelp(); + System.exit(1); + } + + String connectionID = args[0]; + String file = cmd.getOptionValue("contents"); + + ConnectionData connectionData = connectionCLI.connectionClient.getConnection(connectionID); + + MainCLI.printMessage("Connection \"" + connectionID + "\""); + ConnectionCLI.printConnectionData(connectionData); + + if (file != null) { + // store contents to file + PrintWriter out = new PrintWriter(new FileWriter(file)); + String contents = connectionData.getContents(); + if (contents != null) out.print(contents); + out.close(); + } + } +} |