Manipulating messages in the MIME domain

This topic explains how to deal with messages that belong to the MIME domain, and that are parsed by the MIME parser. Use this information in conjunction with the information in Manipulating message body content.

A MIME message does not have to be received over a particular transport. For example a message can be received over HTTP using an HTTPInput node, or over WebSphere MQ using an MQInput node. The MIME parser is used to process a message if one of the following conditions applies:

The logical tree can be manipulated using ESQL before the message is passed on to other nodes in the message flow. A message flow can also create a MIME domain tree using ESQL. When a MIME domain message reaches an output node, the MIME parser is called to rebuild the bit stream from the logical tree.

The following examples show how to manipulate MIME messages:

Creating a new MIME tree

A message flow often receives, modifies and returns a MIME message. In this case you can work with the valid MIME tree created when the input message is parsed. If a message flow receives input from another domain, such as XML, and returns a MIME message you need to create a valid MIME tree. Use the following ESQL in a Compute node to create the top-level structure for a single-part MIME tree:
CREATE FIELD OutputRoot.MIME TYPE Name;
DECLARE M REFERENCE TO OutputRoot.MIME;
CREATE LASTCHILD OF M TYPE Name NAME 'Data';
The message flow also needs to ensure that the MIME Content-Type is set correctly, as explained in Managing Content-Type. The flow then needs to add the message data into the MIME tree. The following ESQL gives examples of how this can be done. Note that in each case Data is created with the domain BLOB.
  • A bit stream from another part of the tree is used. The following example shows how a bit stream could be created from an XML message that the message flow received. The flow then invokes the BLOB parser to store the data under the Data element.
     DECLARE partData BLOB ASBITSTREAM(InputRoot.XML);
     CREATE LASTCHILD OF M.Data DOMAIN('BLOB') PARSE(partData);
  • Instead of parsing the bit stream, create the new structure then attach the data to it. The following ESQL is an example of how to do this:
    DECLARE partData BLOB ASBITSTREAM(InputRoot.XML);
    CREATE LASTCHILD OF M.Data DOMAIN('BLOB') NAME 'BLOB';
    CREATE LASTCHILD OF M.Data.BLOB NAME 'BLOB' VALUE partData;

Both of these approaches create the same tree structure. The first approach is recommended because explicit knowledge of the tree structure that the BLOB parser requires is not built into the flow.

More commonly, the Compute node needs to build a tree for a multipart MIME document. The following ESQL is an example of how you can do this, including setting the top-level Content-Type via the ContentType property:
DECLARE part1Data BLOB ASBITSTREAM(InputRoot.XML.part1);
DECLARE part2Data BLOB ASBITSTREAM(InputRoot.XML.part2);

SET OutputRoot.Properties.ContentType = 'multipart/related; boundary=myBoundary';

CREATE FIELD OutputRoot.MIME TYPE Name;
DECLARE M REFERENCE TO OutputRoot.MIME;
CREATE LASTCHILD OF M TYPE Name NAME 'Parts';
CREATE LASTCHILD OF M.Parts TYPE Name NAME 'Part'; 
DECLARE P1 REFERENCE TO M.Parts.Part[1];  
CREATE FIELD P1."Content-Type" TYPE NameValue VALUE 'text/plain';
CREATE FIELD P1."Content-Id"   TYPE NameValue VALUE 'part one'; 
CREATE LASTCHILD OF P1 TYPE Name NAME 'Data'; 
CREATE LASTCHILD OF P1.Data DOMAIN('BLOB') PARSE(part1Data);

CREATE LASTCHILD OF M.Parts TYPE Name NAME 'Part'; 
DECLARE P2 REFERENCE TO M.Parts.Part[2];  
CREATE FIELD P2."Content-Type" TYPE NameValue VALUE 'text/plain';
CREATE FIELD P2."Content-Id"   TYPE NameValue VALUE 'part two'; 
CREATE LASTCHILD OF P2 TYPE Name NAME 'Data'; 
CREATE LASTCHILD OF P2.Data DOMAIN('BLOB') PARSE(part2Data);

Modifying an existing MIME tree

This example ESQL adds a new MIME part to an existing multipart MIME message. If the message is not multipart it is not modified:
SET OutputRoot = InputRoot;

-- Check to see if the MIME message is multipart or not.
IF LOWER(InputProperties.ContentType) LIKE 'multipart/%'
THEN
  CREATE LASTCHILD OF OutputRoot.MIME.Parts NAME 'Part';

  DECLARE P REFERENCE TO OutputRoot.MIME.Parts.[<];
  CREATE FIELD P."Content-Type" TYPE NameValue VALUE 'text/xml';
  CREATE FIELD P."Content-ID"   TYPE NameValue VALUE 'new part';
  CREATE LASTCHILD OF P TYPE Name NAME 'Data';

  -- This is an artificial way of creating some BLOB data. 
  DECLARE newBlob BLOB '4f6e652074776f2074687265650d0a';
  CREATE LASTCHILD OF P.Data DOMAIN('BLOB') PARSE(newBlob);
END IF;

Managing Content-Type

When you create a new MIME message tree, or when you modify the value of the MIME boundary string, you must make sure that the MIME Content-Type header is set correctly. Set the ContentType value in the broker Properties subtree to do this. The following example shows the ContentType value being set for a MIME part with simple content:
SET OutputRoot.Properties.ContentType = 'text/plain';
Do not set the Content-Type value directly in the MIME tree or HTTP trees. This can lead to the value being ignored or used inconsistently.
Related concepts
MIME parser and domain
Message flows overview
ESQL overview
Message modeling
Related tasks
Manipulating message body content
Designing a message flow
Defining message flow content
Managing ESQL files
Related reference
MIME standard header fields
Compute node
Database node
Filter node