Part 6: Connecting to the On Premise System in Local Development

Brian Heise
5 min readApr 25, 2021
A photo of a laptop with code on the screen.
Photo by Oskar Yildiz on Unsplash

Contents

Welcome to Part 6 of my tutorial on connecting a CAP Java application to an SAP on-premise system. In Parts 1–5 we learned how to set up and deploy our application to BTP, with data read from the on-premise system. However, in our current setup, we cannot test this connection from local development. This is of course a big problem for anyone developing such an application — as things stand, to test any changes to the code that handles interaction with the on-premise system you’d have to deploy it first. As deployment takes several minutes to complete, this is certainly not ideal. In the following tutorial I’ll teach you how to establish a secure connection to your on-premise system from your local development environment.

Step 1

To securely connect to our on-premise system, we need to use the same connectivity and destination services what we bound to our app in the cloud. In order to use them, we need to get the environment variables that our deployed application uses to access them.

Go to BTP and open the space where you deployed your application, then click on the link to your server app, as shown below.

Click “Environment Variables” on the sidebar, as shown below:

You should see a JSON displayed with the key “VCAP_SERVICES”. Copy this JSON. Note that a second JSON called VCAP_APPLICATION is also contained in this page, but you don’t need it so don’t copy it.

Step 2

Return to your local development environment and create a file called “default-env.json”. Paste your VCAP_SERVICES JSON inside of here. For our purposes, we only need the environment variables for your destination service and your connectivity service. If you have additional services bound to your application, delete them — some of them might case errors without additional configuration.

The result should look something like the following:

default-env.json

Step 3

Next we need to add a dependency to our pom.xml file (the one in the srv folder).

<dependency>
<groupId>com.sap.cds</groupId>
<artifactId>cds-integration-cloud-sdk</artifactId>
</dependency>

This dependency from SAP’s Cloud SDK will allow our app to process the details of these environment variables and let us connect to these services without any additional coding on our part.

Step 4

Next, we have to configure an SSH tunnel through our deployed application on BTP to these services. This is a feature of the Cloud Foundry environment — services can only be accessed through the application they are bound to. This means that if we want to connect to them from our local environment we have to SSH into the deployed application first. This is why it’s necessary to deploy the application before initiating any connections to the on-premise system locally.

First, let’s enable SSH on the app.

cf enable-ssh <your-app-name>
cf restart <your-app-name>

Step 5

Next, we actually set up the SSH tunnel. Now we will open an SSH tunnel. Open your default-env.json and find the values for the following in your connectivity service:

  • onpremise_proxy_port
  • onpremise_proxy_host

Use them to construct the following command: cf ssh <app-name> -L <onpremise_proxy_port>:<onpremise_proxy_host>:<onpremise_proxy_port>

Save this command so that you don’t forget it. You will need to use it every time you do connect to the on-premise system locally.

Step 6

Enter the command that you just constructed in the command line. If you are successful, the result should look like this:

Step 7

Next, open “default-env.json”, find your connectivity service, and then replace the value for “onpremise_proxy_host” with “localhost”.

Step 8

Run the following commands:

mvn clean install
mvn spring-boot:run

Open your app in the browser on http://localhost:8080

Click the link. You should see the data extracted from the on-premise system, just as you did at the end of Part 5 of this series.

Conclusion

Congratulations! If you made it to the end of this tutorial you can now connect your CAP Java app to your on-premise system and also continue testing code that requires a connection to the on-premise system without having to deploy it.

If you’ve run into any problems why following this tutorial please feel free to leave a comment and I’ll do my best to help your solve the issue. Happy coding!

Contents

Support

Did you like this blog? Want to make sure I can keep creating them? Then consider subscribing on Patreon!

--

--

Brian Heise
Brian Heise

Written by Brian Heise

Full Stack web developer employed at Liferay Japan

Responses (2)