Search in sources :

Example 16 with ShieldOutputStream

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

the class BinderTemplateMediaResource method prepare.

@Override
public void prepare(HttpServletResponse hres) {
    try {
        hres.setCharacterEncoding("UTF-8");
    } catch (Exception e) {
        log.error("", e);
    }
    try (ZipOutputStream zout = new ZipOutputStream(hres.getOutputStream())) {
        PortfolioService portfolioService = CoreSpringFactory.getImpl(PortfolioService.class);
        Binder loadedTemplate = portfolioService.getBinderByKey(template.getKey());
        String label = loadedTemplate.getTitle();
        String secureLabel = StringHelper.transformDisplayNameToFileSystemName(label);
        String file = secureLabel + ".zip";
        hres.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + StringHelper.urlEncodeUTF8(file));
        hres.setHeader("Content-Description", StringHelper.urlEncodeUTF8(label));
        zout.setLevel(9);
        zout.putNextEntry(new ZipEntry("binder.xml"));
        BinderXStream.toStream(loadedTemplate, zout);
        zout.closeEntry();
        if (StringHelper.containsNonWhitespace(loadedTemplate.getImagePath())) {
            File posterImage = portfolioService.getPosterImageFile(loadedTemplate);
            if (posterImage.exists()) {
                zout.putNextEntry(new ZipEntry(loadedTemplate.getImagePath()));
                FileUtils.copyFile(posterImage, new ShieldOutputStream(zout));
                zout.closeEntry();
            }
        }
        OLATResource resource = templateEntry.getOlatResource();
        File baseContainer = FileResourceManager.getInstance().getFileResource(resource);
        RepositoryEntryImportExport importExport = new RepositoryEntryImportExport(templateEntry, baseContainer);
        importExport.exportDoExportProperties(zout);
    } catch (Exception e) {
        log.error("", e);
    }
}
Also used : Binder(org.olat.modules.portfolio.Binder) ShieldOutputStream(org.olat.core.util.io.ShieldOutputStream) PortfolioService(org.olat.modules.portfolio.PortfolioService) RepositoryEntryImportExport(org.olat.repository.RepositoryEntryImportExport) ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) OLATResource(org.olat.resource.OLATResource) File(java.io.File)

Example 17 with ShieldOutputStream

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

the class CheckListExcelExport method exportAll.

public void exportAll(String filename, ZipOutputStream exportStream) {
    List<AssessmentData> dataList = checkboxManager.getAssessmentDatas(course, courseNode.getIdent(), null, null);
    try (OutputStream out = new ShieldOutputStream(exportStream)) {
        exportStream.putNextEntry(new ZipEntry(filename + ".xlsx"));
        exportWorkbook(dataList, out);
        exportStream.closeEntry();
    } catch (IOException e) {
        log.error("", e);
    }
}
Also used : AssessmentData(org.olat.course.nodes.cl.model.AssessmentData) ShieldOutputStream(org.olat.core.util.io.ShieldOutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) OutputStream(java.io.OutputStream) ShieldOutputStream(org.olat.core.util.io.ShieldOutputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException)

Example 18 with ShieldOutputStream

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

the class TACourseNode method archiveNodeData.

