Code example: PostProcessUserExitHandler API

Retrieves the exit properties, payload, and implements exceptions for error logging.

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

PostProcessGetExitProps.java

/*
 * *****************************************************************************
 * 
 * IBM Confidential
 * 
 * OCO Source Materials
 * 
 * 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.postprocess;

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.PostProcessExitException;
import com.ibm.b2b.userexits.spi.PostProcessUserExitHandler;

/**
 * This is a sample user exit to get user exit properties in post-processing.
 * 
 * @author Praveen S Rao
 * 
 */
public class PostProcessGetExitProps implements PostProcessUserExitHandler {

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

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

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

        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()
                + "]" );

        } catch ( BusinessDocExitException t ) {
            throw new PostProcessExitException( t );
        }

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

        return bdo;
    }

}

            

PostProcessGetPayload.java

               /*
 * *****************************************************************************
 * 
 * IBM Confidential
 * 
 * OCO Source Materials
 * 
 * 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.postprocess;

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.PostProcessExitException;
import com.ibm.b2b.userexits.spi.PostProcessUserExitHandler;

/**
 * This is a sample user exit to get payload data in post-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 mime unpackaging. In an inbound
 * flow, this can be used for flows that have a business response.
 * 
 * @author Praveen S Rao
 * 
 */
public class PostProcessGetPayload implements PostProcessUserExitHandler {

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

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

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

        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 PostProcessExitException( t );
        } finally {
            try {
                if ( is != null ) {
                    is.close();
                }
            } catch ( IOException e ) {
                e.printStackTrace();
            }
        }

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

        return bdo;
    }

}