AnsweredAssumed Answered

Add Person Java API

Question asked by srowsell on May 1, 2014
Latest reply on May 2, 2014 by srowsell
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_620a4b4b488d2eaa651f68dee9e5ae7b3594a32d
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

Outcomes