11-18-2010 09:52 AM
10-04-2011 08:27 PM
10-17-2011 05:23 AM
public class CrestSiteService
{
//private String ticket = null;
//private final String user = "admin";
//private final String password = "admin";
//private String shortName = null;
private static final String HEADER_CONTENT_TYPE = "Content-Type";
private static final String CONTENT_TYPE_JSON = "application/json";
private static final String UTF_8 = "UTF-8";
private static final String CONTENT_TYPE_TEXT_PLAIN = "text/plain";
private static final Log logger = LogFactory.getLog(CrestSiteService.class);
/*private String getUser()
{
return this.user;
}
private String getPassword()
{
return this.password;
}
private String getTicket()
{
return this.ticket;
}
private void setTicket(String ticket)
{
this.ticket = ticket;
}
private String getShortName()
{
return this.shortName;
}
private void setShortName()
{
this.shortName = GUID.generate().substring(0, 4);
}*/
private String getSiteBody(String shortName, String title, String description, boolean isPublic) throws JSONException
{
JSONObject site = new JSONObject();
site.put("sitePreset", "site-dashboard");
site.put("shortName", shortName);
site.put("title", title);
site.put("description", description);
if(isPublic)
site.put("visibility", ScriptSiteService.PUBLIC_SITE);
else
site.put("visibility", ScriptSiteService.PRIVATE_SITE);
//site.put("alf_ticket", getTicket());
return site.toString();
}
public boolean createSite(String url, String ticket, String shortName, String title, String description, boolean isPublic)
{
HttpClient httpClient = new HttpClient();
boolean siteCreated = false;
//String createSiteBody = "{\"isPublic\":\"" + isPublic + "\",\"title\":\"" + title + "\",\"shortName\":\"" + shortName + "\"," + "\"description\":\"" + description
// + "\",\"sitePreset\":\"" + sitePreset + "\"" + (isPublic ? ",\"alfresco-createSite-instance-isPublic-checkbox\":\"on\"}" : "}");
PostMethod createSiteMethod = null;
try
{
//AuthenticationUtils.startSession(userName, password);
//String ticket = AuthenticationUtils.getTicket();
//Get a JSON string with the site details.
String createSiteBody = getSiteBody(shortName, title, description, isPublic);
//Create post method
createSiteMethod = createPostMethod(url+"/s/api/sites?alf_ticket=" + ticket, createSiteBody, CONTENT_TYPE_JSON);
if (logger.isDebugEnabled()) logger.debug("Trying to create site with name: " + shortName + ". URL: " + createSiteMethod.getURI());
//Execute the post method
int createSiteStatus = httpClient.executeMethod(createSiteMethod);
//createSiteMethod.getResponseBody();
if (logger.isDebugEnabled()) logger.debug("Create site method returned status: " + createSiteStatus);
if(createSiteStatus == 200)
{
// create title sites component
siteCreated = createComponent(url, httpClient, shortName, ticket, "title", "title/collaboration-title");
// create navigation sites component
siteCreated = createComponent(url, httpClient, shortName, ticket, "navigation", "navigation/collaboration-navigation");
// create component-1-1 sites component
siteCreated = createComponent(url, httpClient, shortName, ticket, "component-1-1", "dashlets/site-welcome");
// create component-2-1 sites component
siteCreated = createComponent(url, httpClient, shortName, ticket, "component-2-1", "dashlets/docsummary");
// create component-2-2 sites component
siteCreated = createComponent(url, httpClient, shortName, ticket, "component-2-2", "dashlets/activityfeed");
// create component-3-1 sites component
siteCreated = createComponent(url, httpClient, shortName, ticket, "component-3-1", "dashlets/site-profile");
// create component-3-2 sites component
siteCreated = createComponent(url, httpClient, shortName, ticket, "component-3-2", "dashlets/colleagues");
// create component-3-3 sites component
siteCreated = createComponent(url, httpClient, shortName, ticket, "component-3-3", "dashlets/site-links");
// create documetnLibrary folder
siteCreated = createDocumentLibrary(url, httpClient, shortName, ticket);
// create sites dashboard
siteCreated = createSiteDashboard(url, httpClient, shortName, ticket);
// create links folder
siteCreated = createLinks(url, httpClient, shortName, ticket);
}
}
catch (HttpException he)
{
if (logger.isDebugEnabled()) logger.debug("Fail to create site with name: " + shortName + ". Message: " + he.getMessage());
//throw new RuntimeException(he);
}
catch (AuthenticationFault ae)
{
if (logger.isDebugEnabled()) logger.debug("Fail to create site with name: " + shortName + ". Message: " + ae.getMessage());
//throw new RuntimeException(ae);
}
catch (IOException ioe)
{
if (logger.isDebugEnabled()) logger.debug("Fail to create site with name: " + shortName + ". Message: " + ioe.getMessage());
//throw new RuntimeException(ioe);
}
catch (JSONException je)
{
if (logger.isDebugEnabled()) logger.debug("Fail to create site with name: " + shortName + ". Message: " + je.getMessage());
//throw new RuntimeException(je);
}
finally
{
createSiteMethod.releaseConnection();
}
return siteCreated;
}
private boolean createComponent(String url, HttpClient httpClient, String siteName, String ticket, String componentName, String componentURL)
{
boolean componentCreated = false;
String compUrl = url+"/s/remotestore/create/alfresco/site-data/components/page." + componentName + ".site~" + siteName
+ "~dashboard.xml?s=sitestore&alf_ticket=" + ticket;
String body = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<component>\n" +
"<guid>page." + componentName + ".site~" + siteName + "~dashboard</guid>\n" +
"<scope>page</scope>\n" +
"<region-id>" + componentName + "</region-id>\n" +
"<source-id>site/" + siteName + "/dashboard</source-id>\n" +
"<url>/components/" + componentURL + "</url>\n" +
"</component>";
PostMethod postMethod = null;
try
{
postMethod = createPostMethod(compUrl, body, "application/octet-stream");
if (logger.isDebugEnabled()) logger.debug("Trying to create site component with name: " + componentName + ". URL: " + postMethod.getURI());
int status = httpClient.executeMethod(postMethod);
//postMethod.getResponseBody();
if(status == 200)
componentCreated = true;
if (logger.isDebugEnabled()) logger.debug("Create component with name: " + componentName + ". Method returned status: " + status);
}
catch (Exception e)
{
if (logger.isDebugEnabled()) logger.debug("Fail to create site component with name: " + componentName + ". Message: " + e.getMessage());
//throw new RuntimeException(e);
}
finally
{
postMethod.releaseConnection();
}
return componentCreated;
}
/**
* Creates site dashboard
*
* @param httpClient HTTP client
* @param siteName name of the site
* @param user current user
* @throws UnsupportedEncodingException
*/
private boolean createSiteDashboard(String url, HttpClient httpClient, String siteName, String ticket) throws UnsupportedEncodingException
{
boolean dashboardCreated = false;
String createSiteDashboardBody = "";
createSiteDashboardBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<page>\n" +
"<title>Collaboration Site Dashboard</title>\n" +
"<title-id>page.collaboration_site.title</title-id>\n" +
"<description>Collaboration Site dashboard page</description>\n" +
"<description-id>page.collaboration_site.description</description-id>\n" +
"<template-instance>dashboard-3-columns</template-instance>\n" +
"<authentication>user</authentication>\n" +
"<properties>\n" +
"<sitePages>" +
"["+
"{\"pageId\":\"wiki-page\"}," +
"{\"pageId\":\"blog-postlist\"}," +
"{\"pageId\":\"documentlibrary\"}," +
"{\"pageId\":\"calendar\"}," +
"{\"pageId\":\"links\"}," +
"{\"pageId\":\"discussions-topiclist\"}," +
"{\"pageId\":\"data-lists\"}," +
"]" +
"</sitePages>\n" +
"</properties>\n" +
"</page>";
PostMethod createSiteDashboardMethod = createPostMethod(url+"/s/remotestore/create/alfresco/site-data/pages/site/" + siteName
+ "/dashboard.xml?s=sitestore&alf_ticket=" + ticket, createSiteDashboardBody, "application/octet-stream");
try
{
if (logger.isDebugEnabled()) logger.debug("Trying to create site dashboard. URL: " + createSiteDashboardMethod.getURI());
int status = httpClient.executeMethod(createSiteDashboardMethod);
createSiteDashboardMethod.getResponseBody();
if(status == 200)
dashboardCreated = true;
if (logger.isDebugEnabled()) logger.debug("Create site dashboard method returned status: " + status);
}
catch (Exception e)
{
if (logger.isDebugEnabled()) logger.debug("Fail to create site dashboard. Message: " + e.getMessage());
//throw new RuntimeException(e);
}
finally
{
createSiteDashboardMethod.releaseConnection();
}
return dashboardCreated;
}
/**
* Creates documentLibrary folder in site
*
* @param httpClient HTTP client
* @param siteName short name of site
* @param user current user
*/
private boolean createDocumentLibrary(String url, HttpClient httpClient, String siteName, String ticket)
{
boolean docLibCreated = false;
GetMethod createDocumentLibraryFolderMethod = createGetMethod(url+"/s/slingshot/doclib/doclist/documents/site/" + siteName
+ "/documentLibrary?filter=recentlyModified&max=10&alf_ticket=" + ticket);
try
{
if (logger.isDebugEnabled()) logger.debug("Trying to create sites documentLibrary folder. URL: " + createDocumentLibraryFolderMethod.getURI());
int status = httpClient.executeMethod(createDocumentLibraryFolderMethod);
createDocumentLibraryFolderMethod.getResponseBody();
if(status == 200)
docLibCreated = true;
if (logger.isDebugEnabled()) logger.debug("Create sites documentLibrary folder method returned status: " + status);
}
catch (Exception e)
{
if (logger.isDebugEnabled()) logger.debug("Fail to create sites documentLibrary folder. Message: " + e.getMessage());
//throw new RuntimeException(e);
}
finally
{
createDocumentLibraryFolderMethod.releaseConnection();
}
return docLibCreated;
}
/**
* Creates links folder in site
*
* @param httpClient HTTP client
* @param siteName short name of site
* @param user current user
*/
private boolean createLinks(String url, HttpClient httpClient, String siteName, String ticket)
{
boolean linksCreated = false;
GetMethod createLinksFolderMethod = createGetMethod(url+"/s/api/links/site/" + siteName + "/links?page=1&pageSize=512&alf_ticket="
+ ticket);
try
{
if (logger.isDebugEnabled()) logger.debug("Trying to create site links folder. URL: " + createLinksFolderMethod.getURI());
int status = httpClient.executeMethod(createLinksFolderMethod);
createLinksFolderMethod.getResponseBody();
if(status == 200)
linksCreated = true;
if (logger.isDebugEnabled()) logger.debug("Create site links folder method returned status: " + status);
}
catch (Exception e)
{
if (logger.isDebugEnabled()) logger.debug("Fail to create site links folder. Message: " + e.getMessage());
//throw new RuntimeException(e);
}
finally
{
createLinksFolderMethod.releaseConnection();
}
return linksCreated;
}
/**
* Creates POST method
*
* @param url URL for request
* @param body body of request
* @param contentType content type of request
* @return POST method
* @throws UnsupportedEncodingException
*/
private PostMethod createPostMethod(String url, String body, String contentType) throws UnsupportedEncodingException
{
PostMethod postMethod = new PostMethod(url);
postMethod.setRequestHeader(HEADER_CONTENT_TYPE, contentType);
postMethod.setRequestEntity(new StringRequestEntity(body, CONTENT_TYPE_TEXT_PLAIN, UTF_8));
return postMethod;
}
/**
* Creates GET method
*
* @param url URL for request
* @return GET method
*/
private GetMethod createGetMethod(String url)
{
GetMethod getMethod = new GetMethod(url);
return getMethod;
}
/*public static void main(String args[]) throws HttpException, IOException, JSONException
{
CrestSite site = new CrestSite();
AuthenticationUtils.startSession(site.getUser(), site.getPassword());
site.setTicket(AuthenticationUtils.getTicket());
String ticket = AuthenticationUtils.getTicket();
site.setShortName();
//SessionUser user = new User(site.getUser(), site.getTicket());
site.createSite(ticket, "shortName"+site.getShortName(), "title"+site.getShortName(), "desc"+site.getShortName(), true );
}*/
}
07-01-2014 08:25 AM
02-06-2015 04:09 AM
02-23-2012 10:52 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.