Sample HTML data handler

In the sample, the HTML data handler converts the incoming HTML query string into an IBM WebSphere Business Integration Server Express and Express Plus business object. For more on the IBM WebSphere data handler capability, see the IBM WebSphere Business Integration Server Express and Express Plus Data Handler Guide. These are among the noteworthy features of the data handler component:

Figure 10 shows the HTML page as it might appear on a client browser. The HTML data handler relies on the properties associated with text boxes on the page.

Figure 10. The HTML sales quote inquiry page


In Figure 10, each text box has an HTML property associated with it. The HTML text box property contains IBM WebSphere Business Integration Server Express and Express Plus business object grammar. This grammar enables the HTML data handler to convert the data associated with the property into a business object.

For example, the properties associated with the first item are the following:

As shown in Figure 11, the data handler converts the data on the HTML page to a hierarchical SalesQuote business object with child (orderQty, deliveryDate, and so on) business objects.

Figure 11. Hierarchical parent-child business objects


Data-handler meta-object

IBM WebSphere Business Integration Server Express and Express Plus software delivers two top-level data-handler meta-objects, one for the server and one for connectors. In addition, there is a child meta-object for each data handler, several of which are delivered with IBM WebSphere Business Integration Server Express and Express Plus software. When you configure your environment, you can:

You define an attribute in the top-level meta-object for the MIME type and any subtype (BOPrefix) you want to support. This attribute represents a child meta-object, which has attributes to provide the class name and configuration properties required by the data handler to do its work.

Figure 12 shows the text format of two meta-objects:

Sample code for HTML data handler

Here is the HTML data handler Java code sample.

/**
 * @(#) HtmlDataHandler.java
 *
 * Copyright (c) 1997-2000 CrossWorlds Software, Inc.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of IBM, Inc.
 * You shall not disclose such Confidential information and shall
 * use it only in accordance with the terms of the license agreement you entered into
 * with CrossWorlds Software.
*/
import com.crossworlds.DataHandlers.*;
import com.crossworlds.DataHandlers.Exceptions.*;
import AppSide_Connector.JavaConnectorUtil;
 
import CxCommon.BusinessObjectInterface;
 
// java classes
import java.util.*;
import java.io.*;
/**
 ** This is a html data handler which converts a html query 
 ** string to a Crossworlds Business object. This example is 
 ** assumes the incoming html query is structured in a specific
 ** format as explained in the program below. See the comments 
 ** associated with the method parse() in this class. 
 */
public class HtmlDataHandler extends DataHandler
{
 
     /*
      **  A utility method to convert a HTML query string into a crossworlds BO.
      **  See comments associated with the parse() method for a detailed explanation
      **  @param String serializeddata 
      **  @param Object the incoming mime type
      */
     public BusinessObjectInterface getBO(String serializedData, 
       Object config)
          throws Exception
     {
 
          HashMap nameValuePairs = parse((String) serializedData);
 
          /*
           ** Get the BO to be created from the hidden tag BusObjName
           */
          String boName = (String) nameValuePairs.get("BusObjName");
          if (boName == null)
               throw new Exception("Unable to find business object name in " 
                    + "serialized business object");
 
          BusinessObjectInterface bo = JavaConnectorUtil.createBusinessObject(boName);
          String verb = (String) nameValuePairs.get("Verb");
          if (verb == null)
               throw new Exception("Unable to find verb in serialized business object");
 
          bo.setVerb(verb);
 
          /*
           ** Get the elements from the HashMap and set it into the BO
           */
          setValues(bo, nameValuePairs);
          return bo;
     }
 
     /*
     **  Parse an HTML query string looking for tokens of the form &name=value.
     **  The format of the incoming query string must conform to the &name=value
     **  format as well as the following semantics:
     **        if name does not contain syntax of the form name[X].attribute it is
     **        assumed to be the name of an attribute in the parent object otherwise
     **        the expression will be used AS IS to set the value of a child object
     **        and attribute.
     **
     **  For example, the following query string can be successfully parsed by
     **  this method:
     **
     ** CustomerID=&items[0].itemID=44&items[0].orderQty=25&items[0].
     **          deliveryDate1=12/12/00
     **  &items[1].itemID=67&items[1].orderQty=2&items[1].
     ** deliveryDate=12/12/00&Verb=Retrieve&
     **  BusObjName=SalesQuote&SubObjName=CwItem
     **
     ** 
     **  @param String query sent from the webserver to be parsed
     **  @return HashMap a hash map containing the name value pairs
     */
 
