Example Sender and Sender Handlers Implementation Outlines

The following code and pseudo code outline example implementations for senders and sender handlers.

Example Sender

An outline of a sender handler implementation. Protocol specific code should be added by the developer.

public class SampleSenderUserExit implements SenderInterface
{
        SenderResult result = new SenderResult ();
        Connection connection = null;
        public SampleSenderUserExit()
        {
             ...
    }
        public void init(Context context, Config deliveryConfig) 
                 throws BCGSenderException
        {
                //initialization code
           ...
    }
        public SenderResult send(BusinessDocumentInterface document) 
                  throws BCGSenderException
        {
               //destination url set in the BusinessDocumentInterface 
               //is preferred over url set in the Gateway
               URL url = (String)document.getAttribute(DocumentConstant.URI);
                if (url == null)
                  url = (String)config.getConfig(DocumentConstant.URI);
                //obtain other configuration information from the 
                //DeliveryConfig which holds the configuration information
                //set in the Gateway
                File documentFile = document.getDocument();
                try
                {
                        //read contents from File
                    ...
            }
                catch(FileIOException exception)
                {
                        BCGSenderException e = new BCGSenderException(...);
                        throw e;
            }
                try
                {
                        //establish connection with the destination
                    ...
                        //send contents to destination
                    ...
                        //if DocumentConst.GET_SYNC_RESPONSE is true, 
                        //read the response
                        if (String.valueOf(document.getAttribute(
                         DocumentConst.GET_SYNC_RESPONSE).
                                 equalsIgnoreCase(DocumentConstants.TRUE))
                        {
                           //read the response
                       ...
                String uuid = BCGUtil.getDocumentUUID();
                String tmpDir = BCGUtil.TMP_DIR_PATH;
                File responseFile = document.createFile(uuid+".resp");
                //write response to file
                         ...
 
                result.setResponse(responseFile);
                         ...
                      }
                        else
                        {
                              //read the transport response and 
                              //set in the SenderResult object.
                              ...
                      }
              }
                catch(Exception exception)
                {
                        //create an event and add to the SenderResult 
                        Object[] params = {exception};
                        EventInfo eventInfo = EventInfo(eventCode, 
                                                          document, params);
                        result.addEvent(eventInfo);
                result.setStatus(DocumentConstant.BCG_DOC_FAILED);
                        return result;
              }
                //close the connection
              ...
           result.setStatus(DocumentConstant.BCG_DOC_SENT); 
                return result;
      }
 
        public SenderResult cancel() throws BCGSenderException
        {
                    connection.close();
                    EventInfo eventInfo = EventInfo(eventCode, 
                                                       document, params);
                    result.setEvent(eventInfo);
                    return result;
      }
}

Example Pre-processing handler

The following code and pseudo code outlines an example implementation for a sender pre-processing handler. If the toPartner is a preferred customer, it sets some transport attributes in the BusinessDocument accordingly.

public class SampleSenderPreProcessUserExit implements
SenderPreProcessHandlerInterface
{
    public SampleSenderPreProcessUserExit ()
    {
        ...
    }
    public void init(Config handlerConfig) throws BCGSenderException
    {
        //initialization code
        ...
    }
    public boolean applies(Config deliveryConfig, 
                                 BusinessDocumentInterface doc)
                    throws BCGSenderException
    {
        String toProtocol =
              document.getAttribute(DocumentConstants.TOPROTOCOLNAME);
        boolean isAS2 = (toProtocol!= null &&
                     toProtocol.equalsIgnoreCase(DocumentConstants.AS2));
        return isAS2;
    } 
    public BusinessDocumentInterface process (Config deliveryConfig,
                                   BusinessDocumentInterface doc)
                     throws BCGSenderException
    {
        String toPartnerId = (String)doc.getAttribute
                                   (DocumentConstants.TOBUSINESSID);
        //check if partner is a PreferredCustomer
        ...
        if (preferredCustomer) 
        {
            //set transport attributes for PreferredCustomer 
            doc.setAttribute(PRIORITY, "HIGH");
doc.setAttribute(ACKNOWLEDGEMENT_REQUIRED, "true");
            ...
        } 
    }
}

Example post-processing handler

The following code and pseudo code outlines an example implementation for a sender post-processing handler. This example assumes a SOAP message. If the response is an exception or other type of error, but not a SOAP fault, the handler creates a SOAP fault and sets the response in the BusinessDocument.

public class SampleSOAPPostProcessUserExit implements
                                       SenderPostProcessHandlerInterface
{
    public void init(Config handlerConfig) 
             throws BCGSenderException
    {
        //initialization code
        ...
    }
    public SampleSOAPPostProcessUserExit ()
    {
        ...
    }
    public boolean applies(Config deliveryConfig, 
                                   BusinessDocumentInterface doc)
               throws BCGSenderException
    {
        String fromProtocol =
                 (String)document.getAttribute(
                                     DocumentConst.FRPROTOCOLNAME);
        boolean isSoap = (fromProtocol != null &&
        fromProtocol.equalsIgnoreCase(
                                     DocumentConst.WEB_SERVICE_PROTOCOL));
        return isSoap;
    }
 
    public SenderResult process (SenderResult response,
                                 BusinessDocumentInterface doc) 
                    throws BCGSenderException
    {
        SoapFault sf = null;
        String deliveryStatus= response. getDeliveryStatus();
        If (deliveryStatus.equalsIgnoreCase("FAILED")){
            //Get transport header, read the http status code, 
//    and create SOAP fault message
            String faultMessage="Server error";
            sf = new SoapFault(faultString);
            response.setResponse(sf.getBytes());
        }
        return response; 
    }
}

Copyright IBM Corp. 2003, 2004