Objective
...
The JAVA Middleware provides the ability to modernize existing API with SOAJ when these APIs are built using JAVA as a development language.
...
Code Block | ||||
---|---|---|---|---|
| ||||
// install from npm npm install soajs.nodejsjava |
Add this library to your restful service as an external jar (soajs.java.jar)
...
Code Block | ||
---|---|---|
| ||
<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 is initialized, the following list of functions will be available to use through 'req.soajs.reg'
Function | Input | Output | Remarks |
---|---|---|---|
getDatabases | - | returns all core and tenant meta databases | Mongo Client Driver |
getDatabases | dbName | returns database object for dbName provided | Mongo Client Driver |
getServiceConfig | - | returns service configuration object | Services Config |
getDeployer | - | returns deployer object | |
getCustom | - | returns custom object | Services Config |
getResources | - | returns all resources | |
getResources | resourceName | returns resource object for resourceName provided | |
getServices | - | returns all services | Service |
getServices | serviceName | returns service object for serviceName provided | Service |
getDaemons | - | returns all daemons | Daemon |
getDaemons | daemonName | 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:
...
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.
...
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
Code Block | ||
---|---|---|
| ||
<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> |
Consequently, anywhere in your web application you can wire your context and get your soajs object, as follows
Code Block | ||
---|---|---|
| ||
(@Context HttpHeaders headers) headers.getRequestHeader("soajs") |
5- JAXRS Jersey Example
...
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 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; } } |
...