September 28, 2019
In this medium, we will be looking on how to expose a legacy web-service / SOAP service as REST APIs using both WSO2 API Manager (2.6.0) and Enterprise Integrator (6.5.0).
Why EI & API Manager? Can’t we do it using either one of them?
Of course we can. It is possible to expose a SOAP service as REST APIs using either API Manager or Enterprise Integrator. But, if you have added additional mediations and performing heavy-duty mediations in API sequences, then using both the API Manager and Enterprise Integrator is a best solution. Which is because, EI is scoped and capable to perform heavy-mediations as well as the API Manager is scoped to perform only API management processes.
This post illustrates how to add an LDAP user-store as a secondary user-store in WSO2 API Manager (in almost every WSO2 Product).
For demo purposes, I have implemented a mock Developer Management Web Service using Spring Boot and will be using that to configure our EI and API Manager instances.
Let’s dive into the demo …
A Simple Spring Boot (SOAP) Web Service
The Developer Management Web Service contains the following entities and services …
Developer- Add Developer- Get Developer- Delete DeveloperRepo- Add Repo- Get Repo
Clone or download the project from GitHub, and execute the following command from the root folder to start the web-service server
mvn spring-boot:run
If you don’t have maven installed, then use the following to start the server
./mvnw spring-boot:run
After a successful startup, navigate to https://localhost:8443/ws/developers.wsdl
to access the WSDL definitions of our Developer Management service.
We will be starting from WSO2 Enterprise Integrator instance to expose the provided legacy service as REST service.
Beforehand, we want to export the self-signed certificate of our Developer Management service to our EI instance for a hassle-free SSL connectivity and HTTPS communication.
Follow the given steps to export and import certificates of our legacy service to the EI’s trust-store.
Follow this https://www.shellhacks.com/get-ssl-certificate-from-server-site-url-export-download/
to export and download the self-signed certificate from the browser
Navigate to
<EI>/repository/resources/security
folder and execute the following command. Enterwso2carbon
as the password if it prompts
keytool -importcert -file <path to crt> -keystore client-truststore.jks -alias hydrogen.com
Start the EI instance by navigating to the /bin
folder and execute the following command based on your environment …
# linux envsh integrator.sh# windows envintegrator.bat
Fire up your favorite browser and route to https://localhost:9443/carbon
and log in using the following credentials
admin
admin
I have implemented and provided a carbon archive with configurations related to SOAP to REST services, endpoints, and sequences. You can use the following artifact and deploy it our EI instance without any configurations hassle.
Carbon Artifact of Developer Management Service API
Select Add
under Carbon Applications
section and upload and deploy the above-provided carbon archive.
Below given is the source code of Developer Management Service API Carbon artifact
Source code of Developer Management Service API
Before testing, make sure to verify whether the EI instance is running and deploy the provided Carbon archive
Test the exposed REST API service using the below cURL commands. Given below are a couple of cURL commands to test the exposed REST APIs.
Given below is a .http file, can be used with VS Code with REST Client Extension
@hostname = https://localhost:8243/dev-management | |
@username = athiththan11 | |
### Get Developer | |
# @name getDeveloper | |
curl -k -X GET {{hostname}}/devs/{{username}} \ | |
-H "Content-Type: application/json" | |
### Get Repos of a Developer | |
# @name getRepos | |
curl -k -X GET {{hostname}}/devs/{{username}}/repos \ | |
-H "Content-Type: application/json" | |
### Delete a Developer | |
# @name deleteDeveloper | |
curl -k -X DELETE {{hostname}}/devs/{{username}} \ | |
-H "Content-Type: application/json" | |
### Create a Developer | |
# @name addDeveloper | |
curl -k -X POST {{hostname}}/devs \ | |
-d '{ "username": "athiththan11", "name": "Athiththan", "email": "athiththan11@test.com" }' \ | |
-H "Content-Type: application/json" | |
### Create a Repo | |
# @name addRepo | |
curl -k -X POST {{hostname}}/repos \ | |
-d '{ "name": "repodews", "username": "athiththan11", "forks": 0 }' \ | |
-H "Content-Type: application/json" |
Thereafter, we’ll configure a WSO2 API Manager instance to manage the API lifecycle, traffic, subscriptions, and analytics of our exposed REST service.
In advance, we need to configure a port off-set for our API Manager instance, this done because both our EI and API Manager instances will be running on a single machine.
Navigate to <APIM>/repository/conf/carbon.xml
and change the <Offset>
value from 0
to 1
. And, start the API Manager instance by navigating to <APIM>/bin
folder and execute the following command based on your environment …
# linux envsh wso2server.sh# windows envwso2server.bat
After a successful startup, route to https://localhost:9444/publisher
and login using admin as both the username and password.
Given below is a complete Swagger definition for our Developer Management REST API.
Swagger Definition of Developer Management REST API
Click on Add New API
Then select I Have an Existing API
and upload the provided swagger file.
Add the context as /dev-management
and click Next: Implement
Add https://localhost:8243/dev-management
as the Production Endpoint
and click Next: Manage
Select all applicable throttling tiers and click Save & Publish
to publish the Developer Management REST API to the Store portal.
After publishing the REST API from the Publisher portal, navigate to the Store portal by https://localhost:9444/store
and log in as admin
.
Select our published API and subscribe to the DefaultApplication
with Unlimited Tier
.
You can create a new application by navigating to the Application section in the Store portal and subscribe to that API with applicable Tier
Select View Subscriptions
from the alert box and then navigate to the Production Keys
tab and select Generate Keys
to generate new consumer keys for our DefaultApplication
.
After a successful generation, copy the Access Token
value to invoke our defined REST APIs.
Before testing, make sure to verify whether the APIM instance is running and have successfully published the REST API and subscribed
Test the exposed REST APIs using the below cURL commands. Given below are a couple of cURL commands to test the exposed REST APIs. We can also test the REST APIs using the integrated Swagger tool in the Store portal.
You can replace the
consumer-key
andconsumer-secret
fields with the generated values or you can replace the{{access-token}}
with the copiedAccess Token
value and execute the cURL commands
@consumer-key = <Replace with Consumer Key> | |
@consumer-secret = <Replace with Consumer Secret> | |
@api-hostname = https://10.100.5.220:8244/dev-management/v1.0.0 | |
# Username & Password | |
@api-username = admin | |
@api-password = admin | |
@username = athiththan11 | |
### Token Endpoint | |
# @name tokenEndpoint | |
curl -k -d "grant_type=password&username={{api-username}}&password={{api-password}}&scope=default" \ | |
-H "Authorization: Basic <Replace with Base64Encode(ClientID:ClientSecret)>" \ | |
https://localhost:8244/token | |
@access-token = {{tokenEndpoint.response.body.access_token}} | |
@authorization = Bearer {{access-token}} | |
### Get Developer | |
# @name getDeveloperAPI | |
curl -k -X GET {{api-hostname}}/devs/{{username}} \ | |
-H "accept: application/json" \ | |
-H "Authorization: {{authorization}}" | |
### Get Repos of a Developer | |
# @name getReposAPI | |
curl -k -X GET {{api-hostname}}/devs/{{username}}/repos \ | |
-H "accept: application/json" \ | |
-H "Authorization: {{authorization}}" | |
### Delete a Developer | |
# @name deleteDeveloperAPI | |
curl -k -X DELETE {{api-hostname}}/devs/{{username}} \ | |
-H "accept: application/json" \ | |
-H "Authorization: {{authorization}}" | |
### Create a Developer | |
# @name addDeveloperAPI | |
curl -k -X POST {{api-hostname}}/devs \ | |
-d '{ "username": "athiththan11", "name": "Athiththan", "email": "athiththan11@test.com" }' \ | |
-H "Content-Type: application/json" \ | |
-H "accept: application/json" \ | |
-H "Authorization: {{authorization}}" | |
### Create a Repo | |
# @name addRepoAPI | |
curl -k -X POST {{api-hostname}}/repos \ | |
-d '{ "name": "repodews", "username": "athiththan11", "forks": 0 }' \ | |
-H "Content-Type: application/json" \ | |
-H "accept: application/json" \ | |
-H "Authorization: {{authorization}}" |
A Simple Spring Boot (SOAP) Web Service