Search in sources :

Example 6 with OBlob

use of com.orientechnologies.orient.core.record.impl.OBlob in project orientdb by orientechnologies.

the class OHttpMultipartFileToRecordContentParser method parse.

@Override
public ORID parse(final OHttpRequest iRequest, final Map<String, String> headers, final OHttpMultipartContentInputStream in, ODatabaseDocument database) throws IOException {
    final OBlob record = new ORecordBytes();
    record.fromInputStream(in);
    record.save();
    return record.getIdentity();
}
Also used : OBlob(com.orientechnologies.orient.core.record.impl.OBlob) ORecordBytes(com.orientechnologies.orient.core.record.impl.ORecordBytes)

Example 7 with OBlob

use of com.orientechnologies.orient.core.record.impl.OBlob in project orientdb by orientechnologies.

the class OServerCommandGetFileDownload method execute.

@Override
public boolean execute(OHttpRequest iRequest, OHttpResponse iResponse) throws Exception {
    String[] urlParts = checkSyntax(iRequest.url, 3, "Syntax error: fileDownload/<database>/rid/[/<fileName>][/<fileType>].");
    final String fileName = urlParts.length > 3 ? encodeResponseText(urlParts[3]) : "unknown";
    final String fileType = urlParts.length > 5 ? encodeResponseText(urlParts[4]) + '/' + encodeResponseText(urlParts[5]) : (urlParts.length > 4 ? encodeResponseText(urlParts[4]) : "");
    final String rid = urlParts[2];
    iRequest.data.commandInfo = "Download";
    iRequest.data.commandDetail = rid;
    final ORecordAbstract response;
    ODatabaseDocument db = getProfiledDatabaseInstance(iRequest);
    try {
        response = db.load(new ORecordId(rid));
        if (response != null) {
            if (response instanceof OBlob) {
                sendORecordBinaryFileContent(iRequest, iResponse, OHttpUtils.STATUS_OK_CODE, OHttpUtils.STATUS_OK_DESCRIPTION, fileType, (OBlob) response, fileName);
            } else if (response instanceof ODocument) {
                for (OProperty prop : ODocumentInternal.getImmutableSchemaClass(((ODocument) response)).properties()) {
                    if (prop.getType().equals(OType.BINARY))
                        sendBinaryFieldFileContent(iRequest, iResponse, OHttpUtils.STATUS_OK_CODE, OHttpUtils.STATUS_OK_DESCRIPTION, fileType, (byte[]) ((ODocument) response).field(prop.getName()), fileName);
                }
            } else {
                iResponse.send(OHttpUtils.STATUS_INVALIDMETHOD_CODE, "Record requested is not a file nor has a readable schema", OHttpUtils.CONTENT_TEXT_PLAIN, "Record requested is not a file nor has a readable schema", null);
            }
        } else {
            iResponse.send(OHttpUtils.STATUS_INVALIDMETHOD_CODE, "Record requested not exists", OHttpUtils.CONTENT_TEXT_PLAIN, "Record requestes not exists", null);
        }
    } catch (Exception e) {
        iResponse.send(OHttpUtils.STATUS_INTERNALERROR_CODE, OHttpUtils.STATUS_INTERNALERROR_DESCRIPTION, OHttpUtils.CONTENT_TEXT_PLAIN, e.getMessage(), null);
    } finally {
        if (db != null)
            db.close();
    }
    return false;
}
Also used : ORecordAbstract(com.orientechnologies.orient.core.record.ORecordAbstract) OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) OBlob(com.orientechnologies.orient.core.record.impl.OBlob) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) ORecordId(com.orientechnologies.orient.core.id.ORecordId) IOException(java.io.IOException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 8 with OBlob

use of com.orientechnologies.orient.core.record.impl.OBlob in project orientdb by orientechnologies.

the class CRUDObjectPhysicalTestSchemaFull method testOrphanDelete.

@Test(dependsOnMethods = "testObjectDelete")
public void testOrphanDelete() {
    Media media = new Media();
    OBlob testRecord = new ORecordBytes("This is a test".getBytes());
    media.setContent(testRecord);
    media = database.save(media);
    Assert.assertEquals(new String(media.getContent().toStream()), "This is a test");
    // try to delete
    database.delete(media);
}
Also used : OBlob(com.orientechnologies.orient.core.record.impl.OBlob) ORecordBytes(com.orientechnologies.orient.core.record.impl.ORecordBytes) Test(org.testng.annotations.Test)

Example 9 with OBlob

use of com.orientechnologies.orient.core.record.impl.OBlob in project orientdb by orientechnologies.

the class CRUDObjectPhysicalTestSchemaFull method oidentifableFieldsTest.

@Test(dependsOnMethods = "testNoGenericCollectionsWrongAdding")
public void oidentifableFieldsTest() {
    JavaComplexTestClass p = database.newInstance(JavaComplexTestClass.class);
    p.setName("Dean Winchester");
    ODocument testEmbeddedDocument = new ODocument();
    testEmbeddedDocument.field("testEmbeddedField", "testEmbeddedValue");
    p.setEmbeddedDocument(testEmbeddedDocument);
    ODocument testDocument = new ODocument();
    testDocument.field("testField", "testValue");
    p.setDocument(testDocument);
    OBlob testRecordBytes = new ORecordBytes("this is a bytearray test. if you read this Object database has stored it correctly".getBytes());
    p.setByteArray(testRecordBytes);
    database.save(p);
    ORID rid = database.getRecordByUserObject(p, false).getIdentity();
    database.close();
    database = OObjectDatabasePool.global().acquire(url, "admin", "admin");
    JavaComplexTestClass loaded = database.load(rid);
    Assert.assertTrue(loaded.getByteArray() instanceof OBlob);
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            loaded.getByteArray().toOutputStream(out);
            Assert.assertEquals("this is a bytearray test. if you read this Object database has stored it correctly".getBytes(), out.toByteArray());
            Assert.assertEquals("this is a bytearray test. if you read this Object database has stored it correctly", new String(out.toByteArray()));
        } finally {
            out.close();
        }
    } catch (IOException ioe) {
        Assert.assertTrue(false);
        OLogManager.instance().error(this, "Error reading byte[]", ioe);
    }
    Assert.assertTrue(loaded.getDocument() instanceof ODocument);
    Assert.assertEquals("testValue", loaded.getDocument().field("testField"));
    Assert.assertTrue(loaded.getDocument().getIdentity().isPersistent());
    Assert.assertTrue(loaded.getEmbeddedDocument() instanceof ODocument);
    Assert.assertEquals("testEmbeddedValue", loaded.getEmbeddedDocument().field("testEmbeddedField"));
    Assert.assertFalse(loaded.getEmbeddedDocument().getIdentity().isValid());
    database.close();
    database = OObjectDatabasePool.global().acquire(url, "admin", "admin");
    p = database.newInstance(JavaComplexTestClass.class);
    byte[] thumbnailImageBytes = "this is a bytearray test. if you read this Object database has stored it correctlyVERSION2".getBytes();
    OBlob oRecordBytes = new ORecordBytes(database.getUnderlying(), thumbnailImageBytes);
    oRecordBytes.save();
    p.setByteArray(oRecordBytes);
    p = database.save(p);
    Assert.assertTrue(p.getByteArray() instanceof OBlob);
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            p.getByteArray().toOutputStream(out);
            Assert.assertEquals("this is a bytearray test. if you read this Object database has stored it correctlyVERSION2".getBytes(), out.toByteArray());
            Assert.assertEquals("this is a bytearray test. if you read this Object database has stored it correctlyVERSION2", new String(out.toByteArray()));
        } finally {
            out.close();
        }
    } catch (IOException ioe) {
        Assert.assertTrue(false);
        OLogManager.instance().error(this, "Error reading byte[]", ioe);
    }
    rid = database.getIdentity(p);
    database.close();
    database = OObjectDatabasePool.global().acquire(url, "admin", "admin");
    loaded = database.load(rid);
    Assert.assertTrue(loaded.getByteArray() instanceof OBlob);
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            loaded.getByteArray().toOutputStream(out);
            Assert.assertEquals("this is a bytearray test. if you read this Object database has stored it correctlyVERSION2".getBytes(), out.toByteArray());
            Assert.assertEquals("this is a bytearray test. if you read this Object database has stored it correctlyVERSION2", new String(out.toByteArray()));
        } finally {
            out.close();
        }
    } catch (IOException ioe) {
        Assert.assertTrue(false);
        OLogManager.instance().error(this, "Error reading byte[]", ioe);
    }
    database.close();
    database = OObjectDatabasePool.global().acquire(url, "admin", "admin");
    p = new JavaComplexTestClass();
    thumbnailImageBytes = "this is a bytearray test. if you read this Object database has stored it correctlyVERSION2".getBytes();
    oRecordBytes = new ORecordBytes(database.getUnderlying(), thumbnailImageBytes);
    oRecordBytes.save();
    p.setByteArray(oRecordBytes);
    p = database.save(p);
    Assert.assertTrue(p.getByteArray() instanceof OBlob);
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            p.getByteArray().toOutputStream(out);
            Assert.assertEquals("this is a bytearray test. if you read this Object database has stored it correctlyVERSION2".getBytes(), out.toByteArray());
            Assert.assertEquals("this is a bytearray test. if you read this Object database has stored it correctlyVERSION2", new String(out.toByteArray()));
        } finally {
            out.close();
        }
    } catch (IOException ioe) {
        Assert.assertTrue(false);
        OLogManager.instance().error(this, "Error reading byte[]", ioe);
    }
    rid = database.getIdentity(p);
    database.close();
    database = OObjectDatabasePool.global().acquire(url, "admin", "admin");
    loaded = database.load(rid);
    Assert.assertTrue(loaded.getByteArray() instanceof OBlob);
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            loaded.getByteArray().toOutputStream(out);
            Assert.assertEquals("this is a bytearray test. if you read this Object database has stored it correctlyVERSION2".getBytes(), out.toByteArray());
            Assert.assertEquals("this is a bytearray test. if you read this Object database has stored it correctlyVERSION2", new String(out.toByteArray()));
        } finally {
            out.close();
        }
    } catch (IOException ioe) {
        Assert.assertTrue(false);
        OLogManager.instance().error(this, "Error reading byte[]", ioe);
    }
}
Also used : OBlob(com.orientechnologies.orient.core.record.impl.OBlob) ORecordBytes(com.orientechnologies.orient.core.record.impl.ORecordBytes) ORID(com.orientechnologies.orient.core.id.ORID) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 10 with OBlob

