Search in sources :

Example 56 with Binary

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

the class TextExtractionQueryTest method assertFileContains.

private void assertFileContains(String name, String type, String... statements) throws Exception {
    if (testRootNode.hasNode(nodeName1)) {
        testRootNode.getNode(nodeName1).remove();
    }
    testRootNode.getSession().save();
    Node resource = testRootNode.addNode(nodeName1, NodeType.NT_RESOURCE);
    resource.setProperty(JcrConstants.JCR_MIMETYPE, type);
    InputStream stream = getClass().getResourceAsStream(name);
    assertNotNull(stream);
    try {
        Binary binary = testRootNode.getSession().getValueFactory().createBinary(stream);
        resource.setProperty(JcrConstants.JCR_DATA, binary);
    } finally {
        stream.close();
    }
    testRootNode.getSession().save();
    for (String statement : statements) {
        assertContainsQuery(statement, true);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Node(javax.jcr.Node) Binary(javax.jcr.Binary)

Example 57 with Binary

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

the class DefaultSyncContext method createValue.

/**
 * Creates a new JCR value of the given object, checking the internal type.
 * @param v the value
 * @return the JCR value or null
 * @throws RepositoryException if an error occurs
 */
@CheckForNull
protected Value createValue(@Nullable Object v) throws RepositoryException {
    if (v == null) {
        return null;
    } else if (v instanceof Boolean) {
        return valueFactory.createValue((Boolean) v);
    } else if (v instanceof Byte || v instanceof Short || v instanceof Integer || v instanceof Long) {
        return valueFactory.createValue(((Number) v).longValue());
    } else if (v instanceof Float || v instanceof Double) {
        return valueFactory.createValue(((Number) v).doubleValue());
    } else if (v instanceof BigDecimal) {
        return valueFactory.createValue((BigDecimal) v);
    } else if (v instanceof Calendar) {
        return valueFactory.createValue((Calendar) v);
    } else if (v instanceof Date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime((Date) v);
        return valueFactory.createValue(cal);
    } else if (v instanceof byte[]) {
        Binary bin = valueFactory.createBinary(new ByteArrayInputStream((byte[]) v));
        return valueFactory.createValue(bin);
    } else if (v instanceof Binary) {
        return valueFactory.createValue((Binary) v);
    } else if (v instanceof InputStream) {
        return valueFactory.createValue((InputStream) v);
    } else if (v instanceof char[]) {
        return valueFactory.createValue(new String((char[]) v));
    } else {
        return valueFactory.createValue(String.valueOf(v));
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Calendar(java.util.Calendar) BigDecimal(java.math.BigDecimal) Date(java.util.Date) ByteArrayInputStream(java.io.ByteArrayInputStream) Binary(javax.jcr.Binary) CheckForNull(javax.annotation.CheckForNull)

Example 58 with Binary

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

the class DefaultSyncContextTest method testCreateValueFromInputStream.

/**
 * @see <a href="https://issues.apache.org/jira/browse/OAK-4231">OAK-4231</a>
 */
@Test
public void testCreateValueFromInputStream() throws Exception {
    byte[] bytes = new byte[] { 'a', 'b' };
    ByteArrayInputStream is = new ByteArrayInputStream(bytes);
    Binary binary = valueFactory.createBinary(is);
    Value v = syncCtx.createValue(is);
    assertNotNull(v);
    assertEquals(PropertyType.BINARY, v.getType());
    assertEquals(binary, v.getBinary());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Value(javax.jcr.Value) Binary(javax.jcr.Binary) AbstractExternalAuthTest(org.apache.jackrabbit.oak.spi.security.authentication.external.AbstractExternalAuthTest) Test(org.junit.Test)

Example 59 with Binary

use of javax.jcr.Binary in project acs-aem-commons by Adobe-Consulting-Services.

the class EntryNodeWriterTest method testValid.

@Test
public void testValid() throws IOException, RepositoryException {
    final EntryNodeWriterMocks.MockArguments arguments = new EntryNodeWriterMocks.MockArguments();
    arguments.cacheContentCharEncoding = "UTF-8";
    arguments.cacheContentType = "text/html";
    arguments.entryNode = mock(Node.class);
    InputStream inputStream = getClass().getClassLoader().getResourceAsStream(CACHE_CONTENT_LOCATION);
    arguments.cacheContent = inputStream;
    List<String> header1Value = Arrays.asList("header-value");
    List<String> header2Value = Arrays.asList("another-header-value");
    arguments.cacheContentHeaders.put("some-header", header1Value);
    arguments.cacheContentHeaders.put("another-header", header2Value);
    final EntryNodeWriterMocks mocks = new EntryNodeWriterMocks(arguments);
    mocks.getEntryNodeWriter().write();
    verify(mocks.getEntryNode(), times(1)).setProperty(Matchers.startsWith(JCRHttpCacheStoreConstants.PN_CACHEKEY), any(Binary.class));
    ArgumentCaptor<Binary> argumentCaptor = ArgumentCaptor.forClass(Binary.class);
    verify(mocks.getJcrContentNode(), times(1)).setProperty(Matchers.startsWith(JcrConstants.JCR_DATA), argumentCaptor.capture());
    Binary savedBinary = argumentCaptor.getValue();
    IOUtils.contentEquals(inputStream, savedBinary.getStream());
    verify(mocks.getJcrContentNode(), times(1)).setProperty(JcrConstants.JCR_MIMETYPE, arguments.cacheContentType);
// verify(mocks.getHeadersNode().setProperty("some-header", header1Value))
}
Also used : InputStream(java.io.InputStream) Node(javax.jcr.Node) Binary(javax.jcr.Binary) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 60 with Binary

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

the class GarbageCollectorTest method testGC.

public void testGC() throws Exception {
    Node root = testRootNode;
    Session session = root.getSession();
    deleteMyNodes();
    runGC(session, true);
    root.addNode("node1");
    Node node2 = root.addNode("node2");
    Node n = node2.addNode("nodeWithBlob").addNode("sub");
    ValueFactory vf = session.getValueFactory();
    Binary b = vf.createBinary(new RandomInputStream(20, 1000));
    n.setProperty("test", b);
    session.save();
    n = node2.addNode("nodeWithTemporaryBlob");
    n.setProperty("test", vf.createBinary(new RandomInputStream(11, 1000)));
    session.save();
    n.remove();
    session.save();
    GarbageCollector gc = ((SessionImpl) session).createDataStoreGarbageCollector();
    gc.getDataStore().clearInUse();
    gc.setPersistenceManagerScan(false);
    gc.setMarkEventListener(this);
    if (gc.getDataStore() instanceof FileDataStore) {
        // make sure the file is old (access time resolution is 2 seconds)
        Thread.sleep(2000);
    }
    LOG.debug("scanning...");
    gc.mark();
    int count = listIdentifiers(gc);
    LOG.debug("stop scanning; currently " + count + " identifiers");
    LOG.debug("deleting...");
    gc.getDataStore().clearInUse();
    assertTrue(gc.sweep() > 0);
    int count2 = listIdentifiers(gc);
    assertEquals(count - 1, count2);
    // verify the node was moved, and that the binary is still there
    n = root.getNode("node1").getNode("nodeWithBlob").getNode("sub");
    b = n.getProperty("test").getValue().getBinary();
    InputStream in = b.getStream();
    InputStream in2 = new RandomInputStream(20, 1000);
    verifyInputStream(in, in2);
    deleteMyNodes();
    gc.close();
}
Also used : InputStream(java.io.InputStream) Node(javax.jcr.Node) ValueFactory(javax.jcr.ValueFactory) Binary(javax.jcr.Binary) SessionImpl(org.apache.jackrabbit.core.SessionImpl) Session(javax.jcr.Session) DataStoreGarbageCollector(org.apache.jackrabbit.api.management.DataStoreGarbageCollector) GarbageCollector(org.apache.jackrabbit.core.gc.GarbageCollector)

Aggregations

Binary (javax.jcr.Binary)70 Node (javax.jcr.Node)37 ByteArrayInputStream (java.io.ByteArrayInputStream)32 InputStream (java.io.InputStream)29 Value (javax.jcr.Value)18 Session (javax.jcr.Session)15 Test (org.junit.Test)13 RepositoryException (javax.jcr.RepositoryException)12 BigDecimal (java.math.BigDecimal)10 ReferenceBinary (org.apache.jackrabbit.api.ReferenceBinary)8 Property (javax.jcr.Property)7 ValueFactory (javax.jcr.ValueFactory)7 SimpleReferenceBinary (org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary)7 RandomInputStream (org.apache.jackrabbit.core.data.RandomInputStream)7 IOException (java.io.IOException)6 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)5 Calendar (java.util.Calendar)4 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Date (java.util.Date)3