Search in sources :

Example 26 with Binary

use of javax.jcr.Binary in project jackrabbit-oak by apache.

the class BinaryBasedBlobTest method getStream.

@Test
public void getStream() throws RepositoryException, IOException {
    byte[] bytes = "just a test".getBytes();
    Binary binary = new TestBinary(bytes);
    Blob blob = new BinaryBasedBlob(binary);
    assertEquals(bytes.length, blob.length());
    InputStream expected = binary.getStream();
    InputStream actual = blob.getNewStream();
    try {
        for (int e = expected.read(); e != -1; e = expected.read()) {
            assertEquals(e, actual.read());
        }
        assertEquals(-1, actual.read());
    } finally {
        expected.close();
        actual.close();
    }
}
Also used : Blob(org.apache.jackrabbit.oak.api.Blob) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Binary(javax.jcr.Binary) Test(org.junit.Test)

Example 27 with Binary

use of javax.jcr.Binary in project sling by apache.

the class JcrResourceBundleTest method test_json_dictionary.

public void test_json_dictionary() throws Exception {
    Node appsI18n = getSession().getRootNode().addNode("apps").addNode("i18n", "nt:unstructured");
    Node deJson = appsI18n.addNode("de.json", "nt:file");
    deJson.addMixin("mix:language");
    deJson.setProperty("jcr:language", "de");
    Node content = deJson.addNode("jcr:content", "nt:resource");
    content.setProperty("jcr:mimeType", "application/json");
    // manually creating json file, good enough for the test
    StringBuilder json = new StringBuilder();
    json.append("{");
    for (Message msg : MESSAGES_DE_APPS.values()) {
        json.append("\"").append(msg.key).append("\": \"");
        json.append(msg.message).append("\",\n");
    }
    json.append("}");
    InputStream stream = new ByteArrayInputStream(json.toString().getBytes());
    Binary binary = getSession().getValueFactory().createBinary(stream);
    content.setProperty("jcr:data", binary);
    getSession().save();
    // test getString
    JcrResourceBundle bundle = new JcrResourceBundle(new Locale("de"), null, resolver);
    for (Message msg : MESSAGES_DE_APPS.values()) {
        assertEquals(msg.message, bundle.getString(msg.key));
    }
    // test getKeys
    Enumeration<String> keys = bundle.getKeys();
    int counter = 0;
    while (keys.hasMoreElements()) {
        counter++;
        String key = keys.nextElement();
        assertTrue("bundle returned key that is not supposed to be there: " + key, MESSAGES_DE_APPS.containsKey(key));
    }
    assertEquals(MESSAGES_DE.size(), counter);
}
Also used : Locale(java.util.Locale) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Node(javax.jcr.Node) Binary(javax.jcr.Binary)

Example 28 with Binary

use of javax.jcr.Binary in project sling by apache.

the class DistributionPackageUtils method uploadStream.

public static void uploadStream(Resource resource, InputStream stream) throws RepositoryException {
    Node parent = resource.adaptTo(Node.class);
    Node file = JcrUtils.getOrAddNode(parent, "bin", NodeType.NT_FILE);
    Node content = JcrUtils.getOrAddNode(file, Node.JCR_CONTENT, NodeType.NT_RESOURCE);
    Binary binary = parent.getSession().getValueFactory().createBinary(stream);
    content.setProperty(Property.JCR_DATA, binary);
    JcrUtils.getOrAddNode(parent, "refs", NodeType.NT_UNSTRUCTURED);
}
Also used : Node(javax.jcr.Node) Binary(javax.jcr.Binary)

Example 29 with Binary

use of javax.jcr.Binary in project sling by apache.

the class AddOrUpdateNodeCommand method updateFileLikeNodeTypes.

private void updateFileLikeNodeTypes(Node node) throws RepositoryException, IOException {
    // TODO - better handling of file-like nodes - perhaps we need to know the SerializationKind here
    // TODO - avoid IO
    File file = new File(fileInfo.getLocation());
    if (!hasFileLikePrimaryNodeType(node)) {
        return;
    }
    Node contentNode;
    if (node.hasNode(JCR_CONTENT)) {
        contentNode = node.getNode(JCR_CONTENT);
    } else {
        if (node.getProperty(JCR_PRIMARYTYPE).getString().equals(NT_RESOURCE)) {
            contentNode = node;
        } else {
            contentNode = node.addNode(JCR_CONTENT, NT_RESOURCE);
        }
    }
    getLogger().trace("Updating {0} property on node at {1} ", JCR_DATA, contentNode.getPath());
    try (FileInputStream inputStream = new FileInputStream(file)) {
        Binary binary = node.getSession().getValueFactory().createBinary(inputStream);
        contentNode.setProperty(JCR_DATA, binary);
        // TODO: might have to be done differently since the client and server's clocks can differ
        // and the last_modified should maybe be taken from the server's time..
        contentNode.setProperty(JCR_LASTMODIFIED, Calendar.getInstance());
    }
}
Also used : Node(javax.jcr.Node) Binary(javax.jcr.Binary) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 30 with Binary

use of javax.jcr.Binary in project jackrabbit by apache.

the class ValueFactoryTest method testInputStream.

/**
     * Tests whether a passed <code>InputStream</code> is closed
     * by the implementation.
     *
     * @throws RepositoryException
     */
public void testInputStream() throws RepositoryException {
    InputStreamWrapper in = new InputStreamWrapper(new ByteArrayInputStream(binaryValue));
    valueFactory.createValue(in);
    assertTrue("ValueFactory.createValue(InputStream) is expected to close the passed input stream", in.isClosed());
    in = new InputStreamWrapper(new ByteArrayInputStream(binaryValue));
    Binary bin = valueFactory.createBinary(in);
    assertTrue("ValueFactory.createBinary(InputStream) is expected to close the passed input stream", in.isClosed());
    bin.dispose();
}
Also used : InputStreamWrapper(org.apache.jackrabbit.test.api.util.InputStreamWrapper) ByteArrayInputStream(java.io.ByteArrayInputStream) Binary(javax.jcr.Binary)

Aggregations

Binary (javax.jcr.Binary)50 ByteArrayInputStream (java.io.ByteArrayInputStream)28 InputStream (java.io.InputStream)22 Node (javax.jcr.Node)20 Value (javax.jcr.Value)14 Test (org.junit.Test)12 Session (javax.jcr.Session)10 ReferenceBinary (org.apache.jackrabbit.api.ReferenceBinary)8 BigDecimal (java.math.BigDecimal)7 SimpleReferenceBinary (org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary)7 RandomInputStream (org.apache.jackrabbit.core.data.RandomInputStream)7 IOException (java.io.IOException)5 Property (javax.jcr.Property)4 RepositoryException (javax.jcr.RepositoryException)4 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)4 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)4 NamespaceRegistry (javax.jcr.NamespaceRegistry)3 ValueFactory (javax.jcr.ValueFactory)3 NodeTypeTemplate (javax.jcr.nodetype.NodeTypeTemplate)3 QValue (org.apache.jackrabbit.spi.QValue)3