Code example: PreProcessUserExitHandler API

Adds an attachment, auxiliary business ID, retrieves original document, payload, transportation headers, and sets the messaging destination address, HTTP destination address, and payload.

Important: A compressed file with user exit code samples is available in the Members\resources\samples\userexits installation directory.

PreProcessAddAttachment.java

 /*
 * *****************************************************************************
 * 
 * 
 * 5725Q72
 * 
 * (C) Copyright IBM Corp. 2013, 2014
 * 
 * The source code for this program is not published or otherwise divested of its trade secrets, irrespective of what
 * has been deposited with the U.S. Copyright Office.
 * 
 * The sample contained herein is provided to you "AS IS".
 * 
 * It is furnished by IBM as a simple example and has not been thoroughly tested under all conditions. IBM, therefore,
 * cannot guarantee its reliability, serviceability or functionality.
 * 
 * This sample may include the names of individuals, companies, brands and products in order to illustrate concepts as
 * completely as possible. All of these names are fictitious and any similarity to the names and addresses used by
 * actual persons or business enterprises is entirely coincidental.
 * 
 * ******************************************************************************
 */
package com.ibm.b2b.test.comms.userexits.preprocess;

import java.io.ByteArrayInputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.lang.Validate;

import com.ibm.b2b.userexits.spi.BusinessDocument;
import com.ibm.b2b.userexits.spi.PreProcessExitException;
import com.ibm.b2b.userexits.spi.PreProcessUserExitHandler;

/**
 * This is a sample user exit to add an attachment during pre-processing. This attachment will undergo all operations of
 * security and compression since it is added before processing.
 * 
 * @author Praveen S Rao
 * 
 */
public class PreProcessAddAttachment implements PreProcessUserExitHandler {

    private static final String SOURCECLASS = PreProcessAddAttachment.class.getCanonicalName();

    private static final Logger LOGGER = Logger.getLogger( SOURCECLASS );

    /*
     * (non-Javadoc)
     * 
     * @see com.ibm.b2b.userexits.spi.PreProcessUserExitHandler#invoke(com.ibm.b2b.userexits.spi.BusinessDocument)
     */
    public BusinessDocument invoke( BusinessDocument bdo ) throws PreProcessExitException {

        Validate.notNull( bdo );

        final String sourceMethod = "invoke";

        if ( LOGGER.isLoggable( Level.FINER ) ) {
            LOGGER.entering( SOURCECLASS, sourceMethod, bdo.getTransactionID() );
        }

        try {

            LOGGER.logp( Level.INFO, SOURCECLASS, sourceMethod, "Exit properties [" + bdo.getExitProperties()
                + "]" );

            bdo.addAttachment( new ByteArrayInputStream( "<a>sample new attachment</a>".getBytes() ),
                "my-content-id", "text/xml", "orig-new-attachment.xml", "new description", -1, null );

        } catch ( Exception t ) {
            throw new PreProcessExitException( t );
        }

        if ( LOGGER.isLoggable( Level.FINER ) ) {
            LOGGER.exiting( SOURCECLASS, sourceMethod );
        }

        return bdo;
    }

}

               
            

PreProcessAddAuxiliaryBusID.java

 /*
 * *****************************************************************************
 * 
 * 
 * 5725Q72
 * 
 * (C) Copyright IBM Corp. 2013, 2014
 * 
 * The source code for this program is not published or otherwise divested of its trade secrets, irrespective of what
 * has been deposited with the U.S. Copyright Office.
 * 
 * The sample contained herein is provided to you "AS IS".
 * 
 * It is furnished by IBM as a simple example and has not been thoroughly tested under all conditions. IBM, therefore,
 * cannot guarantee its reliability, serviceability or functionality.
 * 
 * This sample may include the names of individuals, companies, brands and products in order to illustrate concepts as
 * completely as possible. All of these names are fictitious and any similarity to the names and addresses used by
 * actual persons or business enterprises is entirely coincidental.
 * 
 * ******************************************************************************
 */
