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);
}
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();
}
}
Aggregations