Skip to content
This repository was archived by the owner on Mar 11, 2022. It is now read-only.

Commit 2294284

Browse files
committed
Multiview request changes to support Couch3
POST directly to the view was deprecated in CouchDB 2.2 and is now removed in Couch3. Try POST to /queries first and if that fails, fallback to POST to the view.
1 parent 5320326 commit 2294284

2 files changed

Lines changed: 30 additions & 6 deletions

File tree

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Unreleased
22
- [NEW] Add getter for total row count to AllDocsResponse
3+
- [FIXED] `ViewMultipleRequest` updated to preferentially use view `queries` format URL instead of
4+
deprecated format.
35

46
# 2.18.0 (2019-10-02)
57
- [FIXED] Create an array of strings for QueryBuilder.fields() when a single field is provided.

cloudant-client/src/main/java/com/cloudant/client/internal/views/ViewMultipleRequester.java

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2015, 2018 IBM Corp. All rights reserved.
2+
* Copyright © 2015, 2020 IBM Corp. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
55
* except in compliance with the License. You may obtain a copy of the License at
@@ -16,12 +16,15 @@
1616

1717
import com.cloudant.client.api.views.ViewMultipleRequest;
1818
import com.cloudant.client.api.views.ViewResponse;
19+
import com.cloudant.client.org.lightcouch.CouchDbException;
1920
import com.cloudant.http.Http;
2021
import com.cloudant.http.HttpConnection;
2122
import com.google.gson.JsonArray;
2223
import com.google.gson.JsonObject;
2324

2425
import java.io.IOException;
26+
import java.net.HttpURLConnection;
27+
import java.net.URI;
2528
import java.util.ArrayList;
2629
import java.util.Collections;
2730
import java.util.List;
@@ -43,12 +46,22 @@ public List<ViewResponse<K, V>> getViewResponses() throws IOException {
4346
}
4447
JsonObject queryJson = new JsonObject();
4548
queryJson.add("queries", queries);
49+
String requestBody = queryJson.toString();
4650
//construct and execute the POST request
47-
HttpConnection post = Http.POST(viewQueryParameters.getViewURIBuilder().build(),
48-
"application/json");
49-
post.setRequestBody(queryJson.toString());
50-
JsonObject jsonResponse = ViewRequester.executeRequestWithResponseAsJson
51-
(viewQueryParameters, post);
51+
URI multiRequestUri = viewQueryParameters.getViewURIBuilder().path("queries").build();
52+
JsonObject jsonResponse = null;
53+
try {
54+
jsonResponse = performMultiRequest(multiRequestUri, viewQueryParameters, requestBody);
55+
} catch (CouchDbException e) {
56+
if (e.getStatusCode() == HttpURLConnection.HTTP_INTERNAL_ERROR && "badmatch".equals(e.getError())) {
57+
// CouchDB versions prior to 2.2 don't support .../_view/{viewname}/queries
58+
// POST directly to view instead
59+
multiRequestUri = viewQueryParameters.getViewURIBuilder().build();
60+
jsonResponse = performMultiRequest(multiRequestUri, viewQueryParameters, requestBody);
61+
} else {
62+
throw e;
63+
}
64+
}
5265
//loop the returned array creating the ViewResponse objects
5366
List<ViewResponse<K, V>> responses = new ArrayList<ViewResponse<K, V>>();
5467
JsonArray jsonResponses = jsonResponse.getAsJsonArray("results");
@@ -68,4 +81,13 @@ public List<ViewResponse<K, V>> getViewResponses() throws IOException {
6881
public void add(ViewQueryParameters<K, V> viewQueryParameters) {
6982
requestParameters.add(viewQueryParameters);
7083
}
84+
85+
private JsonObject performMultiRequest(URI multiRequestUri,
86+
ViewQueryParameters<K, V> viewQueryParameters,
87+
String body) throws CouchDbException, IOException {
88+
//construct and execute the POST request
89+
HttpConnection post = Http.POST(multiRequestUri, "application/json");
90+
post.setRequestBody(body);
91+
return ViewRequester.executeRequestWithResponseAsJson(viewQueryParameters, post);
92+
}
7193
}

0 commit comments

Comments
 (0)