package com.ibm.b2b.test.comms.userexits.preprocess;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.lang.Validate;

import com.ibm.b2b.userexits.spi.BusinessDocExitException;
import com.ibm.b2b.userexits.spi.BusinessDocument;
import com.ibm.b2b.userexits.spi.PreProcessExitException;
import com.ibm.b2b.userexits.spi.PreProcessUserExitHandler;

/**
 * This is a sample user exit to add auxiliary business IDs during pre-processing. The business IDs used will be
 * packaged in the outflow and will appear in the protocol message sent to the trading partner.
 * 
 * @author Praveen S Rao
 * 
 */
public class PreProcessAddAuxiliaryBusID implements PreProcessUserExitHandler {

    private static final String SOURCECLASS = PreProcessAddAuxiliaryBusID.class.getCanonicalName();

    private static final Logger LOGGER = Logger.getLogger( SOURCECLASS );

    /*
     * (non-Javadoc)
     * 
     * @see com.ibm.b2b.userexits.spi.PreProcessUserExitHandler#invoke(com.ibm.b2b.userexits.spi.BusinessDocument)
     */
    public BusinessDocument invoke( BusinessDocument bdo ) throws PreProcessExitException {

        Validate.notNull( bdo );

        final String sourceMethod = "invoke";

        if ( LOGGER.isLoggable( Level.FINER ) ) {
            LOGGER.entering( SOURCECLASS, sourceMethod, bdo.getTransactionID() );
        }

        try {
            bdo.setFromPartyId( "fromnewbusid", "http://duns" );
            bdo.setToPartyId( "tonewbusid", "http://freeform" );
        } catch ( BusinessDocExitException e ) {
            throw new PreProcessExitException( e );
        }

        if ( LOGGER.isLoggable( Level.FINER ) ) {
            LOGGER.exiting( SOURCECLASS, sourceMethod );
        }

        return bdo;
    }

}

            

PreProcessGetOriginalDocument.java

/*
 * *****************************************************************************
 * 
 * 
 * 5725Q72
 * 
 * (C) Copyright IBM Corp. 2013, 2014
 * 
 * The source code for this program is not published or otherwise divested of its trade secrets, irrespective of what
 * has been deposited with the U.S. Copyright Office.
 * 
 * The sample contained herein is provided to you "AS IS".
 * 
 * It is furnished by IBM as a simple example and has not been thoroughly tested under all conditions. IBM, therefore,
 * cannot guarantee its reliability, serviceability or functionality.
 * 
 * This sample may include the names of individuals, companies, brands and products in order to illustrate concepts as
 * completely as possible. All of these names are fictitious and any similarity to the names and addresses used by
 * actual persons or business enterprises is entirely coincidental.
 * 
 * ******************************************************************************
 */
package com.ibm.b2b.test.comms.userexits.preprocess;

import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.Validate;

import com.ibm.b2b.userexits.spi.BusinessDocument;
import com.ibm.b2b.userexits.spi.PreProcessExitException;
import com.ibm.b2b.userexits.spi.PreProcessUserExitHandler;

/**
 * This is a sample user exit to get the raw request during pre-processing. This is used in an inbound flow to get
 * access to the raw request put into storage before protocol processing. Care must be taken to close all streams after
 * use. This can be used for virus checks to scan the data before processing.
 * 
 * @author Praveen S Rao
 * 
 */
public class PreProcessGetOriginalDocument implements PreProcessUserExitHandler {

    private static final String SOURCECLASS = PreProcessGetOriginalDocument.class.getCanonicalName();

    private static final Logger LOGGER = Logger.getLogger( SOURCECLASS );

