1 13 14 package org.ejbca.core.protocol.ws.client; 15 16 import java.io.File ; 17 import java.io.FileOutputStream ; 18 import java.util.ArrayList ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 22 import org.ejbca.core.protocol.ws.client.gen.AuthorizationDeniedException_Exception; 25 import org.ejbca.core.protocol.ws.client.gen.Certificate; 26 import org.ejbca.core.protocol.ws.common.CertificateHelper; 27 import org.ejbca.ui.cli.ErrorAdminCommandException; 28 import org.ejbca.ui.cli.IAdminCommand; 29 import org.ejbca.ui.cli.IllegalAdminCommandException; 30 import org.ejbca.util.CertTools; 31 32 37 public class FindCertsCommand extends EJBCAWSRABaseCommand implements IAdminCommand{ 38 39 40 private static final int ARG_USERNAME = 1; 41 private static final int ARG_ONLYVALID = 2; 42 private static final int ARG_ENCODING = 3; 43 private static final int ARG_OUTPUTPATH = 4; 44 45 50 public FindCertsCommand(String [] args) { 51 super(args); 52 } 53 54 60 public void execute() throws IllegalAdminCommandException, ErrorAdminCommandException { 61 62 try { 63 64 if(args.length != 5){ 65 usage(); 66 System.exit(-1); 67 } 68 69 String username = args[ARG_USERNAME]; 70 boolean onlyValid = getOnlyValid(args[ARG_ONLYVALID]); 71 String encoding = getEncoding(args[ARG_ENCODING]); 72 String outputPath = getOutputPath(args[ARG_OUTPUTPATH]); 73 74 75 try{ 76 List <Certificate> result = getEjbcaRAWS().findCerts(username, onlyValid); 77 78 if(result==null || result.size() == 0){ 79 getPrintStream().println("No certificate could be found for user"); 80 }else{ 81 getPrintStream().println(result.size() + " certificate found, written to " + outputPath); 82 Iterator iter = result.iterator(); 83 int i=0; 84 while(iter.hasNext()){ 85 i++; 86 Certificate cert = (Certificate) iter.next(); 87 if(encoding.equals("DER")){ 88 FileOutputStream fos = new FileOutputStream (outputPath + "/" + username + "-" + i +".cer"); 89 fos.write(CertificateHelper.getCertificate(cert.getCertificateData()).getEncoded()); 90 fos.close(); 91 }else{ 92 FileOutputStream fos = new FileOutputStream (outputPath + "/" + username + "-" + i +".pem"); 93 ArrayList <java.security.cert.Certificate > list = new ArrayList <java.security.cert.Certificate >(); 94 list.add(CertificateHelper.getCertificate(cert.getCertificateData())); 95 fos.write(CertTools.getPEMFromCerts(list)); 96 fos.close(); 97 } 98 } 99 } 100 101 }catch(AuthorizationDeniedException_Exception e){ 102 getPrintStream().println("Error : " + e.getMessage()); 103 } 104 } catch (Exception e) { 105 throw new ErrorAdminCommandException(e); 106 } 107 } 108 109 110 111 112 113 private String getOutputPath(String outputpath) { 114 File dir = new File (outputpath); 115 if(!dir.exists()){ 116 getPrintStream().println("Error : Output directory doesn't seem to exist."); 117 System.exit(-1); 118 } 119 if(!dir.isDirectory()){ 120 getPrintStream().println("Error : Output directory doesn't seem to be a directory."); 121 System.exit(-1); 122 } 123 if(!dir.canWrite()){ 124 getPrintStream().println("Error : Output directory isn't writeable."); 125 System.exit(-1); 126 127 } 128 return outputpath; 129 } 130 131 private String getEncoding(String encoding) { 132 if(!encoding.equalsIgnoreCase("PEM") && !encoding.equalsIgnoreCase("DER")){ 133 usage(); 134 System.exit(-1); 135 } 136 137 return encoding.toUpperCase(); 138 } 139 140 private boolean getOnlyValid(String onlyValid) { 141 if(onlyValid.equalsIgnoreCase("true")){ 142 return true; 143 } 144 if(onlyValid.equalsIgnoreCase("false")){ 145 return false; 146 } 147 usage(); 148 System.exit(-1); 149 return false; } 151 152 protected void usage() { 153 getPrintStream().println("Command used to find a users certificates"); 154 getPrintStream().println("Usage : findcerts <username> <onlyvalid (true|false)> <encoding (DER|PEM)> <outputpath> \n\n"); 155 getPrintStream().println("onlyvalid = true only returns nonexired and unrevoked certificates "); 156 getPrintStream().println("outputpath : directory where certificates are written in form username+nn "); 157 } 158 159 160 } 161 | Popular Tags |