Search in sources :

Example 1 with CloudFoundryService

use of com.cloudant.client.internal.util.CloudFoundryService in project java-cloudant by cloudant.

the class ClientBuilder method bluemix.

/**
 * Sets Cloudant client credentials by inspecting a service information JSON string. This object
 * takes the form of a {@code VCAP_SERVICES} environment variable which is a JSON object that
 * contains information that you can use to interact with a service instance in Bluemix.
 *
 * @param vcapServices service information JSON string, for example the contents of
 *                     {@code VCAP_SERVICES} environment variable
 * @param serviceName name of Bluemix service to use
 * @param instanceName name of Bluemix service instance or {@code null} to try to use the only
 *                    available Cloudant service
 * @return a new ClientBuilder for the account
 *
 * @throws IllegalArgumentException if any of the following conditions are true:
 * <ul>
 *     <li>The {@code vcapServices} is {@code null}.</li>
 *     <li>The {@code serviceName} is {@code null}.</li>
 *     <li>The {@code vcapServices} is not valid.</li>
 *     <li>A service with the name matching {@code serviceName} could not be found in
 *     {@code vcapServices}.</li>
 *     <li>An instance with a name matching {@code instanceName} could not be found in
 *     {@code vcapServices}.</li>
 *     <li>The {@code instanceName} is {@code null} and multiple Cloudant service instances
 *     exist in {@code vcapServices}.</li>
 * </ul>
 */
public static ClientBuilder bluemix(String vcapServices, String serviceName, String instanceName) {
    if (vcapServices == null) {
        throw new IllegalArgumentException("The vcapServices JSON information was null.");
    }
    if (serviceName == null) {
        throw new IllegalArgumentException("No Cloudant services information present.");
    }
    JsonObject obj;
    try {
        obj = (JsonObject) new JsonParser().parse(vcapServices);
    } catch (JsonParseException e) {
        throw new IllegalArgumentException("The vcapServices was not valid JSON.", e);
    }
    Gson gson = new GsonBuilder().create();
    List<CloudFoundryService> cloudantServices = null;
    JsonElement service = obj.get(serviceName);
    if (service != null) {
        Type listType = new TypeToken<ArrayList<CloudFoundryService>>() {
        }.getType();
        cloudantServices = gson.fromJson(service, listType);
    }
    if (cloudantServices == null || cloudantServices.size() == 0) {
        throw new IllegalArgumentException("No Cloudant services information present.");
    }
    if (instanceName == null) {
        if (cloudantServices.size() == 1) {
            CloudFoundryService cloudantService = cloudantServices.get(0);
            if (cloudantService.credentials == null || cloudantService.credentials.url == null) {
                throw new IllegalArgumentException("The Cloudant service instance information was invalid.");
            }
            return ClientBuilder.url(cloudantService.credentials.url);
        } else {
            throw new IllegalArgumentException("Multiple Cloudant service instances present. " + "A service instance name must be specified.");
        }
    }
    for (CloudFoundryService cloudantService : cloudantServices) {
        if (instanceName.equals(cloudantService.name)) {
            if (cloudantService.credentials == null || cloudantService.credentials.url == null) {
                throw new IllegalArgumentException("The Cloudant service instance information was invalid.");
            }
            return ClientBuilder.url(cloudantService.credentials.url);
        }
    }
    throw new IllegalArgumentException(String.format("Cloudant service instance matching name %s was not found.", instanceName));
}
Also used : Type(java.lang.reflect.Type) GsonBuilder(com.google.gson.GsonBuilder) JsonElement(com.google.gson.JsonElement) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) CloudFoundryService(com.cloudant.client.internal.util.CloudFoundryService) JsonParseException(com.google.gson.JsonParseException) JsonParser(com.google.gson.JsonParser)

Aggregations

CloudFoundryService (com.cloudant.client.internal.util.CloudFoundryService)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonParseException (com.google.gson.JsonParseException)1 JsonParser (com.google.gson.JsonParser)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1