@Override
public boolean archiveNodeData(Locale locale, ICourse course, ArchiveOptions options, ZipOutputStream exportStream, String charset) {
    boolean dataFound = false;
    String dropboxPath = DropboxController.getDropboxPathRelToFolderRoot(course.getCourseEnvironment(), this);
    OlatRootFolderImpl dropboxDir = new OlatRootFolderImpl(dropboxPath, null);
    String solutionsPath = TACourseNode.getFoldernodesPathRelToFolderBase(course.getCourseEnvironment()) + "/" + this.getIdent();
    OlatRootFolderImpl solutionDir = new OlatRootFolderImpl(solutionsPath, null);
    String returnboxPath = ReturnboxController.getReturnboxPathRelToFolderRoot(course.getCourseEnvironment(), this);
    OlatRootFolderImpl returnboxDir = new OlatRootFolderImpl(returnboxPath, null);
    Boolean hasTask = (Boolean) getModuleConfiguration().get(TACourseNode.CONF_TASK_ENABLED);
    String dirName = "task_" + StringHelper.transformDisplayNameToFileSystemName(getShortName()) + "_" + Formatter.formatDatetimeFilesystemSave(new Date(System.currentTimeMillis()));
    if (dropboxDir.exists() || solutionDir.exists() || returnboxDir.exists() || hasTask.booleanValue()) {
        // prepare writing course results overview table
        List<Identity> users = ScoreAccountingHelper.loadUsers(course.getCourseEnvironment(), options);
        Set<String> dropboxNames = null;
        if (options != null && (options.getGroup() != null || options.getIdentities() != null)) {
            dropboxNames = new HashSet<String>();
            for (Identity user : users) {
                dropboxNames.add(user.getName());
            }
        }
        String courseTitle = course.getCourseTitle();
        String fileName = ExportUtil.createFileNameWithTimeStamp(courseTitle, "xlsx");
        List<AssessableCourseNode> nodes = Collections.<AssessableCourseNode>singletonList(this);
        // write course results overview table to filesystem
        try (OutputStream out = new ShieldOutputStream(exportStream)) {
            exportStream.putNextEntry(new ZipEntry(dirName + "/" + fileName));
            ScoreAccountingHelper.createCourseResultsOverviewXMLTable(users, nodes, course, locale, out);
            exportStream.closeEntry();
        } catch (IOException e) {
            log.error("", e);
        }
        // copy solutions to tmp dir
        if (solutionDir.exists()) {
            for (VFSItem child : solutionDir.getItems()) {
                dataFound = true;
                ZipUtil.addToZip(child, dirName + "/solutions", exportStream);
            }
        }
        // copy dropboxes to tmp dir
        if (dropboxDir.exists()) {
            // OLAT-6362 archive only dropboxes of users that handed in at least one file -> prevent empty folders in archive
            List<VFSItem> dropBoxContent = dropboxDir.getItems();
            for (VFSItem file : dropBoxContent) {
                if ((dropboxNames == null || dropboxNames.contains(file.getName())) && VFSManager.isDirectoryAndNotEmpty(file)) {
                    dataFound = true;
                    ZipUtil.addToZip(file, dirName + "/dropboxes", exportStream);
                }
            }
        }
        // copy only the choosen task to user taskfolder, loop over all users
        String taskfolderPath = TACourseNode.getTaskFolderPathRelToFolderRoot(course.getCourseEnvironment(), this);
        OlatRootFolderImpl taskfolderDir = new OlatRootFolderImpl(taskfolderPath, null);
        for (Identity identity : users) {
            // check if user already chose a task
            String assignedTask = TaskController.getAssignedTask(identity, course.getCourseEnvironment(), this);
            if (assignedTask != null) {
                VFSItem item = taskfolderDir.resolve(assignedTask);
                if (item != null) {
                    // copy choosen task to user folder
                    ZipUtil.addToZip(item, dirName + "/taskfolders/" + identity.getName(), exportStream);
                    dataFound = true;
                }
            }
        }
        // copy returnboxes
        if (returnboxDir.exists()) {
            // OLAT-6362 archive only existing returnboxes -> prevent empty folders in archive
            List<VFSItem> returnBoxContent = returnboxDir.getItems();
            for (VFSItem file : returnBoxContent) {
                if ((dropboxNames == null || dropboxNames.contains(file.getName())) && VFSManager.isDirectoryAndNotEmpty(file)) {
                    dataFound = true;
                    ZipUtil.addToZip(file, dirName + "/returnboxes", exportStream);
                }
            }
        }
        // assessment documents
        if (getModuleConfiguration().getBooleanSafe(MSCourseNode.CONFIG_KEY_HAS_INDIVIDUAL_ASSESSMENT_DOCS, false)) {
            for (Identity assessedIdentity : users) {
                List<File> assessmentDocuments = course.getCourseEnvironment().getAssessmentManager().getIndividualAssessmentDocuments(this, assessedIdentity);
                if (assessmentDocuments != null && !assessmentDocuments.isEmpty()) {
                    for (File document : assessmentDocuments) {
                        String path = dirName + "/assessment_documents/" + assessedIdentity.getName() + "/" + document.getName();
                        ZipUtil.addFileToZip(path, document, exportStream);
                    }
                }
            }
        }
    }
    return dataFound;
}
Also used : ShieldOutputStream(org.olat.core.util.io.ShieldOutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) OutputStream(java.io.OutputStream) ZipEntry(java.util.zip.ZipEntry) VFSItem(org.olat.core.util.vfs.VFSItem) IOException(java.io.IOException) Date(java.util.Date) OlatRootFolderImpl(org.olat.core.commons.modules.bc.vfs.OlatRootFolderImpl) ShieldOutputStream(org.olat.core.util.io.ShieldOutputStream) Identity(org.olat.core.id.Identity) File(java.io.File)

Example 19 with ShieldOutputStream

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

the class QTI21ExportProcessor method process.

