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 this API is built using JAVA as a development language.

...

Code Block
languagebash
themeMidnight
titleSOAJS Java NPM Package
# install from npm
npm install soajs.java


2- Features

...

Once the middleware is initialized, the following functionality becomes available:

FunctionTypeInputOutputRemarks
getDatabasesMethoddbName (optional)
  • returns database object if dbName provided
  • returns all core and tenant meta databases otherwise
Mongo Client Driver
getServiceConfigMethod-returns service configuration objectServices Config
getDeployerMethod-returns deployer objectRegistry
getCustomMethod-returns custom objectServices Config
getResourcesMethodresourceName (optional)
  • returns resource object if resourceName provided
  • returns all resources otherwise
Registry
getServicesMethodserviceName (optional)
  • returns service object if serviceName provided
  • returns all services otherwise
Service
getDaemonsMethoddaemonName (optional)
  • returns daemon object 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

The Middleware requires the following environment variables to be set prior to starting your service

...

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

...

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 using a POM.xml file

Code Block
languagexml
titleSOAJS Java Middleware as POM.xml
linenumberstrue
<dependency>
    <groupId>soajs</groupId>
    <artifactId>soajs.java</artifactId>
    <version>1.0.0</version>
</dependency>

Then make sure to initialize it in your servlet tag under web.xml as follow:

Code Block
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 to access the data and function that it offers

Code Block
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.

...

Code Block
languagejava
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;

@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);

        }
		catch(Exception e){
            e.printStackTrace();
        }

        return Response.status(200).entity(soajs.toString()).build();
    }
}

...