Search in sources :

Example 66 with Binary

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

the class PathTest method testGetBinary.

public void testGetBinary() throws RepositoryException, IOException {
    Binary binary = prop.getBinary();
    byte[] bytes = new byte[(int) binary.getSize()];
    binary.read(bytes, 0);
    binary.dispose();
    assertEquals(prop.getString(), new String(bytes, "UTF-8"));
}
Also used : Binary(javax.jcr.Binary)

Example 67 with Binary

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

the class PathTest method testGetBinaryFromValue.

public void testGetBinaryFromValue() throws RepositoryException, IOException {
    Value v = superuser.getValueFactory().createValue("/a/b/c", PropertyType.PATH);
    Binary binary = v.getBinary();
    byte[] bytes = new byte[(int) binary.getSize()];
    binary.read(bytes, 0);
    binary.dispose();
    assertEquals(prop.getString(), new String(bytes, "UTF-8"));
}
Also used : Value(javax.jcr.Value) Binary(javax.jcr.Binary)

Example 68 with Binary

use of javax.jcr.Binary 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 69 with Binary

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

the class RepositoryCopierTest method verifyTargetRepository.

private void verifyTargetRepository(File target) throws Exception {
    RepositoryImpl repository = RepositoryImpl.create(RepositoryConfig.create(target));
    try {
        Session session = repository.login(CREDENTIALS);
        try {
            assertEquals("http://www.example.org/", session.getNamespaceURI("test"));
            NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
            assertTrue(manager.hasNodeType("test:unstructured"));
            NodeType type = manager.getNodeType("test:unstructured");
            assertFalse(type.isMixin());
            assertTrue(type.isNodeType("nt:unstructured"));
            assertTrue(session.nodeExists("/properties"));
            Node properties = session.getNode("/properties");
            assertEquals(PropertyType.BOOLEAN, properties.getProperty("boolean").getType());
            assertEquals(true, properties.getProperty("boolean").getBoolean());
            assertEquals(PropertyType.BINARY, properties.getProperty("binary").getType());
            Binary binary = properties.getProperty("binary").getBinary();
            try {
                InputStream stream = binary.getStream();
                try {
                    for (int i = 0; i < BINARY.length; i++) {
                        assertEquals(BINARY[i], (byte) stream.read());
                    }
                    assertEquals(-1, stream.read());
                } finally {
                    stream.close();
                }
            } finally {
                binary.dispose();
            }
            assertEquals(PropertyType.DATE, properties.getProperty("date").getType());
            assertEquals(DATE.getTimeInMillis(), properties.getProperty("date").getDate().getTimeInMillis());
            assertEquals(PropertyType.DECIMAL, properties.getProperty("decimal").getType());
            assertEquals(new BigDecimal(123), properties.getProperty("decimal").getDecimal());
            assertEquals(PropertyType.DOUBLE, properties.getProperty("double").getType());
            assertEquals(Math.PI, properties.getProperty("double").getDouble());
            assertEquals(PropertyType.LONG, properties.getProperty("long").getType());
            assertEquals(9876543210L, properties.getProperty("long").getLong());
            assertEquals(PropertyType.REFERENCE, properties.getProperty("reference").getType());
            assertEquals(identifier, properties.getProperty("reference").getString());
            assertEquals("/referenceable", properties.getProperty("reference").getNode().getPath());
            assertEquals(PropertyType.STRING, properties.getProperty("string").getType());
            assertEquals("test", properties.getProperty("string").getString());
            assertEquals(PropertyType.STRING, properties.getProperty("multiple").getType());
            Value[] values = properties.getProperty("multiple").getValues();
            assertEquals(3, values.length);
            assertEquals("a", values[0].getString());
            assertEquals("b", values[1].getString());
            assertEquals("c", values[2].getString());
        } finally {
            session.logout();
        }
    } finally {
        repository.shutdown();
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NodeType(javax.jcr.nodetype.NodeType) Node(javax.jcr.Node) Value(javax.jcr.Value) Binary(javax.jcr.Binary) BigDecimal(java.math.BigDecimal) Session(javax.jcr.Session)

Example 70 with Binary

use of javax.jcr.Binary in project vorto by eclipse.

the class ModelSequencerOld method execute.

@Override
public boolean execute(Property inputProperty, Node outputNode, Context context) throws Exception {
    Binary binaryValue = inputProperty.getBinary();
    CheckArg.isNotNull(binaryValue, "binary");
    ModelInfo modelResource = null;
    try {
        IModelParser parser = ModelParserFactory.instance().getParser(outputNode.getPath());
        modelResource = parser.parse(binaryValue.getStream());
        outputNode.setProperty("vorto:description", modelResource.getDescription() != null ? modelResource.getDescription() : "");
        outputNode.setProperty("vorto:type", modelResource.getType().name());
        outputNode.setProperty("vorto:displayname", modelResource.getDisplayName());
        outputNode.setProperty("vorto:version", modelResource.getId().getVersion());
        outputNode.setProperty("vorto:namespace", modelResource.getId().getNamespace());
        outputNode.setProperty("vorto:name", modelResource.getId().getName());
        if (outputNode.hasProperty("vorto:references")) {
            // first remove any previous references of the node.
            outputNode.getProperty("vorto:references").remove();
            outputNode.getSession().save();
        }
        ModelReferencesHelper referencesHelper = new ModelReferencesHelper(modelResource.getReferences());
        if (referencesHelper.hasReferences()) {
            List<Value> references = new ArrayList<Value>();
            for (ModelId modelId : referencesHelper.getReferences()) {
                ModelIdHelper modelIdHelper = new ModelIdHelper(modelId);
                Node referencedFolder = outputNode.getSession().getNode(modelIdHelper.getFullPath());
                Node reference = referencedFolder.getNodes().nextNode();
                references.add(context.valueFactory().createValue(reference));
            }
            outputNode.setProperty("vorto:references", references.toArray(new Value[references.size()]));
        }
    } catch (Throwable couldNotParse) {
        outputNode.setProperty("vorto:description", "Erronous Model !!!!");
        outputNode.setProperty("vorto:type", ModelType.create(outputNode.getPath()).name());
        outputNode.setProperty("vorto:displayname", "");
        outputNode.setProperty("vorto:version", "1.0.0");
        outputNode.setProperty("vorto:namespace", "erronous");
        outputNode.setProperty("vorto:name", outputNode.getName());
    }
    return true;
}
Also used : ModelInfo(org.eclipse.vorto.repository.core.ModelInfo) IModelParser(org.eclipse.vorto.repository.core.impl.parser.IModelParser) ModelIdHelper(org.eclipse.vorto.repository.core.impl.utils.ModelIdHelper) ModelReferencesHelper(org.eclipse.vorto.repository.core.impl.utils.ModelReferencesHelper) Node(javax.jcr.Node) Value(javax.jcr.Value) ArrayList(java.util.ArrayList) Binary(javax.jcr.Binary) ModelId(org.eclipse.vorto.model.ModelId)

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