Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

Objective


The JAVA Middleware provides the ability to modernize existing API with SOAJS when these APIs are built using JAVA as a development language.

The middleware exposes several functionality and configuration that SOAJS and which can be consumed by the Microservice 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.

Scenario


  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: https://github.com/soajs/soajs.java


// 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

<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:

<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 is initialized, the following list of functions will be available to use through 'req.soajs.reg'

FunctionInputOutputRemarks
getDatabases-

returns all core and tenant meta databases

Mongo Client Driver
getDatabasesdbName

returns database object for dbName provided

Mongo Client Driver
getServiceConfig-returns service configuration objectServices Config
getDeployer-returns deployer object
getCustom-returns custom objectServices Config
getResources-

returns all resources


getResourcesresourceName

returns resource object for resourceName provided


getServices-

returns all services

Service
getServicesserviceName

returns service object for serviceName provided

Service
getDaemons-

returns all daemons

Daemon
getDaemonsdaemonName

returns daemon object for daemonName provided

Daemon
reload-void (reload registry only)Registry

 

3-How does it work


On Init:

Using the following environment variables:

  • SOAJS_REGISTRY_API
  • SOAJS_ENV

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:


{
  tenant : {
    id : '',
    code : ''
  },
  key : {
    config : {},
    iKey : '',
    eKey : ''
  },
  application : {
    product : '',
    package : '',
    appId : '',
    acl : {},
    acl_all_env : {}
  },
  package :{
    acl :{},
    acl_all_env : {}
  },
  device : '',
  geo : {},
  urac : {},
  awareness : {
    host : '',
    port : '',
    getHost : 'function'
  }
}


Hence you can now use the set of functions appended to req.soajs.reg (shown in features)

4- Using the Middleware


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

Or through POM.xml

<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:

<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 follows


(@Context HttpHeaders headers)
  headers.getRequestHeader("soajs")


5- JAXRS Jersey Example



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 soajs.filters.SoajsRegistry;

import soajs.filters.SoajsRequestUtilities;

/**
 *
 * @author Etienne
 */
@Path("/hello")
public class Hello {

    @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+"]");
        
        return Response.status(200).entity(response.toString()).build();
    }
    
    @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;
    }
}


Full Example on: https://github.com/soajs/soajs.java.jaxrs_jersey




  • No labels