05-01-2014 11:46 AM
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", "[]");
}
}
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
}
05-02-2014 06:22 AM
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;
}
}
05-02-2014 10:28 AM
Tags
Find what you came for
We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.