diff options
author | Ade Lee <alee@redhat.com> | 2013-05-14 15:57:52 -0400 |
---|---|---|
committer | Ade Lee <alee@redhat.com> | 2013-07-22 11:03:02 -0400 |
commit | 9eb2c354b9e3a1d93f89ea951bb713cc684646ed (patch) | |
tree | 82587ac4e9982a63daf85d759a8f8eb11baae80b /base/java-tools/src/com/netscape/cmstools | |
parent | dbf97dfa2f163094b5ce0af299ec4bd902ed3488 (diff) | |
download | pki-9eb2c354b9e3a1d93f89ea951bb713cc684646ed.tar.gz pki-9eb2c354b9e3a1d93f89ea951bb713cc684646ed.tar.xz pki-9eb2c354b9e3a1d93f89ea951bb713cc684646ed.zip |
Add interfaces for managing profiles
This adds the initial framework for viewing and managing profiles.
Also adds CLI code for viewing/adding/deleting and editing profiles.
Diffstat (limited to 'base/java-tools/src/com/netscape/cmstools')
9 files changed, 451 insertions, 0 deletions
diff --git a/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java b/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java index 50095b173..ae93320f9 100644 --- a/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java @@ -39,6 +39,7 @@ import com.netscape.cmstools.cert.CertCLI; import com.netscape.cmstools.client.ClientCLI; import com.netscape.cmstools.group.GroupCLI; import com.netscape.cmstools.key.KeyCLI; +import com.netscape.cmstools.profile.ProfileCLI; import com.netscape.cmstools.system.KRAConnectorCLI; import com.netscape.cmstools.system.SecurityDomainCLI; import com.netscape.cmstools.user.UserCLI; @@ -69,6 +70,7 @@ public class MainCLI extends CLI { addModule(new GroupCLI(this)); addModule(new KeyCLI(this)); addModule(new KRAConnectorCLI(this)); + addModule(new ProfileCLI(this)); addModule(new SecurityDomainCLI(this)); addModule(new UserCLI(this)); } diff --git a/base/java-tools/src/com/netscape/cmstools/profile/ProfileAddCLI.java b/base/java-tools/src/com/netscape/cmstools/profile/ProfileAddCLI.java new file mode 100644 index 000000000..2c30fdbd7 --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileAddCLI.java @@ -0,0 +1,60 @@ +package com.netscape.cmstools.profile; + +import java.io.FileNotFoundException; + +import javax.xml.bind.JAXBException; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.ParseException; + +import com.netscape.certsrv.profile.ProfileData; +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +public class ProfileAddCLI extends CLI { + public ProfileCLI parent; + + public ProfileAddCLI(ProfileCLI parent) { + super("add", "Add profiles"); + this.parent = parent; + } + + public void printHelp() { + formatter.printHelp(parent.name + "-" + name + " <file>", options); + } + + public void execute(String[] args) { + CommandLine cmd = null; + + try { + cmd = parser.parse(options, args); + } catch (ParseException e) { + System.err.println("Error: " + e.getMessage()); + printHelp(); + System.exit(-1); + } + + String[] cLineArgs = cmd.getArgs(); + + if (cLineArgs.length < 1) { + System.err.println("Error: No filename specified."); + printHelp(); + System.exit(-1); + } + String filename = cLineArgs[0]; + if (filename == null || filename.trim().length() == 0) { + System.err.println("Error: Missing input file name."); + printHelp(); + System.exit(-1); + } + + try { + ProfileData data = ProfileCLI.readProfileFromFile(filename); + parent.client.createProfile(data); + MainCLI.printMessage("Added profile " + data.getId()); + } catch (FileNotFoundException | JAXBException e) { + System.err.println("Error: " + e.getMessage()); + System.exit(-1); + } + } +} diff --git a/base/java-tools/src/com/netscape/cmstools/profile/ProfileCLI.java b/base/java-tools/src/com/netscape/cmstools/profile/ProfileCLI.java new file mode 100644 index 000000000..37b88bcb5 --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileCLI.java @@ -0,0 +1,118 @@ +package com.netscape.cmstools.profile; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.util.Arrays; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Marshaller; +import javax.xml.bind.Unmarshaller; + +import org.apache.commons.lang.StringUtils; + +import com.netscape.certsrv.profile.ProfileClient; +import com.netscape.certsrv.profile.ProfileData; +import com.netscape.certsrv.profile.ProfileDataInfo; +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +public class ProfileCLI extends CLI { + public MainCLI parent; + public ProfileClient client; + + public ProfileCLI(MainCLI parent) { + super("profile", "Profile management commands"); + this.parent = parent; + + addModule(new ProfileFindCLI(this)); + addModule(new ProfileShowCLI(this)); + addModule(new ProfileAddCLI(this)); + addModule(new ProfileModifyCLI(this)); + addModule(new ProfileRemoveCLI(this)); + addModule(new ProfileEnableCLI(this)); + addModule(new ProfileDisableCLI(this)); + } + + public void printHelp() { + + System.out.println("Commands:"); + + int leftPadding = 1; + int rightPadding = 25; + + for (CLI module : modules.values()) { + String label = name + "-" + module.getName(); + + int padding = rightPadding - leftPadding - label.length(); + if (padding < 1) + padding = 1; + + System.out.print(StringUtils.repeat(" ", leftPadding)); + System.out.print(label); + System.out.print(StringUtils.repeat(" ", padding)); + System.out.println(module.getDescription()); + } + } + + public void execute(String[] args) throws Exception { + + client = new ProfileClient(parent.client); + + 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 printProfileDataInfo(ProfileDataInfo info) { + System.out.println("Profile ID: " + info.getProfileId()); + System.out.println("Profile URL: " + info.getProfileURL()); + } + + public static void printProfile(ProfileData profileData) { + // TODO Auto-generated method stub + + } + + public static void saveProfileToFile(String filename, ProfileData data) + throws JAXBException, FileNotFoundException { + JAXBContext context = JAXBContext.newInstance(ProfileData.class); + Marshaller marshaller = context.createMarshaller(); + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); + + FileOutputStream stream = new FileOutputStream(filename); + marshaller.marshal(data, stream); + + MainCLI.printMessage("Saved profile " + data.getId() + " to " + filename); + } + + public static ProfileData readProfileFromFile(String filename) + throws JAXBException, FileNotFoundException { + ProfileData data = null; + JAXBContext context = JAXBContext.newInstance(ProfileData.class); + Unmarshaller unmarshaller = context.createUnmarshaller(); + FileInputStream fis = new FileInputStream(filename); + data = (ProfileData) unmarshaller.unmarshal(fis); + return data; + } +} diff --git a/base/java-tools/src/com/netscape/cmstools/profile/ProfileDisableCLI.java b/base/java-tools/src/com/netscape/cmstools/profile/ProfileDisableCLI.java new file mode 100644 index 000000000..0279cf36a --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileDisableCLI.java @@ -0,0 +1,34 @@ +package com.netscape.cmstools.profile; + +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +public class ProfileDisableCLI extends CLI { + + public ProfileCLI parent; + + public ProfileDisableCLI(ProfileCLI parent) { + super("disable", "Disable profiles"); + this.parent = parent; + } + + public void printHelp() { + formatter.printHelp(parent.name + "-" + name + " <profile_id>", options); + } + + public void execute(String[] args) throws Exception { + + if (args.length != 1) { + printHelp(); + System.exit(1); + } + + String profileId = args[0]; + + parent.client.disableProfile(profileId); + + MainCLI.printMessage("Disabled profile \"" + profileId + "\""); + } + + +} diff --git a/base/java-tools/src/com/netscape/cmstools/profile/ProfileEnableCLI.java b/base/java-tools/src/com/netscape/cmstools/profile/ProfileEnableCLI.java new file mode 100644 index 000000000..727e49b0a --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileEnableCLI.java @@ -0,0 +1,33 @@ +package com.netscape.cmstools.profile; + +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +public class ProfileEnableCLI extends CLI { + + public ProfileCLI parent; + + public ProfileEnableCLI(ProfileCLI parent) { + super("enable", "Enable profiles"); + this.parent = parent; + } + + public void printHelp() { + formatter.printHelp(parent.name + "-" + name + " <profile_id>", options); + } + + public void execute(String[] args) throws Exception { + + if (args.length != 1) { + printHelp(); + System.exit(1); + } + + String profileId = args[0]; + + parent.client.enableProfile(profileId); + + MainCLI.printMessage("Enabled profile \"" + profileId + "\""); + } + +} diff --git a/base/java-tools/src/com/netscape/cmstools/profile/ProfileFindCLI.java b/base/java-tools/src/com/netscape/cmstools/profile/ProfileFindCLI.java new file mode 100644 index 000000000..9ced7c0b8 --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileFindCLI.java @@ -0,0 +1,39 @@ +package com.netscape.cmstools.profile; + +import java.util.Collection; + +import com.netscape.certsrv.profile.ProfileDataInfo; +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +public class ProfileFindCLI extends CLI { + + public ProfileCLI parent; + + public ProfileFindCLI(ProfileCLI parent) { + super("find", "Find profiles"); + this.parent = parent; + } + + public void printHelp() { + formatter.printHelp(parent.name + "-" + name + " [FILTER] [OPTIONS...]", options); + } + + public void execute(String[] args) throws Exception { + + Collection<ProfileDataInfo> infos = parent.client.listProfiles().getProfileInfos(); + boolean first = true; + + for (ProfileDataInfo info: infos) { + if (first) { + first = false; + } else { + System.out.println(); + } + ProfileCLI.printProfileDataInfo(info); + } + + MainCLI.printMessage("Number of entries returned " + infos.size()); + } + +} diff --git a/base/java-tools/src/com/netscape/cmstools/profile/ProfileModifyCLI.java b/base/java-tools/src/com/netscape/cmstools/profile/ProfileModifyCLI.java new file mode 100644 index 000000000..5259a7fe7 --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileModifyCLI.java @@ -0,0 +1,61 @@ +package com.netscape.cmstools.profile; + +import java.io.FileNotFoundException; + +import javax.xml.bind.JAXBException; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.ParseException; + +import com.netscape.certsrv.profile.ProfileData; +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +public class ProfileModifyCLI extends CLI { + + public ProfileCLI parent; + + public ProfileModifyCLI(ProfileCLI parent) { + super("mod", "Modify profiles"); + this.parent = parent; + } + + public void printHelp() { + formatter.printHelp(parent.name + "-" + name + " <file>", options); + } + + public void execute(String[] args) { + CommandLine cmd = null; + + try { + cmd = parser.parse(options, args); + } catch (ParseException e) { + System.err.println("Error: " + e.getMessage()); + printHelp(); + System.exit(-1); + } + + String[] cLineArgs = cmd.getArgs(); + + if (cLineArgs.length < 1) { + System.err.println("Error: No filename specified."); + printHelp(); + System.exit(-1); + } + String filename = cLineArgs[0]; + if (filename == null || filename.trim().length() == 0) { + System.err.println("Error: Missing input file name."); + printHelp(); + System.exit(-1); + } + + try { + ProfileData data = ProfileCLI.readProfileFromFile(filename); + parent.client.modifyProfile(data); + MainCLI.printMessage("Modified profile " + data.getId()); + } catch (FileNotFoundException | JAXBException e) { + System.err.println("Error: " + e.getMessage()); + System.exit(-1); + } + } +} diff --git a/base/java-tools/src/com/netscape/cmstools/profile/ProfileRemoveCLI.java b/base/java-tools/src/com/netscape/cmstools/profile/ProfileRemoveCLI.java new file mode 100644 index 000000000..c2e3eae53 --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileRemoveCLI.java @@ -0,0 +1,33 @@ +package com.netscape.cmstools.profile; + +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +public class ProfileRemoveCLI extends CLI { + + public ProfileCLI parent; + + public ProfileRemoveCLI(ProfileCLI parent) { + super("del", "Remove profiles"); + this.parent = parent; + } + + public void printHelp() { + formatter.printHelp(parent.name + "-" + name + " <profile_id>", options); + } + + public void execute(String[] args) throws Exception { + + if (args.length != 1) { + printHelp(); + System.exit(1); + } + + String profileId = args[0]; + + parent.client.deleteProfile(profileId); + + MainCLI.printMessage("Deleted profile \"" + profileId + "\""); + } + +} diff --git a/base/java-tools/src/com/netscape/cmstools/profile/ProfileShowCLI.java b/base/java-tools/src/com/netscape/cmstools/profile/ProfileShowCLI.java new file mode 100644 index 000000000..de83c1eb9 --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileShowCLI.java @@ -0,0 +1,71 @@ +package com.netscape.cmstools.profile; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.ParseException; + +import com.netscape.certsrv.profile.ProfileData; +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +public class ProfileShowCLI extends CLI { + + public ProfileCLI parent; + + public ProfileShowCLI(ProfileCLI parent) { + super("show", "Show profiles"); + this.parent = parent; + } + + public void printHelp() { + formatter.printHelp(parent.name + "-" + name + " <profile_id>", options); + } + + public void execute(String[] args) throws Exception { + CommandLine cmd = null; + + Option option = new Option(null, "output", true, "Output filename"); + option.setArgName("filename"); + options.addOption(option); + + try { + cmd = parser.parse(options, args); + } catch (ParseException e) { + System.err.println("Error: " + e.getMessage()); + printHelp(); + System.exit(-1); + } + + String[] cLineArgs = cmd.getArgs(); + if (cLineArgs.length < 1) { + System.err.println("Error: Missing profile ID."); + printHelp(); + System.exit(-1); + } + + String profileId = cLineArgs[0]; + + String filename = null; + if (cmd.hasOption("output")) { + filename = cmd.getOptionValue("output"); + } else { + System.err.println("Error: Missing output file name."); + printHelp(); + System.exit(-1); + } + + if (filename == null || filename.trim().length() == 0) { + System.err.println("Error: Missing output file name."); + printHelp(); + System.exit(-1); + } + + ProfileData profileData = parent.client.retrieveProfile(profileId); + + MainCLI.printMessage("Profile \"" + profileId + "\""); + + ProfileCLI.printProfile(profileData); + ProfileCLI.saveProfileToFile(filename, profileData); + } + +} |