package no.difi.sdp.client.asice;

import no.difi.sdp.client.asice.signature.CreateSignature;
import no.difi.sdp.client.domain.Avsender;
import no.difi.sdp.client.domain.Forsendelse;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class AsicEGenerator {

    private final CreateManifest createManifest;
    private final CreateSignature createSignature;
    private final CreateZip createZip;

    public AsicEGenerator() {
        createManifest = new CreateManifest();
        createSignature = new CreateSignature();
        createZip = new CreateZip();
    }

    public InputStream createStream(Avsender avsender, Forsendelse forsendelse) {
        // Lag Asic-E manifest
        Manifest manifest = createManifest.createManifest(avsender, forsendelse);

        List<AsicEAttachable> files = new ArrayList<AsicEAttachable>();
        files.add(forsendelse.getDokumentpakke().getHoveddokument());
        files.addAll(forsendelse.getDokumentpakke().getVedlegg());
        files.add(manifest);

        // Lag signatur over alle filene i pakka
        Signature signature = createSignature.createSignature(avsender.getNoekkelpar(), files);
        files.add(signature);

        // Zip filene
        Archive archive = createZip.zipIt(files);

        return new ByteArrayInputStream(archive.getBytes());
    }

}

@@ -180,44 +180,221 @@
 se;%0A
-%0Aimport java.io.ByteArrayInputStream
+import org.apache.commons.io.IOUtils;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Aimport java.io.ByteArrayInputStream;%0Aimport java.io.File;%0Aimport java.io.FileOutputStream;%0Aimport java.io.IOException
 ;%0Aim
@@ -642,16 +642,141 @@
 teZip;%0A%0A
+    private final Logger log = LoggerFactory.getLogger(this.getClass());%0A%0A    private static File debug_writeToDisk = null;%0A%0A
     publ
@@ -1633,66 +1633,1322 @@
     
-return new ByteArrayInputStream(archive.getBytes());%0A    %7D%0A
+if (debug_writeToDisk != null) %7B%0A            writeArchiveToDisk(archive);%0A        %7D%0A%0A        return new ByteArrayInputStream(archive.getBytes());%0A    %7D%0A%0A%0A    private void writeArchiveToDisk(Archive archive) %7B%0A        log.error(%22Writing Asic-E to disk for debug%22);%0A        File file;%0A        if (debug_writeToDisk.isDirectory()) %7B%0A            file = new File(debug_writeToDisk, %22asic-%22 + System.currentTimeMillis() + %22.zip%22);%0A        %7D%0A        else %7B%0A            file = debug_writeToDisk;%0A        %7D%0A%0A        FileOutputStream output = null;%0A        try %7B%0A            output = new FileOutputStream(file);%0A            IOUtils.copy(new ByteArrayInputStream(archive.getBytes()), output);%0A            log.info(%22Wrote Asic-E debug file to %22 + file.getAbsolutePath());%0A        %7D catch (IOException e) %7B%0A            log.error(%22Could not write Asic-E file%22, e);%0A        %7D finally %7B%0A            IOUtils.closeQuietly(output);%0A        %7D%0A    %7D%0A%0A    /**%0A     * Skriv zippet Asic-E til disk for manuell inspeksjon.%0A     *%0A     * @deprecated Ikke for produksjonsbruk.%0A     *%0A     * @param location Mappe eller fil asic-filen skal skrives til. Hvis det er en mappe vil filnavn bli generert automatisk.%0A     */%0A    @Deprecated%0A    public static void debug_writeArchiveToDisk(File location) %7B%0A        debug_writeToDisk = location;%0A    %7D
 %0A%7D%0A

Create support for writing sent asic-e files to disk for debug