use of v7db.files.spi.ContentSHA in project v7files by thiloplanz.
the class MongoContentStorage method getContent.
public Content getContent(ContentPointer pointer) throws IOException {
if (pointer == null)
return null;
if (pointer instanceof InlineContent)
return (Content) pointer;
if (pointer instanceof ContentSHA) {
ContentSHA p = (ContentSHA) pointer;
byte[] sha = p.getSHA();
Content base = getContent(sha);
if (base == null)
throw new IllegalArgumentException("base SHA not found: " + Hex.encodeHexString(sha));
return base;
}
if (pointer instanceof StoredContent) {
StoredContent p = (StoredContent) pointer;
byte[] sha = p.getBaseSHA();
Content base = getContent(sha);
if (base == null)
throw new IllegalArgumentException("base SHA not found: " + Hex.encodeHexString(sha));
if (p.getLength() != base.getLength()) {
return new OffsetAndLength(base, 0, p.getLength());
}
return base;
}
throw new IllegalArgumentException(pointer.getClass().toString());
}
use of v7db.files.spi.ContentSHA in project v7files by thiloplanz.
the class MongoContentStorage method storeContentChunk.
private ContentSHA storeContentChunk(byte[] bytes, final int offset, final int length) throws IOException {
ContentSHA _sha = ContentSHA.calculate(bytes, offset, length);
byte[] sha = _sha.getSHA();
long existing = contentCollection.count(new BasicDBObject(_ID, sha));
if (existing == 0) {
byte[] gzipped = Compression.gzip(bytes, offset, length);
if (gzipped != null && gzipped.length > chunkSize)
gzipped = null;
if (gzipped != null) {
bytes = null;
contentCollection.insert(new BasicDBObject(_ID, sha).append("zin", gzipped).append("store", "gz"), WriteConcern.SAFE);
gzipped = null;
} else {
if (offset > 0 || bytes.length != length) {
bytes = ArrayUtils.subarray(bytes, offset, offset + length);
}
contentCollection.insert(new BasicDBObject(_ID, sha).append("in", bytes), WriteConcern.SAFE);
}
}
return _sha;
}
use of v7db.files.spi.ContentSHA in project v7files by thiloplanz.
the class BucketsServletTest method testEchoPutGET.
public void testEchoPutGET() throws IOException, SAXException {
ServletUnitClient sc = sr.newClient();
{
WebRequest request = new GetMethodWebRequest("http://test/myServlet/1");
request.setParameter("sha", "1234");
try {
sc.getResponse(request);
fail("bucket not found => 404");
} catch (HttpNotFoundException e) {
assertEquals("Bucket '1' not found", e.getResponseMessage());
}
}
prepareBucket("1", "EchoPut", null, null);
{
WebRequest request = new GetMethodWebRequest("http://test/myServlet/1");
request.setParameter("sha", "1234");
try {
sc.getResponse(request);
fail("bucket not found => 404");
} catch (HttpNotFoundException e) {
assertEquals("Bucket '1' does not have a file matching digest '1234'", e.getResponseMessage());
}
}
MongoContentStorage storage = new MongoContentStorage(getMongo().getDB("test"));
ContentSHA sha = storage.storeContent(new ByteArrayInputStream("test".getBytes()));
{
WebRequest request = new GetMethodWebRequest("http://test/myServlet/1");
request.setParameter("sha", sha.getDigest());
request.setParameter("filename", "a.txt");
WebResponse response = sc.getResponse(request);
assertEquals("test", response.getText());
assertEquals(sha.getDigest(), response.getHeaderField("ETag"));
assertEquals(4, response.getContentLength());
assertEquals("attachment; filename=\"a.txt\"", response.getHeaderField("Content-Disposition"));
}
{
WebRequest request = new GetMethodWebRequest("http://test/myServlet/1");
request.setParameter("sha", sha.getDigest());
request.setHeaderField("If-None-Match", sha.getDigest());
WebResponse response = sc.getResponse(request);
assertEquals(HttpServletResponse.SC_NOT_MODIFIED, response.getResponseCode());
}
}
use of v7db.files.spi.ContentSHA in project v7files by thiloplanz.
the class BucketsServletTest method testFormPostGET.
public void testFormPostGET() throws IOException, SAXException {
BasicBSONObject bucket = prepareBucket("1", "FormPost", null, null);
MongoContentStorage storage = new MongoContentStorage(getMongo().getDB("test"));
ContentSHA sha = storage.storeContent(new ByteArrayInputStream("test".getBytes()));
ServletUnitClient sc = sr.newClient();
{
WebRequest request = new GetMethodWebRequest("http://test/myServlet/1");
request.setParameter("sha", sha.getDigest());
try {
sc.getResponse(request);
fail("bucket not found => 404");
} catch (HttpNotFoundException e) {
assertEquals(String.format("Bucket '1' does not have a file matching digest '%s'", sha.getDigest()), e.getResponseMessage());
}
}
bucket.append("FormPost", new BasicBSONObject("data", new BasicBSONObject("files", new BasicBSONObject("file", new BasicBSONObject("filename", "a.txt").append("sha", sha.getSHA())))));
{
WebRequest request = new GetMethodWebRequest("http://test/myServlet/1");
request.setParameter("sha", sha.getDigest());
WebResponse response = sc.getResponse(request);
assertEquals("test", response.getText());
assertEquals(sha.getDigest(), response.getHeaderField("ETag"));
assertEquals(4, response.getContentLength());
assertEquals("attachment; filename=\"a.txt\"", response.getHeaderField("Content-Disposition"));
}
{
WebRequest request = new GetMethodWebRequest("http://test/myServlet/1");
request.setParameter("sha", sha.getDigest());
request.setParameter("filename", "x.txt");
WebResponse response = sc.getResponse(request);
assertEquals("test", response.getText());
assertEquals(sha.getDigest(), response.getHeaderField("ETag"));
assertEquals(4, response.getContentLength());
assertEquals("attachment; filename=\"x.txt\"", response.getHeaderField("Content-Disposition"));
}
{
WebRequest request = new GetMethodWebRequest("http://test/myServlet/1");
request.setParameter("sha", sha.getDigest());
request.setHeaderField("If-None-Match", sha.getDigest());
WebResponse response = sc.getResponse(request);
assertEquals(HttpServletResponse.SC_NOT_MODIFIED, response.getResponseCode());
}
}
use of v7db.files.spi.ContentSHA in project v7files by thiloplanz.
the class MongoContentStorageTest method testSaveAsChunks.
public void testSaveAsChunks() throws IOException {
byte[] data = new byte[10 * 1024 * 1024];
new Random(12345).nextBytes(data);
byte[] sha = DigestUtils.sha(data);
Mongo mongo = getMongo();
MongoContentStorage storage = new MongoContentStorage(mongo.getDB("test"));
ContentPointer pointer = storage.storeContent(new ByteArrayInputStream(data));
BSONObject doc = assertMockMongoContainsDocument("test.v7files.content", sha);
assertEquals("cat", doc.get("store"));
assertEquals(data.length, storage.getContent(pointer).getLength());
assertEquals(Hex.encodeHexString(sha), DigestUtils.shaHex(storage.getContent(pointer).getInputStream()));
ContentSHA storeAgain = storage.storeContent(new ByteArrayInputStream(data));
assertEquals(Hex.encodeHexString(sha), storeAgain.getDigest());
}
Aggregations