TestDebugMsg.java 3 KB
package th.co.ais.ssbsrfc.control;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileReader;
import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPathExpressionException;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import ec02.exception.MessageParserException;
import ec02.server.EC02Handler;
import ec02.server.EC02Server;

public class TestDebugMsg {
	public static void main(String[] args) throws IOException,
			TransformerException, XPathExpressionException,
			ParserConfigurationException, SAXException, MessageParserException {

		BufferedReader in = new BufferedReader(new FileReader(
			"./example.msg/" +
//				"debug.html-text-plain.xml"
//				"debug.html-text-xml.xml"
//				"debug.html-text-xml-header.xml"
//				"debug.ldap.xml"
//				"debug.diameter.xml"
				"debug.xml"
		));

		String str, reqMessage = "";
		String conf = "";
		String temp = "";
		while ((str = in.readLine()) != null) {
			reqMessage += str;
		}
		in.close();
		in = new BufferedReader(new FileReader("./conf/WS1.EC02.SERV.0"));
		while ((temp = in.readLine()) != null) {
			conf += temp;
		}
		in.close();
		String[] a = { "WS1", "SERV", "0", conf };

		EC02Server.main(a);
		EC02Handler handler = new EC02Handler();
		System.out.println(handler.verifyAFConfig(conf));
		System.out.println(TestDebugMsg.formatXml(handler.handle(reqMessage,
				100000)));

		// int max=100;
		// for(int i=0; i<=max; i++)
		// {
		// ProcessingThread pt = new ProcessingThread(conf, reqMessage);
		// Thread t = new Thread(pt, "t"+String.valueOf(i));
		// t.start();
		// }
	}

	public static String formatXml(String xml) {
		try {
			Transformer serializer = TransformerFactory.newInstance()
					.newTransformer();
			serializer.setOutputProperty(OutputKeys.INDENT, "yes");
			serializer.setOutputProperty(
					"{http://xml.apache.org/xslt}indent-amount", "4");
			Source xmlSource = new SAXSource(new InputSource(
					new ByteArrayInputStream(xml.getBytes())));
			StreamResult res = new StreamResult(new ByteArrayOutputStream());
			serializer.transform(xmlSource, res);
			return new String(
					((ByteArrayOutputStream) res.getOutputStream())
							.toByteArray());
		} catch (Exception e) {
			return xml;
		}
	}
}

class ProcessingThread1 implements Runnable {
	private String conf;
	private String reqMessage;

	public ProcessingThread1(String _conf, String _msg) {
		this.conf = _conf;
		this.reqMessage = _msg;
	}

	@Override
	public void run() {
		EC02Handler handler = new EC02Handler();
		System.out.println(handler.verifyAFConfig(conf));
		System.out.println(TestDebugMsg.formatXml(handler.handle(reqMessage,
				100000)));
	}
}