Search in sources :

Example 1 with MongoContentStorage

use of v7db.files.mongodb.MongoContentStorage in project v7files by thiloplanz.

the class CatCommand method main.

public static void main(String[] args) throws MongoException, IOException {
    if (args.length != 3) {
        System.err.println("Output the contents of a file:");
        System.err.println("  by name:   cat <root> <path>");
        System.err.println("  by hash:   cat -sha <shaHex>");
        System.exit(1);
    }
    if ("-sha".equals(args[1])) {
        MongoContentStorage storage = new MongoContentStorage(Configuration.getMongo().getDB(Configuration.getProperty("mongo.db")));
        String sha = args[2];
        try {
            Content file = findContentByPrefix(storage, sha);
            if (file == null) {
                System.err.println("file not found");
                System.exit(1);
            }
            IOUtils.copy(file.getInputStream(), System.out);
        } catch (DecoderException e) {
            System.err.println("invalid parameter :" + sha + " is not a hex-encoded SHA-1 prefix");
            System.exit(1);
        }
    } else {
        V7GridFS fs = new V7GridFS(Configuration.getMongo().getDB(Configuration.getProperty("mongo.db")));
        String root = args[1];
        String path = args[2];
        String[] fullPath = ArrayUtils.add(StringUtils.split(path, '/'), 0, root);
        V7File file = fs.getFile(fullPath);
        if (file == null) {
            System.err.println("file not found");
            System.exit(1);
        }
        IOUtils.copy(file.getInputStream(), System.out);
    }
}
Also used : DecoderException(org.apache.commons.codec.DecoderException) MongoContentStorage(v7db.files.mongodb.MongoContentStorage) V7File(v7db.files.mongodb.V7File) Content(v7db.files.spi.Content) V7GridFS(v7db.files.mongodb.V7GridFS)

Example 2 with MongoContentStorage

use of v7db.files.mongodb.MongoContentStorage in project v7files by thiloplanz.

the class ZipFileTest method testPullOutFileFromZip.

public void testPullOutFileFromZip() throws MongoException, IOException, ZipException, DecoderException {
    ContentStorage storage = new MongoContentStorage(getMongo().getDB("test"));
    ContentPointer zip = storage.storeContent(getClass().getResourceAsStream("mongodb.epub"));
    ContentPointer png = ZipFile.extractFile(storage, zip, "images/img0.png");
    assertEquals("fc012bb0439382f709d3caebab958ff592811d17", DigestUtils.shaHex(storage.getContent(png).getInputStream()));
}
Also used : MongoContentStorage(v7db.files.mongodb.MongoContentStorage) MongoContentStorage(v7db.files.mongodb.MongoContentStorage) ContentStorage(v7db.files.spi.ContentStorage) ContentPointer(v7db.files.spi.ContentPointer)

Example 3 with MongoContentStorage

use of v7db.files.mongodb.MongoContentStorage 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 MongoContentStorage

use of v7db.files.mongodb.MongoContentStorage in project v7files by thiloplanz.

the class ZipFileTest method testIndexZipFile.

public void testIndexZipFile() throws MongoException, IOException, ZipException, DecoderException {
    ContentStorage storage = new MongoContentStorage(getMongo().getDB("test"));
    ContentPointer zip = storage.storeContent(getClass().getResourceAsStream("mongodb.epub"));
    ZipFile.index(storage, zip);
    assertEquals("fc012bb0439382f709d3caebab958ff592811d17", DigestUtils.shaHex(storage.getContent(decodeHex("fc012bb0439382f709d3caebab958ff592811d17".toCharArray())).getInputStream()));
}
Also used : MongoContentStorage(v7db.files.mongodb.MongoContentStorage) MongoContentStorage(v7db.files.mongodb.MongoContentStorage) ContentStorage(v7db.files.spi.ContentStorage) ContentPointer(v7db.files.spi.ContentPointer)

Example 5 with MongoContentStorage

use of v7db.files.mongodb.MongoContentStorage 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)

Aggregations

MongoContentStorage (v7db.files.mongodb.MongoContentStorage)9 ContentSHA (v7db.files.spi.ContentSHA)6 ByteArrayInputStream (java.io.ByteArrayInputStream)4 V7File (v7db.files.mongodb.V7File)3 V7GridFS (v7db.files.mongodb.V7GridFS)3 ContentStorage (v7db.files.spi.ContentStorage)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 IOException (java.io.IOException)2 DecoderException (org.apache.commons.codec.DecoderException)2 ContentPointer (v7db.files.spi.ContentPointer)2 DB (com.mongodb.DB)1 MongoException (com.mongodb.MongoException)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1