package at.gv.egiz.asic.impl; import at.gv.egiz.asic.api.ASiC; import at.gv.egiz.asic.api.ASiCEntry; import at.gv.egiz.asic.api.ASiCFormat; import at.gv.egiz.asic.impl.handler.*; import at.gv.egovernment.moa.spss.MOAApplicationException; import at.gv.egovernment.moa.spss.MOAException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; /** * Created by Andreas Fitzek on 6/15/16. */ public abstract class ASiCBaseFormatFactory implements ASiCContainer { private static final Logger logger = LoggerFactory.getLogger(ASiCBaseFormatFactory.class); protected boolean cadesSigned = false; protected boolean xadesSigned = false; protected boolean timestamped = false; protected String mimeType = null; protected List signatureEntries = new ArrayList(); protected List dataEntries = new ArrayList(); protected List informationEntries = new ArrayList(); protected List handlers = new ArrayList(); @Override public void addDataEntry(ASiCEntry entry) { this.dataEntries.add(entry); } @Override public void addSignatureEntry(ASiCEntry entry) { this.signatureEntries.add(entry); } @Override public void addInformationEntry(ASiCEntry entry) { this.informationEntries.add(entry); } public ASiCBaseFormatFactory() { handlers.add(new MimefileHandler()); handlers.add(new MetaInfHandler()); handlers.add(new AllDataHandler()); } public abstract ASiCFormat factoryFormat(); protected abstract void validate() throws MOAException; public ASiC createASiC(InputStream is) throws MOAException { ZipCommentReaderStream commentReaderStream = new ZipCommentReaderStream(is); ZipInputStream zipInputStream = new ZipInputStream(commentReaderStream); try { for (ZipEntry entry = zipInputStream.getNextEntry(); entry != null; entry = zipInputStream.getNextEntry()) { String entryName = entry.getName(); Iterator handlerIterator = this.handlers.iterator(); while (handlerIterator.hasNext()) { EntryHandler entryHandler = handlerIterator.next(); if (entryHandler.handle(entryName, zipInputStream, this)) { break; } } } } catch(IOException e) { logger.info("Failed to read from ASiC Container", e); throw new MOAApplicationException("asic.0007", null); } if(this.mimeType == null) { String mimeTypeComment = commentReaderStream.getFileComment(); if(mimeTypeComment != null) { this.mimeType = mimeTypeComment; } } this.validate(); // unpack and retrieve all available information on ASiC signature return new ASiCImpl(this.factoryFormat(), this.xadesSigned, this.cadesSigned, this.signatureEntries, dataEntries, this.informationEntries); } @Override public void setMimeType(String mimeType) { this.mimeType = mimeType; } public void setIsXAdES() throws MOAException { if(this.cadesSigned) { throw new MOAApplicationException("asic.0010", null); } this.xadesSigned = true; } public void setIsCAdES() throws MOAException { if(this.xadesSigned) { throw new MOAApplicationException("asic.0010", null); } this.cadesSigned = true; } public void setIsTimestamped() throws MOAException { throw new MOAApplicationException("asic.0013", null); } @Override public void setIsEvidenceERS() throws MOAException { throw new MOAApplicationException("asic.0011", null); } @Override public void setIsEvidenceXML() throws MOAException { throw new MOAApplicationException("asic.0012", null); } }