use of com.orientechnologies.orient.core.record.impl.OBlob in project orientdb by orientechnologies.

the class SQLSelectTest method testBinaryClusterSelect3.

@Test
public void testBinaryClusterSelect3() {
    database.command(new OCommandSQL("create class testBinaryClusterSelect3")).execute();
    database.command(new OCommandSQL("create blob cluster testBinaryClusterSelect3_blob")).execute();
    database.reload();
    OBlob bytes = new ORecordBytes(new byte[] { 1, 2, 3 });
    database.save(bytes, "testBinaryClusterSelect3_blob");
    ODocument doc = new ODocument("testBinaryClusterSelect3");
    doc.field("Blob", bytes);
    doc.save();
    ODocument doc2 = new ODocument("testBinaryClusterSelect3");
    doc2.save();
    List<OIdentifiable> result = database.query(new OSQLSynchQuery<OIdentifiable>("select from cluster:testBinaryClusterSelect3_blob"));
    Assert.assertEquals(result.size(), 1);
    database.command(new OCommandSQL("delete from (select expand(Blob) from testBinaryClusterSelect3)")).execute();
    result = database.query(new OSQLSynchQuery<OIdentifiable>("select from cluster:testBinaryClusterSelect3_blob"));
    Assert.assertEquals(result.size(), 0);
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OBlob(com.orientechnologies.orient.core.record.impl.OBlob) ORecordBytes(com.orientechnologies.orient.core.record.impl.ORecordBytes) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Aggregations

OBlob (com.orientechnologies.orient.core.record.impl.OBlob)32 ORecordBytes (com.orientechnologies.orient.core.record.impl.ORecordBytes)20 Test (org.testng.annotations.Test)19 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)12 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)8 ORID (com.orientechnologies.orient.core.id.ORID)4 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)4 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)3 IOException (java.io.IOException)3 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)2 ORecordId (com.orientechnologies.orient.core.id.ORecordId)2 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)2 BufferedInputStream (java.io.BufferedInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 OAfterDeserialization (com.orientechnologies.orient.core.annotation.OAfterDeserialization)1 OBeforeSerialization (com.orientechnologies.orient.core.annotation.OBeforeSerialization)1 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)1 ORecordLazyList (com.orientechnologies.orient.core.db.record.ORecordLazyList)1