Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
LightCouch set /db/_security
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.entity.StringEntity;
import org.lightcouch.CouchDbClient;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
CouchDbClient dbClient = new CouchDbClient();
JsonArray adminsNames = new JsonArray();
adminsNames.add("superuser");
JsonArray membersNames = new JsonArray();
membersNames.add("user1");
JsonObject admins = new JsonObject();
admins.add("names", adminsNames);
admins.add("roles", new JsonArray()); // empty roles
JsonObject members = new JsonObject();
members.add("names", membersNames);
members.add("roles", new JsonArray());
JsonObject securityObj = new JsonObject();
securityObj.add("admins", admins);
securityObj.add("members", members);
// or use a plain java object "pojo" to represent the security object
// JsonObject securityObj = dbClient.getGson().toJsonTree(pojo).getAsJsonObject();
StringEntity entity = new StringEntity(securityObj.toString(), "UTF-8");
entity.setContentType("application/json");
String securityUri = dbClient.getDBUri() + "_security";
HttpPut put = new HttpPut(securityUri);
put.setEntity(entity);
HttpResponse response = dbClient.executeRequest(put);
HttpClientUtils.closeQuietly(response);
dbClient.shutdown();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment