Search in sources :

Example 26 with HttpConnection

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

the class Database method setPermissions.

/**
 * Set permissions for a user/apiKey on this database.
 * <p>
 * Note this method is only applicable to databases that support the
 * <a target="_blank"
 * href="https://console.bluemix.net/docs/services/Cloudant/api/authorization.html#authorization">
 * Cloudant authorization API
 * </a> such as Cloudant DBaaS. For unsupported databases consider using the /db/_security
 * endpoint.
 * </p>
 * <p>Example usage to set read-only access for a new key on the "example" database:</p>
 * <pre>
 * {@code
 * // generate an API key
 * ApiKey key = client.generateApiKey();
 *
 * // get the "example" database
 * Database db = client.database("example", false);
 *
 * // set read-only permissions
 * db.setPermissions(key.getKey(), EnumSet.<Permissions>of(Permissions._reader));
 * }
 * </pre>
 *
 * @param userNameorApikey the user or key to apply permissions to
 * @param permissions      permissions to grant
 * @throws UnsupportedOperationException if called on a database that does not provide the
 *                                       Cloudant authorization API
 * @see CloudantClient#generateApiKey()
 * @see <a
 * href="https://console.bluemix.net/docs/services/Cloudant/api/authorization.html#roles"
 * target="_blank">Roles</a>
 * @see <a
 * href="https://console.bluemix.net/docs/services/Cloudant/api/authorization.html#modifying-permissions"
 * target="_blank">Modifying permissions</a>
 */
public void setPermissions(String userNameorApikey, EnumSet<Permissions> permissions) {
    assertNotEmpty(userNameorApikey, "userNameorApikey");
    assertNotEmpty(permissions, "permissions");
    final JsonArray jsonPermissions = new JsonArray();
    for (Permissions s : permissions) {
        final JsonPrimitive permission = new JsonPrimitive(s.toString());
        jsonPermissions.add(permission);
    }
    // get existing permissions
    JsonObject perms = getPermissionsObject();
    // now set back
    JsonElement elem = perms.get("cloudant");
    if (elem == null) {
        perms.addProperty("_id", "_security");
        elem = new JsonObject();
        perms.add("cloudant", elem);
    }
    elem.getAsJsonObject().add(userNameorApikey, jsonPermissions);
    HttpConnection put = Http.PUT(apiV2DBSecurityURI, "application/json");
    put.setRequestBody(client.getGson().toJson(perms));
    // CouchDbExceptions will be thrown for non-2XX cases
    client.couchDbClient.executeToResponse(put);
}
Also used : JsonArray(com.google.gson.JsonArray) JsonPrimitive(com.google.gson.JsonPrimitive) HttpConnection(com.cloudant.http.HttpConnection) JsonElement(com.google.gson.JsonElement) Permissions(com.cloudant.client.api.model.Permissions) JsonObject(com.google.gson.JsonObject)

Example 27 with HttpConnection

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

the class ViewMultipleRequester method getViewResponses.

public List<ViewResponse<K, V>> getViewResponses() throws IOException {
    // build the queries array of data to POST
    JsonArray queries = new JsonArray();
    ViewQueryParameters<K, V> viewQueryParameters = null;
    for (ViewQueryParameters<K, V> params : requestParameters) {
        if (viewQueryParameters == null) {
            viewQueryParameters = params;
        }
        queries.add(params.asJson());
    }
    JsonObject queryJson = new JsonObject();
    queryJson.add("queries", queries);
    // 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);
    // loop the returned array creating the ViewResponse objects
    List<ViewResponse<K, V>> responses = new ArrayList<ViewResponse<K, V>>();
    JsonArray jsonResponses = jsonResponse.getAsJsonArray("results");
    if (jsonResponses != null) {
        int index = 0;
        for (ViewQueryParameters<K, V> params : requestParameters) {
            JsonObject response = jsonResponses.get(index).getAsJsonObject();
            responses.add(new ViewResponseImpl<K, V>(params, response));
            index++;
        }
        return responses;
    } else {
        return Collections.emptyList();
    }
}
Also used : JsonArray(com.google.gson.JsonArray) ViewResponse(com.cloudant.client.api.views.ViewResponse) HttpConnection(com.cloudant.http.HttpConnection) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject)

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