@Override public InputStream getStream() throws IOException { return blob.getStream(); }
protected boolean equalsStream(Blob other) { InputStream is = null; InputStream ois = null; try { is = getStream(); ois = other.getStream(); return IOUtils.contentEquals(is, ois); } catch (IOException e) { throw new RuntimeException(e); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(ois); } }
protected String getMD5Digest() { try (InputStream in = getBlob().getStream()) { return DigestUtils.md5Hex(in); } catch (IOException e) { throw new NuxeoException(e); } }
protected static void zip(Blob blob, ZipOutputStream out) throws IOException { String entry = getFileName(blob); InputStream in = blob.getStream(); try { ZipUtils._zip(entry, in, out); } finally { in.close(); } }
protected static void zip(List<Blob> blobs, ZipOutputStream out) throws IOException { // use a set to avoid zipping entries with same names Collection<String> names = new HashSet<String>(); int cnt = 1; for (Blob blob : blobs) { String entry = getFileName(blob); if (!names.add(entry)) { entry = "renamed_" + (cnt++) + "_" + entry; } InputStream in = blob.getStream(); try { ZipUtils._zip(entry, in, out); } finally { in.close(); } } }
@Override public Binary getBinary(Blob blob) throws IOException { if (blob instanceof BinaryBlob) { Binary binary = ((BinaryBlob) blob).getBinary(); if (binary.getBlobProviderId().equals(blobProviderId)) { return binary; } // don't reuse the binary if it comes from another blob provider } try (InputStream stream = blob.getStream()) { return getBinary(stream); } }
@Override public InputStream getInputStream() throws IOException { return blob.getStream(); }
@Override public String writeBlob(Blob blob) throws IOException { byte[] bytes; try (InputStream in = blob.getStream()) { bytes = IOUtils.toByteArray(in); } String k = String.valueOf(counter.incrementAndGet()); blobs.put(k, bytes); return k; }
/** * * @param blob the blob containing the serialized object * @return the unserialized object * @throws Exception if any processing error occurs during unserialization */ public Serializable unserializeObjectIntoBlob(Blob blob) throws Exception { Serializable object = null; ObjectInputStream ois = null; ois = new ObjectInputStream(blob.getStream()); object = (Serializable) ois.readObject(); ois.close(); return object; } }
public String extractAttributes(Blob blob) throws ParserConfigurationException, SAXException, IOException { if (blob == null) { return null; } SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); Set<String> attributes = new HashSet<>(); saxParser.parse(blob.getStream(), new Handler(attributes)); return StringUtils.join(attributes, ' '); }
@OperationMethod(collector = BlobCollector.class) public Blob run(Blob blob) throws IOException { URL target = new URL(url); URLConnection conn = target.openConnection(); conn.setDoOutput(true); try (InputStream in = blob.getStream(); OutputStream out = conn.getOutputStream()) { IOUtils.copy(in, out); out.flush(); } return blob; }
/** * Writes a summary file and puts it in the archive. */ private void addSummaryToZip(ZipOutputStream out, byte[] data, StringBuilder sb) throws IOException { Blob content = Blobs.createBlob(sb.toString()); BufferedInputStream buffi = new BufferedInputStream(content.getStream(), BUFFER); ZipEntry entry = new ZipEntry(SUMMARY_FILENAME); out.putNextEntry(entry); int count = buffi.read(data, 0, BUFFER); while (count != -1) { out.write(data, 0, count); count = buffi.read(data, 0, BUFFER); } out.closeEntry(); buffi.close(); }
@Override public void transferBlobWithByteRange(Blob blob, ByteRange byteRange, Supplier<OutputStream> outputStreamSupplier) { try (InputStream in = blob.getStream()) { @SuppressWarnings("resource") OutputStream out = outputStreamSupplier.get(); // not ours to close BufferingServletOutputStream.stopBuffering(out); if (byteRange == null) { IOUtils.copy(in, out); } else { IOUtils.copyLarge(in, out, byteRange.getStart(), byteRange.getLength()); } out.flush(); } catch (IOException e) { throw new UncheckedIOException(e); } }
protected Blob crop(Blob blob, Map<String, Serializable> coords) { try { BlobHolder bh = new SimpleBlobHolder(blob); String type = blob.getMimeType(); Map<String, Serializable> options = new HashMap<>(); options.put(OPTION_CROP_X, coords.get("x")); options.put(OPTION_CROP_Y, coords.get("y")); options.put(OPTION_RESIZE_HEIGHT, coords.get("h")); options.put(OPTION_RESIZE_WIDTH, coords.get("w")); if (!"image/png".equals(type)) { bh = getConversionService().convert(OPERATION_CROP, bh, options); return Blobs.createBlob(bh.getBlob().getStream(), type); } } catch (IOException e) { throw new NuxeoException("Crop failed", e); } return null; }
@Override public Blob overlayPDF(Blob input, Blob overlayBlob) { try (PDDocument pdfDoc = PDDocument.load(input.getStream()); PDDocument pdfOverlayDoc = PDDocument.load(overlayBlob.getStream())) { Overlay overlay = new Overlay(); overlay.overlay(pdfOverlayDoc, pdfDoc); return saveInTempFile(pdfDoc); } catch (IOException | COSVisitorException e) { throw new NuxeoException(e); } }
@OperationMethod public Blob run(Blob blob) throws OperationException, IOException, COSVisitorException { PDFMergerUtility ut = new PDFMergerUtility(); checkPdf(blob); if (xpathBlobToAppend.isEmpty()) { return blob; } handleBlobToAppend(ut); ut.addSource(blob.getStream()); return appendPDFs(ut); }
@OperationMethod public Blob run(BlobList blobs) throws IOException, OperationException, COSVisitorException { PDFMergerUtility ut = new PDFMergerUtility(); if (!xpathBlobToAppend.isEmpty()) { handleBlobToAppend(ut); } for (Blob blob : blobs) { checkPdf(blob); ut.addSource(blob.getStream()); } return appendPDFs(ut); }
/** * Check if blob to append is a PDF blob. */ protected void handleBlobToAppend(PDFMergerUtility ut) throws IOException, OperationException { try { Blob blobToAppend = (Blob) ctx.get(xpathBlobToAppend); if (blobToAppend == null) { throw new OperationException("The blob to append from variable context: '" + xpathBlobToAppend + "' is null."); } checkPdf(blobToAppend); ut.addSource(blobToAppend.getStream()); } catch (ClassCastException e) { throw new OperationException("The blob to append from variable context: '" + xpathBlobToAppend + "' is not a blob.", e); } }
public void addBlob(Blob blob) throws MessagingException, IOException { MimeBodyPart part = new MimeBodyPart(); part.setDataHandler(new DataHandler(new InputStreamDataSource(blob.getStream(), blob.getMimeType(), blob.getFilename()))); part.setDisposition("attachment"); String filename = blob.getFilename(); if (filename != null) { part.setFileName(filename); } // must set the mime type at end because setFileName is also setting a // wrong content type. String mimeType = blob.getMimeType(); if (mimeType != null) { part.setHeader("Content-type", mimeType); } addBodyPart(part); }
protected void checkBlob(Document doc, BlobAccessor accessor) { Blob blob = accessor.getBlob(); if (!(blob instanceof ManagedBlob)) { return; } // compare current provider with expected String expectedProviderId = getProviderId(doc, blob, accessor.getXPath()); if (((ManagedBlob) blob).getProviderId().equals(expectedProviderId)) { return; } // re-write blob // TODO add APIs so that blob providers can copy blobs efficiently from each other Blob newBlob; try (InputStream in = blob.getStream()) { newBlob = Blobs.createBlob(in, blob.getMimeType(), blob.getEncoding()); newBlob.setFilename(blob.getFilename()); newBlob.setDigest(blob.getDigest()); } catch (IOException e) { throw new RuntimeException(e); } accessor.setBlob(newBlob); }