     private HashMap parse(String queryString)
     {
          HashMap nameValuePairs = new HashMap();
          String content = queryString.replace('+', ' ');
          StringTokenizer st = new StringTokenizer(content,"&");
 
          while (st.hasMoreTokens())
          {
               String token = st.nextToken();
               int i = token.indexOf("=");
               String name = token.substring(0, i);
               String value = token.substring(i+1);
 
               /*
                **  HTTP will encode certain ASCII values as their hex equivalents.
                ** Convert any of these encodings back to ASCII for both the name
                **  and the value strings (i.e. right hand side of = and left hand
                **  side of =)
                */
               name = replaceHexEncodedWithAscii(name);
               value = replaceHexEncodedWithAscii(value);
 
               /*
                **  Store these value in the hashmap so that our caller can look
                **  them up.
                */
               nameValuePairs.put(name, value);
          }
 
          return(nameValuePairs);
     }
 
     /*
      *  Given a Hashmap of name/value pairs, enumerate through the business
      *  object and set each attribute in the BO with the corresponding
      *  value from the Hashtable
      *  @param IBusinessObject target of the set
      *  @param Hashmap contains the name/value pairs
      */
     private void setValues(BusinessObjectInterface bo, HashMap nameValuePairs)
          throws Exception
     {
          String SubObjName = null;
          Iterator aIterator = nameValuePairs.keySet().iterator();
          // Save the SubObject name so we need to save it
          while (aIterator.hasNext())
          {
               String name = (String) aIterator.next();
               /*
                **  Ignore any hidden keywords that we parsed out of the HTML and
                **  stored in the hash map
                */
 
               if (name.equalsIgnoreCase("BusObjName")  ||
                         name.equalsIgnoreCase("Verb") ||
                         name.equalsIgnoreCase("SubObjName") ||
                         name.equalsIgnoreCase("ContainerAttrName"))
               {
                    System.out.println("Skipping Item : " + name);
                    continue;
               }
 
               /*
                **  All subobjects have a grammar in the form of object[X].attribute
                **  where X is the index of the contained subobject.  Therefore, if
                **  the name does not have this embedded string, it's an attribute
                **  of the parent object
                */
               if (name.indexOf("[") == -1)
                bo.setAttrValue(name, (String) nameValuePairs.get(name));
               else
                bo.setAttributeWithCreate(name, (String) nameValuePairs.get(name));
          }
     }
 
     /*
      *  Replace any hex encoded bytes with the ASCII char equivalent and return
      *  the new string to the caller.
      *  @param name The string to convert.
      */
     private String replaceHexEncodedWithAscii(String name)
     {
          int nameLength = name.length();
 
          /*
           **  Replace any hex values (HTTP may send over a hex value int
           **  the form of %XX for certain characters) with their
           **  corresponding char equivalents
           */
          StringBuffer nameBuffer = new StringBuffer();
          for (int i = 0; i < nameLength; ++i)
          {
               char c = name.charAt(i);
               switch (c)
               {
                    case '%':
                         byte[] b = { Byte.parseByte(name.substring(i+1, i+3),
                              16) };
                         nameBuffer.append(new String(b));
                         i += 2;
                         break;
                    default:
                         nameBuffer.append(c);
               }
          }
          return(nameBuffer.toString());
     }
 
     /**
      ** Implementation of abstract methods in the Data Handler class
      **  @param BusinessObjectInterface the actual business object
      **  @param Object config
      **  @return String string representation of the BO
      */
 
     public String getStringFromBO(BusinessObjectInterface theObj, Object config)
          throws Exception
     {
          throw new Exception("Not implemented");
     }
 
     /**
      ** Implementation of abstract methods in the Data Handler class
      *  @param Reader actual data
      *  @param BusinessObjectInterface the actual business object 
      *  @param Object config
      */
     public void getBO(Reader serializedData, BusinessObjectInterface theObj,
          Object config) 
          throws Exception
     {
          throw new Exception("Not Implemented");
     }
 
     /**
      ** Implementation of abstract methods in the Data Handler class
      *  @param String actual data
      *  @param BusinessObjectInterface the actual business object 
      *  @param Object config
      */
     public void getBO(String serializedData, BusinessObjectInterface theObj, 
          Object config) 
          throws Exception
     {
          throw new Exception("Not Implemented");
   }
 
     /**
      ** Implementation of abstract methods in the Data Handler class
      *  @param BusinessObjectInterface the actual business object 
      *  @return InputStream a handle to the stream
      */
     public InputStream getStreamFromBO(BusinessObjectInterface theObj,
          Object config)
          throws Exception
     {
          throw new Exception("Not Implemented");
     }
 
     /**
      ** Implementation of abstract methods in the Data Handler class
      *  @param Reader actual data
      *  @param BusinessObjectInterface the actual business object 
      *  @return BusinessObjectInterface the translated BO
      */
     public BusinessObjectInterface getBO(Reader serializedData, Object config)
          throws Exception
     {
          throw new Exception("Not Implemented");
     }
}

Copyright IBM Corp. 2004