Search in sources :

Example 1 with ReferenceBinary

use of org.apache.jackrabbit.api.ReferenceBinary in project jackrabbit by apache.

the class ValueFactoryImpl method createValue.

@Override
public Value createValue(Binary binary) {
    try {
        if (binary instanceof BLOBInDataStore) {
            BLOBInDataStore blob = (BLOBInDataStore) binary;
            DataIdentifier identifier = blob.getDataIdentifier();
            InternalValue value;
            if (blob.usesDataStore(store)) {
                value = InternalValue.getInternalValue(identifier, store, false);
            } else {
                value = InternalValue.getInternalValue(identifier, store, true);
            }
            if (value != null) {
                // if the value is already in this data store
                return new BinaryValueImpl(value.getBLOBFileValue());
            }
        } else if (binary instanceof BLOBFileValue) {
            return new BinaryValueImpl(((BLOBFileValue) binary).copy());
        } else if (binary instanceof ReferenceBinary) {
            String reference = ((ReferenceBinary) binary).getReference();
            DataRecord record = store.getRecordFromReference(reference);
            if (record != null) {
                return new BinaryValueImpl(BLOBInDataStore.getInstance(store, record.getIdentifier()));
            }
        }
        return createValue(binary.getStream());
    } catch (RepositoryException e) {
        log.error(e.getMessage(), e);
    // ignore - the super method may be smarter
    }
    return super.createValue(binary);
}
Also used : DataIdentifier(org.apache.jackrabbit.core.data.DataIdentifier) RepositoryException(javax.jcr.RepositoryException) DataRecord(org.apache.jackrabbit.core.data.DataRecord) ReferenceBinary(org.apache.jackrabbit.api.ReferenceBinary)

Example 2 with ReferenceBinary

use of org.apache.jackrabbit.api.ReferenceBinary in project jackrabbit-oak by apache.

the class RepositoryTest method testReferenceBinary.

@Test
public void testReferenceBinary() throws RepositoryException {
    ValueFactory valueFactory = getAdminSession().getValueFactory();
    Binary binary = valueFactory.createBinary(new RandomInputStream(1, 256 * 1024));
    String reference = binary instanceof ReferenceBinary ? ((ReferenceBinary) binary).getReference() : null;
    assumeTrue(reference != null);
    Session session = createAdminSession();
    try {
        valueFactory = session.getValueFactory();
        assertEquals(binary, valueFactory.createValue(new SimpleReferenceBinary(reference)).getBinary());
    } finally {
        session.logout();
    }
}
Also used : SimpleReferenceBinary(org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary) ValueFactory(javax.jcr.ValueFactory) SimpleReferenceBinary(org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary) ReferenceBinary(org.apache.jackrabbit.api.ReferenceBinary) Binary(javax.jcr.Binary) JUnitMatchers.containsString(org.junit.matchers.JUnitMatchers.containsString) SimpleReferenceBinary(org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary) ReferenceBinary(org.apache.jackrabbit.api.ReferenceBinary) RandomInputStream(org.apache.jackrabbit.core.data.RandomInputStream) Session(javax.jcr.Session) Test(org.junit.Test)

Example 3 with ReferenceBinary

use of org.apache.jackrabbit.api.ReferenceBinary in project jackrabbit by apache.

the class ReferenceBinaryTest method testReferenceBinaryExchangeWithSharedRepository.

public void testReferenceBinaryExchangeWithSharedRepository() throws Exception {
    Session firstSession = superuser;
    // create a binary
    Binary b = vf.createBinary(new RandomInputStream(1, STREAM_LENGTH));
    ReferenceBinary referenceBinary = null;
    if (b instanceof ReferenceBinary) {
        referenceBinary = (ReferenceBinary) b;
    }
    assertNotNull(referenceBinary);
    assertNotNull(referenceBinary.getReference());
    // in the current test the message is exchanged via repository which is shared as well
    // put the reference message value in a property on a node
    String newNode = "sample_" + System.nanoTime();
    firstSession.getRootNode().addNode(newNode).setProperty("reference", referenceBinary.getReference());
    // save the first session
    firstSession.save();
    // get a second session over the same repository / ds
    Session secondSession = getHelper().getRepository().login(new SimpleCredentials("admin", "admin".toCharArray()));
    // read the binary referenced by the referencing binary
    String reference = secondSession.getRootNode().getNode(newNode).getProperty("reference").getString();
    ReferenceBinary ref = new SimpleReferenceBinary(reference);
    assertEquals(b, secondSession.getValueFactory().createValue(ref).getBinary());
}
Also used : SimpleCredentials(javax.jcr.SimpleCredentials) SimpleReferenceBinary(org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary) SimpleReferenceBinary(org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary) Binary(javax.jcr.Binary) ReferenceBinary(org.apache.jackrabbit.api.ReferenceBinary) SimpleReferenceBinary(org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary) ReferenceBinary(org.apache.jackrabbit.api.ReferenceBinary) Session(javax.jcr.Session) RandomInputStream(org.apache.jackrabbit.core.data.RandomInputStream)

