Skip to content

Commit 4cc3fc8

Browse files
committed
fix #758 - properly get the format for each part from multipart responses
(cherry picked from commit 4797f7c)
1 parent f780290 commit 4cc3fc8

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

src/main/java/com/marklogic/client/impl/OkHttpServices.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,23 +1725,19 @@ private Format getHeaderFormat(Headers headers) {
17251725
}
17261726

17271727
private Format getHeaderFormat(BodyPart part) {
1728-
try {
1729-
String contentDisposition = part.getDisposition();
1730-
String formatRegex = " format=(text|binary|xml|json)";
1731-
String format = getHeader(part, HEADER_VND_MARKLOGIC_DOCUMENT_FORMAT);
1732-
String contentType = getHeader(part, HEADER_CONTENT_TYPE);
1733-
if ( format != null && format.length() > 0 ) {
1734-
return Format.valueOf(format.toUpperCase());
1735-
} else if ( contentDisposition != null && contentDisposition.matches(formatRegex) ) {
1736-
format = contentDisposition.replaceFirst("^.*" + formatRegex + ".*$", "$1");
1737-
return Format.valueOf(format.toUpperCase());
1738-
} else if ( contentType != null && contentType.length() > 0 ) {
1739-
return Format.getFromMimetype(contentType);
1740-
}
1741-
return null;
1742-
} catch (MessagingException e) {
1743-
throw new MarkLogicIOException(e);
1728+
String contentDisposition = getHeader(part, HEADER_CONTENT_DISPOSITION);
1729+
String formatRegex = ".* format=(text|binary|xml|json).*";
1730+
String format = getHeader(part, HEADER_VND_MARKLOGIC_DOCUMENT_FORMAT);
1731+
String contentType = getHeader(part, HEADER_CONTENT_TYPE);
1732+
if ( format != null && format.length() > 0 ) {
1733+
return Format.valueOf(format.toUpperCase());
1734+
} else if ( contentDisposition != null && contentDisposition.matches(formatRegex) ) {
1735+
format = contentDisposition.replaceFirst("^.*" + formatRegex + ".*$", "$1");
1736+
return Format.valueOf(format.toUpperCase());
1737+
} else if ( contentType != null && contentType.length() > 0 ) {
1738+
return Format.getFromMimetype(contentType);
17441739
}
1740+
return null;
17451741
}
17461742

17471743
private void updateMimetype(ContentDescriptor descriptor,

src/test/java/com/marklogic/client/test/BinaryDocumentTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717

1818
import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo;
1919
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertNotNull;
2021
import static org.junit.Assert.assertTrue;
2122

23+
import java.io.File;
2224
import java.io.IOException;
25+
import java.util.Random;
2326

2427
import javax.xml.bind.DatatypeConverter;
2528

@@ -30,11 +33,16 @@
3033
import org.w3c.dom.Document;
3134

3235
import com.marklogic.client.document.DocumentDescriptor;
36+
import com.marklogic.client.document.DocumentPage;
37+
import com.marklogic.client.document.DocumentRecord;
38+
import com.marklogic.client.document.DocumentWriteSet;
3339
import com.marklogic.client.document.DocumentManager.Metadata;
3440
import com.marklogic.client.document.BinaryDocumentManager;
3541
import com.marklogic.client.document.BinaryDocumentManager.MetadataExtraction;
3642
import com.marklogic.client.io.BytesHandle;
3743
import com.marklogic.client.io.DOMHandle;
44+
import com.marklogic.client.io.FileHandle;
45+
import com.marklogic.client.io.Format;
3846
import com.marklogic.client.io.InputStreamHandle;
3947

4048
public class BinaryDocumentTest {
@@ -84,4 +92,19 @@ public void testReadWrite() throws IOException, XpathException {
8492
assertXpathEvaluatesTo("text HD-HTML","string(/*[local-name()='metadata']/*[local-name()='properties']/*[local-name()='filter-capabilities'])", metadataDocument);
8593
assertXpathEvaluatesTo("815","string(/*[local-name()='metadata']/*[local-name()='properties']/*[local-name()='size'])", metadataDocument);
8694
}
95+
96+
@Test
97+
public void test_issue_758() throws Exception {
98+
BinaryDocumentManager docMgr = Common.client.newBinaryDocumentManager();
99+
DocumentWriteSet writeset =docMgr.newWriteSet();
100+
FileHandle h1 = new FileHandle(new File(
101+
"test-complete/src/test/java/com/marklogic/client/functionaltest/data/Sega-4MB.jpg"));
102+
String uri = "BinaryDocumentTest_" + new Random().nextInt(10000) + "/" + "Sega-4MB.jpg";
103+
writeset.add(uri, h1);
104+
docMgr.write(writeset);
105+
DocumentPage page = docMgr.read(uri);
106+
DocumentRecord rec = page.next();
107+
assertNotNull("DocumentRecord should never be null", rec);
108+
assertEquals(rec.getFormat(),Format.BINARY);
109+
}
87110
}

0 commit comments

Comments
 (0)