What is the best API to use to create files in the JCR programatically?
What's best practice for creating regular files, (e.g. .css, .txt, .js etc.), programatically, and locating them under the appropriate node?
What's best practice for creating regular files, (e.g. .css, .txt, .js etc.), programatically, and locating them under the appropriate node?
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.
I hope this would help you.
Thanks and Regards
Kautuk Sahni
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.