From fbf147968a7d9a8931fb8aeead5ba8e74aeb64b1 Mon Sep 17 00:00:00 2001 From: Nourhan Shata Date: Thu, 16 Oct 2025 16:41:43 +0200 Subject: [PATCH 1/3] Removing WARN logs for ai-sdk --- .../connectivity/DefaultOAuth2PropertySupplier.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/cloudplatform/connectivity-oauth/src/main/java/com/sap/cloud/sdk/cloudplatform/connectivity/DefaultOAuth2PropertySupplier.java b/cloudplatform/connectivity-oauth/src/main/java/com/sap/cloud/sdk/cloudplatform/connectivity/DefaultOAuth2PropertySupplier.java index 2daa1f50c..ec89de5be 100644 --- a/cloudplatform/connectivity-oauth/src/main/java/com/sap/cloud/sdk/cloudplatform/connectivity/DefaultOAuth2PropertySupplier.java +++ b/cloudplatform/connectivity-oauth/src/main/java/com/sap/cloud/sdk/cloudplatform/connectivity/DefaultOAuth2PropertySupplier.java @@ -193,12 +193,7 @@ private ClientIdentity getSecretIdentity( @Nonnull final String clientid ) @Nonnull CredentialType getCredentialType() { - return getOAuthCredential(CredentialType.class, "credential-type") - .onEmpty( - () -> log - .warn( - "Credential type not found or not recognised in service binding. Defaulting to BINDING_SECRET.")) - .getOrElse(CredentialType.BINDING_SECRET); + return getOAuthCredential(CredentialType.class, "credential-type").getOrElse(CredentialType.BINDING_SECRET); } /** From 1e84cbdc6a062cba1bafdf4f249227f45b368f31 Mon Sep 17 00:00:00 2001 From: Nourhan Shata Date: Fri, 24 Oct 2025 14:44:49 +0200 Subject: [PATCH 2/3] Some Formatting --- .../DefaultOAuth2PropertySupplier.java | 195 +++++++----------- 1 file changed, 77 insertions(+), 118 deletions(-) diff --git a/cloudplatform/connectivity-oauth/src/main/java/com/sap/cloud/sdk/cloudplatform/connectivity/DefaultOAuth2PropertySupplier.java b/cloudplatform/connectivity-oauth/src/main/java/com/sap/cloud/sdk/cloudplatform/connectivity/DefaultOAuth2PropertySupplier.java index ec89de5be..c7171fb30 100644 --- a/cloudplatform/connectivity-oauth/src/main/java/com/sap/cloud/sdk/cloudplatform/connectivity/DefaultOAuth2PropertySupplier.java +++ b/cloudplatform/connectivity-oauth/src/main/java/com/sap/cloud/sdk/cloudplatform/connectivity/DefaultOAuth2PropertySupplier.java @@ -33,8 +33,7 @@ * @since 4.20.0 */ @Slf4j -public class DefaultOAuth2PropertySupplier implements OAuth2PropertySupplier -{ +public class DefaultOAuth2PropertySupplier implements OAuth2PropertySupplier { /** * By default, the oauth client details are expected to be contained in a {@code uaa} block of the * {@code credentials} of the service binding. @@ -59,51 +58,43 @@ public class DefaultOAuth2PropertySupplier implements OAuth2PropertySupplier * Create a new instance to parse the given service binding. Will use {@link #DEFAULT_UAA_CREDENTIAL_PATH} as the * default path to oauth properties. * - * @param options - * The options containing the service binding to interpret. + * @param options The options containing the service binding to interpret. * @see #DefaultOAuth2PropertySupplier(ServiceBindingDestinationOptions, List) */ - public DefaultOAuth2PropertySupplier( @Nonnull final ServiceBindingDestinationOptions options ) - { + public DefaultOAuth2PropertySupplier(@Nonnull final ServiceBindingDestinationOptions options) { this(options, DEFAULT_UAA_CREDENTIAL_PATH); } /** * Create a new instance to parse the given service binding. Will use the given list as the path to url property. * - * @param options - * The options containing the service binding to interpret. - * @param oauthPropertyPath - * The path to the oauth properties in the service binding. + * @param options The options containing the service binding to interpret. + * @param oauthPropertyPath The path to the oauth properties in the service binding. * @see #DefaultOAuth2PropertySupplier(ServiceBindingDestinationOptions) */ public DefaultOAuth2PropertySupplier( - @Nonnull final ServiceBindingDestinationOptions options, - @Nonnull final List oauthPropertyPath ) - { + @Nonnull final ServiceBindingDestinationOptions options, + @Nonnull final List oauthPropertyPath) { this.options = options; this.oauthPropertyPath = oauthPropertyPath; credentials = TypedMapView.ofCredentials(options.getServiceBinding()); } @Override - public boolean isOAuth2Binding() - { + public boolean isOAuth2Binding() { return getOAuthCredential(String.class, "clientid").isDefined(); } @Override @Nonnull - public URI getServiceUri() - { + public URI getServiceUri() { return getCredentialOrThrow(URI.class, "url"); } @Override @Nonnull - public URI getTokenUri() - { - final String tokenUrlProperty = switch( getCredentialType() ) { + public URI getTokenUri() { + final String tokenUrlProperty = switch (getCredentialType()) { // note: this is for XSUAA only and does not apply for IAS (check the overridden method in the property supplier for IAS) // currently XSUAA only recognizes X509 and X509_ATTESTED, so we don't necessarily have to list X509_PROVIDED and X509_GENERATED here // still, doesn't hurt and is more future-proof in case XSUAA ever starts adopting these identifiers @@ -115,23 +106,21 @@ public URI getTokenUri() @Override @Nonnull - public ClientIdentity getClientIdentity() - { + public ClientIdentity getClientIdentity() { final String clientid = getOAuthCredentialOrThrow(String.class, "clientid"); - return switch( getCredentialType() ) { + return switch (getCredentialType()) { case BINDING_SECRET, INSTANCE_SECRET -> getSecretIdentity(clientid); case X509, X509_GENERATED -> getCertificateIdentity(clientid); case X509_ATTESTED -> getZtisIdentity(clientid); case X509_PROVIDED -> throw new DestinationAccessException( - "Credential type X509_PROVIDED is not supported. Please use X509_GENERATED or X509_ATTESTED instead."); + "Credential type X509_PROVIDED is not supported. Please use X509_GENERATED or X509_ATTESTED instead."); }; } @Nonnull @Override - public OAuth2Options getOAuth2Options() - { + public OAuth2Options getOAuth2Options() { final OAuth2Options.Builder builder = OAuth2Options.builder(); options.getOption(OAuth2Options.TokenRetrievalTimeout.class).peek(builder::withTimeLimiter); options.getOption(OAuth2Options.TokenCacheParameters.class).peek(builder::withTokenCacheParameters); @@ -144,97 +133,81 @@ public OAuth2Options getOAuth2Options() * @return The path to the oauth properties. */ @Nonnull - protected List getOAuthPropertyPath() - { + protected List getOAuthPropertyPath() { return new ArrayList<>(oauthPropertyPath); } @Nonnull - private ClientIdentity getCertificateIdentity( @Nonnull final String clientid ) - { + private ClientIdentity getCertificateIdentity(@Nonnull final String clientid) { final String cert = getOAuthCredentialOrThrow(String.class, "certificate"); final String key = getOAuthCredentialOrThrow(String.class, "key"); return new ClientCertificate(cert, key, clientid); } @Nonnull - private ZtisClientIdentity getZtisIdentity( @Nonnull final String clientid ) - { + private ZtisClientIdentity getZtisIdentity(@Nonnull final String clientid) { try { // sanity check: assert the connectivity-ztis module is present getClass() - .getClassLoader() - .loadClass("com.sap.cloud.sdk.cloudplatform.connectivity.ZeroTrustIdentityService"); - } - catch( final ClassNotFoundException e ) { + .getClassLoader() + .loadClass("com.sap.cloud.sdk.cloudplatform.connectivity.ZeroTrustIdentityService"); + } catch (final ClassNotFoundException e) { throw new CloudPlatformException( - "Failed to load implementation for credential type X509_ATTESTED. Please ensure the 'connectivity-ztis' module is present.", - e); + "Failed to load implementation for credential type X509_ATTESTED. Please ensure the 'connectivity-ztis' module is present.", + e); } final ZeroTrustIdentityService ztis = ZeroTrustIdentityService.getInstance(); final KeyStore keyStore; try { keyStore = ztis.getOrCreateKeyStore(); - } - catch( final Exception e ) { + } catch (final Exception e) { throw new CloudPlatformException("Failed to load X509 certificate for credential type X509_ATTESTED.", e); } return new ZtisClientIdentity(clientid, keyStore); } @Nonnull - private ClientIdentity getSecretIdentity( @Nonnull final String clientid ) - { + private ClientIdentity getSecretIdentity(@Nonnull final String clientid) { final String secret = getOAuthCredentialOrThrow(String.class, "clientsecret"); return new ClientCredentials(clientid, secret); } @Nonnull - CredentialType getCredentialType() - { + CredentialType getCredentialType() { return getOAuthCredential(CredentialType.class, "credential-type").getOrElse(CredentialType.BINDING_SECRET); } /** * Obtain an entry from the credentials block of the service binding. * - * @param - * The type of the entry to obtain. - * @param resultType - * The type of the entry to obtain. - * @param path - * The path to the entry to obtain. + * @param The type of the entry to obtain. + * @param resultType The type of the entry to obtain. + * @param path The path to the entry to obtain. * @return The entry from the credentials block of the service binding. - * @throws DestinationAccessException - * If the property was not found or could not be converted to the requested type. + * @throws DestinationAccessException If the property was not found or could not be converted to the requested type. * @see #getCredential(Class, String...) for a variant that doesn't throw */ @Nonnull - protected T getCredentialOrThrow( @Nonnull final Class resultType, @Nonnull final String... path ) - throws DestinationAccessException - { + protected T getCredentialOrThrow(@Nonnull final Class resultType, @Nonnull final String... path) + throws DestinationAccessException { return getCredential(resultType, path) - .getOrElseThrow( - () -> new DestinationAccessException( - "Failed to resolve property " + Arrays.toString(path) + " from service binding.")); + .getOrElseThrow( + () -> new DestinationAccessException( + "Failed to resolve property " + Arrays.toString(path) + " from service binding.")); } /** * Obtain an entry from the credentials block of the service binding. * - * @param - * The type of the entry to obtain. - * @param resultType - * The type of the entry to obtain. - * @param path - * The path to the entry to obtain. + * @param The type of the entry to obtain. + * @param resultType The type of the entry to obtain. + * @param path The path to the entry to obtain. * @return The entry from the credentials block of the service binding. * @see #getCredentialOrThrow(Class, String...) */ @Nonnull - protected Option getCredential( @Nonnull final Class resultType, @Nonnull final String... path ) - { + protected Option getCredential(@Nonnull final Class resultType, @Nonnull final String... path) { return getCredentialInternal(resultType, credentials, Arrays.asList(path)); } @@ -242,46 +215,37 @@ protected Option getCredential( @Nonnull final Class resultType, @Nonn * Obtain an oauth property from the credentials block of the service binding. Uses {@link #getOAuthPropertyPath()} * as prefix to the path. * - * @param - * The type of the entry to obtain. - * @param resultType - * The type of the entry to obtain. - * @param path - * The path to the entry to obtain. + * @param The type of the entry to obtain. + * @param resultType The type of the entry to obtain. + * @param path The path to the entry to obtain. * @return The oauth property from the credentials block of the service binding. - * @throws DestinationAccessException - * If the property was not found or could not be converted to the requested type. + * @throws DestinationAccessException If the property was not found or could not be converted to the requested type. * @see #getOAuthCredential(Class, String...) (Class, String...) for a variant that doesn't throw */ @Nonnull - protected T getOAuthCredentialOrThrow( @Nonnull final Class resultType, @Nonnull final String... path ) - throws DestinationAccessException - { + protected T getOAuthCredentialOrThrow(@Nonnull final Class resultType, @Nonnull final String... path) + throws DestinationAccessException { return getOAuthCredential(resultType, path) - .getOrElseThrow( - () -> new DestinationAccessException( - "Failed to resolve property " - + getOAuthPropertyPath() - + Arrays.toString(path) - + " from service binding.")); + .getOrElseThrow( + () -> new DestinationAccessException( + "Failed to resolve property " + + getOAuthPropertyPath() + + Arrays.toString(path) + + " from service binding.")); } /** * Obtain an oauth property from the credentials block of the service binding. Uses {@link #getOAuthPropertyPath()} * as prefix to the path. * - * @param - * The type of the entry to obtain. - * @param resultType - * The type of the entry to obtain. - * @param path - * The path to the entry to obtain. + * @param The type of the entry to obtain. + * @param resultType The type of the entry to obtain. + * @param path The path to the entry to obtain. * @return The oauth property from the credentials block of the service binding. * @see #getOAuthCredential(Class, String...) */ @Nonnull - protected Option getOAuthCredential( @Nonnull final Class resultType, @Nonnull final String... path ) - { + protected Option getOAuthCredential(@Nonnull final Class resultType, @Nonnull final String... path) { final List fullPath = getOAuthPropertyPath(); fullPath.addAll(Arrays.asList(path)); return getCredentialInternal(resultType, credentials, fullPath); @@ -289,66 +253,61 @@ protected Option getOAuthCredential( @Nonnull final Class resultType, @Nonnull private Option getCredentialInternal( - @Nonnull final Class resultType, - @Nonnull final TypedMapView currentNode, - @Nonnull final List names ) - { - if( names.isEmpty() ) { + @Nonnull final Class resultType, + @Nonnull final TypedMapView currentNode, + @Nonnull final List names) { + if (names.isEmpty()) { log.warn("Passed an empty property path to load from the service binding. This should never happen."); return Option.none(); } - if( names.size() == 1 ) { + if (names.size() == 1) { return Try.of(() -> currentNode.get(names.get(0))).map(o -> convert(o, resultType)).toOption(); } return Try - .of(() -> currentNode.getMapView(names.get(0))) - .toOption() - .flatMap(it -> getCredentialInternal(resultType, it, names.subList(1, names.size()))); + .of(() -> currentNode.getMapView(names.get(0))) + .toOption() + .flatMap(it -> getCredentialInternal(resultType, it, names.subList(1, names.size()))); } - @SuppressWarnings( "unchecked" ) + @SuppressWarnings("unchecked") @Nullable - static T convert( @Nullable final Object value, @Nonnull final Class cls ) - throws DestinationAccessException - { - if( value == null ) { + static T convert(@Nullable final Object value, @Nonnull final Class cls) + throws DestinationAccessException { + if (value == null) { return null; } - if( cls.isAssignableFrom(value.getClass()) ) { + if (cls.isAssignableFrom(value.getClass())) { return (T) value; } - if( cls == String.class ) { + if (cls == String.class) { return (T) value.toString(); } - if( cls == URI.class ) { + if (cls == URI.class) { try { return (T) new URI(value.toString()); - } - catch( final ClassCastException | URISyntaxException e ) { + } catch (final ClassCastException | URISyntaxException e) { throw new DestinationAccessException("Unable to convert '" + value + "' into an URI.", e); } } - if( cls == Integer.class ) { + if (cls == Integer.class) { try { return (T) Integer.valueOf((String) value); - } - catch( final ClassCastException | NumberFormatException e ) { + } catch (final ClassCastException | NumberFormatException e) { throw new DestinationAccessException("Unable to convert '" + value + "' into an Integer.", e); } } - if( cls == CredentialType.class ) { + if (cls == CredentialType.class) { try { final T result = (T) CredentialType.from((String) value); - if( result == null ) { + if (result == null) { throw new IllegalArgumentException(); } return result; - } - catch( final ClassCastException | IllegalArgumentException e ) { + } catch (final ClassCastException | IllegalArgumentException e) { throw new DestinationAccessException("Unable to convert '" + value + "' into a CredentialType.", e); } } throw new DestinationAccessException( - "Property value " + value + " could not be parsed into unknown type " + cls.getSimpleName()); + "Property value " + value + " could not be parsed into unknown type " + cls.getSimpleName()); } } From 6c91443b16bf420ca71c3645e735d6746801eb89 Mon Sep 17 00:00:00 2001 From: Nourhan Shata Date: Fri, 24 Oct 2025 14:48:39 +0200 Subject: [PATCH 3/3] Some Formatting again --- .../DefaultOAuth2PropertySupplier.java | 195 +++++++++++------- 1 file changed, 118 insertions(+), 77 deletions(-) diff --git a/cloudplatform/connectivity-oauth/src/main/java/com/sap/cloud/sdk/cloudplatform/connectivity/DefaultOAuth2PropertySupplier.java b/cloudplatform/connectivity-oauth/src/main/java/com/sap/cloud/sdk/cloudplatform/connectivity/DefaultOAuth2PropertySupplier.java index c7171fb30..ec89de5be 100644 --- a/cloudplatform/connectivity-oauth/src/main/java/com/sap/cloud/sdk/cloudplatform/connectivity/DefaultOAuth2PropertySupplier.java +++ b/cloudplatform/connectivity-oauth/src/main/java/com/sap/cloud/sdk/cloudplatform/connectivity/DefaultOAuth2PropertySupplier.java @@ -33,7 +33,8 @@ * @since 4.20.0 */ @Slf4j -public class DefaultOAuth2PropertySupplier implements OAuth2PropertySupplier { +public class DefaultOAuth2PropertySupplier implements OAuth2PropertySupplier +{ /** * By default, the oauth client details are expected to be contained in a {@code uaa} block of the * {@code credentials} of the service binding. @@ -58,43 +59,51 @@ public class DefaultOAuth2PropertySupplier implements OAuth2PropertySupplier { * Create a new instance to parse the given service binding. Will use {@link #DEFAULT_UAA_CREDENTIAL_PATH} as the * default path to oauth properties. * - * @param options The options containing the service binding to interpret. + * @param options + * The options containing the service binding to interpret. * @see #DefaultOAuth2PropertySupplier(ServiceBindingDestinationOptions, List) */ - public DefaultOAuth2PropertySupplier(@Nonnull final ServiceBindingDestinationOptions options) { + public DefaultOAuth2PropertySupplier( @Nonnull final ServiceBindingDestinationOptions options ) + { this(options, DEFAULT_UAA_CREDENTIAL_PATH); } /** * Create a new instance to parse the given service binding. Will use the given list as the path to url property. * - * @param options The options containing the service binding to interpret. - * @param oauthPropertyPath The path to the oauth properties in the service binding. + * @param options + * The options containing the service binding to interpret. + * @param oauthPropertyPath + * The path to the oauth properties in the service binding. * @see #DefaultOAuth2PropertySupplier(ServiceBindingDestinationOptions) */ public DefaultOAuth2PropertySupplier( - @Nonnull final ServiceBindingDestinationOptions options, - @Nonnull final List oauthPropertyPath) { + @Nonnull final ServiceBindingDestinationOptions options, + @Nonnull final List oauthPropertyPath ) + { this.options = options; this.oauthPropertyPath = oauthPropertyPath; credentials = TypedMapView.ofCredentials(options.getServiceBinding()); } @Override - public boolean isOAuth2Binding() { + public boolean isOAuth2Binding() + { return getOAuthCredential(String.class, "clientid").isDefined(); } @Override @Nonnull - public URI getServiceUri() { + public URI getServiceUri() + { return getCredentialOrThrow(URI.class, "url"); } @Override @Nonnull - public URI getTokenUri() { - final String tokenUrlProperty = switch (getCredentialType()) { + public URI getTokenUri() + { + final String tokenUrlProperty = switch( getCredentialType() ) { // note: this is for XSUAA only and does not apply for IAS (check the overridden method in the property supplier for IAS) // currently XSUAA only recognizes X509 and X509_ATTESTED, so we don't necessarily have to list X509_PROVIDED and X509_GENERATED here // still, doesn't hurt and is more future-proof in case XSUAA ever starts adopting these identifiers @@ -106,21 +115,23 @@ public URI getTokenUri() { @Override @Nonnull - public ClientIdentity getClientIdentity() { + public ClientIdentity getClientIdentity() + { final String clientid = getOAuthCredentialOrThrow(String.class, "clientid"); - return switch (getCredentialType()) { + return switch( getCredentialType() ) { case BINDING_SECRET, INSTANCE_SECRET -> getSecretIdentity(clientid); case X509, X509_GENERATED -> getCertificateIdentity(clientid); case X509_ATTESTED -> getZtisIdentity(clientid); case X509_PROVIDED -> throw new DestinationAccessException( - "Credential type X509_PROVIDED is not supported. Please use X509_GENERATED or X509_ATTESTED instead."); + "Credential type X509_PROVIDED is not supported. Please use X509_GENERATED or X509_ATTESTED instead."); }; } @Nonnull @Override - public OAuth2Options getOAuth2Options() { + public OAuth2Options getOAuth2Options() + { final OAuth2Options.Builder builder = OAuth2Options.builder(); options.getOption(OAuth2Options.TokenRetrievalTimeout.class).peek(builder::withTimeLimiter); options.getOption(OAuth2Options.TokenCacheParameters.class).peek(builder::withTokenCacheParameters); @@ -133,81 +144,97 @@ public OAuth2Options getOAuth2Options() { * @return The path to the oauth properties. */ @Nonnull - protected List getOAuthPropertyPath() { + protected List getOAuthPropertyPath() + { return new ArrayList<>(oauthPropertyPath); } @Nonnull - private ClientIdentity getCertificateIdentity(@Nonnull final String clientid) { + private ClientIdentity getCertificateIdentity( @Nonnull final String clientid ) + { final String cert = getOAuthCredentialOrThrow(String.class, "certificate"); final String key = getOAuthCredentialOrThrow(String.class, "key"); return new ClientCertificate(cert, key, clientid); } @Nonnull - private ZtisClientIdentity getZtisIdentity(@Nonnull final String clientid) { + private ZtisClientIdentity getZtisIdentity( @Nonnull final String clientid ) + { try { // sanity check: assert the connectivity-ztis module is present getClass() - .getClassLoader() - .loadClass("com.sap.cloud.sdk.cloudplatform.connectivity.ZeroTrustIdentityService"); - } catch (final ClassNotFoundException e) { + .getClassLoader() + .loadClass("com.sap.cloud.sdk.cloudplatform.connectivity.ZeroTrustIdentityService"); + } + catch( final ClassNotFoundException e ) { throw new CloudPlatformException( - "Failed to load implementation for credential type X509_ATTESTED. Please ensure the 'connectivity-ztis' module is present.", - e); + "Failed to load implementation for credential type X509_ATTESTED. Please ensure the 'connectivity-ztis' module is present.", + e); } final ZeroTrustIdentityService ztis = ZeroTrustIdentityService.getInstance(); final KeyStore keyStore; try { keyStore = ztis.getOrCreateKeyStore(); - } catch (final Exception e) { + } + catch( final Exception e ) { throw new CloudPlatformException("Failed to load X509 certificate for credential type X509_ATTESTED.", e); } return new ZtisClientIdentity(clientid, keyStore); } @Nonnull - private ClientIdentity getSecretIdentity(@Nonnull final String clientid) { + private ClientIdentity getSecretIdentity( @Nonnull final String clientid ) + { final String secret = getOAuthCredentialOrThrow(String.class, "clientsecret"); return new ClientCredentials(clientid, secret); } @Nonnull - CredentialType getCredentialType() { + CredentialType getCredentialType() + { return getOAuthCredential(CredentialType.class, "credential-type").getOrElse(CredentialType.BINDING_SECRET); } /** * Obtain an entry from the credentials block of the service binding. * - * @param The type of the entry to obtain. - * @param resultType The type of the entry to obtain. - * @param path The path to the entry to obtain. + * @param + * The type of the entry to obtain. + * @param resultType + * The type of the entry to obtain. + * @param path + * The path to the entry to obtain. * @return The entry from the credentials block of the service binding. - * @throws DestinationAccessException If the property was not found or could not be converted to the requested type. + * @throws DestinationAccessException + * If the property was not found or could not be converted to the requested type. * @see #getCredential(Class, String...) for a variant that doesn't throw */ @Nonnull - protected T getCredentialOrThrow(@Nonnull final Class resultType, @Nonnull final String... path) - throws DestinationAccessException { + protected T getCredentialOrThrow( @Nonnull final Class resultType, @Nonnull final String... path ) + throws DestinationAccessException + { return getCredential(resultType, path) - .getOrElseThrow( - () -> new DestinationAccessException( - "Failed to resolve property " + Arrays.toString(path) + " from service binding.")); + .getOrElseThrow( + () -> new DestinationAccessException( + "Failed to resolve property " + Arrays.toString(path) + " from service binding.")); } /** * Obtain an entry from the credentials block of the service binding. * - * @param The type of the entry to obtain. - * @param resultType The type of the entry to obtain. - * @param path The path to the entry to obtain. + * @param + * The type of the entry to obtain. + * @param resultType + * The type of the entry to obtain. + * @param path + * The path to the entry to obtain. * @return The entry from the credentials block of the service binding. * @see #getCredentialOrThrow(Class, String...) */ @Nonnull - protected Option getCredential(@Nonnull final Class resultType, @Nonnull final String... path) { + protected Option getCredential( @Nonnull final Class resultType, @Nonnull final String... path ) + { return getCredentialInternal(resultType, credentials, Arrays.asList(path)); } @@ -215,37 +242,46 @@ protected Option getCredential(@Nonnull final Class resultType, @Nonnu * Obtain an oauth property from the credentials block of the service binding. Uses {@link #getOAuthPropertyPath()} * as prefix to the path. * - * @param The type of the entry to obtain. - * @param resultType The type of the entry to obtain. - * @param path The path to the entry to obtain. + * @param + * The type of the entry to obtain. + * @param resultType + * The type of the entry to obtain. + * @param path + * The path to the entry to obtain. * @return The oauth property from the credentials block of the service binding. - * @throws DestinationAccessException If the property was not found or could not be converted to the requested type. + * @throws DestinationAccessException + * If the property was not found or could not be converted to the requested type. * @see #getOAuthCredential(Class, String...) (Class, String...) for a variant that doesn't throw */ @Nonnull - protected T getOAuthCredentialOrThrow(@Nonnull final Class resultType, @Nonnull final String... path) - throws DestinationAccessException { + protected T getOAuthCredentialOrThrow( @Nonnull final Class resultType, @Nonnull final String... path ) + throws DestinationAccessException + { return getOAuthCredential(resultType, path) - .getOrElseThrow( - () -> new DestinationAccessException( - "Failed to resolve property " - + getOAuthPropertyPath() - + Arrays.toString(path) - + " from service binding.")); + .getOrElseThrow( + () -> new DestinationAccessException( + "Failed to resolve property " + + getOAuthPropertyPath() + + Arrays.toString(path) + + " from service binding.")); } /** * Obtain an oauth property from the credentials block of the service binding. Uses {@link #getOAuthPropertyPath()} * as prefix to the path. * - * @param The type of the entry to obtain. - * @param resultType The type of the entry to obtain. - * @param path The path to the entry to obtain. + * @param + * The type of the entry to obtain. + * @param resultType + * The type of the entry to obtain. + * @param path + * The path to the entry to obtain. * @return The oauth property from the credentials block of the service binding. * @see #getOAuthCredential(Class, String...) */ @Nonnull - protected Option getOAuthCredential(@Nonnull final Class resultType, @Nonnull final String... path) { + protected Option getOAuthCredential( @Nonnull final Class resultType, @Nonnull final String... path ) + { final List fullPath = getOAuthPropertyPath(); fullPath.addAll(Arrays.asList(path)); return getCredentialInternal(resultType, credentials, fullPath); @@ -253,61 +289,66 @@ protected Option getOAuthCredential(@Nonnull final Class resultType, @ @Nonnull private Option getCredentialInternal( - @Nonnull final Class resultType, - @Nonnull final TypedMapView currentNode, - @Nonnull final List names) { - if (names.isEmpty()) { + @Nonnull final Class resultType, + @Nonnull final TypedMapView currentNode, + @Nonnull final List names ) + { + if( names.isEmpty() ) { log.warn("Passed an empty property path to load from the service binding. This should never happen."); return Option.none(); } - if (names.size() == 1) { + if( names.size() == 1 ) { return Try.of(() -> currentNode.get(names.get(0))).map(o -> convert(o, resultType)).toOption(); } return Try - .of(() -> currentNode.getMapView(names.get(0))) - .toOption() - .flatMap(it -> getCredentialInternal(resultType, it, names.subList(1, names.size()))); + .of(() -> currentNode.getMapView(names.get(0))) + .toOption() + .flatMap(it -> getCredentialInternal(resultType, it, names.subList(1, names.size()))); } - @SuppressWarnings("unchecked") + @SuppressWarnings( "unchecked" ) @Nullable - static T convert(@Nullable final Object value, @Nonnull final Class cls) - throws DestinationAccessException { - if (value == null) { + static T convert( @Nullable final Object value, @Nonnull final Class cls ) + throws DestinationAccessException + { + if( value == null ) { return null; } - if (cls.isAssignableFrom(value.getClass())) { + if( cls.isAssignableFrom(value.getClass()) ) { return (T) value; } - if (cls == String.class) { + if( cls == String.class ) { return (T) value.toString(); } - if (cls == URI.class) { + if( cls == URI.class ) { try { return (T) new URI(value.toString()); - } catch (final ClassCastException | URISyntaxException e) { + } + catch( final ClassCastException | URISyntaxException e ) { throw new DestinationAccessException("Unable to convert '" + value + "' into an URI.", e); } } - if (cls == Integer.class) { + if( cls == Integer.class ) { try { return (T) Integer.valueOf((String) value); - } catch (final ClassCastException | NumberFormatException e) { + } + catch( final ClassCastException | NumberFormatException e ) { throw new DestinationAccessException("Unable to convert '" + value + "' into an Integer.", e); } } - if (cls == CredentialType.class) { + if( cls == CredentialType.class ) { try { final T result = (T) CredentialType.from((String) value); - if (result == null) { + if( result == null ) { throw new IllegalArgumentException(); } return result; - } catch (final ClassCastException | IllegalArgumentException e) { + } + catch( final ClassCastException | IllegalArgumentException e ) { throw new DestinationAccessException("Unable to convert '" + value + "' into a CredentialType.", e); } } throw new DestinationAccessException( - "Property value " + value + " could not be parsed into unknown type " + cls.getSimpleName()); + "Property value " + value + " could not be parsed into unknown type " + cls.getSimpleName()); } }