11/*
2- * Copyright © 2015, 2017 IBM Corp. All rights reserved.
2+ * Copyright © 2015, 2018 IBM Corp. All rights reserved.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
55 * except in compliance with the License. You may obtain a copy of the License at
@@ -180,12 +180,8 @@ public class ClientBuilder {
180180 */
181181 public static ClientBuilder account (String account ) {
182182 logger .config ("Account: " + account );
183- try {
184- URL url = new URL (String .format ("https://%s.cloudant.com" , account ));
185- return ClientBuilder .url (url );
186- } catch (MalformedURLException e ) {
187- throw new IllegalArgumentException ("Could not generate url from account name." , e );
188- }
183+ return ClientBuilder .url (
184+ convertStringToURL (String .format ("https://%s.cloudant.com" , account )));
189185 }
190186
191187 /**
@@ -229,17 +225,13 @@ private ClientBuilder(URL url) {
229225 String urlPath = url .getPath ().trim ();
230226 urlPath = urlPath .endsWith ("/" ) ? urlPath .substring (0 , urlPath .length () - 1 ) : urlPath ;
231227
232- try {
233- // Reconstruct URL without user credentials
234- this .url = new URL (urlProtocol
235- + "://"
236- + urlHost
237- + ":"
238- + urlPort
239- + urlPath );
240- } catch (MalformedURLException e ) {
241- throw new RuntimeException (e );
242- }
228+ // Reconstruct URL without user credentials
229+ this .url = convertStringToURL (urlProtocol
230+ + "://"
231+ + urlHost
232+ + ":"
233+ + urlPort
234+ + urlPath );
243235 }
244236
245237
@@ -748,11 +740,7 @@ public static ClientBuilder bluemix(String vcapServices, String serviceName, Str
748740 if (instanceName == null ) {
749741 if (cloudantServices .size () == 1 ) {
750742 CloudFoundryService cloudantService = cloudantServices .get (0 );
751- if (cloudantService .credentials == null || cloudantService .credentials .url == null ) {
752- throw new IllegalArgumentException (
753- "The Cloudant service instance information was invalid." );
754- }
755- return ClientBuilder .url (cloudantService .credentials .url );
743+ return vcapClientBuilder (cloudantService );
756744 } else {
757745 throw new IllegalArgumentException ("Multiple Cloudant service instances present. " +
758746 "A service instance name must be specified." );
@@ -761,11 +749,7 @@ public static ClientBuilder bluemix(String vcapServices, String serviceName, Str
761749
762750 for (CloudFoundryService cloudantService : cloudantServices ) {
763751 if (instanceName .equals (cloudantService .name )) {
764- if (cloudantService .credentials == null || cloudantService .credentials .url == null ) {
765- throw new IllegalArgumentException (
766- "The Cloudant service instance information was invalid." );
767- }
768- return ClientBuilder .url (cloudantService .credentials .url );
752+ return vcapClientBuilder (cloudantService );
769753 }
770754 }
771755
@@ -774,6 +758,30 @@ public static ClientBuilder bluemix(String vcapServices, String serviceName, Str
774758 );
775759 }
776760
761+ private static ClientBuilder vcapClientBuilder (CloudFoundryService cloudantService ) {
762+ // Create client with IAM if the API key exists
763+ if (cloudantService .credentials != null
764+ && cloudantService .credentials .host != null ) {
765+ URL vcapUrl = convertStringToURL (
766+ String .format ("https://%s" , cloudantService .credentials .host ));
767+ ClientBuilder clientBuilder = ClientBuilder .url (vcapUrl );
768+ if (cloudantService .credentials .apikey != null ) {
769+ return clientBuilder .iamApiKey (cloudantService .credentials .apikey );
770+ } else if (cloudantService .credentials .username != null
771+ && cloudantService .credentials .password != null ) {
772+ return clientBuilder .username (cloudantService .credentials .username )
773+ .password (cloudantService .credentials .password );
774+ } else {
775+ throw new IllegalArgumentException (
776+ "The Cloudant service instance is missing the IAM API key, " +
777+ "or both the username and password credential properties." );
778+ }
779+ } else {
780+ throw new IllegalArgumentException (
781+ "The Cloudant service instance information was invalid." );
782+ }
783+ }
784+
777785 /**
778786 * Sets the
779787 * <a href="https://console.bluemix.net/docs/services/Cloudant/guides/iam.html#ibm-cloud-identity-and-access-management"
@@ -787,4 +795,11 @@ public ClientBuilder iamApiKey(String iamApiKey) {
787795 return this ;
788796 }
789797
798+ private static URL convertStringToURL (String urlAsString ) {
799+ try {
800+ return new URL (urlAsString );
801+ } catch (MalformedURLException e ) {
802+ throw new RuntimeException (e );
803+ }
804+ }
790805}
0 commit comments