private boolean isAsicsMimeType(ZipEntry entry, ZipInputStream zipStream) { return entry.getName().equals(MIME_TYPE_FILE_NAME) && ASICS_MIME_TYPE.equals(new String(new InMemoryDocument(zipStream, entry.getName()).getBytes())); }
private InMemoryDocument getDataFile(byte[] proxyDocument) { try (ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(proxyDocument))) { ZipEntry entry; while ((entry = zipStream.getNextEntry()) != null) { if (!entry.getName().startsWith(META_INF_FOLDER) && !entry.getName().equalsIgnoreCase(MIME_TYPE_FILE_NAME)) { return new InMemoryDocument(zipStream, entry.getName()); } } } catch (IOException e) { throw new RuntimeException(e); } throw new IllegalArgumentException("Invalid document"); }
@Override protected DSSDocument createDssDocument(final ValidationDocument validationDocument) { if (validationDocument == null) { return null; } return new InMemoryDocument(validationDocument.getBytes()); }
/** * Creates in memory document container. * * @param data file content * @param fileName file name with path * @param mimeType MIME type of the data file, for example 'text/plain' or 'application/msword' */ public DataFile(byte[] data, String fileName, String mimeType) { logger.debug("File name: " + fileName + ", mime type: " + mimeType); ByteArrayInputStream stream = new ByteArrayInputStream(data); document = new InMemoryDocument(stream, fileName, getMimeType(mimeType)); IOUtils.closeQuietly(stream); }
private List<InMemoryDocument> getFilesFromContainer(ValidationDocument validationDocument) { List<InMemoryDocument> documents = new ArrayList<>(); try (ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(validationDocument.getBytes()));) { ZipEntry entry; while ((entry = zipStream.getNextEntry()) != null) { documents.add(new InMemoryDocument(zipStream, entry.getName())); } } catch (IOException e) { throw new MalformedDocumentException(e); } return documents; }
private void extractSignature(ZipEntry entry) { logger.debug("Extracting signature"); InputStream zipFileInputStream = getZipEntryInputStream(entry); String fileName = entry.getName(); InMemoryDocument document = new InMemoryDocument(zipFileInputStream, fileName); signatures.add(document); extractSignatureAsicEntry(entry, document); }
/** * Creates in memory document container. * * @param stream file content from stream * @param fileName file name with path * @param mimeType MIME type of the stream file, for example 'text/plain' or 'application/msword' */ public DataFile(InputStream stream, String fileName, String mimeType) { logger.debug("File name: " + fileName + ", mime type: " + mimeType); try { document = new InMemoryDocument(stream, fileName, getMimeType(mimeType)); } catch (Exception e) { logger.error(e.getMessage()); throw new InvalidDataFileException(e); } }
@Override public Signature openAdESSignature(byte[] signatureDocument) { if (signatureDocument == null) { logger.error("Signature cannot be empty"); throw new InvalidSignatureException(); } InMemoryDocument document = new InMemoryDocument(signatureDocument); return createSignature(document); }
/** * Creates signature object from XadES signature xml. * * @param signatureDocument XadES signature xml bytes. * @return builder for creating a signature. */ public Signature openAdESSignature(byte[] signatureDocument) { if (signatureDocument == null) { logger.error("Signature cannot be empty"); throw new InvalidSignatureException(); } InMemoryDocument document = new InMemoryDocument(signatureDocument); return createSignature(document); }
/** * This method returns true if the inputStream starts with an ASN.1 Sequence * * @param is * the inputstream to be tested * @return true if DER encoded */ public static boolean isStartWithASN1SequenceTag(InputStream is) { byte firstByte = readFirstByte(new InMemoryDocument(is)); return DSSASN1Utils.isASN1SequenceTag(firstByte); }
private void parseZipFileManifest() { ZipEntry entry = zipFile.getEntry(MANIFEST); if (entry == null) { return; } try { InputStream manifestStream = getZipEntryInputStream(entry); InMemoryDocument manifestFile = new InMemoryDocument(IOUtils.toByteArray(manifestStream)); parseManifestEntry(manifestFile); } catch (IOException e) { logger.error("Error parsing manifest file: " + e.getMessage()); throw new TechnicalException("Error parsing manifest file", e); } } }
/** * This method creates a new InMemoryDocument with the {@link org.w3c.dom.Document} content and the given name * * @param document * the {@link org.w3c.dom.Document} to store * @param name * the ouput filename * @return a new instance of InMemoryDocument with the XML and the given filename */ public static DSSDocument createDssDocumentFromDomDocument(Document document, String name) { try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { DomUtils.writeDocumentTo(document, baos); return new InMemoryDocument(baos.toByteArray(), name, MimeType.XML); } catch (IOException e) { throw new DSSException(e); } }
private DSSDocument extractStreamDocument(ZipEntry entry) { logger.debug("Zip entry size is <{}> bytes", entry.getSize()); MimeType mimeTypeCode = MimeTypeUtil.mimeTypeOf(this.getDataFileMimeType(entry.getName())); if (this.storeDataFilesOnlyInMemory || entry.getSize() <= this.maxDataFileCachedInBytes) { return new InMemoryDocument(this.getZipEntryInputStream(entry), entry.getName(), mimeTypeCode); } else { return new StreamDocument(this.getZipEntryInputStream(entry), entry.getName(), mimeTypeCode); } }
private DSSDocument surroundWithXadesXmlTag(DSSDocument signedDocument) { logger.debug("Surrounding signature document with xades tag"); Document signatureDom = DomUtils.buildDOM(signedDocument); Element signatureElement = signatureDom.getDocumentElement(); Document document = XmlDomCreator.createDocument(ASiCNamespace.NS, XmlDomCreator.ASICS_NS, signatureElement); byte[] documentBytes = DSSXMLUtils.serializeNode(document); return new InMemoryDocument(documentBytes); }
private void extractMimeType(ZipEntry entry) { try { InputStream zipFileInputStream = getZipEntryInputStream(entry); BOMInputStream bomInputStream = new BOMInputStream(zipFileInputStream); DSSDocument document = new InMemoryDocument(bomInputStream); mimeType = StringUtils.trim(IOUtils.toString(getDocumentBytes(document), "UTF-8")); extractAsicEntry(entry, document); } catch (IOException e) { logger.error("Error parsing container mime type: " + e.getMessage()); throw new TechnicalException("Error parsing container mime type: " + e.getMessage(), e); } }
private static boolean parseAsicContainer(BufferedInputStream stream, MimeType mtype) throws IOException { stream.mark(stream.available() + 1); ZipInputStream zipInputStream = new ZipInputStream(stream); try { ZipEntry entry; while ((entry = zipInputStream.getNextEntry()) != null) { if (StringUtils.equalsIgnoreCase("mimetype", entry.getName())) { InputStream zipFileInputStream = zipInputStream; BOMInputStream bomInputStream = new BOMInputStream(zipFileInputStream); DSSDocument document = new InMemoryDocument(bomInputStream); String mimeType = StringUtils.trim(IOUtils.toString(IOUtils.toByteArray(document.openStream()), "UTF-8")); if (StringUtils.equalsIgnoreCase(mimeType, mtype.getMimeTypeString())) { return true; } } } } catch (IOException e) { logger.error("Error reading asic container stream: " + e.getMessage()); throw new TechnicalException("Error reading asic container stream: ", e); } finally { stream.reset(); } return false; }
DSSDocument createDssDocument(final ValidationDocument validationDocument) { if (validationDocument == null) { return null; } final InMemoryDocument dssDocument = new InMemoryDocument(validationDocument.getBytes()); dssDocument.setName(validationDocument.getName()); dssDocument.setMimeType(MimeType.fromFileName(validationDocument.getName())); return dssDocument; }
private static DataFile getTimestampToken(OnlineTSPSource onlineTSPSource, DigestAlgorithm digestAlgorithm, byte[] digest) { DataFile timeStampToken = new DataFile(); TimeStampToken timeStampResponse = onlineTSPSource.getTimeStampResponse(digestAlgorithm, digest); String timestampFilename = "timestamp"; timeStampToken.setDocument( new InMemoryDocument(DSSASN1Utils.getEncoded(timeStampResponse), timestampFilename, MimeType.TST)); timeStampToken.setMediaType(MimeType.TST.getMimeTypeString()); return timeStampToken; }
@Test public void testNoHeader() { InputStream is = new ByteArrayInputStream(XML_TEXT.getBytes()); assertNotNull(DomUtils.buildDOM(is)); assertNotNull(DomUtils.buildDOM(XML_TEXT)); assertNotNull(DomUtils.buildDOM(new InMemoryDocument(XML_TEXT.getBytes(), "my xml"))); }
@Test public void testHeader() { InputStream is = new ByteArrayInputStream((XML_HEADER + XML_TEXT).getBytes()); assertNotNull(DomUtils.buildDOM(is)); assertNotNull(DomUtils.buildDOM(XML_HEADER + XML_TEXT)); assertNotNull(DomUtils.buildDOM(new InMemoryDocument((XML_HEADER + XML_TEXT).getBytes(), "my xml"))); }