JSON Integration Adapter

From CDOT Wiki
Revision as of 10:59, 23 June 2011 by Greg.fenton (talk | contribs) (Formatting and minor update of JSONMessagePartMapping (it is optional on a message part).)
Jump to: navigation, search


Project Repository

https://bitbucket.org/b_lim/nexj-express-json-integration-adapter/overview

Project Goal

To create an adapter for the NexJ Core similar in methodology and design of existing message adapters (XML, Fixed, CSV etc.) but using the JSON format

Contributors

Current Status

Currently at Phase II and beginning Phase III.

Project Phases

JSON Integration Adapter Phases

Phase I. Research (DONE)

  1. Complete Fundamentals of NexJ Studio tutorial
  2. Complete NexJ Integration tutorial
  3. Install NexJ Studio Express from source

Phase II. Planning and Approval (DONE)

  1. Receive general approval for project
  2. Receive approval for JSON encoding options
  3. Receive approval for JSON formatting options

Phase III. Message Formatter (80% DONE)

  1. Add JSON to .XSD base types
  2. Create sample JSON .message
  3. Parse sample JSON .message with JUnit Test
  4. Create first set of unit tests for Message Formatter
  5. Code Message Formatter first draft
  6. Resolve issues with first set of unit tests
  7. Create second set of unit tests
  8. Resolve issues with second set of unit tests
  9. Refactor and code message formatter

Phase IV. Message Parser

  1. Discover how to connect JSON RPC parser with Message Parser
  2. Create first set of trivial unit tests for Message Parser
  3. Code Message Parser skeleton
  4. Pass first set of trivial unit tests
  5. Create second set of unit tests (non-trivial) for Message Parser
  6. Code Message Formatter first draft
  7. Resolve issues with second set of unit tests
  8. Refactor

Phase V. Integration and Stress Tests

  1. Test Message Formatter and Message Parser with integration and stress tests
  2. Test through NexJ Studio Express GUI (manual testing)

Phase VI. Internal Code Review

  1. Internally review code at CDOT

Phase VII. Optimization

  1. Find more code optimizations relevant to Enterprise environments

Phase VIII. First Code Review at NexJ

  1. Date TBA

Phase IX. Code Rewrite

  1. Rewrite code to conform with NexJ suggestions and standards

Phase X. Second Code Review at NexJ

  1. Date TBA

Phase XI. Project Completion

Project Repository

BitBucket : https://bitbucket.org/b_lim/nexj-express-json-integration-adapter/

Technical Notes

Message Details

Message Fundamentals

Message - Messages can contain values or other messages. The red nodes are messages, the green nodes are values.

NexJ Express Messages

Internally, messages are Transfer Objects. To determine if a node is a message, use instanceof on CompositeMessagePartInstance after retrieving the MessagePart. For example,

public void format(TransferObject tobj, Message message, Output out) throws IntegrationException
{
...
CompositeMessagePart root = message.getRoot(); // Gets the root of the message.
Iterator it = root.getPartIterator();
while (it.hasNext())
{
  part = (MessagePart)it.next());
  if (part instanceof CompositeMessagePartInstance)
  {
    // This part is a message
  }
  else if (part instanceof PrimitiveMessagePart)
  {
    // This part is a value
  }
...
}
...

NOTE: THE MESSAGE PARAMETER DOES NOT CONTAIN THE MESSAGE VALUES. THE MESSAGE PARAMETER CONTAINS THE MESSAGE METADATA. THE TRANSFER OBJECT CONTAINS THE MESSAGE VALUES WHICH MUST BE VALIDATED AGAINST THE MESSAGE TO ENSURE CORRECT FORMAT.

MessagePart
Parts of a message. Messages can contain values or other messages. NexJ Express has two types of message parts, CompositeMessagePart and PrimitiveMessagePart.
CompositeMessagePart
implementation is CompositeMessagePartInstance . The relationship between CompositeMessagePartInstance and PrimitiveMessagePart with the above picture is as follows - CompositeMessagePartInstance are messages (the red nodes) and PrimitiveMessagePart are values (the green nodes). To determine multiplicity of MessageParts, use isCollection() method of MessagePart. Note that multiplicity of the above screenshot are all [0..1], that is zero or one instance. Possible node multiplicities:
[0..1] 
zero or one instance (i.e. an optional entry)
[1..1] 
exactly one instance (i.e. required)
[1..N] 
collection of at least one, at most N instances
[0..0] 
collection of zero to infinite instances (displayed as [*])
[1..0] 
collection of at least one, possibly unlimited instances
XMLJSONMessageMappingLoader
Used by the framework to autoload JSONMessagePartMapping for each of the message parts.
JSONMessagePartMapping
Each node in the above picture may has a corresponding JSONMessagePartMapping. Each node may have its own mapping, with its own values initialized in XMLJSONMessageMappingLoader. In order to get the mapping, first cast MessagePart to a concrete class such as CompositeMessagePartInstance or PrimitiveMessagePart, then use part.getMapping(). The purpose of the mapping is metadata for each node.
JSONMessageFormatter
Used to turn messages into JSON format.
JSONMessageParser
Used to turn JSON into a message.

Algorithm

format(TransferObject tobj, Message message, Output out) Algorithm for format method is as follows

  1. Retrieve the root of the message
  2. Iterate through each of the parts of the root
    1. If the part is a CompositeMessagePartInstance, it is a message node
    2. If the part is a PrimitiveMessagePart, it is a value node
    3. Validate the part against the MessagePartMapping. Different types of validation must be done if the part is a Composite versus if it is a Primitive.
    4. If the part is a CompositeMessagePartInstance, recursively call the format method with TransferObject set to the part. Suggested to overload the format method to format(TransferObject tobj, MessagePart message, Output out) and pass in the part, since retrieving a message root with getRoot() will always get the highest root of the message but what you want is the parent.
    5. If the part is a PrimitiveMessagePart, write the message part to the output.

Resources

JSON RFC : http://www.ietf.org/rfc/rfc4627.txt
Introduction to NexJ Studio Express : https://www.projects.openhealthtools.org/sf/go/doc1771?nav=1
NexJ Developer's Guide
NexJ Integration Fundamentals
Open Health Tools Integration Platform https://www.projects.openhealthtools.org/sf/projects/oht_aip/