cancel
Showing results for 
Search instead for 
Did you mean: 

Add Person Java API

srowsell
Champ in-the-making
Champ in-the-making
I'm trying a simple proof-of-concept Java class which will add a user via the API, but I'm having trouble getting it to work.  I've found some excellent responses already in this forum, and while I think I'm doing it (more or less) the same way I'm getting an unexpected result.

Here is the Java class (which is very similar to another class I found to upload a file elsewhere, and I have adapted to this purpose):

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;

import uploader.InteractiveAuthentication;


public class AddUser {

   public static void addUser(String authTicket, String userName, String password, String firstName,
String lastName, String email, String disableAccount, String quota, String groups) {
      try {
         
         String urlString = "http://localhost:8080/alfresco/service/api/people?alf_ticket="
               + authTicket;
         System.out.println("The addUser url:::" + urlString);
         HttpClient client = new HttpClient();
         
         PostMethod mPost = new PostMethod(urlString);
         
         Part[] parts = {
               new StringPart("userName", userName),
               new StringPart("password", password),
               new StringPart("firstName", firstName),
               new StringPart("lastName", lastName),
               new StringPart("email",email),
               new StringPart("disableAccount", disableAccount),
               new StringPart("quota", quota),
               new StringPart("groups", groups)
         };
         mPost.setRequestEntity(new MultipartRequestEntity(parts, mPost
               .getParams()));
         int statusCode1 = client.executeMethod(mPost);
         System.out.println("statusLine>>>" + statusCode1 + "……"
               + "\n status line \n" + mPost.getStatusLine() + "\nbody \n"
               + mPost.getResponseBodyAsString());
         mPost.releaseConnection();

      } catch (Exception e) {
         System.out.println(e);
      }
   }

   public static void main(String args[]) throws IOException {
      
      String alfrescoTiccketURL = "http://localhost:8080/alfresco"
            + "/service/api/login?u=" + "admin" + "&pw=" + "admin";

      InteractiveAuthentication ticket = new InteractiveAuthentication();
      String ticketURLResponse = ticket.getTicket(alfrescoTiccketURL);

      addUser(ticketURLResponse, "testuser", "testpassword", "testfirst", "testlast",
"testemail@test.com", "false", "-1", "[]");
   }

}


And this is the console output:

response = <?xml version="1.0" encoding="UTF-8"?>
<ticket>TICKET_620a4b4b488d2eaa651f68dee9e5ae7b3594a32d</ticket>

ticket = TICKET_620a4b4b488d2eaa651f68dee9e5ae7b3594a32d
The addUser url:::http://localhost:8080/alfresco/service/api/people?alf_ticket=TICKET_620a4b4b488d2eaa651f68dee9e5ae7b...
statusLine>>>200……
status line
HTTP/1.1 200 OK
body
{
   "url": "\/alfresco\/service\/api\/person\/admin",
   "userName": "admin",
   "enabled": true,
   "firstName": "Administrator",
   "lastName": "",
   "jobtitle": null,
   "organization": null,
   "organizationId": "",
   "location": null,
   "telephone": null,
   "mobile": null,
   "email": "admin@alfresco.com",
   "companyaddress1": null,
   "companyaddress2": null,
   "companyaddress3": null,
   "companypostcode": null,
   "companytelephone": null,
   "companyfax": null,
   "companyemail": null,
   "skype": null,
   "instantmsg": null,
   "userStatus": null,
   "userStatusTime": null,
   "googleusername": null,
   "quota": -1,
   "sizeCurrent": 0,
   "emailFeedDisabled": false,
   "persondescription": null
}


For some reason the response I'm getting is for the admin user (which is being used to authenticate), not the user I'm trying to create.  I can confirm that the new user is not being created.

What am I missing?

Steve
2 REPLIES 2

romschn
Star Collaborator
Star Collaborator
The reason it is not working for you is because this webscript requires the input in the JSON format and as per your code it is not in JSON format.
You should pass the webscript input as JSON string. Below is the overall approach in high level to get this done.
Create a POJO to have the getter/setter for all the required properties for webscript.
Convert the POJO to JSON string.
Pass the JSON string to the webscript as its input parameters.

Below is the updated working code. You will need to add gson-1.7.1.jar to your build path. This is the utility i generally use to convert java object to JSON string.

import com.google.gson.Gson;
public class AddUser {

   public static void addUser(String authTicket, String userName, String password, String firstName,
String lastName, String email, String disableAccount, String quota, String[] groups) {
      try {

         String urlString = "http://localhost:8080/alfresco/service/api/people?alf_ticket="
               + authTicket;
         System.out.println("The addUser url:::" + urlString);
         HttpClient client = new HttpClient();

         PostMethod mPost = new PostMethod(urlString);

         UserDetails objUserDetails = new UserDetails();
         objUserDetails.setUserName(userName);
         objUserDetails.setPassword(password);
         objUserDetails.setFirstName(firstName);
         objUserDetails.setLastName(lastName);
         objUserDetails.setEmail(email);
         objUserDetails.setGroups(groups);
         objUserDetails.setQuota(quota);

         StringRequestEntity requestEntity = new StringRequestEntity(
                           getJSONString(objUserDetails),
                           "application/json",
                           "UTF-8");
           mPost.setRequestEntity(requestEntity);
         int statusCode1 = client.executeMethod(mPost);
         System.out.println("statusLine>>>" + statusCode1 + "……"
               + "\n status line \n" + mPost.getStatusLine() + "\nbody \n"
               + mPost.getResponseBodyAsString());
         mPost.releaseConnection();

      } catch (Exception e) {
         System.out.println(e);
      }
   }

   private static String getJSONString(Object obj)
   {
      Gson gson = new Gson();
      return gson.toJson(obj);
   }

   public static void main(String args[]) throws IOException {

      String alfrescoTiccketURL = "http://localhost:8080/alfresco"
            + "/service/api/login?u=" + "admin" + "&pw=" + "admin";

      InteractiveAuthentication ticket = new InteractiveAuthentication();
      String ticketURLResponse = ticket.getTicket(alfrescoTiccketURL);

      addUser(ticketURLResponse, "testuser", "testpassword", "testfirst", "testlast",
"testemail@test.com", "false", "-1", "new String[1]");
   }

}
class UserDetails {
   private String userName;
   private String password;
   private String firstName;
   private String lastName;
   private String email;
   private String disableAccount;
   private String quota;
   private String[] groups;
   public String getUserName() {
      return userName;
   }
   public void setUserName(String userName) {
      this.userName = userName;
   }
   public String getPassword() {
      return password;
   }
   public void setPassword(String password) {
      this.password = password;
   }
   public String getFirstName() {
      return firstName;
   }
   public void setFirstName(String firstName) {
      this.firstName = firstName;
   }
   public String getLastName() {
      return lastName;
   }
   public void setLastName(String lastName) {
      this.lastName = lastName;
   }
   public String getEmail() {
      return email;
   }
   public void setEmail(String email) {
      this.email = email;
   }
   public String getDisableAccount() {
      return disableAccount;
   }
   public void setDisableAccount(String disableAccount) {
      this.disableAccount = disableAccount;
   }
   public String getQuota() {
      return quota;
   }
   public void setQuota(String quota) {
      this.quota = quota;
   }
   public String[] getGroups() {
      return groups;
   }
   public void setGroups(String[] groups) {
      this.groups = groups;
   }      
}

Hope this helps.

srowsell
Champ in-the-making
Champ in-the-making
That does the trick, thanks.

One thing:  in the addUser() call in the Main method you need to remove the quotes from the last argument.  Other than that it works just fine.

Thanks again,

Steve