jm.pascal

Alfresco Java Client SDK - Usage Part 3 - Creating Nodes

Blog Post created by jm.pascal Employee on Nov 17, 2016

We will now follow Gavin Cornwell 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.

 

Important Notice

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.

 

Prerequisites

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.

 

 

Part 3 - Creating Nodes

 

Create folder

//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

//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 content

// 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 content with name

// 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();

 

Create content with auto rename

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 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 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 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();

 

 

Alfresco Java Client SDK Series

Outcomes