We will now follow Gavin Cornwell https://community.alfresco.com/community/ecm/blog/2016/11/02/v1-rest-api-part-4-managing-nodes 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.
//Create Empty Node with Properties
LinkedTreeMap<String, Object> properties = new LinkedTreeMap<>();
properties.put(ContentModel.PROP_TITLE, "The Title");
NodeBodyCreate emptyFileBody = new NodeBodyCreate("my-file.txt", ContentModel.TYPE_CONTENT, properties, null);
Response<NodeRepresentation> emptyNodeResponse = nodesAPI.createNodeCall(NodesAPI.FOLDER_MY, emptyFileBody).execute();
NodeRepresentation emptyNode = emptyNodeResponse.body();
Assert.assertEquals(emptyNode.getContent().getSizeInBytes(), 0);
//Get Node Information
String nodeId = emptyNode.getId();
NodeRepresentation node = nodesAPI.getNodeCall(nodeId).execute().body();
Assert.assertNotNull(node.getProperties());
Assert.assertNotNull(node.getAspects());
Assert.assertTrue(node.isFile());
Assert.assertFalse(node.isFolder());
Assert.assertNull(node.isLink());
//Get node information with isLink and path
node = nodesAPI.getNodeCall(nodeId, new IncludeParam(Arrays.asList("isLink", "path")), null, null).execute().body();
Assert.assertFalse(node.isLink());
Assert.assertNotNull(node.getPath());
//Update Node Information
LinkedTreeMap<String, Object> props = new LinkedTreeMap<>();
props.put(ContentModel.PROP_DESCRIPTION, "The Description");
props.put(ContentModel.PROP_MANUFACTURER, "Canon");
NodeRepresentation updatedNode = nodesAPI.updateNodeCall(nodeId, new NodeBodyUpdate(props)).execute().body();
Assert.assertEquals(updatedNode.getProperties().get(ContentModel.PROP_DESCRIPTION), "The Description");
Assert.assertEquals(updatedNode.getProperties().get(ContentModel.PROP_MANUFACTURER), "Canon");
//Rename Node
NodeRepresentation renamedNode = nodesAPI.updateNodeCall(nodeId, new NodeBodyUpdate("renamed-file.txt")).execute().body();
Assert.assertEquals(renamedNode.getName(), "renamed-file.txt");
//Change owner
props = new LinkedTreeMap<>();
props.put("cm:owner", "gavinc");
NodeRepresentation ownerNode = nodesAPI.updateNodeCall(nodeId, new NodeBodyUpdate(props)).execute().body();
Assert.assertEquals(((LinkedTreeMap)ownerNode.getProperties().get("cm:owner")).get("id"), "gavinc");
//Remove Exif Aspects
List<String> aspectNames = new ArrayList<>();
aspectNames.add(ContentModel.ASPECT_TITLED);
aspectNames.add(ContentModel.ASPECT_AUDITABLE);
aspectNames.add("cm:ownable");
NodeRepresentation aspectNode = nodesAPI.updateNodeCall(nodeId, new NodeBodyUpdate(null, null, aspectNames)).execute().body();
Assert.assertTrue(aspectNode.getAspects().contains(ContentModel.ASPECT_TITLED));
Assert.assertTrue(aspectNode.getAspects().contains(ContentModel.ASPECT_AUDITABLE));
Assert.assertTrue(aspectNode.getAspects().contains("cm:ownable"));
Assert.assertFalse(aspectNode.getAspects().contains(ContentModel.ASPECT_EXIF));
//Change Node Type
NodeRepresentation changedTypeNode = nodesAPI.updateNodeCall(nodeId, new NodeBodyUpdate(null, "cm:savedquery", null, null)).execute().body();
Assert.assertEquals(changedTypeNode.getNodeType(), "cm:savedquery");
//Update Content
RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain"), "This is the initial content for the file.");
NodeRepresentation updatedContentNode = nodesAPI.updateNodeContentCall(nodeId, requestBody).execute().body();
Assert.assertEquals(updatedContentNode.getContent().getSizeInBytes(), 41);
//Get Content
Call<ResponseBody> downloadCall = nodesAPI.getNodeContentCall(nodeId);
File dlFile = new File("W:\\", "dl.txt");
IOUtils.copyFile(downloadCall.execute().body().byteStream(), dlFile);
//Delete Node
Response<Void> deleteCall = nodesAPI.deleteNodeCall(nodeId).execute();
Assert.assertTrue(deleteCall.isSuccessful());
//Delete Node Permanently
Response<Void> deleteCall = nodesAPI.deleteNodeCall(nodeId, true).execute();
Assert.assertTrue(deleteCall.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.