Skip to content

Instantly share code, notes, and snippets.

@niketapatel
Last active October 10, 2017 10:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niketapatel/aad02ba764c2fa387633429ea57afc4e to your computer and use it in GitHub Desktop.
Save niketapatel/aad02ba764c2fa387633429ea57afc4e to your computer and use it in GitHub Desktop.
Create Alfresco Share site through surf webscript
package com.test.alfresco.share;
import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.json.JSONException;
import org.json.JSONObject;
public class CreateShateSite {
public static void main(String[] args) throws HttpException, IOException, InterruptedException {
HttpClient client = new HttpClient();
String loginData = "username=admin&password=admin"; // provide username and pwd
PostMethod loginPost = new PostMethod("http://127.0.0.1:8080/share/page/dologin");
loginPost.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
loginPost.setRequestEntity(new StringRequestEntity(loginData, "text/plain", "UTF-8"));
// make a post call to get JSESSIONID and other required cookies
System.out.println("dologin script status: " + client.executeMethod(loginPost));
// Make a share touch call to initiate/validate session
GetMethod authenticationGet = new GetMethod("http://127.0.0.1:8080/share/service/modules/authenticated?a=user");
System.out.println("User is authenticated script status : " + client.executeMethod(authenticationGet));
//call share script to create a site.
PostMethod createSitePost = new PostMethod("http://127.0.0.1:8080/share/service/modules/create-site");
String shortName = "TestSiteFromCode";
JSONObject siteObject = new JSONObject();
try {
siteObject.put("shortName", shortName);
siteObject.put("Visiblity", "Public");
siteObject.put("sitePreset", "site-dashboard");
siteObject.put("title", shortName);
siteObject.put("description", shortName);
createSitePost.setRequestHeader("Content-Type", "application/json");
createSitePost.setRequestHeader("Accept", "application/json");
createSitePost.setRequestEntity(new StringRequestEntity(siteObject.toString(), "application/json", "UTF-8"));
int status = client.executeMethod(createSitePost);
System.out.println("create a site script status :: " + status);
if (status == HttpStatus.SC_OK) {
System.out.println("Site created OK");
}
else{
System.out.println("There is error in site creation");
}
} catch (JSONException err) {
err.printStackTrace();
}
}
}
@MosquitoesTeam
Copy link

Hello,
Where should I put this java Class please ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment