We will now follow Gavin Cornwell https://community.alfresco.com/community/ecm/blog/2017/02/24/v1-rest-api-part-8-sites examples and see how we can achieve the same experience using the SDK
To make the exercise more concise we will execute each request in a synchronous way.
Alfresco Java Client is currently in Early Access mode. It evolves as you use them, as you give feedback, and as the developers update and add file. We like to think app & lib development as services that grow and evolve with the involvement of the community.
In order to follow along you'll need an environment to do so, firstly download and install the 5.2.c Early Access Community Release. In our case we will consider Alfresco is available at http://localhost:8080/alfresco and the "admin" user is available and has "admin" as password.
// Select PeopleAPI
PeopleAPI peopleAPI = client.getPeopleAPI();
//Create Test user
PersonBodyCreate personBodyCreate = new PersonBodyCreate("test", "Test", "User", "test@alfresco.com", "test");
Response<PersonRepresentation> personRepresentationResponse = peopleAPI.createPersonCall(personBodyCreate).execute();
PersonRepresentation personRepresentation = personRepresentationResponse.body();
Assert.assertEquals(personRepresentation.getId(), "test");
// Create new Client and connect with new user test/test user
client = new AlfrescoClient.Builder().httpLogging(HttpLoggingInterceptor.Level.BODY)
.connect("http://localhost:8080/alfresco", "test", "test").build();
//Create a public Site
SitesAPI sitesAPI = client.getSitesAPI();
//Create public site
/*SiteBodyCreate siteBodyCreate = new SiteBodyCreate("publicSite", "Public Site", "Public site created for blog post", SiteVisibilityEnum.PUBLIC);
Response<SiteRepresentation> siteRepresentationResponse = sitesAPI.createSiteCall(siteBodyCreate).execute();
SiteRepresentation siteRepresentation = siteRepresentationResponse.body();
Assert.assertEquals(siteRepresentation.getId(), "publicSite");
Assert.assertEquals(siteRepresentation.getVisibilityEnum(), SiteVisibilityEnum.PUBLIC);
Assert.assertEquals(siteRepresentation.getRole(), "SiteManager");
//Retrieve document library container
Response<SiteContainerRepresentation> doclibContainerResponse = sitesAPI.getSiteContainerCall("publicSite", "documentLibrary").execute();
SiteContainerRepresentation doclibContainer = doclibContainerResponse.body();
Assert.assertEquals(doclibContainer.getFolderId(), "documentLibrary");
//Update site description
SiteBodyUpdate update = new SiteBodyUpdate("Public site created for blog post - part 8");
Response<SiteRepresentation> siteRepresentationResponse = sitesAPI.updateSiteCall("publicSite", update).execute();
SiteRepresentation siteRepresentation = siteRepresentationResponse.body();
Assert.assertEquals(siteRepresentation.getId(), "publicSite");
Assert.assertEquals(siteRepresentation.getDescription(), "Public site created for blog post - part 8");
SiteBodyCreate siteBodyCreate = new SiteBodyCreate("moderatedSite", "Moderated Site",
"Moderated site created for blog post", SiteVisibilityEnum.MODERATED);
Response<SiteRepresentation> siteRepresentationResponse = sitesAPI.createSiteCall(siteBodyCreate).execute();
SiteRepresentation siteRepresentation = siteRepresentationResponse.body();
Assert.assertEquals(siteRepresentation.getId(), "moderatedSite");
Assert.assertEquals(siteRepresentation.getVisibilityEnum(), SiteVisibilityEnum.MODERATED);
Assert.assertEquals(siteRepresentation.getRole(), "SiteManager");
PersonBodyCreate personBodyCreate = new PersonBodyCreate("test2", "Test", "User2", "test2@alfresco.com",
"test2");
Response<PersonRepresentation> personRepresentationResponse = peopleAPI.createPersonCall(personBodyCreate)
.execute();
PersonRepresentation personRepresentation = personRepresentationResponse.body();
Assert.assertEquals(personRepresentation.getId(), "test2");
// Create new Client and connect with new user test2/test2 user
AlfrescoClient client2 = new AlfrescoClient.Builder().httpLogging(HttpLoggingInterceptor.Level.BODY)
.connect("http://localhost:8080/alfresco", "test2", "test2").build();
// Join public site
SiteMembershipRequestBodyCreate requestBodyCreate = new SiteMembershipRequestBodyCreate("publicSite");
Response<SiteMembershipRequestRepresentation> publicSiteRepresentationResponse = client2.getSitesAPI()
.createSiteMembershipRequestForPersonCall("test2", requestBodyCreate, null).execute();
SiteMembershipRequestRepresentation requestRepresentation = publicSiteRepresentationResponse.body();
Assert.assertEquals(requestRepresentation.getSite().getRole(), "SiteConsumer");
Assert.assertEquals(requestRepresentation.getSite().getId(), "publicSite");
Assert.assertEquals(requestRepresentation.getId(), "publicSite");
SiteMembershipRequestBodyCreate requestModeratedBodyCreate = new SiteMembershipRequestBodyCreate(
"moderatedSite", "I would like to join this site as it looks interesting", null);
Response<SiteMembershipRequestRepresentation> moderatedSiteRepresentationResponse = client2.getSitesAPI()
.createSiteMembershipRequestForPersonCall("test2", requestModeratedBodyCreate, null).execute();
SiteMembershipRequestRepresentation moderatedRequestRepresentation = moderatedSiteRepresentationResponse.body();
Assert.assertNull(moderatedRequestRepresentation.getSite().getRole());
Assert.assertEquals(moderatedRequestRepresentation.getSite().getId(), "moderatedSite");
Assert.assertEquals(moderatedRequestRepresentation.getId(), "moderatedSite");
ResultPaging<SiteMembershipRequestRepresentation> siteMembershipRequestPaging = client2.getSitesAPI()
.listSiteMembershipRequestsForPersonCall("test2").execute().body();
Assert.assertEquals(siteMembershipRequestPaging.getCount(), 1);
Assert.assertEquals(siteMembershipRequestPaging.getList().get(0).getId(), "moderatedSite");*/
ResultPaging<SiteMemberRepresentation> siteMembers = client2.getSitesAPI().listSiteMembershipsCall("publicSite").execute().body();
Assert.assertEquals(siteMembers.getCount(), 2);
Assert.assertEquals(siteMembers.getList().size(), 2);
ResultPaging<SiteRoleRepresentation> mySites = client2.getSitesAPI().listSiteMembershipsForPersonCall("test2").execute().body();
Assert.assertEquals(mySites.getCount(), 1);
Assert.assertEquals(mySites.getList().get(0).getRole(), "SiteConsumer");
ResultPaging<SiteRepresentation> allSites = client2.getSitesAPI().listSitesCall().execute().body();
Assert.assertEquals(allSites.getCount(), 3);
ResultPaging<SiteRepresentation> findSites = client2.getQueriesAPI().findSitesCall("public").execute().body();
Assert.assertEquals(findSites.getCount(), 1);
Assert.assertEquals(findSites.getList().get(0).getId(), "publicSite");
Response<Void> leaveSiteResponse = client2.getSitesAPI().deleteSiteMembershipForPersonCall("test2", "publicSite").execute();
Assert.assertEquals(leaveSiteResponse.isSuccessful(), true);
Assert.assertEquals(client2.getSitesAPI().listSiteMembershipsForPersonCall("test2").execute().body().getCount(), 0);
SiteMembershipBodyCreate siteMembershipBodyCreate = new SiteMembershipBodyCreate("test2", "SiteContributor");
SiteMemberRepresentation siteMember = client.getSitesAPI()
.createSiteMembershipCall("publicSite", siteMembershipBodyCreate, null).execute().body();
Assert.assertEquals(siteMember.getRole(), "SiteContributor");
Assert.assertEquals(siteMember.getId(), "test2");
Assert.assertEquals(client2.getSitesAPI().listSiteMembershipsForPersonCall("test2").execute().body().getCount(), 1);
SiteMembershipBodyUpdate siteMembershipBodyUpdate = new SiteMembershipBodyUpdate("SiteManager");
SiteMemberRepresentation siteMemberUpdated = client.getSitesAPI()
.updateSiteMembershipCall("publicSite", "test2", siteMembershipBodyUpdate, null).execute().body();
Assert.assertEquals(siteMemberUpdated.getRole(), "SiteManager");
Assert.assertEquals(siteMemberUpdated.getId(), "test2");
Assert.assertEquals(client2.getSitesAPI().listSiteMembershipsForPersonCall("test2").execute().body().getCount(), 1);
Response<Void> deleteSiteResponse = client.getSitesAPI().deleteSiteCall("publicSite").execute();
Assert.assertTrue(deleteSiteResponse.isSuccessful());
Ask for and offer help to other Alfresco Content Services Users and members of the Alfresco team.
Related links:
By using this site, you are agreeing to allow us to collect and use cookies as outlined in Alfresco’s Cookie Statement and Terms of Use (and you have a legitimate interest in Alfresco and our products, authorizing us to contact you in such methods). If you are not ok with these terms, please do not use this website.