We will now follow Gavin Cornwell https://community.alfresco.com/community/ecm/blog/2016/10/24/v1-rest-api-part-3-creating-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 Folder
NodeBodyCreate nodeBodyCreate = new NodeBodyCreate("My Folder", ContentModel.TYPE_FOLDER);
Response<NodeRepresentation> nodeResponse = nodesAPI.createNodeCall(NodesAPI.FOLDER_MY, nodeBodyCreate).execute();
NodeRepresentation folder = nodeResponse.body();
Assert.assertEquals(folder.getName(), "My Folder");
//Create Empty File
NodeBodyCreate emptyFileBody = new NodeBodyCreate("my-file.txt", ContentModel.TYPE_CONTENT);
Response<NodeRepresentation> emptyNodeResponse = nodesAPI.createNodeCall(NodesAPI.FOLDER_MY, emptyFileBody).execute();
NodeRepresentation emptyNode = emptyNodeResponse.body();
Assert.assertEquals(emptyNode.getContent().getSizeInBytes(), 0);
// Create Body
File file = new File("path/to/test.txt");
RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain"), file);
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
multipartBuilder.addFormDataPart("filedata", "test.txt", requestBody);
RequestBody fileRequestBody = multipartBuilder.build();
//Create Content
Response<NodeRepresentation> createdNodeResponse = nodesAPI
.createNodeCall(NodesAPI.FOLDER_MY, fileRequestBody).execute();
// Create Body
File file = new File("path/to/test.txt");
RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain"), file);
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
multipartBuilder.addFormDataPart("filedata", "test.txt", requestBody);
RequestBody fileRequestBody = multipartBuilder.build();
HashMap<String, RequestBody> map = new HashMap<>();
map.put("filedata", fileRequestBody);
map.put("name", RequestBody.create(MediaType.parse("multipart/form-data"), "my-name.txt"));
//Create Content
Response<NodeRepresentation> createdNodeResponse = nodesAPI.createNodeCall(NodesAPI.FOLDER_MY, map).execute();
File file = new File("path/to/test.txt");
RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain"), file);
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
multipartBuilder.addFormDataPart("filedata", "test.txt", requestBody);
RequestBody fileRequestBody = multipartBuilder.build();
HashMap<String, RequestBody> map = new HashMap<>();
map.put("filedata", fileRequestBody);
map.put("name", RequestBody.create(MediaType.parse("multipart/form-data"), "my-name.txt"));
map.put("autorename", RequestBody.create(MediaType.parse("multipart/form-data"), "true"));
//Create Content
Response<NodeRepresentation> createdNodeResponse = nodesAPI.createNodeCall(NodesAPI.FOLDER_MY, map).execute();
// Create content in relative path
// Create Body
File file = new File("W:\\test.txt");
RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain"), file);
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
multipartBuilder.addFormDataPart("filedata", "test.txt", requestBody);
RequestBody fileRequestBody = multipartBuilder.build();
HashMap<String, RequestBody> map = new HashMap<>();
map.put("filedata", fileRequestBody);
map.put("name", RequestBody.create(MediaType.parse("multipart/form-data"), "my-file.txt"));
map.put("relativePath", RequestBody.create(MediaType.parse("multipart/form-data"), "/My Folder"));
//Create Content
Response<NodeRepresentation> createdNodeResponse = nodesAPI.createNodeCall(NodesAPI.FOLDER_MY, map).execute();
// Create content with rendition
// Create Body
File file = new File("W:\\test.txt");
RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain"), file);
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
multipartBuilder.addFormDataPart("filedata", "test.txt", requestBody);
RequestBody fileRequestBody = multipartBuilder.build();
HashMap<String, RequestBody> map = new HashMap<>();
map.put("filedata", fileRequestBody);
map.put("renditions", RequestBody.create(MediaType.parse("multipart/form-data"), "doclib"));
//Create Content
Response<NodeRepresentation> createdNodeResponse = nodesAPI.createNodeCall(NodesAPI.FOLDER_MY, map).execute();
// Create content with properties
// Create Body
File file = new File("Path/To/test.txt");
RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain"), file);
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
multipartBuilder.addFormDataPart("filedata", "test.txt", requestBody);
RequestBody fileRequestBody = multipartBuilder.build();
HashMap<String, RequestBody> map = new HashMap<>();
map.put("filedata", fileRequestBody);
map.put("name", RequestBody.create(MediaType.parse("multipart/form-data"), "my-file.txt"));
map.put("autoRename", RequestBody.create(MediaType.parse("multipart/form-data"), "true"));
map.put(ContentModel.PROP_TITLE, RequestBody.create(MediaType.parse("multipart/form-data"), "The Title"));
map.put(ContentModel.PROP_DESCRIPTION, RequestBody.create(MediaType.parse("multipart/form-data"), "The Description"));
map.put(ContentModel.PROP_MANUFACTURER, RequestBody.create(MediaType.parse("multipart/form-data"), "Canon"));
//Create Content
Response<NodeRepresentation> createdNodeResponse = nodesAPI.createNodeCall(NodesAPI.FOLDER_MY, map).execute();
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.