package testgenerator;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

import iaik.asn1.structures.Name;
import iaik.x509.X509Certificate;

/**
 * @author KNIRSCHS
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */
public class VXResponseBean {

	private X509Certificate cert = null;

	/**
	 * Constructor for VXResponseBean.
	 */
	public String getHeader() {

		String result =
			"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
			"<VerifyXMLSignatureResponse xmlns=\"http://reference.e-government.gv.at/namespace/moa/20020822#\" >\n" +
			"	<SignerInfo>\n" +
			"   	<dsig:X509Data xmlns:dsig=\"http://www.w3.org/2000/09/xmldsig#\">\n";
	
		return result;
	}

	public void init(String Filename) throws Exception {
		if (cert == null) {
			InputStream cfile = getHttpInputStream(Filename);
			cert = new iaik.x509.X509Certificate(cfile);
		}
	}
	public String getCert() throws Exception {
		return "					<dsig:X509Certificate>"
			+ (cert)
			+ "</dsig:X509Certificate>\n";
		
	}


	public String getSubjectDN() throws Exception {
		return "					<dsig:X509SubjectName>"
			+ ((Name) cert.getSubjectDN()).getRFC2253String()
			+ "</dsig:X509SubjectName>\n";
	}
	public String getIssuerDN() throws Exception {
	
	return "					<dsig:X509IssuerName>" + 
		((Name)cert.getIssuerDN()).getRFC2253String() +
			"</dsig:X509IssuerName>\n";
	}		

	public String getSerialNumber() throws Exception {

	return "					<dsig:X509SerialNumber>" +
				cert.getSerialNumber() +
			"</dsig:X509SerialNumber>\n";
	}
	
	public String getFooterSignatureCheck(String signatureCheckCode, String certificateCheckCode)
	{
	String res = null;
	res = "	</HashInputData>\n" + 
		  "     <SignatureCheck>\n" +
		  "     	<Code>" + signatureCheckCode + "</Code>\n" +		  
 		  "		</SignatureCheck>\n" +
 		  "     <CertificateCheck>\n" +
 		  "     	<Code>"+certificateCheckCode+"</Code>\n" +
 		  "     </CertificateCheck>\n" +
 		  "</VerifyXMLSignatureResponse>";

	return res;
		
	}
	public String getFooterXMLDSIGManifestCheck(String xmlDSIGManifestCheckCode , String certificateCheckCode)
	{
	String res = null;
/*	res = "</XMLContent>\n" +
		  "		</HashInputData>\n" + 
		  "     <XMLDSIGManifestCheck>\n" +
		  "     	<Code>" + xmlDSIGManifestCheckCode + "</Code>\n" +		  
 		  "		</XMLDSIGManifestCheck>\n" +
 		  "     <CertificateCheck>\n" +
 		  "     	<Code>"+certificateCheckCode+"</Code>\n" +
 		  "     </CertificateCheck>\n" +
 		  "</VerifyXMLSignatureResponse>";
*/
	res = "		<XMLDSIGManifestCheck>\n" +
		  "     	<Code>" + xmlDSIGManifestCheckCode + "</Code>\n" +		  
 		  "		</XMLDSIGManifestCheck>\n" +
 		  "     <CertificateCheck>\n" +
 		  "     	<Code>"+certificateCheckCode+"</Code>\n" +
 		  "     </CertificateCheck>\n" +
 		  "</VerifyXMLSignatureResponse>";

	return res;
		
	}
public String createXMLDSIGManifestCheck(String xmlDSIGManifestCheckCode, String failedReference, String referringSigRefData)
	{	
		
		if(failedReference != null && !failedReference.trim().equals(""))
		{
			String res = null;
			res = "		<XMLDSIGManifestCheck>\n" +
				  "				<Code>" + xmlDSIGManifestCheckCode + "</Code>\n" +		  
				  "				<Info><FailedReference>" + failedReference +
				  "</FailedReference><ReferringSigReference>" + referringSigRefData + "</ReferringSigReference>" +
				  "				</Info>\n" +		  		  
		 		  "		</XMLDSIGManifestCheck>\n";
		
			return res;
		}
		else
		{
			String res = null;
			res = "		<XMLDSIGManifestCheck>\n" +
				  "				<Code>" + xmlDSIGManifestCheckCode + "</Code>\n" +		  
				  "				<Info>" + failedReference +
				  "						<ReferringSigReference>" + referringSigRefData + "</ReferringSigReference>" +
				  "				</Info>\n" +		  		  
		 		  "		</XMLDSIGManifestCheck>\n";
		
			return res;
		}
		
	}
	
public String createSignatureCheck(String signatureCheckCode)
	{
	String res = null;
	res =  "    		<SignatureCheck>\n" +
		   "     			<Code>" + signatureCheckCode + "</Code>\n" +		  
 		   "		</SignatureCheck>\n";
	return res;
		
	}
public String createSignatureManifestCheck(String signatureCheckCode)
	{
	String res = null;
	res =  "    		<SignatureManifestCheck>\n" +
		   "     			<Code>" + signatureCheckCode + "</Code>\n" +		  
 		   "		</SignatureManifestCheck>\n";
	return res;
		
	}
	
public String createSignatureManifestCheck(String signatureCheckCode,String info)
	{
	String res = null;
	res =  "    		<SignatureManifestCheck>\n" +
		   "     			<Code>" + signatureCheckCode + "</Code>\n" + info +	  
 		   "		</SignatureManifestCheck>\n";
	return res;
		
	}
public String createCertificateCheck(String certificateCheckCode)
	{
	String res = null;
	res = "		<CertificateCheck>\n" +
 		  "			<Code>"+certificateCheckCode+"</Code>\n" +
 		  "		</CertificateCheck>\n";
	return res;
	}
private InputStream getHttpInputStream(String url) throws Exception
	{

		return  ((HttpURLConnection)new URL(url).openConnection()).getInputStream();		
	}
	
}