Network Deployment (Distributed operating systems), v8.0 > Reference > Developer best practices
Transcoding between message formats
A mediation can convert a message from one format to another without changing the semantic meaning of the message. This operation is referred to as transcoding a message.
Example
The following code is an example mediation handler that transcodes a message into a new message format, providing that the message can be transcoded:private static final String NEW_FORMAT = "JMS:text"; public boolean(MessageContext context) throws MessageContextException { try { SIMessageContext msgCtx = (SIMessageContext)context; SIMessage msg = msgCtx.getMessage(); DataGraph newDg = msg.getNewDataGraph(NEW_FORMAT); msg.setDataGraph(newDg,NEW_FORMAT); return true; } catch(Exception e) { // Reroute the original message to the exception destination MessageContextException mce = new MessageContextException("Unable to transcode to "+NEW_FORMAT",e); throw mce; } }The table below describes which messages can be transcoded, and gives the outcome for each format pairing. Note that the abbreviation DG represents "data graph". The numbers within brackets in the table are explained as follows:
- (1) A message with format JMS: cannot have a payload. It does not carry any message data other than the message properties. If a mediation calls getDataGraph() on a message with format JMS:, null is always returned. All other message formats must have a message payload. This means that a message with format JMS: cannot be transcoded into another format. If a mediation needs to change a message with format JMS: into a message with any other format, the mediation needs to call the methods SIDataGraphFactory.getInstance().createDataGraph(newFormat) and setDataGraph on the SIMessage object to change the message contents.
- (2) null is always returned if a mediation calls getDataGraph() on a message with format JMS:
- (3) A mediation can call the method getNewDataGraph() on a message to return a copy datagraph with the same format as the message. The copy can be edited, leaving the original message unchanged. For SOAP and Beans, you can change the message model by editing the format string to change the value that follows the ":".
Message transcoding. The table contains the different formats that can be used to convert the messages from one format to another. The table shows the transcoding options that is possible between the different formats, and it also provides the effect the conversion has on the messages.
To JMS: To JMS:text To JMS:bytes To JMS:stream To JMS:object To SOAP: To Bean: From JMS: DG=null (1) DG=null (1) DG=null (1) DG=null (1) DG=null (1) DG=null (1) DG=null (1) From JMS:text DG=null (2) Yes (3) Yes, bytes contain UTF-8 Yes, if text contains XML that conforms to the correct schema. No Yes, if message content is valid SOAP. Yes, if message content is valid SOAP. From JMS:bytes DG=null (2) Yes, but only when the bytes can correctly be interpreted as a UTF-8 string. Yes (3) Yes, if bytes contain XML that conforms to the correct schema. Yes, assume that bytes are a serialized object. Yes, if message content is valid SOAP. Yes, if message content is valid SOAP. From JMS:stream DG=null (2) Yes, text is XML transcoding. Yes, bytes contain XML transcoding. Yes (3) No No No From JMS:object DG=null (2) No Yes, bytes contain the object serialization. No Yes (3) No No From SOAP: DG=null (2) Yes Yes No No Yes (3) - if message content matches the new WSDL. Yes From Bean: DG=null (2) Yes Yes No No Yes Yes (3) - if message content matches the new WSDL.
Related
XML schema definition for JMS stream messages
Transcoding a byte array into a message payload
Transcoding a message payload into a byte array