Resolving Infor XA ERP System Link request response message using java

Keywords: xml Java Apache log4j

The SystemLink response message of Infor XA ERP is a relatively complex xml, which records the status of operation success and operation results or error instructions.

SystemLink parsing is to parse the operation result state from the xml message of the response, and extract the error message if the operation fails. Because Infor XA ERP SystemLink requests are divided into transactional and non-transactional requests, the parsing is also slightly different, and the details are not explained here. Go straight to the code and see how I parse messages in java code

Analytic Abstract classes:

package cn.markwins.yinfor.utils.xml;

import java.util.List;

import org.xml.sax.helpers.DefaultHandler;

import cn.markwins.yinfor.utils.vo.systemlink.SystemLinkMessage;

public abstract class SystemLinkStockDefaultHandler extends DefaultHandler{
	/* Return message  */
	protected SystemLinkMessage systemLinkMessage = null;
	protected List<String> systemLinkExceptionList = null;
	public SystemLinkMessage getSystemLinkMessage() {
		return systemLinkMessage;
	}
	
	protected String preTagName = null;
}


Resolve non-transactional SystemLink messages:
package cn.markwins.yinfor.utils.xml;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import cn.markwins.yinfor.utils.common.StringTools;
import cn.markwins.yinfor.utils.vo.systemlink.SystemLinkMessage;

/**
 * @Description Single System Link Response Message xml Parser without Transaction Control
 * @author Lee yi Hui
 * @date 2016 July 5th 2013
 */
public class SystemLinkTransactionNHandler extends SystemLinkStockDefaultHandler {
	private boolean messageTypeError = false;
	private boolean responseTag = false;
	private boolean exceptionTag = false;
	private boolean messageTag = false;
	private int errMsgIndex = 0;
	
	@Override
	public void startDocument() throws SAXException {
		systemLinkMessage = new SystemLinkMessage();
		systemLinkExceptionList = new ArrayList<>();
		systemLinkMessage.setStatus(true);
	}
	
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if(qName.matches("[A-Za-z0-9]+Response$")){
			responseTag = true;
			if("LoginResponse".equals(qName)){
				if("false".equals(attributes.getValue("actionSucceeded"))){
					systemLinkMessage.setStatus(false);
				}
			}
			
		}else{
			switch (qName) {
			case "Exception":
				exceptionTag = true;
				errMsgIndex = 0;
				break;
			case "Response":
				if("true".equals(attributes.getValue("hasErrors"))){
					systemLinkMessage.setStatus(false);
				}
				break;
			case "Message":
				messageTag = true;
				if("error".equals(attributes.getValue("type"))){
					messageTypeError = true;
					++errMsgIndex;
				}
				break;
			default:
				break;
			}
		}
		
		this.preTagName = qName;
	}
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		//Exception Information Node
		if(messageTypeError && messageTag && exceptionTag && "Text".equals(this.preTagName)){
			String text = null;
			if(!responseTag){	//Exception information occurs before any Response is entered, which indicates that the SystemLink server is abnormal and processing fails.
				systemLinkMessage.setStatus(false);
			}
			if(!systemLinkMessage.getStatus()){
				text = new String(ch, start, length);
			}
			if(!StringTools.isNullOrWhiteSpace(text)){
//				If (systemLinkExceptionList. isEmpty (){// Take only the last error type message response
//					systemLinkExceptionList.add(text);
//				}else{
//					systemLinkExceptionList.set(0, text);
//				}
				if(errMsgIndex == 2 && systemLinkExceptionList.size() < 2){	//If there are two or more errors, the first one must be that there are errors in the general summary description.
					systemLinkExceptionList.set(0, text);
				}else{					//Others are specific error messages.
					systemLinkExceptionList.add(text);
				}
			}
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if(qName.matches("[A-Za-z0-9]+Response$")){
			responseTag = false;
		}else{
			switch (qName) {
			case "Message":
				messageTag = false;
				messageTypeError = false;
				break;
			case "Exception":
				exceptionTag = false;
				break;
			default:
				break;
			}
		}
		
		this.preTagName = null;
	}
	
	@Override
	public void endDocument() throws SAXException {
		if(!systemLinkMessage.getStatus()){
			systemLinkMessage.setSystemLinkExceptionList(systemLinkExceptionList);
		}
	}

}


