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();
}
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;
}
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);
}
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);
}
}
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);
}
Aggregations