Custom protocol handler sample code

The following sample code can be used as a guide when developing a custom protocol handler.

/**
 * This package hierarchy is used to write the Protocol Handler.
 * [ProtocolName] should be substituted with the name of the protocol
 * for which the handler is being written.
 * For example com.crossworlds.connectors.utils.ProtocolHandlers.ftp 
   or com.crossworlds.connectors.utils.ProtocolHandlers.http
 */
package com.crossworlds.connectors.utils.ProtocolHandlers.[ProtocolName];


import CxCommon.BusinessObjectInterface;
import com.crossworlds.connectors.utils.ProtocolHandlers.CWURLConnection;
import com.crossworlds.DataHandlers.DataHandler;

import AppSide_Connector.JavaConnectorUtil;

import java.net.*;
import java.io.*;

/**
 * The handler class creates a ProtocolNameConnection class instance
 * It is invoked indirectly via Java's URL getContent() mechanism.
 *
 * how to use it:
 * System.setProperty ("java.protocol.handler.pkgs",
 *"com.crossworlds.ProtocolHandler");
 *URL url = new URL ("the URL");
 *CWURLConnection uc  = (CWURLConnection) url.openConnection ();
* /
public class Handler
{
   // this will return the appropriate URLConnection
   // But the constructor takes only one argument - the URL.  As this
   // is called by Java Networking Framework.
   public URLConnection openConnection(URL url) throws IOException
   {
      // you can pass in any parameters here.
        return new MyURLConnection (url);
   }
}

class MyURLConnection extends CWURLConnection {

   /**
    * This is instantiated by URL.openConnection()
    */
   public MyURLConnection(URL url)
   {
      // store this URL some where
   }

   /**
    * This method returns the content type of the data
    */
   public String getContentType()
   {
      // here is where you have to determine the content Type (aka
      // Mimetype) of URL streams
   }

   /**
    *  This method is used to create a connection
    */
   public synchronized void connect() throws IOException
   {
      // you might call super().connect as it suffices most of the
      // time.
      // If it is custom protocol, do the handshaking stuff here
   }

   /**
    * getContent () : The getContent method used by CrossWorlds.
    * This method takes in 5 parameters 
    *   - input Object,
    *   - content type for the data & 
    *   - Business Object Prefix to * be used to create the Business
    *   Object name 
    *       - Long ServerTimeout
    *   - Credentials
    *   It returns an appropriate Object back to the caller.  This
    *   method interacts with the DataHandler using the exposed APIs
    *   for the DataHandler.
    */
   public Object getContent(Object input,  String mimeType,
 String BOprefix, Long ServerTimeout, String Credentials)
                                throws IOException
   {
      // log a message
      JavaConnectorUtil.logMessage 
      ("logging a message", JavaConnectorUtil.XRD_INFO); 

      // write a trace
      if (JavaConnectorUtil.isTraceEnabled (JavaConnectorUtil.LEVEL3))
         JavaConnectorUtil.traceWrite  (JavaConnectorUtil.LEVEL3, 
         "Level 3 trace msg"); 

      
      // get a datahandler
      DataHandler dh = DataHandler.createHandler (null, mimeType, BOprefix);

      InputStream in = dh.getStreamFromBO 
      ( (BusinessObjectInterface) input, null);

      // Send this to URL
         - read data from Input Stream
         - write to URL
         - repeat until input stream is drained.

      // Now read the response
      String replyString = // some how read the reply from URL
      String outputType = // get the mime of reply some how

// remember to get a fresh DH, as the incoming data may be of
// different mime type than was originally received by the
// protocol handler 
      DataHandler dh2 = DataHandler.createHandler
      (null, outputType, BOprefix);

      BusinessObjectInterface replyBO = dh2.getBO 
      (replyString, outputType);

      return replyBO;  // DONE!

   }

}

Copyright IBM Corporation 1997, 2004. All Rights Reserved.