KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19
20 import org.ejbca.core.model.SecConst;
21 import org.ejbca.core.model.ra.UserDataConstants;
22 import org.ejbca.core.protocol.ws.client.gen.AuthorizationDeniedException_Exception;
23 import org.ejbca.core.protocol.ws.client.gen.UserDataVOWS;
24 import org.ejbca.core.protocol.ws.client.gen.UserMatch;
25 //import org.ejbca.core.protocol.ws.wsclient.UserDataVOWS;
26
//import org.ejbca.core.protocol.ws.wsclient.UserMatch;
27
import org.ejbca.ui.cli.ErrorAdminCommandException;
28 import org.ejbca.ui.cli.IAdminCommand;
29 import org.ejbca.ui.cli.IllegalAdminCommandException;
30
31 /**
32  * Finds a user in the database
33  *
34  * @version $Id: FindUserCommand.java,v 1.2 2006/10/08 22:53:26 herrvendil Exp $
35  */

36 public class FindUserCommand extends EJBCAWSRABaseCommand implements IAdminCommand{
37
38     
39     private static final int ARG_MATCHWITH = 1;
40     private static final int ARG_MATCHTYPE = 2;
41     private static final int ARG_MATCHVALUE = 3;
42     
43     
44
45     
46     private static final String JavaDoc[] MATCHWITHTEXTS = {"USERNAME","EMAIL", "STATUS", "ENDENTITYPROFILE",
47                                                "CERTIFICATEPROFILE", "CA", "TOKEN", "DN", "UID",
48                                                "COMMONNAME","DNSERIALNUMBER","GIVENNAME","INITIALS",
49                                                "SURNAME","TITLE","ORGANIZATIONUNIT","ORGANIZATION",
50                                                "LOCALE","STATE","DOMAINCOMPONENT","COUNTRY"};
51
52     private static final int[] MATCHWITHVALUES = { org.ejbca.util.query.UserMatch.MATCH_WITH_USERNAME,
53                                                    org.ejbca.util.query.UserMatch.MATCH_WITH_EMAIL,
54                                                    org.ejbca.util.query.UserMatch.MATCH_WITH_STATUS,
55                                                    org.ejbca.util.query.UserMatch.MATCH_WITH_ENDENTITYPROFILE,
56                                                    org.ejbca.util.query.UserMatch.MATCH_WITH_CERTIFICATEPROFILE,
57                                                    org.ejbca.util.query.UserMatch.MATCH_WITH_CA,
58                                                    org.ejbca.util.query.UserMatch.MATCH_WITH_TOKEN,
59                                                    org.ejbca.util.query.UserMatch.MATCH_WITH_DN,
60                                                    org.ejbca.util.query.UserMatch.MATCH_WITH_UID,
61                                                    org.ejbca.util.query.UserMatch.MATCH_WITH_COMMONNAME,
62                                                    org.ejbca.util.query.UserMatch.MATCH_WITH_DNSERIALNUMBER,
63                                                    org.ejbca.util.query.UserMatch.MATCH_WITH_GIVENNAME,
64                                                    org.ejbca.util.query.UserMatch.MATCH_WITH_INITIALS,
65                                                    org.ejbca.util.query.UserMatch.MATCH_WITH_SURNAME,
66                                                    org.ejbca.util.query.UserMatch.MATCH_WITH_TITLE,
67                                                    org.ejbca.util.query.UserMatch.MATCH_WITH_ORGANIZATIONUNIT,
68                                                    org.ejbca.util.query.UserMatch.MATCH_WITH_ORGANIZATION,
69                                                    org.ejbca.util.query.UserMatch.MATCH_WITH_LOCALE,
70                                                    org.ejbca.util.query.UserMatch.MATCH_WITH_STATE,
71                                                    org.ejbca.util.query.UserMatch.MATCH_WITH_DOMAINCOMPONENT,
72                                                    org.ejbca.util.query.UserMatch.MATCH_WITH_COUNTRY};
73     
74     private static final String JavaDoc[] STATUS_TEXTS = {"NEW", "FAILED","INITIALIZED",
75                                                   "INPROCESS","GENERATED","REVOKED",
76                                                   "HISTORICAL","KEYRECOVERY"};
77     private static final int[] STATUS_VALUES = {UserDataConstants.STATUS_NEW, UserDataConstants.STATUS_FAILED, UserDataConstants.STATUS_INITIALIZED,
78                                                 UserDataConstants.STATUS_INPROCESS, UserDataConstants.STATUS_GENERATED,
79                                                 UserDataConstants.STATUS_REVOKED, UserDataConstants.STATUS_HISTORICAL,
80                                                 UserDataConstants.STATUS_KEYRECOVERY};
81     
82     /**
83      * Creates a new instance of RaAddUserCommand
84      *
85      * @param args command line arguments
86      */

87     public FindUserCommand(String JavaDoc[] args) {
88         super(args);
89     }
90
91     /**
92      * Runs the command
93      *
94      * @throws IllegalAdminCommandException Error in command args
95      * @throws ErrorAdminCommandException Error running command
96      */

97     public void execute() throws IllegalAdminCommandException, ErrorAdminCommandException {
98
99         try {
100            
101             if(args.length != 4){
102                 usage();
103                 System.exit(-1);
104             }
105             
106             int matchwith = getMatchWith(args[ARG_MATCHWITH]);
107             int matchtype = getMatchType(args[ARG_MATCHTYPE], matchwith);
108             String JavaDoc matchvalue = args[ARG_MATCHVALUE];
109             
110             
111             try{
112                 UserMatch match = new UserMatch();
113                 match.setMatchtype(matchtype);
114                 match.setMatchvalue(matchvalue);
115                 match.setMatchwith(matchwith);
116                 
117                 List JavaDoc<UserDataVOWS> result = getEjbcaRAWS().findUser(match);
118                 
119                 Iterator JavaDoc iter = result.iterator();
120                 if(result==null || result.size() == 0){
121                     getPrintStream().println("No matching users could be found in database");
122                 }else{
123                     getPrintStream().println("The following users found in database :");
124                     for(int i=0;i<result.size();i++){
125                         UserDataVOWS next = (UserDataVOWS) iter.next();
126                         getPrintStream().println("\nUser : " + (i +1));
127                         getPrintStream().println(" Username: "+next.getUsername());
128                         getPrintStream().println(" Subject DN: "+next.getSubjectDN());
129                         if(next.getSubjectAltName() == null){
130                             getPrintStream().println(" Subject Altname: NONE");
131                         }else{
132                             getPrintStream().println(" Subject Altname: "+next.getSubjectAltName());
133                         }
134                         if(next.getEmail() == null){
135                             getPrintStream().println(" Email: NONE");
136                         }else{
137                             getPrintStream().println(" Email: "+next.getEmail());
138                         }
139                         getPrintStream().println(" CA Name: "+next.getCaName());
140                         getPrintStream().println(" Type: "+getType(next));
141                         getPrintStream().println(" Token: "+next.getTokenType());
142                         getPrintStream().println(" Status: "+ getStatus(next.getStatus()));
143                         getPrintStream().println(" Certificate profile: "+next.getCertificateProfileName());
144                         getPrintStream().println(" End entity profile: "+next.getEndEntityProfileName());
145                         if(next.getHardTokenIssuerName() == null){
146                             getPrintStream().println(" Hard Token Issuer Alias: NONE");
147                         }else{
148                             getPrintStream().println(" Hard Token Issuer Alias: " + next.getHardTokenIssuerName());
149                         }
150                     }
151                 }
152                              
153             }catch(AuthorizationDeniedException_Exception e){
154                 getPrintStream().println("Error : " + e.getMessage());
155             }
156         } catch (Exception JavaDoc e) {
157             throw new ErrorAdminCommandException(e);
158         }
159     }
160
161     private int getMatchType(String JavaDoc matchtype, int matchwith) {
162         if(matchtype.equalsIgnoreCase("EQUALS")){
163             if(matchwith >= org.ejbca.util.query.UserMatch.MATCH_WITH_UID){
164                 getPrintStream().println("Error: Matchtype 'EQUALS' cannot be used with DN fields, only 'BEGINSWITH' can be used.");
165                 usage();
166                 System.exit(-1);
167             }else{
168                 return org.ejbca.util.query.UserMatch.MATCH_TYPE_EQUALS;
169             }
170         }
171         if(matchtype.equalsIgnoreCase("BEGINSWITH")){
172             if(matchwith > org.ejbca.util.query.UserMatch.MATCH_WITH_UID || matchwith == org.ejbca.util.query.UserMatch.MATCH_WITH_DN
173                     || matchwith == org.ejbca.util.query.UserMatch.MATCH_WITH_EMAIL){
174                 return org.ejbca.util.query.UserMatch.MATCH_TYPE_BEGINSWITH;
175             }else{
176                 getPrintStream().println("Error: Matchtype 'BEGINSWITH' can only be used with DN fields, full DN and EMAIL, use EQUALS");
177                 usage();
178                 System.exit(-1);
179             }
180         }
181         if(matchtype.equalsIgnoreCase("CONTAINS")){
182             if(matchwith == org.ejbca.util.query.UserMatch.MATCH_WITH_DN){
183               return org.ejbca.util.query.UserMatch.MATCH_TYPE_BEGINSWITH;
184             }else{
185               getPrintStream().println("Error: Matchtype 'CONTAINS' can only be used with matchwith 'DN'.");
186               usage();
187               System.exit(-1);
188             }
189     }
190         usage();
191         System.exit(-1);
192         return 0; // will never happen
193
}
194
195     private int getMatchWith(String JavaDoc matchwith) {
196         for(int i=0;i< MATCHWITHTEXTS.length; i++){
197             if(MATCHWITHTEXTS[i].equalsIgnoreCase(matchwith)){
198                 return MATCHWITHVALUES[i];
199             }
200         }
201         usage();
202         
203         System.exit(-1);
204         return 0; // Will never happen
205
}
206
207     private String JavaDoc getStatus(int status){
208         for(int i=0;i<STATUS_VALUES.length;i++){
209             if(STATUS_VALUES[i] == status){
210                 return STATUS_TEXTS[i];
211             }
212         }
213         return "ERROR : Status text not found";
214     }
215     
216     private int getType(UserDataVOWS userData) {
217         int type = 1;
218         
219         if(userData.isSendNotification())
220             type = type | SecConst.USER_SENDNOTIFICATION;
221         else
222             type = type & (~SecConst.USER_SENDNOTIFICATION);
223         
224         if(userData.isKeyRecoverable())
225             type = type | SecConst.USER_KEYRECOVERABLE;
226         else
227             type = type & (~SecConst.USER_KEYRECOVERABLE);
228                 
229         return type;
230     }
231     
232     protected void usage() {
233         getPrintStream().println("Command used to find userdata, maximum of 100 users will be returned ");
234         getPrintStream().println("Usage : finduser <matchwith> <matchtype> <value> \n\n");
235         getPrintStream().print("Matchwith can be : ");
236         for(int i=0;i<MATCHWITHTEXTS.length-1;i++){
237             getPrintStream().print(MATCHWITHTEXTS[i] +", ");
238         }
239         getPrintStream().println(MATCHWITHTEXTS[MATCHWITHTEXTS.length-1]);
240         getPrintStream().println("Matchtype can be : EQUALS (not DN fields), BEGINSWITH (DN fields), CONTAINS (matchwith complete DN only)");
241     }
242
243
244 }
245
Popular Tags