    /*
     * (non-Javadoc)
     * 
     * @see com.ibm.b2b.userexits.spi.PreProcessUserExitHandler#invoke(com.ibm.b2b.userexits.spi.BusinessDocument)
     */
    public BusinessDocument invoke( BusinessDocument bdo ) throws PreProcessExitException {

        Validate.notNull( bdo );

        final String sourceMethod = "invoke";

        if ( LOGGER.isLoggable( Level.FINER ) ) {
            LOGGER.entering( SOURCECLASS, sourceMethod, bdo.getTransactionID() );
        }

        InputStream is = null;
        try {

            is = bdo.getOriginalDocument();

            if ( is != null ) {
                LOGGER.logp( Level.INFO, SOURCECLASS, sourceMethod, "Original document [" + IOUtils.toString( is )
                    + "]" );
            } else {
                LOGGER.logp( Level.INFO, SOURCECLASS, sourceMethod, "Original document is NULL" );
            }

        } catch ( Exception t ) {
            throw new PreProcessExitException( t );
        } finally {
            if ( is != null ) {
                try {
                    is.close();
                } catch ( IOException e ) {
                    e.printStackTrace();
                }
            }
        }

        if ( LOGGER.isLoggable( Level.FINER ) ) {
            LOGGER.exiting( SOURCECLASS, sourceMethod );
        }

        return bdo;
    }

}

            

PreProcessGetPayload.java

/*
 * *****************************************************************************
 * 
 * 
 * 5725Q72
 * 
 * (C) Copyright IBM Corp. 2013, 2014
 * 
 * The source code for this program is not published or otherwise divested of its trade secrets, irrespective of what
 * has been deposited with the U.S. Copyright Office.
 * 
 * The sample contained herein is provided to you "AS IS".
 * 
 * It is furnished by IBM as a simple example and has not been thoroughly tested under all conditions. IBM, therefore,
 * cannot guarantee its reliability, serviceability or functionality.
 * 
 * This sample may include the names of individuals, companies, brands and products in order to illustrate concepts as
 * completely as possible. All of these names are fictitious and any similarity to the names and addresses used by
 * actual persons or business enterprises is entirely coincidental.
 * 
 * ******************************************************************************
 */
package com.ibm.b2b.test.comms.userexits.preprocess;

import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.Validate;

import com.ibm.b2b.userexits.spi.BusinessDocument;
import com.ibm.b2b.userexits.spi.Payload;
import com.ibm.b2b.userexits.spi.PreProcessExitException;
import com.ibm.b2b.userexits.spi.PreProcessUserExitHandler;

/**
 * This is a sample user exit to get payload data in pre-processing. Care must be taken to close the payload stream if
 * {@link Payload#getData()} api is accessed. In an outbound flow, this can be after receiving a BDO from the backend.
 * In an inbound flow, this can be used for virus checks to scan the data before processing.
 * 
 * @author Praveen S Rao
 * 
 */
public class PreProcessGetPayload implements PreProcessUserExitHandler {

    private static final String SOURCECLASS = PreProcessGetPayload.class.getCanonicalName();

    private static final Logger LOGGER = Logger.getLogger( SOURCECLASS );

