// Keep in mind we do not keep local document revision history
  private BasicDocumentRevision doGetLocalDocument(String docId, String revId) {
    assert !Strings.isNullOrEmpty(docId);
    Cursor cursor = null;
    try {
      String[] args = {docId};
      cursor = this.sqlDb.rawQuery("SELECT revid, json FROM localdocs WHERE docid=?", args);
      if (cursor.moveToFirst()) {
        String gotRevID = cursor.getString(0);

        if (revId != null && !revId.equals(gotRevID)) {
          //                    throw new DocumentNotFoundException("No local document found with
          // id: " + docId + ", revId: " + revId);
          return null;
        }

        byte[] json = cursor.getBlob(1);

        DocumentRevisionBuilder builder =
            new DocumentRevisionBuilder()
                .setDocId(docId)
                .setRevId(gotRevID)
                .setBody(BasicDocumentBody.bodyWith(json));

        return builder.buildBasicDBObjectLocalDocument();
      } else {
        return null;
      }
    } catch (SQLException e) {
      throw new SQLRuntimeException("Error getting local document with id: " + docId, e);
    } finally {
      DatabaseUtils.closeCursorQuietly(cursor);
    }
  }
Пример #2
0
  static BasicDocumentRevision getFullRevisionFromCurrentCursor(Cursor cursor) {
    String docId = cursor.getString(0);
    long internalId = cursor.getLong(1);
    String revId = cursor.getString(2);
    long sequence = cursor.getLong(3);
    byte[] json = cursor.getBlob(4);
    boolean current = cursor.getInt(5) > 0;
    boolean deleted = cursor.getInt(6) > 0;

    long parent = -1L;
    if (cursor.columnType(7) == Cursor.FIELD_TYPE_INTEGER) {
      parent = cursor.getLong(7);
    } else if (cursor.columnType(7) == Cursor.FIELD_TYPE_NULL) {
    } else {
      throw new IllegalArgumentException("Unexpected type: " + cursor.columnType(7));
    }

    DocumentRevisionBuilder builder =
        new DocumentRevisionBuilder()
            .setDocId(docId)
            .setRevId(revId)
            .setBody(BasicDocumentBody.bodyWith(json))
            .setDeleted(deleted)
            .setSequence(sequence)
            .setInternalId(internalId)
            .setCurrnet(current)
            .setParent(parent);

    return builder.buildBasicDBObject();
  }
  @Test
  public void buildRevisionFromMapValidMapWithAttachments() throws Exception {

    // lets the get the attachment encoded

    File file = TestUtils.loadFixture("fixture/bonsai-boston.jpg");

    byte[] unencodedAttachment = FileUtils.readFileToByteArray(file);
    byte[] encodedAttachment = this.encodeAttachment(unencodedAttachment);

    Map<String, Map<String, Object>> attachments = new HashMap<String, Map<String, Object>>();
    Map<String, Object> bonsai = createAttachmentMap(encodedAttachment, "image/jpeg", false);

    attachments.put("bonsai-boston.jpg", bonsai);
    documentRev.put("_attachments", attachments);

    DocumentRevision revision =
        DocumentRevisionBuilder.buildRevisionFromMap(documentURI, documentRev);

    Assert.assertNotNull(revision);
    Assert.assertEquals(body, revision.getBody().asMap());
    Assert.assertEquals(revision.getId(), "someIdHere");
    Assert.assertEquals(revision.getRevision(), "3-750dac460a6cc41e6999f8943b8e603e");
    Assert.assertEquals(revision.getAttachments().size(), 1);
    ByteArrayInputStream expected = new ByteArrayInputStream(unencodedAttachment);
    InputStream actual = revision.getAttachments().get("bonsai-boston.jpg").getInputStream();
    Assert.assertTrue(TestUtils.streamsEqual(expected, actual));
  }
  @Test
  public void buildRevisionFromMapValidMapWithAttachmentsDataExcluded() throws Exception {

    Assume.assumeFalse(
        "Not running test as 'buildRevisionFromMap' can't retrieve the "
            + "attachment with cookie authentication enabled",
        TestOptions.COOKIE_AUTH);

    // lets the get the attachment encoded

    File file = TestUtils.loadFixture("fixture/bonsai-boston.jpg");

    byte[] unencodedAttachment = FileUtils.readFileToByteArray(file);
    byte[] encodedAttachment = this.encodeAttachment(unencodedAttachment);

    // create a revision on a couchDB instance so we can test the download
    // of attachments
    Map<String, Object> remoteDoc = new HashMap<String, Object>();
    remoteDoc.put("_id", "someIdHere");
    Map<String, Map<String, Object>> remoteAttachments = new HashMap<String, Map<String, Object>>();
    Map<String, Object> remoteBonsai = createAttachmentMap(encodedAttachment, "image/jpeg", false);

    remoteAttachments.put("bonsai-boston.jpg", remoteBonsai);
    remoteDoc.put("_attachments", remoteAttachments);

    remoteDb.create(remoteDoc);

    // build up the test json map

    Map<String, Map<String, Object>> attachments = new HashMap<String, Map<String, Object>>();
    Map<String, Object> bonsai = createAttachmentMap(encodedAttachment, "image/jpeg", true);

    attachments.put("bonsai-boston.jpg", bonsai);
    documentRev.put("_attachments", attachments);

    URI uri = new URI(remoteDb.getCouchClient().getRootUri().toString() + "/" + "someIdHere");

    // create the document revision and test

    DocumentRevision revision = DocumentRevisionBuilder.buildRevisionFromMap(uri, documentRev);

    Assert.assertNotNull(revision);
    Assert.assertEquals(body, revision.getBody().asMap());
    Assert.assertEquals(revision.getId(), "someIdHere");
    Assert.assertEquals(revision.getRevision(), "3-750dac460a6cc41e6999f8943b8e603e");
    Assert.assertEquals(revision.getAttachments().size(), 1);
    InputStream attachmentInputStream =
        revision.getAttachments().get("bonsai-boston.jpg").getInputStream();
    InputStream expectedInputStream = new ByteArrayInputStream(unencodedAttachment);
    Assert.assertTrue(TestUtils.streamsEqual(expectedInputStream, attachmentInputStream));
  }
  @Test
  public void buildRevisionFromMapValidMapWithTextAttachmentsDataExcluded() throws Exception {

    Assume.assumeFalse(
        "Not running test as 'buildRevisionFromMap' can't retrieve the "
            + "attachment with cookie authentication enabled",
        TestOptions.COOKIE_AUTH);

    // lets the get the attachment encoded

    String attachmentText = "Hello World";

    byte[] unencodedAttachment = attachmentText.getBytes();
    byte[] encodedAttachment = this.encodeAttachment(unencodedAttachment);

    // create a revision on a couchDB instance so we can test the download
    // of attachments
    Map<String, Object> remoteDoc = new HashMap<String, Object>();
    remoteDoc.put("_id", "someIdHere");
    Map<String, Map<String, Object>> remoteAttachments = new HashMap<String, Map<String, Object>>();

    remoteAttachments.put(
        "hello.txt", this.createAttachmentMap(encodedAttachment, "text/plain", false));
    remoteDoc.put("_attachments", remoteAttachments);

    remoteDb.create(remoteDoc);
    documentURI = new URI(remoteDb.getCouchClient().getRootUri().toString() + "/" + "someIdHere");

    Map<String, Map<String, Object>> attachments = new HashMap<String, Map<String, Object>>();
    Map<String, Object> bonsai =
        createAttachmentMap(encodedAttachment, "text/plain", true); // new HashMap<String,Object>();
    bonsai.put("encoding", "gzip");

    attachments.put("hello.txt", bonsai);
    documentRev.put("_attachments", attachments);

    // create the document revision and test

    DocumentRevision revision =
        DocumentRevisionBuilder.buildRevisionFromMap(documentURI, documentRev);

    Assert.assertNotNull(revision);
    Assert.assertEquals(body, revision.getBody().asMap());
    Assert.assertEquals(revision.getId(), "someIdHere");
    Assert.assertEquals(revision.getRevision(), "3-750dac460a6cc41e6999f8943b8e603e");
    Assert.assertEquals(revision.getAttachments().size(), 1);
    InputStream attachmentInputStream = revision.getAttachments().get("hello.txt").getInputStream();
    InputStream expectedInputStream = new ByteArrayInputStream(unencodedAttachment);
    Assert.assertTrue(TestUtils.streamsEqual(expectedInputStream, attachmentInputStream));
  }