cancel
Showing results for 
Search instead for 
Did you mean: 

Adapter Module Code Check

Former Member
0 Kudos

Hi,

We had a requirement to perform XML validation on the incoming message. Accordingly an Adapter Module was developed and below is the JAVA code which our JAVA developer has written:


/**
 * 
 */
package com.sap.pi.adaptermodule;

import javax.ejb.CreateException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext; // Classes for Module development & Trace
import com.sap.aii.af.lib.mp.module.*;
import com.sap.engine.interfaces.messaging.api.*;
import com.sap.engine.interfaces.messaging.api.auditlog.*;
import com.sap.tc.logging.*; // XML parsing and transformation classes
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.InputSource;
import java.io.InputStream;
import java.io.ByteArrayOutputStream;
import java.io.StringReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

/**
 * @author ABC
 *
 */
public class ValidateMessageBean implements SessionBean, Module {

	public static final String VERSION_ID = "$Id://tc/aii/30_REL/src/_adapters/_sample/java/user/module/GetHostName.java#1 $";
	static final long serialVersionUID = 7435850550539048631L;
	private SessionContext myContext;

	public void ejbRemove() {
	}
	public void ejbActivate() {
	}
	public void ejbPassivate() {
	}
	public void setSessionContext(SessionContext context) {
		myContext = context;
	}
	public void ejbCreate() throws CreateException {
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sap.aii.af.lib.mp.module.Module#process(com.sap.aii.af.lib.mp.module.ModuleContext,
	 *      com.sap.aii.af.lib.mp.module.ModuleData)
	 */
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sap.aii.af.lib.mp.module.Module#process(com.sap.aii.af.lib.mp.module.ModuleContext,
	 *      com.sap.aii.af.lib.mp.module.ModuleData)
	 */
	public ModuleData process(ModuleContext moduleContext,
			ModuleData inputModuleData) throws ModuleException {
		String SIGNATURE = "process(ModuleContext moduleContext, ModuleData inputModuleData)";
		Location location = null;
		AuditAccess audit = null;
		// Create the location always new to avoid serialization/transient of
		// location
		/*
		 * try { location = Location.getLocation(this.getClass().getName()); }
		 * catch (Exception t) { t.printStackTrace(); ModuleException me = new
		 * ModuleException( "Unable to create trace location", t); throw me; }
		 */
		Object obj = null;
		Message msg = null;
		// String hostName = getHostName();
		MessageKey key = null;
		
			obj = inputModuleData.getPrincipalData();
			msg = (Message) obj;
			key = new MessageKey(msg.getMessageId(), msg.getMessageDirection());
			/*audit = PublicAPIAccessFactory.getPublicAPIAccess()
					.getAuditAccess();
			audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS,
					"GetHostName: Module called");*/
		/*
		 * } catch (Exception e) { ModuleException me = new ModuleException(e);
		 * throw me; }
		 */
		// Read the channel ID, channel and the module configuration
		String hostElementName = null;
		
		Document document=null;
		XMLPayload xmlpayload=null;
		try {
			xmlpayload = msg.getDocument();
			DocumentBuilderFactory factory;
			factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			document = builder.parse((InputStream) xmlpayload
					.getInputStream());
			
		} catch (Exception e) {
		try{
			if (document == null) {
				DocumentBuilderFactory factory;
				factory = DocumentBuilderFactory.newInstance();
				DocumentBuilder builder = factory.newDocumentBuilder();
				String str="<MT_Validation><Result>FAIL</Result><Header><Sender>ABC</Sender></Header><ValidationErrors><ErrorCode>ILL-FORMED MESSAGE</ErrorCode><ErrorAttribute>"+ e.getMessage()+ "</ErrorAttribute><ErrorValue></ErrorValue></ValidationErrors></MT_Validation>";
				document = builder.parse(new InputSource(new StringReader(str))); 
			}
			}catch(Exception e1) {ModuleException me = new ModuleException(e);
			     throw me;}
		}finally{
				try{
				TransformerFactory tfactory = TransformerFactory.newInstance();
				Transformer transformer = tfactory.newTransformer();
				Source src = new DOMSource(document);
				ByteArrayOutputStream myBytes = new ByteArrayOutputStream();
				Result dest = new StreamResult(myBytes);
				transformer.transform(src, dest);
				byte[] docContent = myBytes.toByteArray();
				if (docContent != null) {
					xmlpayload.setContent(docContent);
					inputModuleData.setPrincipalData(msg);
				}
				}catch(Exception e) {ModuleException me = new ModuleException(e);
			     throw me;}
			    return inputModuleData;
		}
}
}

The logic is basically to check if the message picked by the Communication channel is XML or not; if not XML then generate a custom Error message.

Can anyone be kind enough to review this JAVA code and please tell us for any loopholes? NWDS did not report any error

Thank you,

Pankaj.

Edited by: Pankaj Sharma XI on Dec 9, 2010 5:11 AM

Accepted Solutions (1)

Accepted Solutions (1)

stefan_grube
Active Contributor
0 Kudos

> Can anyone be kind enough to review this JAVA code and please tell us for any loopholes?

From first impression I would say, that the module will do what is expected.

Answers (0)