    /*
     * (non-Javadoc)
     * 
     * @see com.ibm.b2b.userexits.spi.PreProcessUserExitHandler#invoke(com.ibm.b2b.userexits.spi.BusinessDocument)
     */
    public BusinessDocument invoke( BusinessDocument bdo ) throws PreProcessExitException {

        Validate.notNull( bdo );

        final String sourceMethod = "invoke";

        if ( LOGGER.isLoggable( Level.FINER ) ) {
            LOGGER.entering( SOURCECLASS, sourceMethod, bdo.getTransactionID() );
        }

        InputStream is = null;

        try {

            Payload payload = bdo.getPayload();

            if ( payload != null ) {
                LOGGER.logp( Level.INFO, SOURCECLASS, sourceMethod,
                    "Payload content length [" + payload.getContentLength() + "]" );
                LOGGER.logp( Level.INFO, SOURCECLASS, sourceMethod,
                    "Payload content type [" + payload.getContentType() + "]" );

                is = payload.getData();

                if ( is != null ) {
                    LOGGER.logp( Level.INFO, SOURCECLASS, sourceMethod, "Payload data [" + IOUtils.toString( is )
                        + "]" );
                }
            }

        } catch ( Exception t ) {
            throw new PreProcessExitException( t );
        } finally {
            try {
                if ( is != null ) {
                    is.close();
                }
            } catch ( IOException e ) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        if ( LOGGER.isLoggable( Level.FINER ) ) {
            LOGGER.exiting( SOURCECLASS, sourceMethod );
        }

        return bdo;
    }

}

            

PreProcessGetTransportHeaders.java

/*
 * *****************************************************************************
 * 
 * 
 * 5725Q72
 * 
 * (C) Copyright IBM Corp. 2013, 2014
 * 
 * The source code for this program is not published or otherwise divested of its trade secrets, irrespective of what
 * has been deposited with the U.S. Copyright Office.
 * 
 * The sample contained herein is provided to you "AS IS".
 * 
 * It is furnished by IBM as a simple example and has not been thoroughly tested under all conditions. IBM, therefore,
 * cannot guarantee its reliability, serviceability or functionality.
 * 
 * This sample may include the names of individuals, companies, brands and products in order to illustrate concepts as
 * completely as possible. All of these names are fictitious and any similarity to the names and addresses used by
 * actual persons or business enterprises is entirely coincidental.
 * 
 * ******************************************************************************
 */
package com.ibm.b2b.test.comms.userexits.preprocess;

import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.lang.Validate;

import com.ibm.b2b.userexits.spi.BusinessDocument;
import com.ibm.b2b.userexits.spi.PreProcessExitException;
import com.ibm.b2b.userexits.spi.PreProcessUserExitHandler;

/**
 * This is a sample user exit to get the transport headers in pre-processing. This can be used in an inbound flow after
 * the raw request is read over HTTP(S) and before handing over the data to protocol processing.
 * 
 * This is usually used when the partner wants to inspect any transport headers and do processing based on them.
 * 
 * @author Praveen S Rao
 * 
 */
public class PreProcessGetTransportHeaders implements PreProcessUserExitHandler {

    private static final String SOURCECLASS = PreProcessGetTransportHeaders.class.getCanonicalName();

    private static final Logger LOGGER = Logger.getLogger( SOURCECLASS );

    /*
     * (non-Javadoc)
     * 
     * @see com.ibm.b2b.userexits.spi.PreProcessUserExitHandler#invoke(com.ibm.b2b.userexits.spi.BusinessDocument)
     */
    public BusinessDocument invoke( BusinessDocument bdo ) throws PreProcessExitException {

        Validate.notNull( bdo );

        final String sourceMethod = "invoke";

        if ( LOGGER.isLoggable( Level.FINER ) ) {
            LOGGER.entering( SOURCECLASS, sourceMethod, bdo.getTransactionID() );
        }

        try {

            Map<String, String> transportHeaders = bdo.getTransportHeaders();

            LOGGER.logp( Level.INFO, SOURCECLASS, sourceMethod, "Transport headers " + transportHeaders );

        } catch ( Exception t ) {
            throw new PreProcessExitException( t );
        }

        if ( LOGGER.isLoggable( Level.FINER ) ) {
            LOGGER.exiting( SOURCECLASS, sourceMethod );
        }

        return bdo;
    }

}

    
            

PreProcessSetFabricDestinationAddress.java

/*
 * *****************************************************************************
 * 
 * 5725Q72
 * 
 * (C) Copyright IBM Corp. 2013, 2014
 * 
 * The source code for this program is not published or otherwise divested of its trade secrets, irrespective of what
 * has been deposited with the U.S. Copyright Office.
 * 
 * The sample contained herein is provided to you "AS IS".
 * 
 * It is furnished by IBM as a simple example and has not been thoroughly tested under all conditions. IBM, therefore,
 * cannot guarantee its reliability, serviceability or functionality.
 * 
 * This sample may include the names of individuals, companies, brands and products in order to illustrate concepts as
 * completely as possible. All of these names are fictitious and any similarity to the names and addresses used by
 * actual persons or business enterprises is entirely coincidental.
 * 
 * ******************************************************************************
 */
package com.ibm.b2b.test.comms.userexits.preprocess;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.lang.Validate;

import com.ibm.b2b.userexits.spi.BusinessDocument;
import com.ibm.b2b.userexits.spi.PreProcessExitException;
import com.ibm.b2b.userexits.spi.PreProcessUserExitHandler;

/**
 * This is a sample user exit to override the fabric endpoint - to push the business message into - in pre-processing.
 * This can be used in an inbound flow to push a message into a different endpoint than configured in the exchange
 * profile. This is usually coupled with {@link BusinessDocument#getTransportHeaders()} to do header based routing.
 * 
 * @author Praveen S Rao
 * 
 */
public class PreProcessSetFabricDestinationAddress implements PreProcessUserExitHandler {

