/** * Gives data file as stream * * @return data file stream */ public InputStream getStream() { logger.debug(""); return document.openStream(); }
/** * Returns the data file name. * * @return filename */ public String getName() { String documentName = document.getName(); String name = new File(documentName).getName(); logger.trace("File name: for document " + documentName + " is " + name); return name; }
public void setMediaType(String mediaType) { MimeType mimeType = getMimeType(mediaType); document.setMimeType(mimeType); }
MimeType mimeType = new MimeType(); mimeType.setMimeTypeString(mimeTypeString); documentToBeSigned.setMimeType(mimeType); DSSDocument signedDocument = service.signDocument(documentToBeSigned, parameters, signatureValue); return IOUtils.toByteArray(signedDocument.openStream());
/** * Returns the file media type. * * @return media type */ public String getMediaType() { logger.debug(""); String mediaType = document.getMimeType().getMimeTypeString(); logger.debug("Media type is: " + mediaType); return mediaType; }
/** * Saves a copy of the data file as a file with the specified file name. * * @param path full file path where the data file should be saved to. If the file exists it will be overwritten */ //TODO exception - method throws DSSException which can be caused by other exceptions public void saveAs(String path) { try { logger.debug("Path: " + path); document.save(path); } catch (IOException e) { logger.error("Failed to save path " + path); throw new TechnicalException("Failed to save path " + path, e); } }
/** * Returns the data file size. * * @return file size in bytes */ public long getFileSize() { logger.debug(""); long fileSize; if (document instanceof StreamDocument || document instanceof FileDocument) { try { fileSize = Files.size(Paths.get(document.getAbsolutePath())); logger.debug("Document size: " + fileSize); return fileSize; } catch (IOException e) { logger.error(e.getMessage()); throw new DigiDoc4JException(e); } } fileSize = getBytes().length; logger.debug("File document size: " + fileSize); return fileSize; }
private byte[] getDocumentBytes(DSSDocument document) { try { return IOUtils.toByteArray(document.openStream()); } catch (IOException e) { logger.error("Error getting document content: " + e.getMessage()); throw new TechnicalException("Error getting document content: " + e.getMessage(), e); } }
private List<String> getFilesInContainer() { List<String> fileEntries = new ArrayList<>(); List<String> signatureFileNames = getSignatureFileNames(); for (DSSDocument detachedContent : detachedContents) { String name = detachedContent.getName(); if (!(MANIFEST_PATH.equals(name) || ("META-INF/".equals(name)) || (MIMETYPE_PATH.equals(name) || signatureFileNames.contains(name)))) { fileEntries.add(name); } } return fileEntries; }
/** * Creates container. * * @param path file name with path * @param mimeType MIME type of the data file, for example 'text/plain' or 'application/msword' */ public DataFile(String path, String mimeType) { logger.debug("Path: " + path + ", mime type: " + mimeType); try { document = new FileDocument(path); document.setMimeType(getMimeType(mimeType)); } catch (Exception e) { logger.error(e.getMessage()); throw new InvalidDataFileException(e); } }
/** * This method returns the {@link org.w3c.dom.Document} created based on the {@link eu.europa.esig.dss.DSSDocument}. * * @param dssDocument * The DSS representation of the document from which the dssDocument is created. * @return a new {@link org.w3c.dom.Document} from {@link eu.europa.esig.dss.DSSDocument} */ public static Document buildDOM(final DSSDocument dssDocument) { return buildDOM(dssDocument.openStream()); }
private void removeExistingSignature(BDocSignature signature) { DSSDocument signatureDocument = signature.getSignatureDocument(); if (signatureDocument == null) { return; } String signatureFileName = signatureDocument.getName(); removeExistingFileFromContainer(signatureFileName); }
@Override public byte[] getAdESSignature() { logger.debug("Getting full XAdES signature byte array"); try { return IOUtils.toByteArray(signatureDocument.openStream()); } catch (IOException e) { throw new TechnicalException("Error parsing xades signature: " + e.getMessage(), e); } }
originalName = "container"; } else { originalName = originalFile.getName();
/** * Gives file bytes * * @return data as bytes */ public byte[] getBytes() { logger.debug(""); try { return IOUtils.toByteArray(document.openStream()); } catch (IOException e) { throw new TechnicalException("Error reading document bytes: " + e.getMessage(), e); } }
/** * Reads the first byte from the DSSDocument * * @param dssDocument * the document * @return the first byte */ public static byte readFirstByte(final DSSDocument dssDocument) { byte[] result = new byte[1]; try (InputStream inputStream = dssDocument.openStream()) { inputStream.read(result, 0, 1); } catch (IOException e) { throw new DSSException(e); } return result[0]; }
/** * Reads maximum {@code headerLength} bytes from {@code dssDocument} to the given {@code byte} array. * * @param dssDocument * {@code DSSDocument} to read * @param headerLength * {@code int}: maximum number of bytes to read * @param destinationByteArray * destination {@code byte} array * @return the number of read bytes */ public static int readToArray(final DSSDocument dssDocument, final int headerLength, final byte[] destinationByteArray) { try (InputStream inputStream = dssDocument.openStream()) { return inputStream.read(destinationByteArray, 0, headerLength); } catch (IOException e) { throw new DSSException(e); } }
public static byte[] digest(DigestAlgorithm digestAlgorithm, DSSDocument document) { try (InputStream is = document.openStream()) { return digest(digestAlgorithm, is); } catch (IOException e) { throw new DSSException(e); } }
/** * This method create a new document from a sub-part of another document * * @param origin * the original document * @param start * the start position to retrieve * @param end * the end position to retrieve * @return a new DSSDocument */ public static DSSDocument splitDocument(DSSDocument origin, int start, int end) { try (InputStream is = origin.openStream(); BufferedInputStream bis = new BufferedInputStream(is); ByteArrayOutputStream baos = new ByteArrayOutputStream()) { int i = 0; int r; while ((r = bis.read()) != -1) { if (i >= start && i <= end) { baos.write(r); } i++; } baos.flush(); return new InMemoryDocument(baos.toByteArray()); } catch (Exception e) { throw new DSSException("Unable to split document", 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; }