Search in sources :

Example 11 with HttpConnection

use of com.cloudant.http.HttpConnection in project java-cloudant by cloudant.

the class CouchDbClient method get.

/**
 * Performs a HTTP GET request.
 *
 * @return Class type of object T (i.e. {@link Response}
 */
public <T> T get(URI uri, Class<T> classType) {
    HttpConnection connection = Http.GET(uri);
    InputStream response = executeToInputStream(connection);
    try {
        return getResponse(response, classType, getGson());
    } finally {
        close(response);
    }
}
Also used : HttpConnection(com.cloudant.http.HttpConnection) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream)

Example 12 with HttpConnection

use of com.cloudant.http.HttpConnection in project java-cloudant by cloudant.

the class Utils method removeReplicatorTestDoc.

public static void removeReplicatorTestDoc(CloudantClient account, String replicatorDocId) throws Exception {
    // Grab replicator doc revision using HTTP HEAD command
    String replicatorDb = "_replicator";
    URI uri = new URIBase(account.getBaseUri()).path(replicatorDb).path(replicatorDocId).build();
    HttpConnection head = Http.HEAD(uri);
    // add a response interceptor to allow us to retrieve the ETag revision header
    final AtomicReference<String> revisionRef = new AtomicReference<String>();
    head.responseInterceptors.add(new HttpConnectionResponseInterceptor() {

        @Override
        public HttpConnectionInterceptorContext interceptResponse(HttpConnectionInterceptorContext context) {
            revisionRef.set(context.connection.getConnection().getHeaderField("ETag"));
            return context;
        }
    });
    account.executeRequest(head);
    String revision = revisionRef.get();
    assertNotNull("The revision should not be null", revision);
    Database replicator = account.database(replicatorDb, false);
    Response removeResponse = replicator.remove(replicatorDocId, revision.replaceAll("\"", ""));
    assertThat(removeResponse.getError(), is(nullValue()));
}
Also used : Response(com.cloudant.client.api.model.Response) HttpConnection(com.cloudant.http.HttpConnection) Database(com.cloudant.client.api.Database) AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpConnectionResponseInterceptor(com.cloudant.http.HttpConnectionResponseInterceptor) HttpConnectionInterceptorContext(com.cloudant.http.HttpConnectionInterceptorContext) URI(java.net.URI) URIBase(com.cloudant.client.internal.URIBase)

Example 13 with HttpConnection

use of com.cloudant.http.HttpConnection in project java-cloudant by cloudant.

the class Database method deleteIndex.

/**
 * Delete an index with the specified name and type in the given design document.
 *
 * @param indexName   name of the index
 * @param designDocId ID of the design doc (the _design prefix will be added if not present)
 * @param type        type of the index, valid values or "text" or "json"
 */
public void deleteIndex(String indexName, String designDocId, String type) {
    assertNotEmpty(indexName, "indexName");
    assertNotEmpty(designDocId, "designDocId");
    assertNotNull(type, "type");
    if (!designDocId.startsWith("_design")) {
        designDocId = "_design/" + designDocId;
    }
    URI uri = new DatabaseURIHelper(db.getDBUri()).path("_index").path(designDocId).path(type).path(indexName).build();
    InputStream response = null;
    try {
        HttpConnection connection = Http.DELETE(uri);
        response = client.couchDbClient.executeToInputStream(connection);
        getResponse(response, Response.class, client.getGson());
    } finally {
        close(response);
    }
}
Also used : HttpConnection(com.cloudant.http.HttpConnection) InputStream(java.io.InputStream) DatabaseURIHelper(com.cloudant.client.internal.DatabaseURIHelper) URI(java.net.URI)

Example 14 with HttpConnection

use of com.cloudant.http.HttpConnection in project java-cloudant by cloudant.

the class Search method queryForStream.

// Query options
/**
 * Performs a Cloudant Search and returns the result as an {@link InputStream}
 *
 * @param query the Lucene query to be passed to the Search index
 *              <p>The stream should be properly closed after usage, as to avoid connection
 *              leaks.
 * @return The result as an {@link InputStream}.
 */
public InputStream queryForStream(String query) {
    key(query);
    URI uri = databaseHelper.build();
    HttpConnection get = Http.GET(uri);
    get.requestProperties.put("Accept", "application/json");
    return client.couchDbClient.executeToInputStream(get);
}
Also used : HttpConnection(com.cloudant.http.HttpConnection) URI(java.net.URI)

Example 15 with HttpConnection

use of com.cloudant.http.HttpConnection in project java-cloudant by cloudant.

the class HttpTest method inputStreamRetryString.

@TestTemplate
public void inputStreamRetryString() throws Exception {
    HttpConnection request = Http.POST(mockWebServer.url("/").url(), "application/json");
    String content = "abcde";
    request.setRequestBody(content);
    testInputStreamRetry(request, content.getBytes("UTF-8"));
}
Also used : HttpConnection(com.cloudant.http.HttpConnection) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) TestTemplate(org.junit.jupiter.api.TestTemplate)

Aggregations

HttpConnection (com.cloudant.http.HttpConnection)27 TestTemplate (org.junit.jupiter.api.TestTemplate)11 JsonObject (com.google.gson.JsonObject)6 ByteArrayInputStream (java.io.ByteArrayInputStream)6 InputStream (java.io.InputStream)6 IOException (java.io.IOException)5 URI (java.net.URI)5 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)5 URL (java.net.URL)4 CloudantClient (com.cloudant.client.api.CloudantClient)3 DatabaseURIHelper (com.cloudant.client.internal.DatabaseURIHelper)3 HttpConnectionInterceptorContext (com.cloudant.http.HttpConnectionInterceptorContext)3 RequiresCloudant (com.cloudant.test.main.RequiresCloudant)3 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)3 HttpConnectionResponseInterceptor (com.cloudant.http.HttpConnectionResponseInterceptor)2 Gson (com.google.gson.Gson)2 JsonArray (com.google.gson.JsonArray)2 InputStreamReader (java.io.InputStreamReader)2 MockResponse (okhttp3.mockwebserver.MockResponse)2 Test (org.junit.jupiter.api.Test)2