Skip to content

Commit ee87ee5

Browse files
authored
Merge pull request #1687 from marklogic/feature/multipart-response-refactor
Added failing test for reading a DocumentPage
2 parents 647b1a4 + 3edc5d5 commit ee87ee5

File tree

4 files changed

+413
-353
lines changed

4 files changed

+413
-353
lines changed

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

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,10 @@ public void close() {
800800
}
801801
}
802802

803-
private OkHttpResultIterator getBulkDocumentsImpl(RequestLogger reqlog, long serverTimestamp,
803+
/**
804+
* Uses v1/documents.
805+
*/
806+
private OkHttpResultIterator getBulkDocumentsImpl(RequestLogger reqlog, long serverTimestamp,
804807
Transaction transaction, Set<Metadata> categories,
805808
Format format, RequestParameters extraParams, boolean withContent,
806809
String... uris)
@@ -831,6 +834,9 @@ private OkHttpResultIterator getBulkDocumentsImpl(RequestLogger reqlog, long ser
831834
return iterator;
832835
}
833836

837+
/**
838+
* Uses v1/search.
839+
*/
834840
private OkHttpResultIterator getBulkDocumentsImpl(RequestLogger reqlog, long serverTimestamp,
835841
SearchQueryDefinition querydef, long start, long pageLength,
836842
Transaction transaction, SearchReadHandle searchHandle, QueryView view,
@@ -4416,45 +4422,46 @@ private <T> T makeResult(RequestLogger reqlog, String operation,
44164422
return (reqlog != null) ? reqlog.copyContent(entity) : entity;
44174423
}
44184424

4419-
private <U extends OkHttpResultIterator> U makeResults(
4420-
ResultIteratorConstructor<U> constructor, RequestLogger reqlog,
4421-
String operation, String entityType, Response response) {
4422-
if ( response == null ) return null;
4423-
ResponseBody body = response.body();
4424-
long length = body.contentLength();
4425-
MimeMultipart entity = length != 0 ?
4426-
getEntity(body, MimeMultipart.class) : null;
4427-
4428-
try {
4429-
if (length == -1 && entity != null) entity.getCount();
4430-
} catch (MessagingException e) {
4431-
entity = null;
4432-
}
4433-
List<BodyPart> partList = getPartList(entity);
4434-
4435-
String mlErrorCode = null;
4436-
String mlErrorMessage = null;
4437-
try {
4438-
Headers trailers = response.trailers();
4439-
mlErrorCode = trailers.get("ml-error-code");
4440-
mlErrorMessage = trailers.get("ml-error-message");
4441-
} catch (IOException e) {
4442-
// This does not seem worthy of causing the entire operation to fail; we also don't expect this to occur, as it
4443-
// should only occur due to a programming error where the response body has already been consumed
4444-
logger.warn("Unexpected IO error while getting HTTP response trailers: " + e.getMessage());
4445-
}
4446-
4447-
if (mlErrorCode != null && !"N/A".equals(mlErrorCode)) {
4448-
FailedRequest failure = new FailedRequest();
4449-
failure.setMessageString(mlErrorCode);
4450-
failure.setStatusString(mlErrorMessage);
4451-
failure.setStatusCode(500);
4452-
throw new FailedRequestException("failed to " + operation + " "
4453-
+ entityType + " at rows" + ": " + mlErrorCode + ", " + mlErrorMessage, failure);
4454-
}
4425+
static private List<BodyPart> readMultipartBodyParts(ResponseBody body) {
4426+
long length = body.contentLength();
4427+
MimeMultipart entity = length != 0 ? getEntity(body, MimeMultipart.class) : null;
4428+
try {
4429+
if (length == -1 && entity != null) entity.getCount();
4430+
} catch (MessagingException e) {
4431+
entity = null;
4432+
}
4433+
return getPartList(entity);
4434+
}
4435+
4436+
private <U extends OkHttpResultIterator> U makeResults(ResultIteratorConstructor<U> constructor, RequestLogger reqlog,
4437+
String operation, String entityType, Response response) {
4438+
if ( response == null ) return null;
4439+
final List<BodyPart> partList = readMultipartBodyParts(response.body());
4440+
throwExceptionIfErrorInTrailers(operation, entityType, response);
4441+
return makeResults(constructor, reqlog, operation, entityType, partList, response, response);
4442+
}
4443+
4444+
static private void throwExceptionIfErrorInTrailers(String operation, String entityType, Response response) {
4445+
String mlErrorCode = null;
4446+
String mlErrorMessage = null;
4447+
try {
4448+
Headers trailers = response.trailers();
4449+
mlErrorCode = trailers.get("ml-error-code");
4450+
mlErrorMessage = trailers.get("ml-error-message");
4451+
} catch (IOException e) {
4452+
// This does not seem worthy of causing the entire operation to fail; we also don't expect this to occur, as it
4453+
// should only occur due to a programming error where the response body has already been consumed
4454+
logger.warn("Unexpected IO error while getting HTTP response trailers: " + e.getMessage());
4455+
}
44554456

4456-
Closeable closeable = response;
4457-
return makeResults(constructor, reqlog, operation, entityType, partList, response, closeable);
4457+
if (mlErrorCode != null && !"N/A".equals(mlErrorCode)) {
4458+
FailedRequest failure = new FailedRequest();
4459+
failure.setMessageString(mlErrorCode);
4460+
failure.setStatusString(mlErrorMessage);
4461+
failure.setStatusCode(500);
4462+
String message = String.format("failed to %s %s at rows: %s, %s", operation, entityType, mlErrorCode, mlErrorMessage);
4463+
throw new FailedRequestException(message, failure);
4464+
}
44584465
}
44594466

44604467
private <U extends OkHttpResultIterator> U makeResults(

0 commit comments

Comments
 (0)