Search in sources :

Example 1 with ContentSHA

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());
}
Also used : OffsetAndLength(v7db.files.spi.OffsetAndLength) ContentSHA(v7db.files.spi.ContentSHA) InlineContent(v7db.files.spi.InlineContent) GzippedContent(v7db.files.spi.GzippedContent) Content(v7db.files.spi.Content) StoredContent(v7db.files.spi.StoredContent) InlineContent(v7db.files.spi.InlineContent) StoredContent(v7db.files.spi.StoredContent)

Example 2 with ContentSHA

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;
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) ContentSHA(v7db.files.spi.ContentSHA)

Example 3 with ContentSHA

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());
    }
}
Also used : HttpNotFoundException(com.meterware.httpunit.HttpNotFoundException) MongoContentStorage(v7db.files.mongodb.MongoContentStorage) WebResponse(com.meterware.httpunit.WebResponse) WebRequest(com.meterware.httpunit.WebRequest) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest) PutMethodWebRequest(com.meterware.httpunit.PutMethodWebRequest) PostMethodWebRequest(com.meterware.httpunit.PostMethodWebRequest) ContentSHA(v7db.files.spi.ContentSHA) ByteArrayInputStream(java.io.ByteArrayInputStream) ServletUnitClient(com.meterware.servletunit.ServletUnitClient) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest)

Example 4 with ContentSHA

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());
    }
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) HttpNotFoundException(com.meterware.httpunit.HttpNotFoundException) MongoContentStorage(v7db.files.mongodb.MongoContentStorage) WebResponse(com.meterware.httpunit.WebResponse) WebRequest(com.meterware.httpunit.WebRequest) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest) PutMethodWebRequest(com.meterware.httpunit.PutMethodWebRequest) PostMethodWebRequest(com.meterware.httpunit.PostMethodWebRequest) ContentSHA(v7db.files.spi.ContentSHA) ByteArrayInputStream(java.io.ByteArrayInputStream) ServletUnitClient(com.meterware.servletunit.ServletUnitClient) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest)

Example 5 with ContentSHA

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());
}
Also used : Random(java.util.Random) Mongo(com.mongodb.Mongo) ByteArrayInputStream(java.io.ByteArrayInputStream) ContentSHA(v7db.files.spi.ContentSHA) ContentPointer(v7db.files.spi.ContentPointer) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject)

Aggregations

ContentSHA (v7db.files.spi.ContentSHA)12 MongoContentStorage (v7db.files.mongodb.MongoContentStorage)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 BasicDBObject (com.mongodb.BasicDBObject)3 IOException (java.io.IOException)3 ContentPointer (v7db.files.spi.ContentPointer)3 InlineContent (v7db.files.spi.InlineContent)3 GetMethodWebRequest (com.meterware.httpunit.GetMethodWebRequest)2 HttpNotFoundException (com.meterware.httpunit.HttpNotFoundException)2 PostMethodWebRequest (com.meterware.httpunit.PostMethodWebRequest)2 PutMethodWebRequest (com.meterware.httpunit.PutMethodWebRequest)2 WebRequest (com.meterware.httpunit.WebRequest)2 WebResponse (com.meterware.httpunit.WebResponse)2 ServletUnitClient (com.meterware.servletunit.ServletUnitClient)2 ArrayList (java.util.ArrayList)2 BSONObject (org.bson.BSONObject)2 BasicBSONObject (org.bson.BasicBSONObject)2 V7File (v7db.files.mongodb.V7File)2 V7GridFS (v7db.files.mongodb.V7GridFS)2 StoredContent (v7db.files.spi.StoredContent)2