Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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.

...

  1. How to Install 
  2. Features
    1. Configuration
    2. Functionality
  3. How does it work
    1. on init
    2. on request
  4. Using the Middleware
    1. Initialization State
    2. Runtime State
  5. Examples
    1. 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
languagejsbash
themeMidnight
titleSOAJS Java NPM Package
# install from npm
npm install soajs.java

Add this library to your restful service as an external jar (soajs.java.jar)

Or through POM.xml

Code Block
languagejs
<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
languagejs
<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:

for Service for serviceName
FunctionTypeInputOutputRemarks
getDatabases-MethoddbName (optional)
  • returns database object if dbName provided
  • returns all core and tenant meta databases otherwise
Mongo Client Driver
getDatabasesdbName

returns database object for dbName provided

Mongo Client Driver
getServiceConfiggetServiceConfigMethod-returns service configuration objectServices Config
getDeployerMethod-returns deployer objectRegistry
getCustomMethod-returns custom objectServices Config
getResources-

returns all resources

getResourcesMethodresourceName (optional)
  • returns resource object
  • if resourceName provided
getServices-

returns all services

  • returns all resources otherwise
Registry
getServicesMethodserviceName (optional)
  • returns service object
  • if serviceName provided
  • returns all services otherwise
Service
getDaemons-

returns all daemons

DaemongetDaemonsMethoddaemonName (optional)
  • returns daemon object
for daemonName provided
  • if daemonName provided
  • returns all daemons otherwise
Daemon
reloadMethod-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 VariableDescriptionExampleReference

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 in127.0.0.1:5000/Environment Variables

SOAJS_ENV

environment code that this service will run inDEVEnvironment 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:

...

languagejs

...

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:

PropertyCode SnippetDescriptionReference
Tenant


Code Block
languagejs
titlereq.soajs.tenant
tenant : {
    id : '',
    code : ''

...


...

}


Represents the tenant that is making the call to the API.

Multitenancy

Key

Device

Geo


Code Block
languagejs
titlereq.soajs[ key | device | geo ]
key : {
    config : {},
    iKey : '',
    eKey : ''
},
device : 

...

'',
geo : {}


Represents the key configuration which belongs to the tenant making the call to the API.

Security

Services Config

Application


Code Block
languagejs
titlereq.soajs.application
application : {
    product : '',
    package : '',
    appId : '',
    acl : {},
    acl_all_env : {}

...


...

}


Represents the application information which belongs to the tenant making the call to the API.

Multitenancy

Access Levels

Package


Code Block
languagejs
titlereq.soajs.package
package :{
    acl :{},
    acl_all_env : {}

...


...

}


Represents the Productization that the tenant making the call to the API is using.

Productization

Access Levels

URAC


Code Block
languagejs
titlereq.soajs.urac
urac : {}

...


Represents the User whose access token was found in the request.URAC
Awareness


Code Block
languagejs
titlereq.soajs.awareness
awareness : {
    host : '',
    port : '',
    getHost : 'function'

...

}

...


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
js
languagexml
titleSOAJS Java Middleware as POM.xml
linenumberstrue
<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
js
languagexml
titleInitialize SOAJS Java Middleware
linenumberstrue
<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
js
languagejava
titleExample of Using the SOAJS Java Middleware
linenumberstrue
(@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
languagejsjava
themeMidnight
titleJava Web App & SOAJS Java Middleware
linenumberstrue
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;
    }
}

...