package test;

import java.io.ByteArrayInputStream;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import net.aocat.siri.proxy.client.SIRIProxyClient;
import net.aocat.siri.proxy.client.utils.Utils;

import org.apache.axis.utils.XMLUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * <p>Title: Test</p>
 * <p>Description: Test.</p>
 */
public class EnviarPeticioPCIAppSincron
{
	public static void main (String[] args) throws Exception
	{		
		Document xml = file2dom(args[0]);
		
		//PREPRODUCCIÓ
		URL url= new URL("https://serveis-pre.app.aoc.cat/siri-proxy/services/Sincron");
		
		SIRIProxyClient proxy = new SIRIProxyClient();
		Element res = proxy.invoca(xml.getDocumentElement(),url,true);
		Utils.printXmlObject(XMLUtils.ElementToString(res));
					
	}//main

	  public static Document file2dom (String file) throws Exception
	  {
		  byte[] dcocRequest = Utils.getBytesFromFile(file);
		  String dcocRequestStr = new String(dcocRequest);
		  dcocRequestStr = dcocRequestStr.replaceAll("XXXXXXXXXX", Long.toString(System.currentTimeMillis()));
		  
	      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	      factory.setNamespaceAware(true);
	      DocumentBuilder builder = factory.newDocumentBuilder();
	      return builder.parse(new ByteArrayInputStream(dcocRequestStr.getBytes()));

	  }//file2dom
	  
}//Test
