Search in sources :

Example 16 with ShieldInputStream

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

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 17 with ShieldInputStream

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

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 18 with ShieldInputStream

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

the class QTI21ImportProcessor method process.

public List<QuestionItem> process(File file) {
    // export zip file
    List<QuestionItem> items = new ArrayList<>();
    try (FileSystem fs = FileSystems.newFileSystem(file.toPath(), null)) {
        Path fPath = fs.getPath("/");
        if (fPath != null) {
            ImsManifestVisitor visitor = new ImsManifestVisitor();
            Files.walkFileTree(fPath, visitor);
            List<Path> imsmanifests = visitor.getImsmanifestFiles();
            for (Path imsmanifest : imsmanifests) {
                InputStream in = Files.newInputStream(imsmanifest);
                ManifestBuilder manifestBuilder = ManifestBuilder.read(new ShieldInputStream(in));
                List<ResourceType> resources = manifestBuilder.getResourceList();
                for (ResourceType resource : resources) {
                    ManifestMetadataBuilder metadataBuilder = manifestBuilder.getMetadataBuilder(resource, true);
                    QuestionItem qitem = processResource(resource, imsmanifest, metadataBuilder);
                    if (qitem != null) {
                        items.add(qitem);
                    }
                }
            }
        }
    } catch (IOException e) {
        log.error("", e);
    }
    return items;
}
Also used : Path(java.nio.file.Path) ManifestBuilder(org.olat.ims.qti21.model.xml.ManifestBuilder) ShieldInputStream(org.olat.core.util.io.ShieldInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ResourceType(org.olat.imscp.xml.manifest.ResourceType) IOException(java.io.IOException) ShieldInputStream(org.olat.core.util.io.ShieldInputStream) ManifestMetadataBuilder(org.olat.ims.qti21.model.xml.ManifestMetadataBuilder) FileSystem(java.nio.file.FileSystem) QuestionItem(org.olat.modules.qpool.QuestionItem)

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