SystemLink response message parsing transaction type:

package cn.markwins.yinfor.utils.xml;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import cn.markwins.yinfor.utils.common.StringTools;
import cn.markwins.yinfor.utils.vo.systemlink.SystemLinkMessage;

/**
 * @Description SystemLink Response Message xml Parser for Transaction Control
 * @author Lee yi Hui
 * @date 2016 July 5th 2013
 */
public class SystemLinkTransactionYHandler extends SystemLinkStockDefaultHandler {
	private boolean messageTypeError = false;
	private boolean responseTag = false;
	private boolean exceptionTag = false;
	private boolean messageTag = false;
	private int errMsgIndex = 0;
	private String responseName = null;
	private Integer txReqIndex = null;
	
	@Override
	public void startDocument() throws SAXException {
		systemLinkMessage = new SystemLinkMessage();
		systemLinkExceptionList = new ArrayList<>();
		systemLinkMessage.setStatus(true);
	}
	
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if(qName.matches("[A-Za-z0-9]+Response$")){
			responseTag = true;
			if("LoginResponse".equals(qName)){
				if("false".equals(attributes.getValue("actionSucceeded"))){
					systemLinkMessage.setStatus(false);
				}
			}else{
				responseName = attributes.getValue("name");
				if(!StringTools.isNullOrWhiteSpace(responseName)){
					int dashIndex = responseName.lastIndexOf('_');
					if(dashIndex > 1){
						txReqIndex = Integer.valueOf(responseName.substring(dashIndex + 1, responseName.length()));
					}
				}
			}
		}else{
			switch (qName) {
			case "Exception":
				exceptionTag = true;
				errMsgIndex = 0;
				break;
			case "Response":
				if("true".equals(attributes.getValue("hasErrors"))){
					systemLinkMessage.setStatus(false);
				}
				break;
			case "Message":
				messageTag = true;
				if("error".equals(attributes.getValue("type"))){
					messageTypeError = true;
					++errMsgIndex;
				}
				break;
			default:
				break;
			}
		}
		
		this.preTagName = qName;
	}
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		//Exception Information Node
		if(messageTypeError && messageTag && exceptionTag && "Text".equals(this.preTagName)){
			String text = null;
			//Exception information occurs before any Response is entered, which indicates that the SystemLink server is abnormal and processing fails.
			if(!responseTag){
				systemLinkMessage.setStatus(false);
			}
			//systemLink responds to error messages
			if(!systemLinkMessage.getStatus()){
				text = new String(ch, start, length);
			}
			if(!StringTools.isNullOrWhiteSpace(text)){
				if(errMsgIndex == 2 && systemLinkExceptionList.size() < 2){	//If there are two or more errors, the first one must be that there are errors in the general summary description.
					systemLinkExceptionList.set(0, text);
				}else{					//Others are specific error messages.
					if(txReqIndex != null && txReqIndex > 0){
						text = txReqIndex + ":" + text;
					}
					systemLinkExceptionList.add(text);
				}
			}
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if(qName.matches("[A-Za-z0-9]+Response$")){
			responseTag = false;
			responseName = null;
			txReqIndex = null;
		}else{
			switch (qName) {
			case "Message":
				messageTag = false;
				messageTypeError = false;
				break;
			case "Exception":
				exceptionTag = false;
				break;
			default:
				break;
			}
		}
		
		this.preTagName = null;
	}
	
	@Override
	public void endDocument() throws SAXException {
		if(!systemLinkMessage.getStatus()){
			systemLinkMessage.setSystemLinkExceptionList(systemLinkExceptionList);
		}
	}

}

Finally, this is the tool class for generic invocation entry:

package cn.markwins.yinfor.utils.xml;

