Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
roger_alluivall
Participant

Here you'll find a module I wrote to zip and unzip messages using gzip. I hope you find it useful:


PayloadGZipBean

package com.integrations.adapter.module;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.rmi.RemoteException;

import java.util.zip.GZIPInputStream;

import java.util.zip.GZIPOutputStream;

import javax.ejb.EJBException;

import javax.ejb.SessionBean;

import javax.ejb.SessionContext;

import javax.ejb.TimedObject;

import javax.ejb.Timer;

import com.sap.aii.af.lib.mp.module.ModuleContext;

import com.sap.aii.af.lib.mp.module.ModuleData;

import com.sap.aii.af.lib.mp.module.ModuleException;

import com.sap.aii.af.service.auditlog.Audit;

import com.sap.engine.interfaces.messaging.api.Message;

import com.sap.engine.interfaces.messaging.api.MessageKey;

import com.sap.engine.interfaces.messaging.api.auditlog.AuditLogStatus;

/**

* @author Roger Allué i Vall

*

*/

public class PayloadGZipBean implements SessionBean, TimedObject {

    private static final String C_PARAM_MODE         = "zip.mode";

    private static final String C_MODE_ZIP           = "zip";

    private static final String C_MODE_UNZIP         = "unzip";

    private static final int    C_BUFFER_SIZE        = 8192;

    private static MessageKey amk;

    /* (non-Javadoc)

     * @see javax.ejb.SessionBean#ejbActivate()

     */

    @Override

    public void ejbActivate() throws EJBException, RemoteException {

        // TODO Auto-generated method stub

    }

    /* (non-Javadoc)

     * @see javax.ejb.SessionBean#ejbPassivate()

     */

    @Override

    public void ejbPassivate() throws EJBException, RemoteException {

        // TODO Auto-generated method stub

    }

    /* (non-Javadoc)

     * @see javax.ejb.SessionBean#ejbRemove()

     */

    @Override

    public void ejbRemove() throws EJBException, RemoteException {

        // TODO Auto-generated method stub

    }

    /* (non-Javadoc)

     * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)

     */

    @Override

    public void setSessionContext(SessionContext arg0) throws EJBException,

            RemoteException {

        // TODO Auto-generated method stub

    }

    /* (non-Javadoc)

     * @see javax.ejb.TimedObject#ejbTimeout(javax.ejb.Timer)

     */

    @Override

    public void ejbTimeout(Timer arg0) {

        // TODO Auto-generated method stub

    }

    public void ejbCreate() throws javax.ejb.CreateException {

    }

    public ModuleData process(ModuleContext moduleContext, ModuleData inputModuleData) throws ModuleException {

        Message msg;

        ByteArrayInputStream  payloadIn;

        ByteArrayOutputStream payloadOut;

        try {

            msg = (Message) inputModuleData.getPrincipalData();           

            amk               = new MessageKey(msg.getMessageId(), msg.getMessageDirection());

            addInfo("PayloadGZipBean Begin...");

            addInfo("Payload initialization");

            payloadIn = new ByteArrayInputStream(msg.getDocument().getContent());       

            payloadOut = new ByteArrayOutputStream();

            addInfo("Getting " + C_PARAM_MODE + " from parameters");           

            String mode            = moduleContext.getContextData(C_PARAM_MODE);

            if (mode!=null && mode.equals(C_MODE_ZIP)){

                addInfo("zip.mode from parameters");

                addInfo(C_PARAM_MODE + " found: " + C_MODE_ZIP);               

                compress(payloadIn,payloadOut);   

                addInfo("PayloadGZipBean: Payload successfully zipped");

            }else if (mode!=null && mode.equals(C_MODE_UNZIP)){

                addInfo(C_PARAM_MODE + " found: " + C_MODE_UNZIP);

                uncompress(payloadIn,payloadOut);

                addInfo("PayloadGZipBean: Payload successfully unzipped");

            }else{

            }

             msg.getDocument().setContent(payloadOut.toByteArray());        

             inputModuleData.setPrincipalData(msg);

        }

        catch (Exception e) {

            ModuleException me = new ModuleException(e);

            throw me;

            }

        addInfo("PayloadGZipBean End...");       

        return inputModuleData;

    }

    private void compress(InputStream in, OutputStream out) throws ModuleException{           

        try {           

        GZIPOutputStream goutstream = new GZIPOutputStream(out);                   

        byte[] buf = new byte[C_BUFFER_SIZE];

         int len;        

            while ((len = in.read(buf)) > 0)

            {

                 goutstream.write(buf, 0, len);

            }

        } catch (IOException e) {

            addError("Compress: Exception found!");

            ModuleException me = new ModuleException(e);

            throw me;

        }       

    }

    private void uncompress(InputStream in, OutputStream out) throws ModuleException{

        try {

        GZIPInputStream ginstream = new GZIPInputStream(in);                   

        byte[] buf = new byte[C_BUFFER_SIZE];

         int len;

         while ((len = ginstream.read(buf)) > 0)

        {

           out.write(buf, 0, len);

        }

        } catch (IOException e) {

            addError("Uncompress: Exception found!");

            ModuleException me = new ModuleException(e);

            throw me;

        }

    }

    private void addInfo (String msg){

        try{

        Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,msg);

        }

        catch (Exception e){

            Audit.addAuditLogEntry(amk, AuditLogStatus.ERROR,"Error a l'obrir el fitxer!");

        }

        }

    private void addWarning (String msg){

        try{

        Audit.addAuditLogEntry(amk, AuditLogStatus.WARNING,msg);

        }

        catch (Exception e){

            Audit.addAuditLogEntry(amk, AuditLogStatus.ERROR,"Error a l'obrir el fitxer!");

        }

        }

    private void addError (String msg){

        try{

        Audit.addAuditLogEntry(amk, AuditLogStatus.ERROR,msg);

        }

        catch (Exception e){

            Audit.addAuditLogEntry(amk, AuditLogStatus.ERROR,"Error a l'obrir el fitxer!");

        }

        }   

}


Regards!

3 Comments
Labels in this area