    private static final String SOURCECLASS = PreProcessSetFabricDestinationAddress.class.getCanonicalName();

    private static final Logger LOGGER = Logger.getLogger( SOURCECLASS );

    /*
     * (non-Javadoc)
     * 
     * @see com.ibm.b2b.userexits.spi.PreProcessUserExitHandler#invoke(com.ibm.b2b.userexits.spi.BusinessDocument)
     */
    public BusinessDocument invoke( BusinessDocument bdo ) throws PreProcessExitException {

        Validate.notNull( bdo );

        final String sourceMethod = "invoke";

        if ( LOGGER.isLoggable( Level.FINER ) ) {
            LOGGER.entering( SOURCECLASS, sourceMethod, bdo.getTransactionID() );
        }

        try {

            LOGGER.logp( Level.INFO, SOURCECLASS, sourceMethod, "Exit properties [" + bdo.getExitProperties()
                + "]" );

            bdo.setDestinationAddress( "TEST_DEST_QUEUE" );

        } catch ( Exception t ) {
            throw new PreProcessExitException( t );
        }

        if ( LOGGER.isLoggable( Level.FINER ) ) {
            LOGGER.exiting( SOURCECLASS, sourceMethod );
        }

        return bdo;
    }

}

            

PreProcessSetHTTPDestinationAddress.java

/*
 * *****************************************************************************
 * 
 * 
 * 5725Q72
 * 
 * (C) Copyright IBM Corp. 2013, 2014
 * 
 * The source code for this program is not published or otherwise divested of its trade secrets, irrespective of what
 * has been deposited with the U.S. Copyright Office.
 * 
 * The sample contained herein is provided to you "AS IS".
 * 
 * It is furnished by IBM as a simple example and has not been thoroughly tested under all conditions. IBM, therefore,
 * cannot guarantee its reliability, serviceability or functionality.
 * 
 * This sample may include the names of individuals, companies, brands and products in order to illustrate concepts as
 * completely as possible. All of these names are fictitious and any similarity to the names and addresses used by
 * actual persons or business enterprises is entirely coincidental.
 * 
 * ******************************************************************************
 */
package com.ibm.b2b.test.comms.userexits.preprocess;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.lang.Validate;

import com.ibm.b2b.userexits.spi.BusinessDocument;
import com.ibm.b2b.userexits.spi.PreProcessExitException;
import com.ibm.b2b.userexits.spi.PreProcessUserExitHandler;

/**
 * This is a sample user exit to override the HTTP destination URL in pre-processing. This is used in an outbound flow
 * to send the request to a different partner endpoint than configured in the exchange profile. This is usually coupled
 * with {@link BusinessDocument#getExitProperties()} to do property based routing.
 * 
 * @author Praveen S Rao
 * 
 */
public class PreProcessSetHTTPDestinationAddress implements PreProcessUserExitHandler {

    private static final String SOURCECLASS = PreProcessSetHTTPDestinationAddress.class.getCanonicalName();

