Search in sources :

Example 6 with Node

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();
        }
    }
}
Also used : Exchange(org.apache.camel.Exchange) Node(javax.jcr.Node) Session(javax.jcr.Session) Test(org.junit.Test)

Example 7 with Node

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();
        }
    }
}
Also used : Message(org.apache.camel.Message) Node(javax.jcr.Node) PropertyIterator(javax.jcr.PropertyIterator) TypeConverter(org.apache.camel.TypeConverter) Value(javax.jcr.Value) Property(javax.jcr.Property) Session(javax.jcr.Session)

Example 8 with Node

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");
}
Also used : Node(javax.jcr.Node)

Example 9 with Node

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());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Node(javax.jcr.Node)

Example 10 with Node

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")));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Node(javax.jcr.Node) ValueFactory(javax.jcr.ValueFactory)

Aggregations

Node (javax.jcr.Node)2620 Session (javax.jcr.Session)645 Test (org.junit.Test)643 RepositoryException (javax.jcr.RepositoryException)317 Property (javax.jcr.Property)251 NodeIterator (javax.jcr.NodeIterator)214 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)182 Value (javax.jcr.Value)158 Version (javax.jcr.version.Version)155 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)151 Query (javax.jcr.query.Query)122 QueryResult (javax.jcr.query.QueryResult)108 Event (javax.jcr.observation.Event)103 VersionManager (javax.jcr.version.VersionManager)97 Resource (org.apache.sling.api.resource.Resource)96 ArrayList (java.util.ArrayList)93 AccessDeniedException (javax.jcr.AccessDeniedException)89 InvalidItemStateException (javax.jcr.InvalidItemStateException)82 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)82 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)81