Enterprise IT Consultant Views on Technologies and Trends

Mar 28 2011   3:48AM GMT

Advanced Mashup Scripting in EMML – Part II

Sasirekha R Profile: Sasirekha R

EMML Advanced Mashup Techniques – Part II

In this blog, listing some more techniques that enable us to create advanced Mashups using EMML.

Adding HTTP Headers to the Mashup Result

HTTP headers can be sent with a Mashup result, when the mashup is invoked. Add <variable> declarations for HTTP header using reserved names in the form httpResponse.header-name. The header-name portion can be any standard HTTP header or a custom header (that must begin with X-). For example:

  <variable name=”httpResponse.Content-Type” type=”string” default=”text/csv”/>
  <variable name=”httpResponse.Last-Modified” type=”date”/>
  <variable name=”httpResponse.X-CUSTOM-HEADER” type=”string”>
<assign outputvariable=”$X-CUSTOM-HEADER” fromexpr=”……”/>

Setting the Mashup Response Character Encoding

If the response is generally going to be returned in an XML format, the XML declaration <?xml version=”1.0″?> in the response also has an optional encoding property. Currently, the XML declaration encoding property directly in EMML cannot be set directly. Instead, a custom XPath function as shown below needs to be used.

package com.mycompany.mashups;
import org.oma.emml.client.EMMLUserFunction;
public class CustomFunctions extends EMMLUserFunction {
  public static String ChangeDocEncoding(Document doc, String encoding) throws Exception {
    DOMSource domSrc = new DOMSource(doc.getDocumentElement());
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    t.setOutputProperty(OutputKeys.ENCODING, encoding);
    t.transform(domSrc, result);
    return writer.toString();

Opening and Using XML Files in Mashups

Using XMLConstructorFromFile macro, we can construct variables in a mashup from XML content in files. To use XML files as source data for mashup scripts, the file must be deployed in the EMML Engine classpath.

The optional encoding attribute in the XML declaration (<?xml ?>) at the beginning of an XML file identifies the character encoding that is used to read the file. If this attribute is not specified, the XML parser may deduce the character encoding (or) read the file assuming UTF-8 as the character encoding.

    <variable name=”xmlConfigData” type=”document”/>

Formatting Results as CSV

Instead of the typical XML format, we can format the mashup results as CSV (comma-separated values), using the FormatAsCSV macro (installed with the EMML Reference Runtime Engine).

  <macro:FormatAsCSV xmldoc=”$sampleXML” outputvariable=”$csvString”/>

For this macro to work correctly, the structure of the input document must contain at least one node that repeats. The element names of all children nodes in the first ‘record’ (first node in the repeating set) are returned as field headers in the CSV output.

Ideally, repeating nodes should contain a single level of children with simple data that is converted to the CSV value. If a child of a repeating node also has descendants, then the CSV value for that child appends the values from all its descendants to the value of that child.

Wrapping POJO Classes with Mashups

In EMML, POJO classes can be used as services by wrapping them in mashup scripts. Include the POJO classes in the EMML Reference Runtime Engine classpath.

Create a separate wrapper mashup script for each POJO method that you want to expose as a service.

In the wrapper mashup script, add <input> declarations to accept input parameters for the Java method and an <output> declaration to receive the method return. Add a <script> statement to invoke the Java code.  An example is given below:

<script type=”text/javascript”>
  var list = new Packages.java.util.ArrayList();
  var listLength = list.size();
  print listLength;

 Pre-Processing/Post-Processing Services with Mashups

Typically decoding encrypted data, encrypting data, decompressing or compressing data, filtering data or data scrubbing is done as part of pre- or post-processing. This can be handled by mashup that uses scripting to invoke pre- or post-processing code.

To implement pre- or post-processing, you must:

1. Write the logic to handle pre-processing / post-processing as a Java class or use Java libraries, as needed.

2. Add the custom Java classes to the classpath for the EMML Reference Runtime Engine.

3. Restart the EMML Engine.

Sample mashup script for pre-processing is given below:

Declare <input> variables to receive the data to be pre-processed and an <output> variable for the result.

<operation name=”getreviews”>
  <input name=”userId” type=”string” default=”someone@myOrg.com” />
  <input name=”accessId” type=”string”
  <output name=”result” type=”string”/>


Add a <script> statement to call the pre-processing logic. In this example, an encryption class that decrypts the accessed input parameter.

  <output name=”result” type=”string” inputvariables=”$accessId”
  <script type=”text/javascript”>
      var encrypter = new
        Packages.com.myOrg.services.Encrypter(“My Pass Phrase!”)
      accessId = encrypter.decrypt(accessId);


Invoke the service with the pre-processed data.


For detailed information refer to http://www.openmashup.org/omadocs/v1.0/emml/advMashupIntro.html.

 Comment on this Post

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: