Package v7db.files.spi

Examples of v7db.files.spi.ContentSHA


  }

  public void testSHA() throws IOException {
    MongoContentStorage storage = new MongoContentStorage(getMongo().getDB(
        "test"));
    ContentSHA sha = storage.storeContent(new ByteArrayInputStream("test"
        .getBytes()));

    CopyCommand.main(new String[] { "copy", "-sha", sha.getDigest(), "x",
        "copy.txt" });

    V7GridFS fs = new V7GridFS(getMongo().getDB("test"));
    V7File file = fs.getFile("x", "copy.txt");
    assertEquals("test", IOUtils.toString(file.getInputStream()));
View Full Code Here


      }
    }

    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());
    }
View Full Code Here

  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());
    }
View Full Code Here

  }

  public void testSha() throws IOException {
    MongoContentStorage storage = new MongoContentStorage(getMongo().getDB(
        "test"));
    ContentSHA sha = storage.storeContent(new ByteArrayInputStream("test"
        .getBytes()));
    CatCommand.main(new String[] { "cat", "-sha", sha.getDigest() });
    System.out.flush();
    assertEquals("test", out.toString());
  }
View Full Code Here

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

  }
View Full Code Here

    for (int i = 1; i < args.length; i++) {
      File f = new File(args[i]);
      if (f.isFile() && f.canRead()) {
        try {
          ContentSHA up = storage
              .storeContent(new FileInputStream(f));

          // TODO: display if chunked or not
          System.out.format("-      %10d %80s %40s\n", f.length(), f
              .getName(), up.getDigest());
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }
View Full Code Here

    if ("-sha".equals(args[1])) {
      MongoContentStorage storage = new MongoContentStorage(db);
      String sha = args[2];
      try {
        ContentSHA file = findContentByPrefix(storage, sha);
        if (file == null)
          throw new FileNotFoundException("-sha " + sha);
        String[] path = getPath(args[3], args[4]);
        createFile(fs, file, path, null);
      } catch (DecoderException e) {
View Full Code Here

      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;

    }
View Full Code Here

      List<Map<String, Object>> bases = new ArrayList<Map<String, Object>>(
          chunks.size());
      for (ContentSHA c : chunks) {
        bases.add(c.serialize());
      }
      ContentSHA result = ContentSHA.forDigestAndLength(completeSHA
          .digest(), completeLength);
      long existing = contentCollection.count(new BasicDBObject(_ID,
          result.getSHA()));
      if (existing == 0) {
        contentCollection
            .insert(new BasicDBObject(_ID, result.getSHA()).append(
                "store", "cat").append("base", bases),
                WriteConcern.SAFE);
      }
      return result;
    } catch (NoSuchAlgorithmException e) {
View Full Code Here

  }

  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)
View Full Code Here

TOP

Related Classes of v7db.files.spi.ContentSHA

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.