Created
March 2, 2011 09:27
-
-
Save skihero/850685 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
/** | |
* Adapter class to generate command string suitable for | |
* SoapCommandUtil class from User Object class | |
* CommandAdapter returns a command string of the form | |
* CreateAccountRequest/name=test_username@domainuc.com | |
* ../password=testpassword -a admin@domainuc.com -p coe -n | |
* ../a=active -v @n=zimbraAccountStatus ../a=princess -v @n=displayName; | |
* | |
* Added zimbra options from conf file: Feb 28 : Kish | |
* | |
*/ | |
class CommandAdapter { | |
User destinedUser ; /* local copy */ | |
ConfReader confRead ; /* conf handle */ | |
private Map user_creation_params ; | |
private Map<String, String> misc_user_params ; | |
/* Different actions in the SOAP construction */ | |
private String add_action = " CreateAccountRequest/name"; | |
private String search_action = " GetAccountInfoRequest/account"; | |
CommandAdapter(User destinedUser) { | |
/* update our copy */ | |
this.destinedUser = destinedUser; | |
} | |
void paramReader(){ | |
System.out.println("System props "); | |
System.out.println(System.getProperties() ); | |
System.out.println("user dir") ; | |
System.out.println(System.getProperty("user.dir")); | |
/* read all params for zimbra/user specfic tags */ | |
confRead = new ConfReader(); | |
/* Set the global file location */ | |
/* This would mean we would have to place the conf file at place where | |
* we're deploying it::: MAD | |
*/ | |
confRead.setConfigFile(System.getProperty("user.dir") + "/" + "global.xml"); | |
System.out.println("conf filen: "+ System.getProperty("user.dir") + "/" +"global.xml"); | |
System.out.println("System props "); | |
System.out.println(System.getProperties() ); | |
//user_creation_params = confRead.readUser(); // or confRead.readZimbra(); | |
try { | |
user_creation_params = confRead.readZimbraParams(); | |
} | |
catch (Exception e1) { | |
System.err.println("Unable to read the conf file"); | |
e1.printStackTrace(); | |
} | |
try { | |
/* this should be a map that could iterate over later */ | |
misc_user_params = confRead.readMiscZimbraParams() ; | |
} catch (Exception e ){ | |
/* We continue with the Map params */ | |
System.err.println("Misc methods not found.. continuing without them " ); | |
} | |
} | |
String [] createAccountRequest() throws Exception{ | |
/* Read the local copy of the User object and get the params. */ | |
String fname = destinedUser.getFName(); | |
String lname = destinedUser.getLName(); | |
String create_password = destinedUser.getPassword(); | |
//TODO: verify create name | |
String create_username = fname + lname ; | |
//String create_password = destinedUser.getPassword(); | |
/* will be used after the User class is updated */ | |
Map<String, String> attributes = destinedUser.getAttributes(); | |
/* Use the confreader to get the other params */ | |
this.paramReader() ; | |
String domain = (String) this.user_creation_params.get("domain") ; | |
String admin_username = (String) this.user_creation_params.get("admin_username"); | |
String admin_password = (String) this.user_creation_params.get("admin_password"); | |
String json_flag = (String) this.user_creation_params.get("json") ; | |
String trial_run = (String) this.user_creation_params.get("trial_run") ; | |
String verbosity = (String) this.user_creation_params.get("verbosity") ; | |
/* If the flags are set to true apply the switches */ | |
json_flag = json_flag.equalsIgnoreCase("TRUE") ? " --json " : " " ; | |
trial_run = trial_run.equalsIgnoreCase("TRUE") ? " -n " : " " ; | |
verbosity = verbosity.equalsIgnoreCase("TRUE") ? " -vv " : " " ; | |
String add_contact_comm_str_verb = add_action | |
+ "=" | |
+ create_username | |
+ "@" | |
+ domain | |
+ " ../password=" | |
+ create_password | |
+ " -a " | |
+ admin_username | |
+ " -p " | |
+ admin_password | |
+ " " + verbosity + " " | |
+ " " + trial_run + " " | |
+ " " + json_flag + " " ; | |
if (! misc_user_params.isEmpty() ) { | |
/* Use the attributes in the command string | |
These attributes are the extra ones that are used for full | |
customization of the zimbra accound holder */ | |
for ( int i = 0 ; i < misc_user_params.size(); i++) | |
{ | |
List misc_keys = new ArrayList<String>( misc_user_params.keySet() ); | |
List misc_values = new ArrayList<String>( misc_user_params.values() ); | |
/* Error check */ | |
if ( misc_keys.size() != misc_values.size() ){ | |
throw new Exception("Miscellaneous params list are not complete"); | |
} | |
for ( Map.Entry <String,String> attr : misc_user_params.entrySet() ) { | |
System.out.println("key "+ attr.getKey() ) ; | |
System.out.println("value"+ attr.getValue() ) ; | |
add_contact_comm_str_verb += " ../a=" + attr.getValue() + " -v " | |
+ "@n=" + attr.getKey() ; | |
} | |
} | |
System.out.println("String out : " + add_contact_comm_str_verb ) ; | |
} | |
/* | |
* | |
* CreateAcc untRequest/name=zmsoap_user@domainuc.com | |
* ../password=coe12345 -a admin@domainuc.com -p coe12345 -vv ../a=active | |
* -v @n=zimbraAccountStatus ../a=soapy_suds -v @n=displayName | |
* | |
* This creates this request <CreateAccountRequest | |
* xmlns="urn:zimbraAdmin"> <name>zmsoap_user@domainuc.com</name> | |
* <password>ourpassword12345</password> <a n="zimbraAccountStatus">active</a> | |
* <a n="displayName">soapy_suds</a> </CreateAccountRequest> | |
* | |
*/ | |
System.out.println("After adding remaining attributes:\n " | |
+ add_contact_comm_str_verb); | |
/* construct an array and return it */ | |
/* Sanitize the string | |
* Change multiple spaces to one */ | |
add_contact_comm_str_verb = add_contact_comm_str_verb.replaceAll(" +", " "); | |
String split [] = add_contact_comm_str_verb.split(" "); | |
/* Check it */ | |
for (String s: split) | |
System.out.println("S: " + s); | |
/* the command string in an array: Yatta!! */ | |
return split; | |
} | |
void getAccountRequest() { | |
} | |
/* Test the various methods | |
public static void main(String a [ ]) throws Exception{ | |
// Create a User object and populate the values | |
User test_user = new User() ; | |
UCService ucs = new UCService () ; | |
test_user.setFName("I'm test user " ); | |
test_user.setLName(" last name of test user ") ; | |
test_user.setPassword("opensesame"); | |
ucs.setServiceName("SipX"); | |
ucs.setXmlServiceConfiguration("xml conf placeholder"); | |
// WTF | |
List <UCService> list_ucs = new ArrayList<UCService>(); | |
// add the subscribed services to a list | |
list_ucs.add(ucs) ; | |
Map<String, String> mp = new HashMap<String, String>(); | |
mp.put("zimbraAccountStatus", "active"); | |
mp.put("displayName", "PATAPON"); | |
test_user.setMiscParameters(mp); | |
test_user.setSubscribedServices(list_ucs); | |
CommandAdapter ca = new CommandAdapter(test_user); | |
String create_result [] = ca.createAccountRequest(); | |
System.out.println("create result " + create_result.toString()); | |
} | |
/* */ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment