KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > ui > cli > CaGetRootCertCommand


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13  
14 package org.ejbca.ui.cli;
15
16 import java.io.*;
17 import java.util.ArrayList JavaDoc;
18 import java.security.cert.X509Certificate JavaDoc;
19
20 import org.ejbca.util.CertTools;
21
22
23 /**
24  * Export root CA certificate.
25  *
26  * @version $Id: CaGetRootCertCommand.java,v 1.1 2006/01/17 20:28:05 anatom Exp $
27  */

28 public class CaGetRootCertCommand extends BaseCaAdminCommand {
29     /**
30      * Creates a new instance of CaGetRootCertCommand
31      *
32      * @param args command line arguments
33      */

34     public CaGetRootCertCommand(String JavaDoc[] args) {
35         super(args);
36     }
37
38     /**
39      * Runs the command
40      *
41      * @throws IllegalAdminCommandException Error in command args
42      * @throws ErrorAdminCommandException Error running command
43      */

44     public void execute() throws IllegalAdminCommandException, ErrorAdminCommandException {
45         
46         if (args.length < 3) {
47             String JavaDoc msg = "Save root CA certificate (PEM- or DER-format) to file.\n";
48             msg += "Usage: CA getrootcert <caname> <filename> <-der>";
49             throw new IllegalAdminCommandException(msg);
50         }
51         
52         String JavaDoc caname = args[1];
53         String JavaDoc filename = args[2];
54         boolean pem = true;
55         if (args.length > 3) {
56             if (("-der").equals(args[3])) {
57                 pem = false;
58             }
59         }
60             
61         getOutputStream().flush();
62         try {
63             ArrayList JavaDoc chain = new ArrayList JavaDoc(getCertChain(caname));
64             if (chain.size() > 0) {
65                 X509Certificate JavaDoc rootcert = (X509Certificate JavaDoc)chain.get(chain.size()-1);
66  
67                 FileOutputStream fos = new FileOutputStream(filename);
68                 if (pem) {
69                     fos.write(CertTools.getPEMFromCerts(chain));
70                 } else {
71                     fos.write(rootcert.getEncoded());
72                 }
73                 fos.close();
74                 getOutputStream().println("Wrote Root CA certificate to '" + filename + "'");
75             } else {
76                 getOutputStream().println("No CA certificate found.");
77             }
78         } catch (Exception JavaDoc e) {
79             throw new ErrorAdminCommandException(e);
80         }
81     } // execute
82
}
83
Popular Tags