KickJava   Java API By Example, From Geeks To Geeks.

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


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

34 public class EditUserCommand extends EJBCAWSRABaseCommand implements IAdminCommand{
35
36     
37     private static final int ARG_USERNAME = 1;
38     private static final int ARG_PASSWORD = 2;
39     private static final int ARG_CLEARPWD = 3;
40     private static final int ARG_SUBJECTDN = 4;
41     private static final int ARG_SUBJECTALTNAME = 5;
42     private static final int ARG_EMAIL = 6;
43     private static final int ARG_CA = 7;
44     private static final int ARG_TYPE = 8;
45     private static final int ARG_TOKEN = 9;
46     private static final int ARG_STATUS = 10;
47     private static final int ARG_ENDENTITYPROFILE = 11;
48     private static final int ARG_CERTIFICATEPROFILE = 12;
49     private static final int ARG_ISSUERALIAS = 13;
50     
51     /**
52      * Creates a new instance of RaAddUserCommand
53      *
54      * @param args command line arguments
55      */

56     public EditUserCommand(String JavaDoc[] args) {
57         super(args);
58     }
59
60     /**
61      * Runs the command
62      *
63      * @throws IllegalAdminCommandException Error in command args
64      * @throws ErrorAdminCommandException Error running command
65      */

66     public void execute() throws IllegalAdminCommandException, ErrorAdminCommandException {
67         
68         try {
69            
70             if(args.length < 13 || args.length > 14){
71                 usage();
72                 System.exit(-1);
73             }
74             
75
76             UserDataVOWS userdata = new UserDataVOWS();
77             userdata.setUsername(args[ARG_USERNAME]);
78             userdata.setPassword(args[ARG_PASSWORD]);
79             userdata.setClearPwd(args[ARG_CLEARPWD].equalsIgnoreCase("true"));
80             userdata.setSubjectDN(args[ARG_SUBJECTDN]);
81             if(!args[ARG_SUBJECTALTNAME].equalsIgnoreCase("NULL")){
82                 userdata.setSubjectAltName(args[ARG_SUBJECTALTNAME]);
83             }
84             if(!args[ARG_EMAIL].equalsIgnoreCase("NULL")){
85                 userdata.setEmail(args[ARG_EMAIL]);
86             }
87             userdata.setCaName(args[ARG_CA]);
88             userdata.setTokenType(args[ARG_TOKEN]);
89             userdata.setStatus(getStatus(args[ARG_STATUS]));
90             userdata.setEndEntityProfileName(args[ARG_ENDENTITYPROFILE]);
91             userdata.setCertificateProfileName(args[ARG_CERTIFICATEPROFILE]);
92             
93             int type = Integer.parseInt(args[ARG_TYPE]);
94             
95             if((type & SecConst.USER_SENDNOTIFICATION) != 0){
96                 userdata.setSendNotification(true);
97             }
98             if((type & SecConst.USER_KEYRECOVERABLE) != 0){
99                 userdata.setKeyRecoverable(true);
100             }
101
102             if(args.length == 14){
103               userdata.setHardTokenIssuerName(args[ARG_ISSUERALIAS]);
104             }
105    
106             getPrintStream().println("Trying to add user:");
107             getPrintStream().println("Username: "+userdata.getUsername());
108             getPrintStream().println("Subject DN: "+userdata.getSubjectDN());
109             getPrintStream().println("Subject Altname: "+userdata.getSubjectAltName());
110             getPrintStream().println("Email: "+userdata.getEmail());
111             getPrintStream().println("CA Name: "+userdata.getCaName());
112             getPrintStream().println("Type: "+type);
113             getPrintStream().println("Token: "+userdata.getTokenType());
114             getPrintStream().println("Status: "+userdata.getStatus());
115             getPrintStream().println("End entity profile: "+userdata.getEndEntityProfileName());
116             getPrintStream().println("Certificate profile: "+userdata.getCertificateProfileName());
117
118             if(userdata.getHardTokenIssuerName() == null){
119                 getPrintStream().println("Hard Token Issuer Alias: NONE");
120             }else{
121                 getPrintStream().println("Hard Token Issuer Alias: " + userdata.getHardTokenIssuerName());
122             }
123             
124             
125            try{
126                 getEjbcaRAWS().editUser(userdata);
127
128                 
129                 getPrintStream().println("User '"+userdata.getUsername()+"' has been added/edited.");
130                 getPrintStream().println();
131             }catch(AuthorizationDeniedException_Exception e){
132                 getPrintStream().println("Error : " + e.getMessage());
133             }catch(UserDoesntFullfillEndEntityProfile_Exception e){
134                 getPrintStream().println("Error : Given userdata doesn't fullfill end entity profile. : " + e.getMessage());
135             }
136                       
137         } catch (Exception JavaDoc e) {
138             throw new ErrorAdminCommandException(e);
139         }
140     }
141
142     private int getStatus(String JavaDoc status) {
143         if(status.equalsIgnoreCase("NEW")){
144             return UserDataConstants.STATUS_NEW;
145         }
146         if(status.equalsIgnoreCase("INPROCESS")){
147             return UserDataConstants.STATUS_INPROCESS;
148         }
149         if(status.equalsIgnoreCase("FAILED")){
150             return UserDataConstants.STATUS_FAILED;
151         }
152         if(status.equalsIgnoreCase("HISTORICAL")){
153             return UserDataConstants.STATUS_HISTORICAL;
154         }
155         
156         getPrintStream().println("Error in status string : " + status );
157         usage();
158         System.exit(-1);
159         return 0;
160     }
161
162     protected void usage() {
163         getPrintStream().println("Command used to add or edit userdata, if user exist will the data be overwritten.");
164         getPrintStream().println("Usage : edituser <username> <password> <clearpwd (true|false)> <subjectdn> <subjectaltname or NULL> <email or NULL> <caname> <type> <token> <status> <endentityprofilename> <certificateprofilename> <issueralias (Optional)> \n\n");
165         getPrintStream().println("DN is of form \"C=SE, O=MyOrg, OU=MyOrgUnit, CN=MyName\" etc.");
166         getPrintStream().println(
167             "SubjectAltName is of form \"rfc822Name=<email>, dNSName=<host name>, uri=<http://host.com/>, ipaddress=<address>, guid=<globally unique id>\"");
168
169         getPrintStream().println("Type (mask): INVALID=0; END-USER=1; KEYRECOVERABLE=128; SENDNOTIFICATION=256");
170         
171         getPrintStream().print("Existing tokens : " + "USERGENERATED" + ", " +
172                 "P12" + ", "+ "JKS" + ", " + "PEM" + "\n");
173         getPrintStream().print("Existing statuses (new users will always be set as NEW) : NEW, INPROCESS, FAILED, HISTORICAL");
174     }
175
176
177 }
178
Popular Tags