package at.gv.egiz.asic.api; import at.gv.egiz.asic.exceptions.ASiCException; import at.gv.egiz.asic.impl.ASiCBaseFormatFactory; import at.gv.egiz.asic.impl.ASiCExtendedFormatFactory; import at.gv.egiz.asic.impl.ASiCSimpleFormatFactory; import at.gv.egiz.asic.impl.ZipCommentReaderStream; import at.gv.egovernment.moa.spss.MOAApplicationException; import at.gv.egovernment.moa.spss.MOAException; import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; /** * Created by afitzek on 6/15/16. */ public class ASiCFactory { private static final Logger logger = LoggerFactory.getLogger(ASiCFactory.class); private static final String MIMETYPE_FORMAT_E = "application/vnd.etsi.asic-e+zip"; private static final String MIMETYPE_FORMAT_S = "application/vnd.etsi.asic-s+zip"; public static ASiC parseASiC(InputStream is, ASiCFormat format) throws MOAException { InputStream newInputStream = is; // Try to determine the asic format! if (!newInputStream.markSupported()) { ByteArrayOutputStream asicContainer = new ByteArrayOutputStream(); try { IOUtils.copy(newInputStream, asicContainer); } catch (IOException e) { throw new MOAApplicationException("asic.0003", null); } newInputStream = new ByteArrayInputStream(asicContainer.toByteArray()); } String mimeTypeFile = null; ZipCommentReaderStream commentReaderStream = new ZipCommentReaderStream(newInputStream); byte[] buffer = new byte[8096]; try { while (commentReaderStream.read(buffer) >= 0) { } newInputStream.reset(); } catch (IOException e) { throw new MOAApplicationException("asic.0003", null); } ZipInputStream zipInputStream = new ZipInputStream(newInputStream); try { for (ZipEntry entry = zipInputStream.getNextEntry(); entry != null; entry = zipInputStream.getNextEntry()) { String entryName = entry.getName(); if("mimetype".equalsIgnoreCase(entryName)) { if(mimeTypeFile == null) { mimeTypeFile = IOUtils.toString(zipInputStream, "UTF-8"); } else { logger.warn("multiple mimetype files found in archiv"); } } } newInputStream.reset(); } catch (IOException e) { throw new MOAApplicationException("asic.0007", null); } String fileComment = commentReaderStream.getFileComment(); ASiCFormat fileCommentFormat = null; if (fileComment != null) { logger.info("Found file comment in ASiC {}", fileComment); if(fileComment.startsWith("mimetype=")) { String fileCommentMimeType = fileComment.substring("mimetype=".length()); if(fileCommentMimeType.startsWith(MIMETYPE_FORMAT_E)) { fileCommentFormat = ASiCFormat.ASiCE; } else if(fileCommentMimeType.startsWith(MIMETYPE_FORMAT_S)) { fileCommentFormat = ASiCFormat.ASiCS; } } } else { logger.info("No file comment in ASiC"); } ASiCFormat mimeTypeFileFormat = null; if (mimeTypeFile != null) { logger.info("Found mimetype file in ASiC {}", mimeTypeFile); if(MIMETYPE_FORMAT_E.equalsIgnoreCase(mimeTypeFile)) { mimeTypeFileFormat = ASiCFormat.ASiCE; } else if(MIMETYPE_FORMAT_S.equalsIgnoreCase(mimeTypeFile)) { mimeTypeFileFormat = ASiCFormat.ASiCS; } } else { logger.info("No mimetype file in ASiC"); } if (format == null) { if (fileCommentFormat != null && mimeTypeFileFormat != null) { // both are set if (fileCommentFormat == mimeTypeFileFormat) { format = fileCommentFormat; } else { throw new MOAApplicationException("asic.0009", null); } } else if (fileCommentFormat != null) { format = fileCommentFormat; } else if (mimeTypeFileFormat != null) { format = mimeTypeFileFormat; } else { throw new MOAApplicationException("asic.0008", null); } } else { // format is provided, only check for missmatches if (fileCommentFormat != null && fileCommentFormat != format) { logger.warn("ASiC format missmatch file comment {} vs provided {}", fileCommentFormat, format); throw new MOAApplicationException("asic.0009", null); } if (mimeTypeFileFormat != null && mimeTypeFileFormat != format) { logger.warn("ASiC format missmatch mimetype file {} vs provided {}", mimeTypeFileFormat, format); throw new MOAApplicationException("asic.0009", null); } if (fileCommentFormat != null && mimeTypeFileFormat != null) { // both are set if (fileCommentFormat != mimeTypeFileFormat) { logger.warn("ASiC format missmatch file comment {} vs mimetype file {}", fileCommentFormat, mimeTypeFileFormat); throw new MOAApplicationException("asic.0009", null); } } } ASiCBaseFormatFactory formatFactory = null; if (format == null) { throw new MOAApplicationException("asic.0008", null); } switch (format) { case ASiCE: formatFactory = new ASiCExtendedFormatFactory(); break; case ASiCS: formatFactory = new ASiCSimpleFormatFactory(); break; } if (formatFactory == null) { throw new MOAApplicationException("asic.0008", null); } return formatFactory.createASiC(newInputStream); } }