KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > protocol > ws > client > FindCertsCommand


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.core.protocol.ws.client;
15
16 import java.io.File JavaDoc;
17 import java.io.FileOutputStream JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21
22 //import org.ejbca.core.model.authorization.wsclient.AuthorizationDeniedException;
23
//import org.ejbca.core.protocol.ws.wsclient.Certificate;
24
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 /**
33  * Finds a certificates in the database
34  *
35  * @version $Id: FindCertsCommand.java,v 1.2 2006/10/08 22:53:26 herrvendil Exp $
36  */

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     /**
46      * Creates a new instance of RaAddUserCommand
47      *
48      * @param args command line arguments
49      */

50     public FindCertsCommand(String JavaDoc[] args) {
51         super(args);
52     }
53
54     /**
55      * Runs the command
56      *
57      * @throws IllegalAdminCommandException Error in command args
58      * @throws ErrorAdminCommandException Error running command
59      */

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 JavaDoc username = args[ARG_USERNAME];
70             boolean onlyValid = getOnlyValid(args[ARG_ONLYVALID]);
71             String JavaDoc encoding = getEncoding(args[ARG_ENCODING]);
72             String JavaDoc outputPath = getOutputPath(args[ARG_OUTPUTPATH]);
73             
74             
75             try{
76                 List JavaDoc<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 JavaDoc 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 JavaDoc fos = new FileOutputStream JavaDoc(outputPath + "/" + username + "-" + i +".cer");
89                             fos.write(CertificateHelper.getCertificate(cert.getCertificateData()).getEncoded());
90                             fos.close();
91                         }else{
92                             FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(outputPath + "/" + username + "-" + i +".pem");
93                             ArrayList JavaDoc<java.security.cert.Certificate JavaDoc> list = new ArrayList JavaDoc<java.security.cert.Certificate JavaDoc>();
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 JavaDoc e) {
105             throw new ErrorAdminCommandException(e);
106         }
107     }
108
109
110
111     
112
113     private String JavaDoc getOutputPath(String JavaDoc outputpath) {
114         File JavaDoc dir = new File JavaDoc(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 JavaDoc getEncoding(String JavaDoc 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 JavaDoc 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; // Should never happen
150
}
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