Example 4 with ReferenceBinary

use of org.apache.jackrabbit.api.ReferenceBinary in project jackrabbit-oak by apache.

the class ReferenceBinaryIT method testReferenceBinaryExchangeWithSharedRepository.

/**
     * Taken from org.apache.jackrabbit.core.value.ReferenceBinaryTest
     * @throws Exception
     */
@Test
public void testReferenceBinaryExchangeWithSharedRepository() throws Exception {
    Session firstSession = createAdminSession();
    // create a binary
    Binary b = firstSession.getValueFactory().createBinary(new RandomInputStream(1, STREAM_LENGTH));
    ReferenceBinary referenceBinary = null;
    if (b instanceof ReferenceBinary) {
        referenceBinary = (ReferenceBinary) b;
    }
    assertNotNull(referenceBinary);
    assertNotNull(referenceBinary.getReference());
    // in the current test the message is exchanged via repository which is shared as well
    // put the reference message value in a property on a node
    String newNode = "sample_" + System.nanoTime();
    firstSession.getRootNode().addNode(newNode).setProperty("reference", referenceBinary.getReference());
    // save the first session
    firstSession.save();
    // get a second session over the same repository / ds
    Session secondSession = repository.login(new SimpleCredentials("admin", "admin".toCharArray()));
    // read the binary referenced by the referencing binary
    String reference = secondSession.getRootNode().getNode(newNode).getProperty("reference").getString();
    ReferenceBinary ref = new SimpleReferenceBinary(reference);
    assertEquals(b, secondSession.getValueFactory().createValue(ref).getBinary());
    safeLogout(firstSession);
    safeLogout(secondSession);
}
Also used : SimpleCredentials(javax.jcr.SimpleCredentials) SimpleReferenceBinary(org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary) Binary(javax.jcr.Binary) SimpleReferenceBinary(org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary) ReferenceBinary(org.apache.jackrabbit.api.ReferenceBinary) SimpleReferenceBinary(org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary) ReferenceBinary(org.apache.jackrabbit.api.ReferenceBinary) Session(javax.jcr.Session) RandomInputStream(org.apache.jackrabbit.core.data.RandomInputStream) Test(org.junit.Test)

Example 5 with ReferenceBinary

use of org.apache.jackrabbit.api.ReferenceBinary in project jackrabbit-oak by apache.

the class JackrabbitNodeState method createBlob.

private Blob createBlob(final InternalValue value) {
    checkArgument(checkNotNull(value).getType() == PropertyType.BINARY);
    return new AbstractBlob() {

        @Override
        public long length() {
            try {
                return value.getLength();
            } catch (RepositoryException e) {
                warn("Unable to access blob length", e);
                return 0;
            }
        }

        @Nonnull
        @Override
        public InputStream getNewStream() {
            try {
                return value.getStream();
            } catch (RepositoryException e) {
                warn("Unable to access blob contents", e);
                return new ByteArrayInputStream(new byte[0]);
            }
        }

        @Override
        public String getReference() {
            if (!useBinaryReferences) {
                return null;
            }
            try {
                Binary binary = value.getBinary();
                try {
                    if (binary instanceof ReferenceBinary) {
                        return ((ReferenceBinary) binary).getReference();
                    } else {
                        return null;
                    }
                } finally {
                    binary.dispose();
                }
            } catch (RepositoryException e) {
                warn("Unable to get blob reference", e);
                return null;
            }
        }

        @Override
        public String getContentIdentity() {
            final String reference = getReference();
            if (reference != null) {
                final int pos = reference.indexOf(":");
                final String blobHash;
                if (pos > -1) {
                    blobHash = reference.substring(0, pos);
                } else {
                    blobHash = reference;
                }
                return blobHash + "#" + length();
            }
            return super.getContentIdentity();
        }
    };
}
Also used : AbstractBlob(org.apache.jackrabbit.oak.plugins.memory.AbstractBlob) ByteArrayInputStream(java.io.ByteArrayInputStream) RepositoryException(javax.jcr.RepositoryException) ReferenceBinary(org.apache.jackrabbit.api.ReferenceBinary) Binary(javax.jcr.Binary) ReferenceBinary(org.apache.jackrabbit.api.ReferenceBinary)

Aggregations

ReferenceBinary (org.apache.jackrabbit.api.ReferenceBinary)5 Binary (javax.jcr.Binary)4 Session (javax.jcr.Session)3 SimpleReferenceBinary (org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary)3 RandomInputStream (org.apache.jackrabbit.core.data.RandomInputStream)3 RepositoryException (javax.jcr.RepositoryException)2 SimpleCredentials (javax.jcr.SimpleCredentials)2 Test (org.junit.Test)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ValueFactory (javax.jcr.ValueFactory)1 DataIdentifier (org.apache.jackrabbit.core.data.DataIdentifier)1 DataRecord (org.apache.jackrabbit.core.data.DataRecord)1 AbstractBlob (org.apache.jackrabbit.oak.plugins.memory.AbstractBlob)1 JUnitMatchers.containsString (org.junit.matchers.JUnitMatchers.containsString)1