KickJava   Java API By Example, From Geeks To Geeks.

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


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.math.BigInteger JavaDoc;
17
18
19 import org.ejbca.core.model.ca.crl.RevokedCertInfo;
20 import org.ejbca.core.protocol.ws.client.gen.AuthorizationDeniedException_Exception;
21 import org.ejbca.core.protocol.ws.client.gen.RevokeStatus;
22 import org.ejbca.ui.cli.ErrorAdminCommandException;
23 import org.ejbca.ui.cli.IAdminCommand;
24 import org.ejbca.ui.cli.IllegalAdminCommandException;
25 import org.ejbca.util.CertTools;
26
27 /**
28  * Revokes a given certificate
29  *
30  * @version $Id: RevokeCertCommand.java,v 1.2 2006/10/08 22:53:26 herrvendil Exp $
31  */

32 public class RevokeCertCommand extends EJBCAWSRABaseCommand implements IAdminCommand{
33
34     
35     private static final int ARG_ISSUERDN = 1;
36     private static final int ARG_CERTSN = 2;
37     private static final int ARG_REASON = 3;
38     
39     
40     /**
41      * Creates a new instance of RevokeCertCommand
42      *
43      * @param args command line arguments
44      */

45     public RevokeCertCommand(String JavaDoc[] args) {
46         super(args);
47     }
48
49     /**
50      * Runs the command
51      *
52      * @throws IllegalAdminCommandException Error in command args
53      * @throws ErrorAdminCommandException Error running command
54      */

55     public void execute() throws IllegalAdminCommandException, ErrorAdminCommandException {
56
57         try {
58            
59             if(args.length != 4){
60                 usage();
61                 System.exit(-1);
62             }
63             
64             String JavaDoc issuerdn = CertTools.stringToBCDNString(args[ARG_ISSUERDN]);
65             String JavaDoc certsn = getCertSN(args[ARG_CERTSN]);
66             int reason = getRevokeReason(args[ARG_REASON]);
67             
68             if(reason == RevokedCertInfo.NOT_REVOKED){
69                 getPrintStream().println("Error : Unsupported reason " + reason);
70                 usage();
71                 System.exit(-1);
72             }
73                         
74             try{
75                 
76                 RevokeStatus status = getEjbcaRAWS().checkRevokationStatus(issuerdn,certsn);
77                 if(status.getReason() != RevokedCertInfo.NOT_REVOKED){
78                     getPrintStream().println("Error : Certificate is already revoked");
79                     System.exit(-1);
80                 }
81                 
82                 getEjbcaRAWS().revokeCert(issuerdn,certsn,reason);
83                 getPrintStream().println("Certificate revoked sucessfully");
84             }catch(AuthorizationDeniedException_Exception e){
85                 getPrintStream().println("Error : " + e.getMessage());
86             }
87         } catch (Exception JavaDoc e) {
88             throw new ErrorAdminCommandException(e);
89         }
90     }
91
92
93     private String JavaDoc getCertSN(String JavaDoc certsn) {
94         try{
95             new BigInteger JavaDoc(certsn,16);
96         }catch(NumberFormatException JavaDoc e){
97             getPrintStream().println("Error in Certificate SN");
98             usage();
99             System.exit(-1);
100         }
101         return certsn;
102     }
103
104     protected void usage() {
105         getPrintStream().println("Command used to revoke a certificate");
106         getPrintStream().println("Usage : revokecert <issuerdn> <certificatesn (HEX)> <reason> \n\n");
107         getPrintStream().println("Reason should be one of : ");
108         for(int i=1; i< REASON_TEXTS.length-1;i++){
109             getPrintStream().print(REASON_TEXTS[i] + ", ");
110         }
111         getPrintStream().print(REASON_TEXTS[REASON_TEXTS.length-1]);
112    }
113
114
115 }
116
Popular Tags