<commit_before>package org.opensirf.jaxrs.api;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringWriter;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.StreamingOutput;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;

import org.eclipse.persistence.jaxb.MarshallerProperties;
import org.opensirf.catalog.SIRFCatalog;
import org.opensirf.container.ProvenanceInformation;
import org.opensirf.container.SIRFContainer;
import org.opensirf.format.ProvenanceInformationMarshaller;
import org.opensirf.format.SIRFCatalogMarshaller;
import org.opensirf.format.SIRFCatalogUnmarshaller;
import org.opensirf.jaxrs.config.ContainerConfiguration;
import org.opensirf.jaxrs.config.SwiftConfiguration;
import org.opensirf.jaxrs.model.MagicObject;

public class SwiftStrategy implements StorageContainerStrategy {
	protected SwiftStrategy() {
		
	}
	
	protected SwiftStrategy(ContainerConfiguration c) { 
		this.config = c;
	}
	
	public void setConfig(ContainerConfiguration c) {
		this.config = c;
	}

	public ContainerConfiguration getConfig() {
		return config;
	}

	private ContainerConfiguration config;

	@Override
	public MagicObject retrieveMagicObject() {
		try {
			SwiftDriver driver = new SwiftDriver(config);
			MagicObject mo = driver.containerMetadata(config.getContainerName());
			driver.close();
			return mo;
		} catch (IOException ioe) {
			ioe.printStackTrace();
		} 
		
		return null;
	}

	@Override
	public void createContainer(String containerName) {
		try {
			SwiftDriver driver = new SwiftDriver(config);
			System.out.println("Calling driver, container name = " + containerName);
			driver.createContainer(containerName);
			driver.close();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}

	@Override
	public void pushProvenanceInformation(String authorName) {
		pushProvenanceInformation(authorName, config.getContainerName());
	}

	@Override
	public void pushCatalog(SIRFCatalog catalog) {
		pushCatalog(catalog, config.getContainerName());
	}

	@Override
	public void deleteContainer() {
		SwiftDriver driver = new SwiftDriver(config);

		try {
			driver.deleteContainer(config.getContainerName());
			driver.close();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}

	@Override
	public SIRFCatalog getCatalog() {
		SIRFCatalog catalog = null;

		try {
			SwiftDriver driver = new SwiftDriver(config);
			InputStream is = driver.getFileInputStream(config.getContainerName(), SIRFContainer.SIRF_DEFAULT_CATALOG_ID);
			
			catalog = new SIRFCatalogUnmarshaller("application/json").unmarshalCatalog(is);
			driver.close();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		} catch (JAXBException jbe) {
			jbe.printStackTrace();
		}

		return catalog;
	}

	@Override
	public StreamingOutput getPreservationObjectStreamingOutput(String poName) {
		SwiftDriver driver = new SwiftDriver(config);
		StreamingOutput so = null;
		
		try {
			final InputStream is = driver.getFileInputStream(config.getContainerName(), poName);
			
			so = new StreamingOutput() {
				public void write(OutputStream out) throws IOException, WebApplicationException {
                    int read = 0;
                    byte[] bytes = new byte[1024];

                    while ((read = is.read(bytes)) != -1)
                        out.write(bytes, 0, read);
				}
			};
			
			driver.close();<commit_msg>Ammending previous commit.<commit_after>package org.opensirf.jaxrs.api;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringWriter;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.StreamingOutput;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;

import org.eclipse.persistence.jaxb.MarshallerProperties;
import org.opensirf.catalog.SIRFCatalog;
import org.opensirf.container.ProvenanceInformation;
import org.opensirf.container.SIRFContainer;
import org.opensirf.format.ProvenanceInformationMarshaller;
import org.opensirf.format.SIRFCatalogMarshaller;
import org.opensirf.format.SIRFCatalogUnmarshaller;
import org.opensirf.jaxrs.config.ContainerConfiguration;
import org.opensirf.jaxrs.config.SwiftConfiguration;
import org.opensirf.jaxrs.model.MagicObject;

public class SwiftStrategy implements StorageContainerStrategy {
	protected SwiftStrategy() {
		
	}
	
	protected SwiftStrategy(ContainerConfiguration c) { 
		this.config = c;
	}
	
	public void setConfig(ContainerConfiguration c) {
		this.config = c;
	}

	public ContainerConfiguration getConfig() {
		return config;
	}

	private ContainerConfiguration config;

	@Override
	public MagicObject retrieveMagicObject() {
		try {
			SwiftDriver driver = new SwiftDriver(config);
			MagicObject mo = driver.containerMetadata(config.getContainerName());
			driver.close();
			return mo;
		} catch (IOException ioe) {
			ioe.printStackTrace();
		} 
		
		return null;
	}

	@Override
	public void createContainer(String containerName) {
		try {
			SwiftDriver driver = new SwiftDriver(config);
			System.out.println("Calling driver, container name = " + containerName);
			driver.createContainer(containerName);
			driver.close();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}

	@Override
	public void pushProvenanceInformation(String authorName) {
		pushProvenanceInformation(authorName, config.getContainerName());
	}

	@Override
	public void pushCatalog(SIRFCatalog catalog) {
		pushCatalog(catalog, config.getContainerName());
	}

	@Override
	public void deleteContainer() {
		SwiftDriver driver = new SwiftDriver(config);

		try {
			driver.deleteContainer(config.getContainerName());
			driver.close();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}

	@Override
	public SIRFCatalog getCatalog() {
		SIRFCatalog catalog = null;

		try {
			SwiftDriver driver = new SwiftDriver(config);
			InputStream is = driver.getFileInputStream(config.getContainerName(), SIRFContainer.SIRF_DEFAULT_CATALOG_ID);
			
			catalog = new SIRFCatalogUnmarshaller("application/json").unmarshalCatalog(is);
			driver.close();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		} catch (JAXBException jbe) {
			jbe.printStackTrace();
		} catch(NullPointerException npe) {
			return null;
		}

		return catalog;
	}

	@Override
	public StreamingOutput getPreservationObjectStreamingOutput(String poName) {
		SwiftDriver driver = new SwiftDriver(config);
		StreamingOutput so = null;
		
		try {
			final InputStream is = driver.getFileInputStream(config.getContainerName(), poName);
			
			so = new StreamingOutput() {
				public void write(OutputStream out) throws IOException, WebApplicationException {
                    int read = 0;
                    byte[] bytes = new byte[1024];

                    while ((read = is.read(bytes)) != -1)
                        out.write(bytes, 0, read);
				}
			};
			
			driver.close();
{'fraction_non_alphanumeric': 0.0997676643433101, 'fraction_numerical': 0.001913352466858002, 'mean_word_length': 4.400738007380074, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4551645', 'n_tokens_mistral': 2383, 'n_tokens_neox': 2147, 'n_words': 441}