package uk.ac.ebi.biosamples.ncbi;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketException;
import java.net.URI;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.zip.GZIPInputStream;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestOperations;

import uk.ac.ebi.biosamples.PipelinesProperties;

@Component
public class NCBIHTTP {

	@Autowired
	private PipelinesProperties pipelinesProperties;

	private Logger log = LoggerFactory.getLogger(getClass());

	private NCBIHTTP() {
	}

	/**
	 * Always downloads the target to a local copy, and then returns 
	 * an InputStream from that local copy for further processing.
	 * 
	 * Note: ensure that this stream is closed in a finally block to prevent
	 * leakage.
	 * @return
	 * @throws IOException
	 * @throws ParseException
	 */
	public InputStream streamFromLocalCopy() throws IOException, ParseException {
		
		log.info("Starting streaming fom local copy of NCBI XML");

		URI remoteFileName = pipelinesProperties.getNCBIHttpUri();
		Path tempCopy = Files.createTempFile("biosamples_ncbi", ".tmp");
		Path localCopy = pipelinesProperties.getNCBILocalFile().toPath();

		Files.copy(remoteFileName.toURL().openStream(), tempCopy, StandardCopyOption.REPLACE_EXISTING);

		log.info("Downloaded to temporary location");
		
		Files.move(tempCopy, localCopy, StandardCopyOption.REPLACE_EXISTING);
		

		log.info("Moved to final location");
		
		// now open a stream for the local version
		return new GZIPInputStream(new BufferedInputStream(new FileInputStream(localCopy.toFile())));
	}
	
	public InputStream streamFromRemote() throws IOException, ParseException {
		
		log.info("Starting streaming fom remote of NCBI XML");

		URI remoteFileName = pipelinesProperties.getNCBIHttpUri();
		
		// now open a stream for the local version
		return new GZIPInputStream(new BufferedInputStream(remoteFileName.toURL().openStream()));
	}
}

@@ -1204,1058 +1204,8 @@
 %7B%0A%09%7D
-%0A%0A%09/**%0A%09 * Always downloads the target to a local copy, and then returns %0A%09 * an InputStream from that local copy for further processing.%0A%09 * %0A%09 * Note: ensure that this stream is closed in a finally block to prevent%0A%09 * leakage.%0A%09 * @return%0A%09 * @throws IOException%0A%09 * @throws ParseException%0A%09 */%0A%09public InputStream streamFromLocalCopy() throws IOException, ParseException %7B%0A%09%09%0A%09%09log.info(%22Starting streaming fom local copy of NCBI XML%22);%0A%0A%09%09URI remoteFileName = pipelinesProperties.getNCBIHttpUri();%0A%09%09Path tempCopy = Files.createTempFile(%22biosamples_ncbi%22, %22.tmp%22);%0A%09%09Path localCopy = pipelinesProperties.getNCBILocalFile().toPath();%0A%0A%09%09Files.copy(remoteFileName.toURL().openStream(), tempCopy, StandardCopyOption.REPLACE_EXISTING);%0A%0A%09%09log.info(%22Downloaded to temporary location%22);%0A%09%09%0A%09%09Files.move(tempCopy, localCopy, StandardCopyOption.REPLACE_EXISTING);%0A%09%09%0A%0A%09%09log.info(%22Moved to final location%22);%0A%09%09%0A%09%09// now open a stream for the local version%0A%09%09return new GZIPInputStream(new BufferedInputStream(new FileInputStream(localCopy.toFile())));%0A%09%7D
 %0A%09%0A%09

clean unused function