    private static final Logger LOGGER = Logger.getLogger( SOURCECLASS );

    /*
     * (non-Javadoc)
     * 
     * @see com.ibm.b2b.userexits.spi.PreProcessUserExitHandler#invoke(com.ibm.b2b.userexits.spi.BusinessDocument)
     */
    public BusinessDocument invoke( BusinessDocument bdo ) throws PreProcessExitException {

        Validate.notNull( bdo );

        final String sourceMethod = "invoke";

        if ( LOGGER.isLoggable( Level.FINER ) ) {
            LOGGER.entering( SOURCECLASS, sourceMethod, bdo.getTransactionID() );
        }

        try {

            LOGGER.logp( Level.INFO, SOURCECLASS, sourceMethod, "Exit properties [" + bdo.getExitProperties()
                + "]" );

            bdo.setDestinationAddress( "http://testserver:30045/as4" );

        } catch ( Exception t ) {
            throw new PreProcessExitException( t );
        }

        if ( LOGGER.isLoggable( Level.FINER ) ) {
            LOGGER.exiting( SOURCECLASS, sourceMethod );
        }

        return bdo;
    }

}


            

PreProcessSetPayload.java

/*
 * *****************************************************************************
 * 
 * 5725Q72
 * 
 * (C) Copyright IBM Corp. 2013, 2014
 * 
 * The source code for this program is not published or otherwise divested of its trade secrets, irrespective of what
 * has been deposited with the U.S. Copyright Office.
 * 
 * The sample contained herein is provided to you "AS IS".
 * 
 * It is furnished by IBM as a simple example and has not been thoroughly tested under all conditions. IBM, therefore,
 * cannot guarantee its reliability, serviceability or functionality.
 * 
 * This sample may include the names of individuals, companies, brands and products in order to illustrate concepts as
 * completely as possible. All of these names are fictitious and any similarity to the names and addresses used by
 * actual persons or business enterprises is entirely coincidental.
 * 
 * ******************************************************************************
 */
package com.ibm.b2b.test.comms.userexits.preprocess;

import java.io.ByteArrayInputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.lang.Validate;

import com.ibm.b2b.userexits.spi.BusinessDocument;
import com.ibm.b2b.userexits.spi.PreProcessExitException;
import com.ibm.b2b.userexits.spi.PreProcessUserExitHandler;

/**
 * This is a sample user exit to override the payload to be sent out during pre-processing. This can be used in an
 * outbound flow to change the payload based on certain properties in the flow.
 * 
 * @author Praveen S Rao
 * 
 */
public class PreProcessSetPayload implements PreProcessUserExitHandler {

    private static final String SOURCECLASS = PreProcessSetPayload.class.getCanonicalName();

    private static final Logger LOGGER = Logger.getLogger( SOURCECLASS );

    /*
     * (non-Javadoc)
     * 
     * @see com.ibm.b2b.userexits.spi.PreProcessUserExitHandler#invoke(com.ibm.b2b.userexits.spi.BusinessDocument)
     */
    public BusinessDocument invoke( BusinessDocument bdo ) throws PreProcessExitException {

        Validate.notNull( bdo );

        final String sourceMethod = "invoke";

        if ( LOGGER.isLoggable( Level.FINER ) ) {
            LOGGER.entering( SOURCECLASS, sourceMethod, bdo.getTransactionID() );
        }

        try {

            LOGGER.logp( Level.INFO, SOURCECLASS, sourceMethod, "Exit properties [" + bdo.getExitProperties()
                + "]" );

            bdo.setPayload( new ByteArrayInputStream( "<a>sample new payload</a>".getBytes() ), "text/xml",
                "orig-new-payload.xml", "new description", -1, null );

        } catch ( Exception t ) {
            throw new PreProcessExitException( t );
        }

        if ( LOGGER.isLoggable( Level.FINER ) ) {
            LOGGER.exiting( SOURCECLASS, sourceMethod );
        }

        return bdo;
    }

}