1 13 14 package org.ejbca.ui.cli; 15 16 import java.beans.XMLEncoder ; 17 import java.io.FileOutputStream ; 18 import java.util.Collection ; 19 import java.util.Iterator ; 20 21 import org.ejbca.core.model.SecConst; 22 import org.ejbca.core.model.ca.certificateprofiles.CertificateProfile; 23 import org.ejbca.core.model.ra.raadmin.EndEntityProfile; 24 25 26 27 32 public class CaExportProfilesCommand extends BaseCaAdminCommand { 33 38 public CaExportProfilesCommand(String [] args) { 39 super(args); 40 } 41 42 48 public void execute() throws IllegalAdminCommandException, ErrorAdminCommandException { 49 try { 50 Collection certprofids = getCertificateStoreSession().getAuthorizedCertificateProfileIds(administrator,0); 51 Collection endentityprofids = getRaAdminSession().getAuthorizedEndEntityProfileIds(administrator); 52 53 if (args.length < 2) { 54 getOutputStream().println( 55 "Usage: CA exportprofiles <outpath>"); 56 getOutputStream().print("\n"); 57 return; 58 } 59 60 String outpath = args[1]; 61 62 getOutputStream().println("Exporting certificate profiles: "); 63 Iterator iter = certprofids.iterator(); 64 while (iter.hasNext()) { 65 int profileid = ((Integer ) iter.next()).intValue(); 66 if (profileid == SecConst.PROFILE_NO_PROFILE) { getOutputStream().println("Error : Couldn't find certificate profile '"+profileid+"' in database."); 68 } else { 69 String profilename = getCertificateStoreSession().getCertificateProfileName(administrator, profileid); 70 CertificateProfile profile = getCertificateStoreSession().getCertificateProfile(administrator,profileid); 71 if (profile == null) { 72 getOutputStream().println("Error : Couldn't find certificate profile '"+profilename+"'-"+profileid+" in database."); 73 } else { 74 String outfile = outpath+"/certprofile_"+profilename+"-"+profileid+".xml"; 75 getOutputStream().println(outfile+"."); 76 XMLEncoder encoder = new XMLEncoder (new FileOutputStream (outfile)); 77 encoder.writeObject(profile.saveData()); 78 encoder.close(); 79 } 80 } 81 } 82 83 getOutputStream().println("Exporting end entity profiles: "); 84 iter = endentityprofids.iterator(); 85 while (iter.hasNext()){ 86 int profileid = ((Integer ) iter.next()).intValue(); 87 if (profileid == SecConst.PROFILE_NO_PROFILE) { getOutputStream().println("Error : Couldn't find entity profile '"+profileid+"' in database."); 89 } else { 90 String profilename = getRaAdminSession().getEndEntityProfileName(administrator, profileid); 91 EndEntityProfile profile = getRaAdminSession().getEndEntityProfile(administrator, profileid); 92 if (profile == null) { 93 getOutputStream().println("Error : Couldn't find entity profile '"+profilename+"'-"+profileid+" in database."); 94 } else { 95 String outfile = outpath+"/entityprofile_"+profilename+"-"+profileid+".xml"; 96 getOutputStream().println(outfile+"."); 97 XMLEncoder encoder = new XMLEncoder (new FileOutputStream (outfile)); 98 encoder.writeObject(profile.saveData()); 99 encoder.close(); 100 } 101 } 102 } 103 } catch (Exception e) { 104 throw new ErrorAdminCommandException(e); 105 } 106 } 107 } 108 | Popular Tags |