1 /*
2  * @(#) $Id: ListCommand.java,v 1.2 2003/07/08 08:13:52 pankaj Exp $
3  *
4  * Copyright (c) 2002-03 by Pankaj Kumar (http://www.pankaj-k.net). 
5  * All rights reserved.
6  *
7  * The license governing the use of this file can be found in the 
8  * root directory of the containing software.
9  */
10package org.jstk.cert.rep;
11
12import java.util.HashMap;
13import java.util.Iterator;
14import java.security.cert.X509Certificate;
15import java.security.cert.X509CRL;
16import java.io.File;
17import java.io.InputStream;
18import java.io.FileInputStream;
19import java.io.BufferedInputStream;
20
21import org.jstk.*;
22
23public class ListCommand extends JSTKCommandAdapter {
24    private int index = 0;
25    private static HashMap defaults = new HashMap();
26    static {
27        defaults.put("reptype", "JSTK");
28        defaults.put("repfile", "my.rep");
29    }
30
31    public String briefDescription(){
32        String briefDesc = "lists the contents of a repository";
33        return briefDesc;
34    }
35
36    public String optionsDescription(){
37        String optionsDesc =
38            "  -repfile <repfile>: Repository file.[" +
39            defaults.get("repfile") + "]\n";
40        return optionsDesc;
41    }
42    public String[] useForms(){
43        String[] useForms = {
44            "[-repfile <repfile>]"
45        };
46        return useForms;
47    }
48    public String[] sampleUses(){
49        String[] sampleUses = {
50            "-repfile test.rep"
51        };
52        return sampleUses;
53    }
54
55    public void formatX509Certificate(X509Certificate cert, StringBuffer sb){
56        sb.append("X509CERT[" + index + "]: ");
57        sb.append("Serial No: " + cert.getSerialNumber() );
58        sb.append(", Issuer: " + cert.getIssuerX500Principal());
59        sb.append(", Subject: " + cert.getSubjectX500Principal() + "\n");
60    }
61
62    public void formatX509CRL(X509CRL crl, StringBuffer sb){
63        sb.append("X509CRL[" + index + "]: ");
64        sb.append("Issuer: " + crl.getIssuerX500Principal());
65        sb.append(", This Update: " + crl.getThisUpdate());
66        sb.append(", Next Update: " + crl.getNextUpdate() + "\n");
67    }
68
69    public Object execute(JSTKArgs args) throws JSTKException{
70        try {
71            args.setDefaults(defaults);
72            String infile = args.get("infile");
73            String repfile = args.get("repfile");
74            FileBasedRepository fbr = new FileBasedRepository(repfile);
75            Iterator itr = fbr.getRepository().iterator();
76            StringBuffer sb = new StringBuffer();
77
78            index = 0;
79            while (itr.hasNext()){
80                Object entry = itr.next();
81                if (entry instanceof X509Certificate){
82                    formatX509Certificate((X509Certificate)entry, sb);
83                } else if (entry instanceof X509CRL){
84                    formatX509CRL((X509CRL)entry, sb);
85                }
86                ++index;
87            }
88
89            return new JSTKResult(null, true, sb.toString());
90        } catch (Exception exc){
91            throw new JSTKException("ListCommand execution failed", exc);
92        }
93    }
94
95    public static void main(String[] args) throws Exception {
96        JSTKOptions opts = new JSTKOptions();
97        opts.parse(args, 0);
98        ListCommand listCmd = new ListCommand();
99        JSTKResult result = (JSTKResult)listCmd.execute(opts);
00        System.out.println(result.getText());
01        System.exit(result.isSuccess()? 0 : 1);
02    }
03}