KickJava   Java API By Example, From Geeks To Geeks.

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


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
17 import java.util.List JavaDoc;
18
19 import org.ejbca.core.model.ca.crl.RevokedCertInfo;
20 import org.ejbca.core.model.ra.UserDataConstants;
21 import org.ejbca.core.protocol.ws.client.gen.AuthorizationDeniedException_Exception;
22 import org.ejbca.core.protocol.ws.client.gen.UserDataVOWS;
23 import org.ejbca.core.protocol.ws.client.gen.UserMatch;
24 //import org.ejbca.core.protocol.ws.wsclient.UserDataVOWS;
25
//import org.ejbca.core.protocol.ws.wsclient.UserMatch;
26
import org.ejbca.ui.cli.ErrorAdminCommandException;
27 import org.ejbca.ui.cli.IAdminCommand;
28 import org.ejbca.ui.cli.IllegalAdminCommandException;
29
30 /**
31  * Revokes a given users certificate and set's it status to REVOKED
32  *
33  * @version $Id: RevokeUserCommand.java,v 1.2 2006/10/08 22:53:26 herrvendil Exp $
34  */

35 public class RevokeUserCommand extends EJBCAWSRABaseCommand implements IAdminCommand{
36
37     
38     private static final int ARG_USERNAME = 1;
39     private static final int ARG_REASON = 2;
40     private static final int ARG_DELETE = 3;
41     
42     
43     /**
44      * Creates a new instance of RevokeTokenCommand
45      *
46      * @param args command line arguments
47      */

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

58     public void execute() throws IllegalAdminCommandException, ErrorAdminCommandException {
59          try {
60            
61             if(args.length != 4){
62                 usage();
63                 System.exit(-1);
64             }
65             
66             String JavaDoc username = args[ARG_USERNAME];
67             int reason = getRevokeReason(args[ARG_REASON]);
68             boolean delete = getDelete(args[ARG_DELETE]);
69             
70             if(reason == RevokedCertInfo.NOT_REVOKED){
71                 getPrintStream().println("Error : Unsupported reason " + reason);
72                 usage();
73                 System.exit(-1);
74             }
75                         
76             try{
77                 UserMatch match = new UserMatch();
78                 match.setMatchtype(org.ejbca.util.query.UserMatch.MATCH_TYPE_EQUALS);
79                 match.setMatchwith(org.ejbca.util.query.UserMatch.MATCH_WITH_USERNAME);
80                 match.setMatchvalue(username);
81                                 
82                 List JavaDoc<UserDataVOWS> result = getEjbcaRAWS().findUser(match);
83                 if(result == null || result.size() != 1){
84                     getPrintStream().println("Error : User doesn't exist.");
85                     System.exit(-1);
86                 }
87                 
88                 UserDataVOWS user = result.iterator().next();
89                 if(user.getStatus() == UserDataConstants.STATUS_REVOKED){
90                     getPrintStream().println("Error : User already revoked.");
91                     System.exit(-1);
92                 }
93                 
94                 getEjbcaRAWS().revokeUser(username,reason,delete);
95                 getPrintStream().println("User revoked sucessfully");
96             }catch(AuthorizationDeniedException_Exception e){
97                 getPrintStream().println("Error : " + e.getMessage());
98             }
99         } catch (Exception JavaDoc e) {
100             throw new ErrorAdminCommandException(e);
101         }
102     }
103
104
105     private boolean getDelete(String JavaDoc delete) {
106         if(delete.equalsIgnoreCase("true")){
107             return true;
108         }
109         if(delete.equalsIgnoreCase("false")){
110             return false;
111         }
112         usage();
113         System.exit(-1);
114         return false; // Should never happen
115
}
116
117
118     protected void usage() {
119         getPrintStream().println("Command used to revoke a users certificate");
120         getPrintStream().println("Usage : revokecert <hardtokensn> <reason> <delete (true|false)> \n\n");
121         getPrintStream().println("Reason should be one of : ");
122         for(int i=1; i< REASON_TEXTS.length-1;i++){
123             getPrintStream().print(REASON_TEXTS[i] + ", ");
124         }
125         getPrintStream().print(REASON_TEXTS[REASON_TEXTS.length-1]);
126    }
127
128
129 }
130
Popular Tags