Skip to content
This repository was archived by the owner on Mar 11, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Unreleased
- [NEW] Add getter for total row count to AllDocsResponse
- [FIXED] `ViewMultipleRequest` updated to preferentially use view `queries` format URL instead of
deprecated format.

# 2.18.0 (2019-10-02)
- [FIXED] Create an array of strings for QueryBuilder.fields() when a single field is provided.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2015, 2018 IBM Corp. All rights reserved.
* Copyright © 2015, 2020 IBM Corp. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
Expand All @@ -16,12 +16,15 @@

import com.cloudant.client.api.views.ViewMultipleRequest;
import com.cloudant.client.api.views.ViewResponse;
import com.cloudant.client.org.lightcouch.CouchDbException;
import com.cloudant.http.Http;
import com.cloudant.http.HttpConnection;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -43,12 +46,22 @@ public List<ViewResponse<K, V>> getViewResponses() throws IOException {
}
JsonObject queryJson = new JsonObject();
queryJson.add("queries", queries);
String requestBody = queryJson.toString();
//construct and execute the POST request
HttpConnection post = Http.POST(viewQueryParameters.getViewURIBuilder().build(),
"application/json");
post.setRequestBody(queryJson.toString());
JsonObject jsonResponse = ViewRequester.executeRequestWithResponseAsJson
(viewQueryParameters, post);
URI multiRequestUri = viewQueryParameters.getViewURIBuilder().path("queries").build();
JsonObject jsonResponse = null;
try {
jsonResponse = performMultiRequest(multiRequestUri, viewQueryParameters, requestBody);
} catch (CouchDbException e) {
if (e.getStatusCode() == HttpURLConnection.HTTP_INTERNAL_ERROR && "badmatch".equals(e.getError())) {
// CouchDB versions prior to 2.2 don't support .../_view/{viewname}/queries
// POST directly to view instead
multiRequestUri = viewQueryParameters.getViewURIBuilder().build();
jsonResponse = performMultiRequest(multiRequestUri, viewQueryParameters, requestBody);
} else {
throw e;
}
}
//loop the returned array creating the ViewResponse objects
List<ViewResponse<K, V>> responses = new ArrayList<ViewResponse<K, V>>();
JsonArray jsonResponses = jsonResponse.getAsJsonArray("results");
Expand All @@ -68,4 +81,13 @@ public List<ViewResponse<K, V>> getViewResponses() throws IOException {
public void add(ViewQueryParameters<K, V> viewQueryParameters) {
requestParameters.add(viewQueryParameters);
}

private JsonObject performMultiRequest(URI multiRequestUri,
ViewQueryParameters<K, V> viewQueryParameters,
String body) throws CouchDbException, IOException {
//construct and execute the POST request
HttpConnection post = Http.POST(multiRequestUri, "application/json");
post.setRequestBody(body);
return ViewRequester.executeRequestWithResponseAsJson(viewQueryParameters, post);
}
}