Objective
...
The JAVA Middleware provides the ability to modernize existing API with SOAJ when these APIs are this API is built using JAVA as a development language.
The middleware exposes several functionality and configuration that SOAJS offers and which can be consumed by the Microservice API upon initialization or during runtime when requests to its APIs arrive.
This page explains the JAVA middleware; the functionality and configuration that it offers, how to install it and how to use it.
...
- How to Install
- Features
- Configuration
- Functionality
- How does it work
- on init
- on request
- Using the Middleware
- Initialization State
- Runtime State
- Examples
- JAXRS Jersey Express
1- How to Install
...
Source Code: The SOAJS Java middleware is open source and is available under Github, you can use the following reference to pull the source code → https://github.com/soajs/soajs.java
The middleware is also available as an NPM package and can be download for free.
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| //
| |||||||
# install from npm npm install soajs.nodejs |
...
soajs.java |
...
Or through POM.xml
Code Block | |
---|---|
language | js|
<dependency>
<groupId>soajs</groupId>
<artifactId>soajs.java</artifactId>
<version>1.0.0</version>
</dependency> |
And make sure to initialize it in your servlet tag in (web.xml), as follows:
Code Block | ||
---|---|---|
| ||
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
<param-value>soajs.filters.SoajsContainerRequestFilter</param-value>
</init-param> |
2- Features
...
Once the mw middleware is initialized, the following list of functions will be available to use through 'req.soajs.reg'
...
functionality becomes available:
Function | Type | Input | Output | Remarks |
---|---|---|---|---|
getDatabases |
Method | dbName (optional) |
| Mongo Client Driver |
getServiceConfig |
returns database object for dbName provided
Method | - | returns service configuration object | Services Config | |
getDeployer | Method | - | returns deployer object | Registry |
getCustom | Method | - | returns custom object | Services Config |
getResources |
returns all resources
Method | resourceName (optional) |
|
|
|
|
Registry | |||
getServices | Method | serviceName (optional) |
|
| Service |
getDaemons |
returns all daemons
Method | daemonName (optional) |
|
| Daemon | |||
reload | Method | - | void (reload registry only) | Registry |
This middleware functionality is accessible via the request that is made to the API. The middleware traps the request once it hits the API, injects its functionality and then hands it back to you to add your business logic.
3-How does it work
...
On Init
...
Using The Middleware requires the following environment variables :to be set prior to starting your service
Environment Variable | Description | Example | Reference |
---|---|---|---|
SOAJS_REGISTRY_API | The location of the SOAJS API Gateway along with the maintenance port value that it is using in the environment where the service will run in | 127.0.0.1:5000/ | Environment Variables |
SOAJS_ENV | environment code that this service will run in | DEV | Environment Variables |
Once your service starts, the middleware is going to invoke the controller and get the registry. Using the service configuration provided, the mw will auto reload the registry every "serviceConfig.awareness.autoRelaodRegistry"
Once the registry is loaded, the middleware is now ready to act on request.
On request:
Once a request is reached, the SOAJS controller will be passing through the request headers a 'soajsinjectobj' which will be mapped within the middleware to the following standard object:
...
language | js |
---|
...
will make a call to the SOAJS API Gateway and retrieve the registry of the environment, it will cache it in memory.
From this registry, it uses variable autoReloadRegistry (which represents the auto reload duration in seconds) and every time this value is met, the middleware automatically pulls the new version of the registry and updates its cached version.
On request
SOAJS midlleware augments your service by trapping all arriving requests and injecting additional data then hands the updated version of request to your business logic.
The following table illustrates the data that gets injected by the middleware on every request:
Property | Code Snippet | Description | Reference | |||||
---|---|---|---|---|---|---|---|---|
Tenant |
|
...
| Represents the tenant that is making the call to the API. | Multitenancy | ||||||
Key Device Geo |
|
---|
...
| Represents the key configuration which belongs to the tenant making the call to the API. | |||||||
Application |
|
---|
...
| Represents the application information which belongs to the tenant making the call to the API. | |||||||
Package |
|
---|
...
| Represents the Productization that the tenant making the call to the API is using. | |||||||
URAC |
|
---|
...
Represents the User whose access token was found in the request. | URAC | |||||||
Awareness |
|
---|
...
|
...
Represents the SOAJS API Gateway internal network host & port based on the environment where the API is deployed and running in. | Request |
4- Using the Middleware
...
Add this library to your Once you download the middleware, simply instruct your application to use it by adding it as a library to the restful service as an external jar (file soajs.java.jar)
Or through jar OR using a POM.xml file
Code Block | |||||||
---|---|---|---|---|---|---|---|
| |||||||
<dependency> <groupId>soajs</groupId> <artifactId>soajs.java</artifactId> <version>1.0.0</version> </dependency> |
And make Then make sure to initialize it in your servlet tag in (under web.xml), as follows as follow:
Code Block | |||||||
---|---|---|---|---|---|---|---|
| |||||||
<init-param> <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name> <param-value>soajs.filters.SoajsContainerRequestFilter</param-value> </init-param> |
Consequently, anywhere in your web application you can wire your context and get your soajs object, as followsto access the data and function that it offers
Code Block | |||||||
---|---|---|---|---|---|---|---|
| |||||||
(@Context HttpHeaders headers)
headers.getRequestHeader("soajs") |
5- Examples
...
JAXRS Jersey
The following code snippets shows a basic example built using Java and how the SOAJS Java Middleware was added to the code to augment the web server created.
Full Example on: https://github.com/soajs/soajs.java.jaxrs_jersey
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
package soajs.test.rest; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Context; import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.json.JSONObject; //Import Filters from SOAJS Java Middleware import soajs.filters.SoajsRegistry; import soajs.filters.SoajsRequestUtilities; /** * * @author Etienne */ @Path("/hello") public class Hello { //Invoke the middleware and use it in the App @GET @Produces(MediaType.APPLICATION_JSON) public Response getTest( @QueryParam("username") String username, @QueryParam("lastname") String lastname ) { JSONObject response = new JSONObject(); response.put("message", "Hello, I am a JAVA service, you are [" + username + "] and your last name is : [" + lastname + "]"); response.put("dbs", SoajsRegistry.getDatabases() ); return Response.status(200).entity(response.toString()).build(); } //Invoke the middleware }and use it in the App @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response postTest(String data, @Context HttpHeaders headers, @Context HttpServletRequest httpServletRequest, @Context HttpServletResponse httpServletResponse ) { JSONObject soajs = new JSONObject(); try{ System.out.println("Post service reached: trying to fetch request header at SOAJS ..."); System.out.println("Soajs request header length = [" + headers.getRequestHeader("soajs").size() + "]"); soajs = new JSONObject(headers.getRequestHeader("soajs").get(0)); String host = SoajsRequestUtilities.getHost(soajs); soajs.put("controller", host); soajs.put("databases", SoajsRegistry.getDatabases()); }} catch(Exception e){ e.printStackTrace(); } return Response.status(200).entity(soajs.toString()).build(); } @DELETE @Produces(MediaType.TEXT_PLAIN) public String deleteTest(@QueryParam("queryParam1") String queryParam1) { return "delete : " + queryParam1; } @PUT @Produces(MediaType.TEXT_PLAIN) public String putTest(@QueryParam("queryParam1") String queryParam1) { return "put : " + queryParam1; } } |
...