import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.log4j.Logger;

import cn.markwins.yinfor.utils.common.StreamTools;
import cn.markwins.yinfor.utils.common.StringTools;
import cn.markwins.yinfor.utils.vo.systemlink.SystemLinkMessage;

/**
 * @Description SystemLink Response message parser
 * @author Lee yi Hui
 * @date 2016 July 6th 2013
 */
public class SystemLinkRespMessageTools {
	
	private static Logger logger = Logger.getLogger(SystemLinkRespMessageTools.class);
	
	/**
	 * @Description Parsing systemLink message response
	 * @param systemLinkRespXML systemLink xml response message
	 * @param transactionFlag Parsing way
	 * 							true:Transaction type systemLink xml response
	 * 							false:Non-transactional type systemLink xml response
	 * @return SystemLinkMessage Parsed message encapsulation
	 */
	public static SystemLinkMessage parseStockSystemLinkRespXML(String systemLinkRespXML, boolean transactionFlag) {
		SystemLinkMessage systemLinkMessage = new SystemLinkMessage();
		systemLinkMessage.setStatus(false);
		List<String> systemLinkExceptionList = new ArrayList<>();
		
		//Long time no response
		if(StringTools.isNullOrWhiteSpace(systemLinkRespXML)){
			systemLinkExceptionList.add("XA The system has no response for a long time,Please Infor XA System check whether accounts have been cleared,Feedback to System Administrator");
			systemLinkMessage.setSystemLinkExceptionList(systemLinkExceptionList);
			logger.error("Infor XA SystemLink Long time no response,Unresponsive message");
			return systemLinkMessage;
		}
		
		//Response message non-standardized xml,sax can not be parsed, special processing
		/*1,com.pjx.xsaa.entry.ServerNotFoundException*/
		if(systemLinkRespXML.contains("com.pjx.xsaa.entry.ServerNotFoundException")){
			systemLinkExceptionList.add("Posting failure,Infor XA SystemLink Server Not opened,Feedback to System Administrator");
			systemLinkMessage.setSystemLinkExceptionList(systemLinkExceptionList);
			return systemLinkMessage;
		}
		
		//Analytic message
		try {
			SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
			SAXParser saxParser = saxParserFactory.newSAXParser();
			SystemLinkStockDefaultHandler handler = null;
			
			if(transactionFlag){
				handler = new SystemLinkTransactionYHandler();	//Open the system link transaction parser
			}else{
				handler = new SystemLinkTransactionNHandler();	//System Link transaction parser not activated
			}
			
			saxParser.parse(StreamTools.getInputStreamFromString(systemLinkRespXML), handler);
			SystemLinkMessage systemLinkMessageParsed = handler.getSystemLinkMessage();
			if(systemLinkMessageParsed == null || systemLinkMessageParsed.getStatus() == null){
				systemLinkExceptionList.add("System exception,Please XA ERP Check whether accounts have been cleared,Feedback to System Administrator");
				systemLinkMessage.setSystemLinkExceptionList(systemLinkExceptionList);
				logger.error("SystemLink Message parsing exception,Unable to extract parse state");
				return systemLinkMessage;
			}
			
			systemLinkMessage.setStatus(systemLinkMessageParsed.getStatus());
			systemLinkMessage.setSystemLinkExceptionList(systemLinkMessageParsed.getSystemLinkExceptionList());
		} catch (Exception e) {
			systemLinkMessage.setStatus(false);
			systemLinkExceptionList.add("System exception,Please XA ERP Check whether accounts have been cleared,Feedback to System Administrator");
			systemLinkMessage.setSystemLinkExceptionList(systemLinkExceptionList);
			logger.error("System exception,Please Infor XA System check whether accounts have been cleared,Feedback to System Administrator--SystemLink Message parsing exception", e);
		} 
		
		return systemLinkMessage;
	}
}


http://blog.csdn.net/yihuiworld


Posted by mbeals on Tue, 12 Feb 2019 14:36:17 -0800