public void process(QuestionItemFull qitem, ZipOutputStream zout) {
    String dir = qitem.getDirectory();
    File rootDirectory = qpoolFileStorage.getDirectory(dir);
    String rootDir = "qitem_" + qitem.getKey();
    File imsmanifest = new File(rootDirectory, "imsmanifest.xml");
    ManifestBuilder manifestBuilder;
    if (imsmanifest.exists()) {
        manifestBuilder = ManifestBuilder.read(imsmanifest);
    } else {
        manifestBuilder = new ManifestBuilder();
    }
    File resourceFile = new File(rootDirectory, qitem.getRootFilename());
    URI assessmentItemUri = resourceFile.toURI();
    ResolvedAssessmentItem resolvedAssessmentItem = qtiService.loadAndResolveAssessmentItemForCopy(assessmentItemUri, rootDirectory);
    enrichWithMetadata(qitem, resolvedAssessmentItem, manifestBuilder);
    try {
        zout.putNextEntry(new ZipEntry(rootDir + "/imsmanifest.xml"));
        manifestBuilder.write(new ShieldOutputStream(zout));
        zout.closeEntry();
    } catch (Exception e) {
        log.error("", e);
    }
    try {
        Files.walkFileTree(rootDirectory.toPath(), new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                String filename = file.getFileName().toString();
                if (!"imsmanifest.xml".equals(filename) && !filename.startsWith(".")) {
                    String relPath = rootDirectory.toPath().relativize(file).toString();
                    ZipUtil.addFileToZip(rootDir + "/" + relPath, file, zout);
                }
                return FileVisitResult.CONTINUE;
            }
        });
    } catch (IOException e) {
        log.error("", e);
    }
}
Also used : Path(java.nio.file.Path) ManifestBuilder(org.olat.ims.qti21.model.xml.ManifestBuilder) ZipEntry(java.util.zip.ZipEntry) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) ShieldOutputStream(org.olat.core.util.io.ShieldOutputStream) ResolvedAssessmentItem(uk.ac.ed.ph.jqtiplus.resolution.ResolvedAssessmentItem) File(java.io.File) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 20 with ShieldOutputStream

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

the class QTI21WordExport method exportTest.

private void exportTest(AssessmentTest assessmentTest, String header, ZipOutputStream out, boolean withResponses) {
    try (ShieldOutputStream sout = new ShieldOutputStream(out);
        ZipOutputStream zout = new ZipOutputStream(sout)) {
        zout.setLevel(9);
        OpenXMLDocument document = new OpenXMLDocument();
        document.setMediaContainer(mediaContainer);
        document.setDocumentHeader(header);
        Translator translator = Util.createPackageTranslator(AssessmentTestDisplayController.class, locale, Util.createPackageTranslator(AssessmentTestComposerController.class, locale));
        renderAssessmentTest(assessmentTest, document, translator);
        for (TestPart testPart : assessmentTest.getChildAbstractParts()) {
            List<AssessmentSection> assessmentSections = testPart.getAssessmentSections();
            for (AssessmentSection assessmentSection : assessmentSections) {
                renderAssessmentSection(assessmentSection, document, withResponses, translator);
            }
        }
        OpenXMLDocumentWriter writer = new OpenXMLDocumentWriter();
        writer.createDocument(zout, document);
    } catch (Exception e) {
        log.error("", e);
    }
}
Also used : OpenXMLDocumentWriter(org.olat.core.util.openxml.OpenXMLDocumentWriter) ShieldOutputStream(org.olat.core.util.io.ShieldOutputStream) Translator(org.olat.core.gui.translator.Translator) ZipOutputStream(java.util.zip.ZipOutputStream) AssessmentSection(uk.ac.ed.ph.jqtiplus.node.test.AssessmentSection) TestPart(uk.ac.ed.ph.jqtiplus.node.test.TestPart) OpenXMLDocument(org.olat.core.util.openxml.OpenXMLDocument) AssessmentTestComposerController(org.olat.ims.qti21.ui.editor.AssessmentTestComposerController) SAXException(org.xml.sax.SAXException) IOException(java.io.IOException)

Aggregations

ShieldOutputStream (org.olat.core.util.io.ShieldOutputStream)26 ZipEntry (java.util.zip.ZipEntry)24 ZipOutputStream (java.util.zip.ZipOutputStream)20 IOException (java.io.IOException)18 File (java.io.File)12 OutputStream (java.io.OutputStream)12 Identity (org.olat.core.id.Identity)8 Date (java.util.Date)6 URISyntaxException (java.net.URISyntaxException)4 FileVisitResult (java.nio.file.FileVisitResult)4 Path (java.nio.file.Path)4 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)4 OLATRuntimeException (org.olat.core.logging.OLATRuntimeException)4 AssessableCourseNode (org.olat.course.nodes.AssessableCourseNode)4 GTAManager (org.olat.course.nodes.gta.GTAManager)4 TaskList (org.olat.course.nodes.gta.TaskList)4 BusinessGroup (org.olat.group.BusinessGroup)4 ManifestBuilder (org.olat.ims.qti21.model.xml.ManifestBuilder)4 Binder (org.olat.modules.portfolio.Binder)4 SAXException (org.xml.sax.SAXException)4