use of javax.jcr.Node in project camel by apache.
the class JcrProducerTest method testJcrProducer.
@Test
public void testJcrProducer() throws Exception {
Exchange exchange = createExchangeWithBody("<hello>world!</hello>");
exchange.getIn().setHeader(JcrConstants.JCR_NODE_NAME, "node");
exchange.getIn().setHeader("my.contents.property", exchange.getIn().getBody());
Exchange out = template.send("direct:a", exchange);
assertNotNull(out);
String uuid = out.getOut().getBody(String.class);
Session session = openSession();
try {
Node node = session.getNodeByIdentifier(uuid);
assertNotNull(node);
assertEquals("/home/test/node", node.getPath());
assertEquals("<hello>world!</hello>", node.getProperty("my.contents.property").getString());
} finally {
if (session != null && session.isLive()) {
session.logout();
}
}
}
use of javax.jcr.Node in project camel by apache.
the class JcrProducer method process.
public void process(Exchange exchange) throws Exception {
TypeConverter converter = exchange.getContext().getTypeConverter();
Session session = openSession();
Message message = exchange.getIn();
String operation = determineOperation(message);
try {
if (JcrConstants.JCR_INSERT.equals(operation)) {
Node base = findOrCreateNode(session.getRootNode(), getJcrEndpoint().getBase(), "");
Node node = findOrCreateNode(base, getNodeName(message), getNodeType(message));
Map<String, Object> headers = filterComponentHeaders(message.getHeaders());
for (String key : headers.keySet()) {
Object header = message.getHeader(key);
if (header != null && Object[].class.isAssignableFrom(header.getClass())) {
Value[] value = converter.convertTo(Value[].class, exchange, header);
node.setProperty(key, value);
} else {
Value value = converter.convertTo(Value.class, exchange, header);
node.setProperty(key, value);
}
}
node.addMixin("mix:referenceable");
exchange.getOut().setBody(node.getIdentifier());
session.save();
} else if (JcrConstants.JCR_GET_BY_ID.equals(operation)) {
Node node = session.getNodeByIdentifier(exchange.getIn().getMandatoryBody(String.class));
PropertyIterator properties = node.getProperties();
while (properties.hasNext()) {
Property property = properties.nextProperty();
Class<?> aClass = classForJCRType(property);
Object value;
if (property.isMultiple()) {
value = converter.convertTo(aClass, exchange, property.getValues());
} else {
value = converter.convertTo(aClass, exchange, property.getValue());
}
message.setHeader(property.getName(), value);
}
} else {
throw new RuntimeException("Unsupported operation: " + operation);
}
} finally {
if (session != null && session.isLive()) {
session.logout();
}
}
}
use of javax.jcr.Node in project jackrabbit by apache.
the class TestContentLoader method addExportTestData.
private void addExportTestData(Node node) throws RepositoryException, IOException {
getOrAddNode(node, "invalidXmlName").setProperty("propName", "some text");
// three nodes which should be serialized as xml text in docView export
// separated with spaces
getOrAddNode(node, "jcr:xmltext").setProperty("jcr:xmlcharacters", "A text without any special character.");
getOrAddNode(node, "some-element");
getOrAddNode(node, "jcr:xmltext").setProperty("jcr:xmlcharacters", " The entity reference characters: <, ', ,&, >, \" should" + " be escaped in xml export. ");
getOrAddNode(node, "some-element");
getOrAddNode(node, "jcr:xmltext").setProperty("jcr:xmlcharacters", "A text without any special character.");
Node big = getOrAddNode(node, "bigNode");
big.setProperty("propName0", "SGVsbG8gd8O2cmxkLg==;SGVsbG8gd8O2cmxkLg==".split(";"), PropertyType.BINARY);
big.setProperty("propName1", "text 1");
big.setProperty("propName2", "multival text 1;multival text 2;multival text 3".split(";"));
big.setProperty("propName3", "text 1");
addExportValues(node, "propName");
addExportValues(node, "Prop<>prop");
}
use of javax.jcr.Node in project jackrabbit by apache.
the class TestContentLoader method addExportValues.
/**
* create nodes with following properties
* binary & single
* binary & multival
* notbinary & single
* notbinary & multival
*/
private void addExportValues(Node node, String name) throws RepositoryException, IOException {
String prefix = "valid";
if (name.indexOf('<') != -1) {
prefix = "invalid";
}
node = getOrAddNode(node, prefix + "Names");
String[] texts = new String[] { "multival text 1", "multival text 2", "multival text 3" };
getOrAddNode(node, prefix + "MultiNoBin").setProperty(name, texts);
Node resource = getOrAddNode(node, prefix + "MultiBin");
resource.setProperty("jcr:encoding", ENCODING);
resource.setProperty("jcr:mimeType", "text/plain");
String[] values = new String[] { "SGVsbG8gd8O2cmxkLg==", "SGVsbG8gd8O2cmxkLg==" };
resource.setProperty(name, values, PropertyType.BINARY);
resource.setProperty("jcr:lastModified", Calendar.getInstance());
getOrAddNode(node, prefix + "NoBin").setProperty(name, "text 1");
resource = getOrAddNode(node, "invalidBin");
resource.setProperty("jcr:encoding", ENCODING);
resource.setProperty("jcr:mimeType", "text/plain");
byte[] bytes = "Hello wörld.".getBytes(ENCODING);
resource.setProperty(name, new ByteArrayInputStream(bytes));
resource.setProperty("jcr:lastModified", Calendar.getInstance());
}
use of javax.jcr.Node in project jackrabbit by apache.
the class TestContentLoader method addNodeTestData.
/**
* Creates three nodes under the given node: one of type nt:resource
* and the other nodes referencing it.
*/
private void addNodeTestData(Node node) throws RepositoryException, IOException {
if (node.hasNode("multiReference")) {
node.getNode("multiReference").remove();
}
if (node.hasNode("resReference")) {
node.getNode("resReference").remove();
}
if (node.hasNode("myResource")) {
node.getNode("myResource").remove();
}
Node resource = node.addNode("myResource", "nt:resource");
// nt:resource not longer referenceable since JCR 2.0
resource.addMixin("mix:referenceable");
resource.setProperty("jcr:encoding", ENCODING);
resource.setProperty("jcr:mimeType", "text/plain");
resource.setProperty("jcr:data", new ByteArrayInputStream("Hello wörld.".getBytes(ENCODING)));
resource.setProperty("jcr:lastModified", Calendar.getInstance());
Node resReference = getOrAddNode(node, "reference");
resReference.setProperty("ref", resource);
// make this node itself referenceable
resReference.addMixin("mix:referenceable");
Node multiReference = node.addNode("multiReference");
ValueFactory factory = node.getSession().getValueFactory();
multiReference.setProperty("ref", new Value[] { factory.createValue(resource), factory.createValue(resReference) });
// NodeDefTest requires a test node with a mandatory child node
JcrUtils.putFile(node, "testFile", "text/plain", new ByteArrayInputStream("Hello, World!".getBytes("UTF-8")));
}
Aggregations