Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

What is the best API to use to create files in the JCR programatically?

Avatar

Level 4

What's best practice for creating regular files, (e.g. .css, .txt, .js etc.), programatically, and locating them under the appropriate node?

1 Accepted Solution

Avatar

Correct answer by
Administrator

Hi

Apart from what Scott has mentioned, please refer to the article mentioned below, this will help you create a new file under JCR node.

Option 1:-

Link:- http://aem-cq-tutorials.blogspot.in/2014/12/creating-file-in-cqaem.html

//

We can use JCR API to create a new node type and register it. Following is the code snippet to register it.

     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
session = slingRepository.loginAdministrative(null);
 NodeTypeManager manager = (NodeTypeManager)session.getWorkspace().getNodeTypeManager();
NamespaceRegistry ns=session.getWorkspace().getNamespaceRegistry();
ns.registerNamespace("ig","http://www.intelligrape.com/CustomNode");
// Create node type
 NodeTypeTemplate nodeTypeTemplate = manager.createNodeTypeTemplate();
 nodeTypeTemplate.setName("ig:testNodeType");
// Create a new property
 PropertyDefinitionTemplate customProperty1 = manager.createPropertyDefinitionTemplate();
customProperty1.setName("ig:Name");
customProperty1.setRequiredType(PropertyType.STRING);
PropertyDefinitionTemplate customProperty2 = manager.createPropertyDefinitionTemplate();
customProperty2.setName("ig:City");
customProperty2.setRequiredType(PropertyType.STRING);
 // Add property to node type
nodeTypeTemplate.getPropertyDefinitionTemplates().add(customProperty1);
nodeTypeTemplate.getPropertyDefinitionTemplates().add(customProperty2);
/* Register node type */
 manager.registerNodeType(nodeTypeTemplate, true);
 session.save();

 

Option 2:- Using Pipes, 

Pipes are good solution here. However, in order to implement them properly, you have to use two threads: first should write data into the PipedOutputStream and the second should create a Binaryfrom PipedInputStream and save it into JCR:

final PipedInputStream pis = new PipedInputStream(); final PipedOutputStream pos = new PipedOutputStream(pis); Executors.newSingleThreadExecutor().submit(new Runnable() { @Override public void run() { try { OutputStreamWriter writer = new OutputStreamWriter(pos); writer.append("append here some data"); writer.close(); } catch (IOException e) { e.printStackTrace(); } } }); Binary binary = session.getValueFactory().createBinary(pis); session.getNode("/content/myNode").setProperty("xyz", binary); session.save();

The symmetrical solution, in which you handle the JCR in the new thread would be also good.

 

Some reference link:- http://www.tothenew.com/blog/custom-node-type-in-aem/

//

Creating and Registering the Custom Nodetype 

There are broadly following three ways of creating custom node types.

  1. Using Node Type Administration console.
  2. Programmatically
  3. Using Package Manager

I hope this would help you.

Thanks and Regards

Kautuk Sahni



Kautuk Sahni

View solution in original post

4 Replies

Avatar

Level 10

We have many AEM community articles that show how to create files via an API in the JCR. See this one https://helpx.adobe.com/experience-manager/using/post_files.html        

Avatar

Level 9

au4liferz wrote...

What's best practice for creating regular files, (e.g. .css, .txt, .js etc.), programatically, and locating them under the appropriate node?

 

Hi,

Below code makes the whole thing (copied from Scott article to explain), I am not sure if there is any other way to do the same thing.

//Invoke the adaptTo method to create a Session
    ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);
    session = resourceResolver.adaptTo(Session.class);
      
    Node node = session.getNode(path);  //Get the client lib node in which to write the posted file
    javax.jcr.ValueFactory valueFactory = session.getValueFactory();            
    javax.jcr.Binary contentValue = valueFactory.createBinary(is);           
    Node fileNode = node.addNode(fileName, "nt:file");
    fileNode.addMixin("mix:referenceable");
    Node resNode = fileNode.addNode("jcr:content", "nt:resource");
    resNode.setProperty("jcr:mimeType", mimetype);
    resNode.setProperty("jcr:data", contentValue);
    Calendar lastModified = Calendar.getInstance();
    lastModified.setTimeInMillis(lastModified.getTimeInMillis());
    resNode.setProperty("jcr:lastModified", lastModified);
    session.save();           
    session.logout();

 

Jitendra

Avatar

Correct answer by
Administrator

Hi

Apart from what Scott has mentioned, please refer to the article mentioned below, this will help you create a new file under JCR node.

Option 1:-

Link:- http://aem-cq-tutorials.blogspot.in/2014/12/creating-file-in-cqaem.html

//

We can use JCR API to create a new node type and register it. Following is the code snippet to register it.

     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
session = slingRepository.loginAdministrative(null);
 NodeTypeManager manager = (NodeTypeManager)session.getWorkspace().getNodeTypeManager();
NamespaceRegistry ns=session.getWorkspace().getNamespaceRegistry();
ns.registerNamespace("ig","http://www.intelligrape.com/CustomNode");
// Create node type
 NodeTypeTemplate nodeTypeTemplate = manager.createNodeTypeTemplate();
 nodeTypeTemplate.setName("ig:testNodeType");
// Create a new property
 PropertyDefinitionTemplate customProperty1 = manager.createPropertyDefinitionTemplate();
customProperty1.setName("ig:Name");
customProperty1.setRequiredType(PropertyType.STRING);
PropertyDefinitionTemplate customProperty2 = manager.createPropertyDefinitionTemplate();
customProperty2.setName("ig:City");
customProperty2.setRequiredType(PropertyType.STRING);
 // Add property to node type
nodeTypeTemplate.getPropertyDefinitionTemplates().add(customProperty1);
nodeTypeTemplate.getPropertyDefinitionTemplates().add(customProperty2);
/* Register node type */
 manager.registerNodeType(nodeTypeTemplate, true);
 session.save();

 

Option 2:- Using Pipes, 

Pipes are good solution here. However, in order to implement them properly, you have to use two threads: first should write data into the PipedOutputStream and the second should create a Binaryfrom PipedInputStream and save it into JCR:

final PipedInputStream pis = new PipedInputStream(); final PipedOutputStream pos = new PipedOutputStream(pis); Executors.newSingleThreadExecutor().submit(new Runnable() { @Override public void run() { try { OutputStreamWriter writer = new OutputStreamWriter(pos); writer.append("append here some data"); writer.close(); } catch (IOException e) { e.printStackTrace(); } } }); Binary binary = session.getValueFactory().createBinary(pis); session.getNode("/content/myNode").setProperty("xyz", binary); session.save();

The symmetrical solution, in which you handle the JCR in the new thread would be also good.

 

Some reference link:- http://www.tothenew.com/blog/custom-node-type-in-aem/

//

Creating and Registering the Custom Nodetype 

There are broadly following three ways of creating custom node types.

  1. Using Node Type Administration console.
  2. Programmatically
  3. Using Package Manager

I hope this would help you.

Thanks and Regards

Kautuk Sahni



Kautuk Sahni

Avatar

Level 10

The article i posted is for the JCR API to create files. However- If you want to place assets in the AEM DAM - you use the AssetManager API. See this article to learn how to use the AssetManager API to create new assets in the DAM: 

https://helpx.adobe.com/experience-manager/using/uploading-files-aem1.html