zongke ren
zongke ren

Reputation: 11

How to get subscriber's destination in SCP multitanant app

The situation is i have a multitanant app running on SCP. some customers will subscribe my app. They will define their own destination for external system. I have set up the could-connector. Another thing is my app have no request context ,it's just based on schedule task.

Env: SCP Cloudfoundry

I have try to get destination from provider side successfully. but failed for subscriber side.

the code snipt below is how i get destination

    log.info("==========Begin logic to get destination==========");
    Callable<Destination> callable = new Callable<Destination>() {
      @Override
      public Destination call() throws Exception {
        DestinationAccessor
            .setRetrievalStrategy("xxx", DestinationRetrievalStrategy.ALWAYS_SUBSCRIBER);
        return DestinationAccessor.getDestination("xxx");
      }
    };
    return new RequestContextExecutor().execute(callable);

Upvotes: 1

Views: 705

Answers (1)

Pablo Caceres
Pablo Caceres

Reputation: 161

For SAP Cloud SDK version 2:

This version needs some extra information for JwtBasedRequestContextExecutor.onBehalfOfTenant to work. For each tenant you want to operate on, the following are needed:

  1. The tenant ID. When making the application subscription-ready you had to implement a subscription callback endpoint that SCP calls whenever a tenant subscribes to your application. The URL that SCP calls contains the tenant ID which you can extract. Your callback servlet should now persist this tenant ID somewhere.

  2. A fully-qualified URI to the tenant's XSUAA instance. Part of this you can also get from the subscription callback endpoint. SCP calls your endpoint with a JSON payload that contains a property called subscribedSubdomain. Your callback servlet should now persist this somewhere along with the tenant ID. This takes care of the host name portion but JwtBasedRequestContextExecutor needs a full XSUAA URI (e.g. https://[subscribedSubdomain].[uaadomain]). One way is to get the provider's XSUAA information and extract the uaadomain parameter (basically loading from VCAP_SERVICES) using CloudPlatformAccessor, like so:

    final String tenantXsuaaUri = "https://" + subscribedSubdomain + "." + ((ScpCfCloudPlatform)CloudPlatformAccessor.getCloudPlatform()).getXsuaaServiceCredentials().get("uaadomain").getAsString();
    
  3. XS application name of provider's XSUAA instance. For this you can use the CloudPlatformAccessor like so (again, loading from VCAP_SERVICES):

    final String xsAppName = ((ScpCfCloudPlatform)CloudPlatformAccessor.getCloudPlatform()).getXsAppName();
    

Once you finally have all the needed parameters, you can call JwtBasedRequestContextExecutor.onBehalfOfTenant like so to run something as the desired tenant:

new JwtBasedRequestContextExecutor()
    .onBehalfOfTenant(tenantId, tenantXsuaaUri, Collections.singletonList(xsAppName))
    .withParentRequestContext()
    .execute(() -> {

    // your code goes here

});



For SAP Cloud SDK version 3:

This version has been improved and it is easier to execute code as another tenant, so I would recommend upgrading to version 3 if possible. With version 3 you only need the tenant ID and the subscribedSubdomain value (no need to form a complete XSUAA URI). Instead of JwtBasedRequestContextExecutor you would use the TenantAccessor class and the executeWithTenant method like so:

final Tenant subscribedTenant = new ScpCfTenant(tenantID, subscribedSubdomain);

TenantAccessor.executeWithTenant(subscribedTenant, () -> {

    // your code goes here

});


Overview of SAP Cloud SDK version 3: https://blogs.sap.com/2019/08/01/version-3-of-the-sap-cloud-sdk-for-java-is-here/

Version 3 Migration Guide: https://blogs.sap.com/2019/08/01/migrate-to-version-3.0.0-of-the-sap-cloud-sdk-for-java/

Version 3 release notes: https://help.sap.com/doc/6c02295dfa8f47cf9c08a19f2e172901/1.0/en-US/index.html

Upvotes: 3

Related Questions