package com.mycompany.openapi.test; import java.util.Map; import java.util.TreeMap; import org.jdom.JDOMException; import org.restlet.data.Form; import org.restlet.data.Status; import com.jalios.rest.client.ClientSession; import com.jalios.rest.client.ClientUtil; import com.jalios.rest.client.DataElement; import com.jalios.rest.client.JcmsApp; import com.jalios.rest.client.JcmsResource; import com.jalios.rest.client.RestException; import com.jalios.rest.client.authentication.BasicAuthentication; import com.jalios.util.Util; import com.jalios.util.XmlUtil; /** * This class is an application given as an example of JCMS Open API. * It is not intended for production use. * * Java Application for member management in a remote JCMS through OpenAPI. * Call the application with no argument to get the usage description. * The authentication details are not implemented functionnaly. * * @author Jalios SA 2009 */ public class RemoteMemberManagement { protected ClientSession session; protected JcmsApp jcms; public RemoteMemberManagement(String baseHref) { session = new ClientSession(baseHref); jcms = new JcmsApp(session); } private static String usage = "Usage: 'java RemoteMemberManagement ' \n" + "Action possible values : read create modify delete disable enable\n"+ "Options possible for action read :\n" + " --login= : gives the login of \n" + " --details : optionnal, shows the details for the given member \n" + "Options possible for action create :\n" + " --name= : mandatory\n" + " --firstname= : optionnal\n" + " --login= : mandatory\n" + " --password= : mandatory\n" + " --salutation= : optionnal : mr, mrs or miss \n" + " --jobTitle= : optionnal\n" + " --email= : optionnal\n" + " --phone= : optionnal\n" + " --mobile= : optionnal\n" + " --address=
: optionnal\n" + " --info= : optionnal\n" + " --admin= : optionnal: true or false, default false\n" + " --emailFormat= : optionnal: text or html, default text\n" + " --emailVisible=: optionnal: true or false, default defined by a property\n" + " --newsletter= : optionnal: true or false, default false\n" + " --language= : optionnal: fr or en \n" + " --gids= : mandatory: groups id, comma separated\n" + "Options possible for action modify :\n" + " --login= : login or id must be present, not both \n" + " --id= : login or id must be present, not both \n" + " --name= : optionnal\n" + " --firstname= : optionnal\n" + " --password= : optionnal\n" + " --salutation= : optionnal: mr, mrs or miss\n" + " --jobTitle= : optionnal\n" + " --email= : optionnal\n" + " --phone= : optionnal\n" + " --mobile= : optionnal\n" + " --address=
: optionnal\n" + " --info= : optionnal\n" + " --admin= : optionnal: true or false, default false\n" + " --emailFormat= : optionnal: text or html, default text\n" + " --emailVisible=: optionnal: true or false, default defined by a property\n" + " --newsletter= : optionnal: true or false, default false\n" + " --language= : optionnal: fr or en\n" + " --gids= : optionnal: groups id, comma separated\n" + "Options possible for action delete :\n" + " --login= : login or id must be present, not both \n" + " --id= : login or id must be present, not both \n" + "Options possible for action disable :\n" + " --login= : mandatory \n" + "Options possible for action enable :\n" + " --login= : mandatory \n" + " --password= : mandatory \n"; public static void main(String[] args) throws Exception { if (Util.isEmpty(args) || args.length <= 2) { System.out.print(usage); return; } String baseHref = args[0]; RemoteMemberManagement application; try { application = new RemoteMemberManagement(baseHref); } catch (IllegalArgumentException iae) { System.out.println("baseHref is not well formatted"); return; } String action = args[1]; Map options = getOptions(args); application.prepareAuthenticationStuff(options); if ("read".equals(action)) { application.read(options); } else if ("create".equals(action)) { application.create(options); } else if ("modify".equals(action)) { application.modify(options); } else if ("delete".equals(action)) { application.delete(options); } else if ("disable".equals(action)) { application.disable(options); } else if ("enable".equals(action)) { application.enable(options); } else { System.out.println("Unknown action '"+ action + "'. Allowed actions : 'read', 'create', 'modify', 'delete', 'disable', 'enable'."); } } public void read(Map options) { String login = options.get("login"); boolean details = Util.toBoolean(options.get("details"), false); read(login, details); } public void read(String login, boolean details) { JcmsResource memberResource; try { memberResource = jcms.getMember(login); } catch (RestException ex) { System.out.println("An error occured while calling the remote site"); ex.printStackTrace(System.err); return; } DataElement memberData; try { memberData = new DataElement(ClientUtil.getFirstElement(memberResource, "/data"), session); } catch (JDOMException ex) { System.out.println("An error occured while parsing the result"); ex.printStackTrace(System.err); return; } System.out.println("Member is :" + XmlUtil.getStringNode(memberData.getField("author"))); if (details) { System.out.println("Details for login : "+ login + "\n" + memberResource.getEntityText()) ; } } public static String[] createMandatoryFields = new String[]{"name", "login"}; public static String[] createMandatoryMultiValuedFields = new String[]{"gids"}; public static String[] createOptionnalFields = new String[]{"firstname", "salutation", "jobTitle", "email", "phone", "mobile", "address", "info", "admin", "emailFormat", "emailVisible", "newsletter", "language"}; public void create(Map options) { // Fields are filled from parameters in arguments. Form fields = new Form(); for (int i = 0; i < createMandatoryFields.length; i++) { String fieldName = createMandatoryFields[i]; String fieldValue = options.get(fieldName); if (Util.isEmpty(fieldValue)) { System.out.println("Bad usage : Field " + fieldName + " not specified." + usage); return; } else { fields.add(fieldName, fieldValue); } } for (int i = 0; i < createMandatoryMultiValuedFields.length; i++) { String fieldName = createMandatoryMultiValuedFields[i]; String fieldValue = options.get(fieldName); if (Util.isEmpty(fieldValue)) { System.out.println("Bad usage : Field " + fieldName + " not specified." + usage); return; } else { String[] fieldValues = fieldValue.split(","); for (int j = 0; j < fieldValues.length; j++) { fields.add(fieldName, fieldValues[j]); } } } for (int i = 0; i < createOptionnalFields.length; i++) { String fieldName = createOptionnalFields[i]; String fieldValue = options.get(fieldName); if (Util.notEmpty(fieldValue)) { fields.add(fieldName, fieldValue); } } { // Password is special, because in the web interface, it must be filled twice String password = options.get("password"); if (Util.isEmpty(password)) { System.out.println("Bad usage : Field password not specified." + usage); return; } else { fields.add("password1", password); fields.add("password2", password); } } // /rest/data/Member is invoqued in POST to create the member JcmsResource response = jcms.createData("Member", fields); // Does the response indicates a creation has been done ? if (Status.SUCCESS_CREATED.equals(response.getStatus())) { System.out.println("Member created"); System.out.println("Location: " + response.getRedirectRef().toString()); } else { System.out.println("Problem : status : " + response.getStatus().getDescription() + " (" + response.getStatus().getCode() + ")"); return; } } public static String[] modifyOptionnalFields = new String[]{"name", "firstname", "salutation", "jobTitle", "email", "phone", "mobile", "address", "info", "admin", "emailFormat", "emailVisible", "newsletter", "language"}; public static String[] modifyOptionnalMultiValuedFields = new String[]{"gids"}; public void modify(Map options) { // If member is is not specified, look for it from the login String memberId = options.get("id"); if (Util.isEmpty(memberId)) { memberId = jcms.getIdFromLogin(options.get("login")); if (Util.isEmpty(memberId)) { System.out.println("Empty memberId"); return; } } // Fields are filled from parameters in arguments. Form fields = new Form(); for (int i = 0; i < modifyOptionnalMultiValuedFields.length; i++) { String fieldName = modifyOptionnalMultiValuedFields[i]; String fieldValue = options.get(fieldName); if (Util.notEmpty(fieldValue)) { String[] fieldValues = fieldValue.split(","); for (int j = 0; j < fieldValues.length; j++) { fields.add(fieldName, fieldValues[j]); } } } for (int i = 0; i < modifyOptionnalFields.length; i++) { String fieldName = modifyOptionnalFields[i]; String fieldValue = options.get(fieldName); if (Util.notEmpty(fieldValue)) { fields.add(fieldName, fieldValue); } } { String fieldName = "password"; String fieldValue = options.get(fieldName); if (Util.notEmpty(fieldValue)) { fields.add(fieldName + "1", fieldValue); fields.add(fieldName + "2", fieldValue); } } // /rest/data/ is invoqued with PUT to modify the member JcmsResource response = jcms.updateData(memberId, fields); if (Status.SUCCESS_OK.equals(response.getStatus())) { System.out.println("Member modified"); } else { System.out.println("Problem : status : " + response.getStatus().getDescription() + " (" + response.getStatus().getCode() + ")"); return; } } public void delete(Map options) { // If member is is not specified, look for it from the login String memberId = options.get("id"); if (Util.isEmpty(memberId)) { memberId = jcms.getIdFromLogin(options.get("login")); if (Util.isEmpty(memberId)) { System.out.println("Empty memberId"); return; } } // /rest/data/ is invoked with DELETE JcmsResource response = jcms.deleteData(memberId); if (Status.SUCCESS_OK.equals(response.getStatus())) { System.out.println("Member deleted"); } else { System.out.println("Problem : status : " + response.getStatus().getDescription() + " (" + response.getStatus().getCode() + ")"); return; } } public void disable(Map options) { String login = options.get("login"); if (Util.isEmpty(login)) { System.out.println("Bad usage : Field login not specified." + usage); return; } JcmsResource response = jcms.disableMember(login); if (Status.SUCCESS_OK.equals(response.getStatus())) { System.out.println("Member disabled"); } else { System.out.println("Problem : status : " + response.getStatus().getDescription() + " (" + response.getStatus().getCode() + ")"); return; } } public void enable(Map options) { // If member is is not specified, look for it from the login String login = options.get("login"); String password = options.get("password"); if (Util.isEmpty(login) || Util.isEmpty(password)) { System.out.println("Bad usage : Fields login and password are mandatory." + usage); return; } JcmsResource response = jcms.enableMember(login, password); if (Status.SUCCESS_OK.equals(response.getStatus())) { System.out.println("Member enabled"); } else { System.out.println("Problem : status : " + response.getStatus().getDescription() + " (" + response.getStatus().getCode() + ")"); return; } } /** * Provides a usefull Map with options from the arguments in parameter of the applications. * If an option is indicated by "--optionName=optionValue", it leads to : * optionName ==> optionValue * in the Map returned, if an option is set as "--optionName", it leads to : * optionName ==> "true" * in the Map returned. * * @param args * @return a simple map of the arguments */ static public Map getOptions(String[] args) { Map options = new TreeMap(); if (Util.isEmpty(args)) { return options; } for (int i = 2; i < args.length; i++) { String optionString = args[i]; if (Util.isEmpty(optionString)) { continue; } if (!optionString.startsWith("--")) { System.err.println("Wrong pattern for option :'" + optionString + "'."); continue; } int equalsIndex = optionString.indexOf('='); if (equalsIndex < 0) { options.put(optionString.substring(2), "true"); } else { options.put(optionString.substring(2, equalsIndex), optionString.substring(equalsIndex + 1)); } } return options; } public void prepareAuthenticationStuff(Map options) { session.setAuthentication(new BasicAuthentication("admin", "admin")); } }