Search in sources :

Example 11 with ShieldInputStream

use of org.olat.core.util.io.ShieldInputStream in project OpenOLAT by OpenOLAT.

the class ItemFileResourceValidator method validate.

public boolean validate(String filename, InputStream in) {
    boolean valid = false;
    if (filename.toLowerCase().endsWith(".xml")) {
        valid = validateXml(in);
        IOUtils.closeQuietly(in);
    } else if (filename.toLowerCase().endsWith(".zip")) {
        ZipInputStream oZip = new ZipInputStream(in);
        try {
            ZipEntry oEntr = oZip.getNextEntry();
            while (oEntr != null) {
                if (!oEntr.isDirectory()) {
                    if (validateXml(new ShieldInputStream(oZip))) {
                        valid = true;
                    }
                }
                oZip.closeEntry();
                oEntr = oZip.getNextEntry();
            }
        } catch (Exception e) {
            log.error("", e);
            valid = false;
        } finally {
            IOUtils.closeQuietly(oZip);
            IOUtils.closeQuietly(in);
        }
    }
    return valid;
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) ShieldInputStream(org.olat.core.util.io.ShieldInputStream) FileNotFoundException(java.io.FileNotFoundException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 12 with ShieldInputStream

use of org.olat.core.util.io.ShieldInputStream in project OpenOLAT by OpenOLAT.

the class AssessmentItemFileResourceValidator method walkZip.

private boolean walkZip(ZipInputStream oZip) {
    boolean valid = false;
    try {
        ZipEntry oEntr = oZip.getNextEntry();
        while (oEntr != null) {
            if (!oEntr.isDirectory()) {
                String fname = oEntr.getName().toLowerCase();
                if (!"imsmanifest.xml".equals(fname) && fname.endsWith(".xml")) {
                    if (validateXml(new ShieldInputStream(oZip))) {
                        valid = true;
                    }
                } else if (fname.endsWith(".zip")) {
                    ZipInputStream subZip = new ZipInputStream(new ShieldInputStream(oZip));
                    valid |= walkZip(subZip);
                }
            }
            oZip.closeEntry();
            oEntr = oZip.getNextEntry();
        }
    } catch (Exception e) {
        log.error("", e);
        valid = false;
    }
    return valid;
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) ShieldInputStream(org.olat.core.util.io.ShieldInputStream) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 13 with ShieldInputStream

use of org.olat.core.util.io.ShieldInputStream in project openolat by klemens.

the class ExcelOOXMLDocument method parseSheets.

private String parseSheets(Map<String, String> sharedStrings, VFSLeaf leaf) throws IOException, DocumentException {
    try (InputStream stream = leaf.getInputStream();
        ZipInputStream zip = new ZipInputStream(stream)) {
        ZipEntry entry = zip.getNextEntry();
        LimitedContentWriter writer = new LimitedContentWriter(100000, FileDocumentFactory.getMaxFileSize());
        while (entry != null) {
            if (writer.accept()) {
                String name = entry.getName();
                if (name.startsWith(SHEET) && name.endsWith(".xml")) {
                    OfficeDocumentHandler dh = new OfficeDocumentHandler(writer, sharedStrings);
                    parse(new ShieldInputStream(zip), dh);
                }
            }
            entry = zip.getNextEntry();
        }
        return writer.toString();
    } catch (DocumentException e) {
        throw e;
    } catch (Exception e) {
        throw new DocumentException(e.getMessage());
    }
}
Also used : LimitedContentWriter(org.olat.core.util.io.LimitedContentWriter) ZipInputStream(java.util.zip.ZipInputStream) ZipInputStream(java.util.zip.ZipInputStream) ShieldInputStream(org.olat.core.util.io.ShieldInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ShieldInputStream(org.olat.core.util.io.ShieldInputStream) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 14 with ShieldInputStream

use of org.olat.core.util.io.ShieldInputStream in project openolat by klemens.

the class PowerPointOOXMLDocument method readContent.

@Override
public FileContent readContent(VFSLeaf leaf) throws IOException, DocumentException {
    File file = ((JavaIOItem) leaf).getBasefile();
    LimitedContentWriter writer = new LimitedContentWriter(100000, FileDocumentFactory.getMaxFileSize());
    try (ZipFile wordFile = new ZipFile(file)) {
        List<String> contents = new ArrayList<>();
        for (Enumeration<? extends ZipEntry> entriesEnumeration = wordFile.entries(); entriesEnumeration.hasMoreElements(); ) {
            ZipEntry entry = entriesEnumeration.nextElement();
            String name = entry.getName();
            if (name.startsWith(SLIDE) && name.endsWith(".xml")) {
                contents.add(name);
            }
        }
        if (contents.size() > 1) {
            Collections.sort(contents, new PowerPointDocumentComparator());
        }
        for (String content : contents) {
            if (writer.accept()) {
                ZipEntry entry = wordFile.getEntry(content);
                InputStream zip = wordFile.getInputStream(entry);
                OfficeDocumentHandler dh = new OfficeDocumentHandler(writer);
                parse(new ShieldInputStream(zip), dh);
                zip.close();
            }
        }
    } catch (DocumentException e) {
        throw e;
    } catch (Exception e) {
        throw new DocumentException(e.getMessage());
    }
    return new FileContent(writer.toString());
}
Also used : JavaIOItem(org.olat.core.util.vfs.JavaIOItem) ShieldInputStream(org.olat.core.util.io.ShieldInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) ShieldInputStream(org.olat.core.util.io.ShieldInputStream) IOException(java.io.IOException) LimitedContentWriter(org.olat.core.util.io.LimitedContentWriter) ZipFile(java.util.zip.ZipFile) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 15 with ShieldInputStream

use of org.olat.core.util.io.ShieldInputStream in project openolat by klemens.

the class WordOOXMLDocument method readContent.

@Override
public FileContent readContent(VFSLeaf leaf) throws IOException, DocumentException {
    File file = ((JavaIOItem) leaf).getBasefile();
    LimitedContentWriter writer = new LimitedContentWriter(100000, FileDocumentFactory.getMaxFileSize());
    try (ZipFile wordFile = new ZipFile(file)) {
        List<String> contents = new ArrayList<>();
        for (Enumeration<? extends ZipEntry> entriesEnumeration = wordFile.entries(); entriesEnumeration.hasMoreElements(); ) {
            ZipEntry entry = entriesEnumeration.nextElement();
            String name = entry.getName();
            if (name.endsWith("word/document.xml")) {
                contents.add(name);
            } else if (name.startsWith(HEADER) && name.endsWith(".xml")) {
                contents.add(name);
            } else if (name.startsWith(FOOTER) && name.endsWith(".xml")) {
                contents.add(name);
            }
        }
        if (contents.size() > 1) {
            Collections.sort(contents, new WordDocumentComparator());
        }
        for (String content : contents) {
            if (writer.accept()) {
                ZipEntry entry = wordFile.getEntry(content);
                InputStream zip = wordFile.getInputStream(entry);
                OfficeDocumentHandler dh = new OfficeDocumentHandler(writer);
                parse(new ShieldInputStream(zip), dh);
                zip.close();
            }
        }
    } catch (DocumentException e) {
        throw e;
    } catch (Exception e) {
        throw new DocumentException(e.getMessage());
    }
    return new FileContent(writer.toString());
}
Also used : JavaIOItem(org.olat.core.util.vfs.JavaIOItem) ShieldInputStream(org.olat.core.util.io.ShieldInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) ShieldInputStream(org.olat.core.util.io.ShieldInputStream) IOException(java.io.IOException) LimitedContentWriter(org.olat.core.util.io.LimitedContentWriter) ZipFile(java.util.zip.ZipFile) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Aggregations

ShieldInputStream (org.olat.core.util.io.ShieldInputStream)18 IOException (java.io.IOException)16 ZipEntry (java.util.zip.ZipEntry)16 InputStream (java.io.InputStream)12 ZipInputStream (java.util.zip.ZipInputStream)12 SAXException (org.xml.sax.SAXException)10 ArrayList (java.util.ArrayList)8 LimitedContentWriter (org.olat.core.util.io.LimitedContentWriter)6 File (java.io.File)4 ZipFile (java.util.zip.ZipFile)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4 JavaIOItem (org.olat.core.util.vfs.JavaIOItem)4 SAXParseException (org.xml.sax.SAXParseException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 URISyntaxException (java.net.URISyntaxException)2 FileSystem (java.nio.file.FileSystem)2 Path (java.nio.file.Path)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2