Permalink
Browse files

Merge pull request #89 from dlvenable/attachments-in-order-parser

AttachmentsInOrderParser for MIME multipart/related
  • Loading branch information...
2 parents e00730b + c8a4016 commit 1deb4eb362ff09efccf9f9a7f6d5ba463726f662 @helun committed Sep 3, 2012
@@ -0,0 +1,89 @@
+package org.ektorp.support;
+
+import org.codehaus.jackson.JsonParseException;
+import org.codehaus.jackson.JsonParser;
+import org.codehaus.jackson.JsonToken;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * A document's MIME multipart/related representation produced by CouchDB
+ * uses the order of the attachments in the JSON _attachments object as the
+ * order of the attachments in the multipart/related. Thus, the order must
+ * be preserved in order to parse a CouchDB multipart/related message.
+ *
+ * This class parses a document and returns the order of the attachments.
+ *
+ * @author David Venable
+ */
+public class AttachmentsInOrderParser
+{
+ /**
+ * Parses a CouchDB document in the form of a JsonParser to get the
+ * attachments order. It is important that the JsonParser come straight
+ * from the source document and not from an object, or the order will
+ * be incorrect.
+ * @param documentJsonParser a JsonParser which is at the very root of a JSON CouchDB document
+ * @return the list of attachment names in the order provided in the document
+ * @throws IOException
+ */
+ public static List<String> parseAttachmentNames(JsonParser documentJsonParser) throws IOException
+ {
+ documentJsonParser.nextToken();
+
+ JsonToken jsonToken;
+ while((jsonToken = documentJsonParser.nextToken()) != JsonToken.END_OBJECT)
+ {
+ if(CouchDbDocument.ATTACHMENTS_NAME.equals(documentJsonParser.getCurrentName()))
+ {
+ return readAttachments(documentJsonParser);
+ }
+ else if(jsonToken == JsonToken.START_OBJECT)
+ {
+ readIgnoreObject(documentJsonParser);
+ }
+ }
+ return null;
+ }
+
+ private static List<String> readAttachments(JsonParser jsonParser) throws IOException
+ {
+ jsonParser.nextToken();
+ return readAttachmentsObject(jsonParser);
+ }
+
+ private static List<String> readAttachmentsObject(JsonParser jsonParser) throws IOException
+ {
+ List<String> attachmentNameList = new ArrayList<String>();
+ while(jsonParser.nextToken() != JsonToken.END_OBJECT)
+ {
+ String attachmentName = jsonParser.getCurrentName();
+
+ jsonParser.nextToken();
+ if(jsonParser.getCurrentToken() != JsonToken.START_OBJECT)
+ {
+ String message = CouchDbDocument.ATTACHMENTS_NAME + " contains an invalid object.";
+ throw new JsonParseException(message, jsonParser.getCurrentLocation());
+ }
+
+ readIgnoreObject(jsonParser);
+
+ attachmentNameList.add(attachmentName);
+ }
+
+ return attachmentNameList;
+ }
+
+ private static void readIgnoreObject(JsonParser jsonParser) throws IOException
+ {
+ while(jsonParser.nextToken() != JsonToken.END_OBJECT)
+ {
+ if(jsonParser.getCurrentToken() == JsonToken.START_OBJECT)
+ {
+ readIgnoreObject(jsonParser);
+ }
+ }
+ }
+}
@@ -17,6 +17,8 @@
@JsonSerialize(include = Inclusion.NON_NULL)
public class CouchDbDocument implements Serializable {
+ public static final String ATTACHMENTS_NAME = "_attachments";
+
private static final long serialVersionUID = 1L;
private String id;
private String revision;
@@ -60,12 +62,12 @@ public boolean isNew() {
return revision == null;
}
- @JsonProperty("_attachments")
+ @JsonProperty(ATTACHMENTS_NAME)
public Map<String, Attachment> getAttachments() {
return attachments;
}
- @JsonProperty("_attachments")
+ @JsonProperty(ATTACHMENTS_NAME)
void setAttachments(Map<String, Attachment> attachments) {
this.attachments = attachments;
}
@@ -0,0 +1,54 @@
+package org.ektorp.support;
+
+import org.codehaus.jackson.JsonFactory;
+import org.codehaus.jackson.JsonParser;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.junit.Assert.assertThat;
+
+public class AttachmentsInOrderParserTest
+{
+ private static final String[] KNOWN_ORDER = {
+ "washington.html",
+ "idaho.html",
+ "wyoming.html",
+ "colorado.html",
+ "kansas.html",
+ "missouri.html",
+ "tennessee.html",
+ "georgia.html",
+ "florida.html"
+ };
+
+ private JsonParser attachmentsJsonParser;
+ private List<String> knownOrderList;
+
+ @Before
+ public void setUp() throws IOException
+ {
+ InputStream attachmentsInputStream = getClass().getResourceAsStream("attachments.json");
+
+ JsonFactory jsonFactory = new JsonFactory();
+ attachmentsJsonParser = jsonFactory.createJsonParser(attachmentsInputStream);
+
+ knownOrderList = Arrays.asList(KNOWN_ORDER);
+ }
+
+ @Test
+ public void parseAttachmentNames_should_return_the_attachment_names_in_order() throws IOException
+ {
+ List<String> attachmentNames = AttachmentsInOrderParser.parseAttachmentNames(attachmentsJsonParser);
+
+ assertThat(attachmentNames, notNullValue());
+
+ assertThat(attachmentNames, is(knownOrderList));
+ }
+}
@@ -0,0 +1,96 @@
+{
+ "_id": "attachments-test-document",
+ "_attachments": {
+ "washington.html": {
+ "content_type": "text/plain; charset=us-ascii",
+ "digest": "md5-l8BzdkgEM74VsU/w0WBd0w==",
+ "encoded_length": 278,
+ "encoding": "gzip",
+ "follows": true,
+ "length": 502,
+ "revpos": 1
+ },
+ "idaho.html": {
+ "content_type": "text/html",
+ "digest": "md5-1O21C+Rq+uG/KCuV2LMwtg==",
+ "encoded_length": 427,
+ "encoding": "gzip",
+ "follows": true,
+ "length": 709,
+ "revpos": 4
+ },
+ "wyoming.html": {
+ "content_type": "text/html",
+ "digest": "md5-vDYgGS6SsqxsYjMSSLdDlA==",
+ "encoded_length": 1203,
+ "encoding": "gzip",
+ "follows": true,
+ "length": 2712,
+ "revpos": 2
+ },
+ "colorado.html": {
+ "content_type": "text/html",
+ "digest": "md5-8nSQ7dUFYecARBmfl93YDQ==",
+ "encoded_length": 510,
+ "encoding": "gzip",
+ "follows": true,
+ "length": 998,
+ "revpos": 2
+ },
+ "kansas.html": {
+ "content_type": "text/html",
+ "digest": "md5-cxvFZTxFeOEB7CwyKh51mQ==",
+ "encoded_length": 571,
+ "encoding": "gzip",
+ "follows": true,
+ "length": 1182,
+ "revpos": 1
+ },
+ "missouri.html": {
+ "content_type": "text/html",
+ "digest": "md5-W1CMoe1X7PdhZ07lmjAVLg==",
+ "encoded_length": 762,
+ "encoding": "gzip",
+ "follows": true,
+ "length": 1567,
+ "revpos": 1
+ },
+ "tennessee.html": {
+ "content_type": "text/html",
+ "digest": "md5-q9Tca/ZHK9kAmA8a8hRE6Q==",
+ "encoded_length": 770,
+ "encoding": "gzip",
+ "follows": true,
+ "length": 1719,
+ "revpos": 4
+ },
+ "georgia.html": {
+ "content_type": "text/html",
+ "digest": "md5-DUzAxXFzzn8BHJzdX4Jyow==",
+ "encoded_length": 1174,
+ "encoding": "gzip",
+ "follows": true,
+ "length": 2449,
+ "revpos": 3
+ },
+ "florida.html": {
+ "content_type": "text/html",
+ "digest": "md5-Dcmv3Ss5LesAs4CN/g4jjg==",
+ "encoded_length": 391,
+ "encoding": "gzip",
+ "follows": true,
+ "length": 684,
+ "revpos": 2
+ }
+ },
+ "_rev": "4-d8698ded32802dbb59312b25e575cb20",
+ "_revisions": {
+ "ids": [
+ "d8698ded32802dbb59312b25e575cb20",
+ "f162e23593a409ac7d0795150c873bf0",
+ "800c2f50e757679531a749e432848366",
+ "85a0f26dd1f2b8c66757678c6266c51e"
+ ],
+ "start": 4
+ }
+}

0 comments on commit 1deb4eb

Please sign in to comment.