package org.yeastrc.nrseq_fasta_importer.taxonomy_id_determination.webservice_clients.ncbi.by_taxonomy_id;

import java.io.InputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.log4j.Logger;
import org.yeastrc.nrseq_fasta_importer.constants.NCBITaxonomyDataConstants;
import org.yeastrc.nrseq_fasta_importer.taxonomy_id_determination.webservice_clients.ncbi.by_taxonomy_id.webservice_response_dto.NcbiTaxonomyDataRootDTO;
import org.yeastrc.nrseq_fasta_importer.taxonomy_id_determination.webservice_clients.ncbi.by_taxonomy_id.webservice_response_dto.NcbiTaxonomyErrorRootDTO;




/**
 * 
 *
 */
public class GetNCBITaxonomyDataFromNCBIWebservice {

	private static final Logger log = Logger.getLogger(GetNCBITaxonomyDataFromNCBIWebservice.class);
	
	private GetNCBITaxonomyDataFromNCBIWebservice() { }
	public static GetNCBITaxonomyDataFromNCBIWebservice getInstance() { 
		return new GetNCBITaxonomyDataFromNCBIWebservice(); 
	}
	
	/**
	 * @param taxonomyId
	 * @return
	 * @throws Exception 
	 */
	public GetNCBITaxonomyDataFromNCBIWebserviceResponse getNCBITaxonomyDataFromNCBIWebservice( long taxonomyId ) throws Exception {
		
		GetNCBITaxonomyDataFromNCBIWebserviceResponse response = new GetNCBITaxonomyDataFromNCBIWebserviceResponse();
		
		
		String ncbiUrl = NCBITaxonomyDataConstants.URL_BASE + taxonomyId;
		

		HttpClient client = null;
		HttpGet httpGet = null;

		HttpResponse httpResponse = null;
		
		InputStream inputStreamHttpResponse = null;

		Unmarshaller unmarshaller = null;

		try {

			JAXBContext jc = JAXBContext.newInstance( NcbiTaxonomyDataRootDTO.class, NcbiTaxonomyErrorRootDTO.class );

			unmarshaller = jc.createUnmarshaller();
			

			client = new DefaultHttpClient();

			httpGet = new HttpGet( ncbiUrl );

			httpResponse = client.execute(httpGet);

			inputStreamHttpResponse = httpResponse.getEntity().getContent(); 

			Object returnedObject = unmarshaller.unmarshal( inputStreamHttpResponse );
			
			if ( returnedObject instanceof NcbiTaxonomyDataRootDTO ) {
				
				NcbiTaxonomyDataRootDTO item = (NcbiTaxonomyDataRootDTO) returnedObject;
				
				response.setNcbiTaxonomyDataRootDTO(item);
				
			} else if ( returnedObject instanceof NcbiTaxonomyErrorRootDTO ) {

				NcbiTaxonomyErrorRootDTO item = (NcbiTaxonomyErrorRootDTO) returnedObject;
				
				response.setNcbiTaxonomyErrorRootDTO(item);
				
			} else {
				
				String msg = "Failed to deserialize response into expected classes";
				log.error( msg );
				throw new Exception(msg);
			}

		} catch (Exception e) {

			log.error("Failed to get NCBI data for taxonomy id.  URL = " + ncbiUrl, e );
			throw e;

		} finally { 

			if ( inputStreamHttpResponse != null ) {
				inputStreamHttpResponse.close();
			}
		}

		
		return response;
	}
}

@@ -200,16 +200,101 @@
 shaller;
+%0Aimport javax.xml.parsers.SAXParserFactory;%0Aimport javax.xml.transform.sax.SAXSource;
 %0A%0Aimport
@@ -495,24 +495,86 @@
 g4j.Logger;%0A
+import org.xml.sax.InputSource;%0Aimport org.xml.sax.XMLReader;%0A
 import org.y
@@ -2232,86 +2232,553 @@
 ); %0A
+%09%09%09
 %0A%09%09%09
-Object returnedObject = unmarshaller.unmarshal( inputStreamHttpResponse );
+%0A%09%09%09//  Turn off loading external DTDs%0A%09%09%09%0A%09%09%09SAXParserFactory spf = SAXParserFactory.newInstance();%0A%09%09%09spf.setFeature(%22http://apache.org/xml/features/nonvalidating/load-external-dtd%22, false);%0A%09%09%09spf.setFeature(%22http://xml.org/sax/features/validation%22, false);%0A%0A%09%09%09XMLReader xmlReader = spf.newSAXParser().getXMLReader();%0A%09%09%09InputSource inputSource = new InputSource( inputStreamHttpResponse );%0A%09%09%09%0A%09%09%09SAXSource saxSource = new SAXSource(xmlReader, inputSource);%0A%09%09%09%0A%09%09%09Object returnedObject = unmarshaller.unmarshal( saxSource );%0A%09%09%09%0A%09%09%09
 %0A%09%09%09

Update GetNCBITaxonomyDataFromNCBIWebservice to config JAXB to not get DTD file for NCBI response XML. Needed for Java 8.