Search in sources :

Example 21 with ShieldOutputStream

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

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 22 with ShieldOutputStream

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

the class GTACourseNode method archiveNodeData.

@Override
public boolean archiveNodeData(Locale locale, ICourse course, ArchiveOptions options, ZipOutputStream exportStream, String charset) {
    final GTAManager gtaManager = CoreSpringFactory.getImpl(GTAManager.class);
    final ModuleConfiguration config = getModuleConfiguration();
    String prefix;
    if (GTAType.group.name().equals(config.getStringValue(GTACourseNode.GTASK_TYPE))) {
        prefix = "grouptask_";
    } else {
        prefix = "ita_";
    }
    String dirName = prefix + StringHelper.transformDisplayNameToFileSystemName(getShortName()) + "_" + Formatter.formatDatetimeFilesystemSave(new Date(System.currentTimeMillis()));
    TaskList taskList = gtaManager.getTaskList(course.getCourseEnvironment().getCourseGroupManager().getCourseEntry(), this);
    // save assessment datas
    List<Identity> users = null;
    if (config.getBooleanSafe(GTASK_GRADING)) {
        users = ScoreAccountingHelper.loadUsers(course.getCourseEnvironment(), options);
        String courseTitle = course.getCourseTitle();
        String fileName = ExportUtil.createFileNameWithTimeStamp(courseTitle, "xlsx");
        List<AssessableCourseNode> nodes = Collections.<AssessableCourseNode>singletonList(this);
        try (OutputStream out = new ShieldOutputStream(exportStream)) {
            exportStream.putNextEntry(new ZipEntry(dirName + "/" + fileName));
            ScoreAccountingHelper.createCourseResultsOverviewXMLTable(users, nodes, course, locale, out);
            exportStream.closeEntry();
        } catch (Exception e) {
            log.error("", e);
        }
    }
    // copy tasks
    if (taskList != null) {
        if (GTAType.group.name().equals(config.getStringValue(GTACourseNode.GTASK_TYPE))) {
            List<BusinessGroup> selectedGroups;
            if (options != null && options.getGroup() != null) {
                selectedGroups = Collections.singletonList(options.getGroup());
            } else {
                selectedGroups = gtaManager.getBusinessGroups(this);
            }
            for (BusinessGroup businessGroup : selectedGroups) {
                archiveNodeData(course, businessGroup, taskList, dirName, exportStream);
            }
        } else {
            if (users == null) {
                users = ScoreAccountingHelper.loadUsers(course.getCourseEnvironment(), options);
            }
            Set<Identity> uniqueUsers = new HashSet<>(users);
            for (Identity user : uniqueUsers) {
                archiveNodeData(course, user, taskList, dirName, exportStream);
            }
        }
    }
    // copy solutions
    if (config.getBooleanSafe(GTACourseNode.GTASK_SAMPLE_SOLUTION)) {
        VFSContainer solutions = gtaManager.getSolutionsContainer(course.getCourseEnvironment(), this);
        if (solutions.exists()) {
            String solutionDirName = dirName + "/solutions";
            for (VFSItem solution : solutions.getItems(new SystemItemFilter())) {
                ZipUtil.addToZip(solution, solutionDirName, exportStream);
            }
        }
    }
    return true;
}
Also used : ModuleConfiguration(org.olat.modules.ModuleConfiguration) BusinessGroup(org.olat.group.BusinessGroup) TaskList(org.olat.course.nodes.gta.TaskList) ShieldOutputStream(org.olat.core.util.io.ShieldOutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) OutputStream(java.io.OutputStream) ZipEntry(java.util.zip.ZipEntry) VFSContainer(org.olat.core.util.vfs.VFSContainer) VFSItem(org.olat.core.util.vfs.VFSItem) SystemItemFilter(org.olat.core.util.vfs.filters.SystemItemFilter) Date(java.util.Date) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) ShieldOutputStream(org.olat.core.util.io.ShieldOutputStream) GTAManager(org.olat.course.nodes.gta.GTAManager) Identity(org.olat.core.id.Identity) HashSet(java.util.HashSet)

Example 23 with ShieldOutputStream

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

the class ScoreAccountingHelper method createCourseResultsOverview.

public static void createCourseResultsOverview(List<Identity> identities, List<AssessableCourseNode> nodes, ICourse course, Locale locale, ZipOutputStream zout) {
    try (OutputStream out = new ShieldOutputStream(zout)) {
        zout.putNextEntry(new ZipEntry("Course_results.xlsx"));
        createCourseResultsOverviewXMLTable(identities, nodes, course, locale, out);
        zout.closeEntry();
    } catch (IOException e) {
        log.error("", e);
    }
    for (AssessableCourseNode node : nodes) {
        String dir = "Assessment_documents/" + StringHelper.transformDisplayNameToFileSystemName(node.getShortName());
        if (node instanceof IQTESTCourseNode || node.getModuleConfiguration().getBooleanSafe(MSCourseNode.CONFIG_KEY_HAS_INDIVIDUAL_ASSESSMENT_DOCS, false)) {
            for (Identity assessedIdentity : identities) {
                List<File> assessmentDocuments = course.getCourseEnvironment().getAssessmentManager().getIndividualAssessmentDocuments(node, assessedIdentity);
                if (assessmentDocuments != null && !assessmentDocuments.isEmpty()) {
                    String name = assessedIdentity.getUser().getLastName() + "_" + assessedIdentity.getUser().getFirstName() + "_" + assessedIdentity.getName();
                    String userDirName = dir + "/" + StringHelper.transformDisplayNameToFileSystemName(name);
                    for (File document : assessmentDocuments) {
                        String path = userDirName + "/" + document.getName();
                        ZipUtil.addFileToZip(path, document, zout);
                    }
                }
            }
        }
    }
}
Also used : AssessableCourseNode(org.olat.course.nodes.AssessableCourseNode) IQTESTCourseNode(org.olat.course.nodes.IQTESTCourseNode) ShieldOutputStream(org.olat.core.util.io.ShieldOutputStream) ShieldOutputStream(org.olat.core.util.io.ShieldOutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) OutputStream(java.io.OutputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) Identity(org.olat.core.id.Identity) File(java.io.File)

Example 24 with ShieldOutputStream

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

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 25 with ShieldOutputStream

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

the class QTI21ArchiveFormat method export.

public void export(String filename, ZipOutputStream exportStream) {
    try (OutputStream out = new ShieldOutputStream(exportStream)) {
        exportStream.putNextEntry(new ZipEntry(filename));
        exportWorkbook(out);
        exportStream.closeEntry();
    } catch (IOException e) {
        log.error("", e);
    }
}
Also used : ShieldOutputStream(org.olat.core.util.io.ShieldOutputStream) ShieldOutputStream(org.olat.core.util.io.ShieldOutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) OutputStream(java.io.OutputStream) ZipEntry(java.util.zip.ZipEntry) 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