Search in sources :

Example 1 with ShieldOutputStream

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

the class GroupBulkDownloadResource method prepare.

@Override
public void prepare(HttpServletResponse hres) {
    try {
        hres.setCharacterEncoding(encoding);
    } catch (Exception e) {
        log.error("", e);
    }
    String label = StringHelper.transformDisplayNameToFileSystemName(courseNode.getShortName()) + "_" + Formatter.formatDatetimeWithMinutes(new Date()) + ".zip";
    String urlEncodedLabel = StringHelper.urlEncodeUTF8(label);
    hres.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + urlEncodedLabel);
    hres.setHeader("Content-Description", urlEncodedLabel);
    try (ZipOutputStream zout = new ZipOutputStream(hres.getOutputStream())) {
        zout.setLevel(9);
        ICourse course = CourseFactory.loadCourse(courseOres);
        GTAManager gtaManager = CoreSpringFactory.getImpl(GTAManager.class);
        if (courseNode.getModuleConfiguration().getBooleanSafe(GTACourseNode.GTASK_GRADING)) {
            List<Identity> assessableIdentities = CoreSpringFactory.getImpl(BusinessGroupService.class).getMembers(groups, GroupRoles.participant.name());
            String courseTitle = course.getCourseTitle();
            String fileName = ExportUtil.createFileNameWithTimeStamp(courseTitle, "xlsx");
            List<AssessableCourseNode> nodes = Collections.<AssessableCourseNode>singletonList(courseNode);
            try (OutputStream out = new ShieldOutputStream(zout)) {
                zout.putNextEntry(new ZipEntry(fileName));
                ScoreAccountingHelper.createCourseResultsOverviewXMLTable(assessableIdentities, nodes, course, locale, out);
                zout.closeEntry();
            } catch (Exception e) {
                log.error("", e);
            }
        }
        TaskList taskList = gtaManager.getTaskList(course.getCourseEnvironment().getCourseGroupManager().getCourseEntry(), courseNode);
        for (BusinessGroup businessGroup : groups) {
            courseNode.archiveNodeData(course, businessGroup, taskList, "", zout);
        }
    } catch (Exception e) {
        log.error("", e);
    }
}
Also used : BusinessGroup(org.olat.group.BusinessGroup) ZipOutputStream(java.util.zip.ZipOutputStream) OutputStream(java.io.OutputStream) ShieldOutputStream(org.olat.core.util.io.ShieldOutputStream) ZipEntry(java.util.zip.ZipEntry) TaskList(org.olat.course.nodes.gta.TaskList) ICourse(org.olat.course.ICourse) Date(java.util.Date) AssessableCourseNode(org.olat.course.nodes.AssessableCourseNode) ShieldOutputStream(org.olat.core.util.io.ShieldOutputStream) BusinessGroupService(org.olat.group.BusinessGroupService) ZipOutputStream(java.util.zip.ZipOutputStream) GTAManager(org.olat.course.nodes.gta.GTAManager) Identity(org.olat.core.id.Identity)

Example 2 with ShieldOutputStream

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

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

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

the class ExportBinderAsCPResource 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())) {
        Binder binder = portfolioService.getBinderByKey(binderRef.getKey());
        String label = binder.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));
        // load pages
        List<Section> sections = portfolioService.getSections(binder);
        List<Page> pages = portfolioService.getPages(binder, null);
        // manifest
        ManifestType manifest = createImsManifest(binder, sections, pages);
        zout.putNextEntry(new ZipEntry("imsmanifest.xml"));
        write(manifest, new ShieldOutputStream(zout));
        zout.closeEntry();
        // write pages
        for (Section section : sections) {
            exportSection(section, zout);
        }
        // write pages
        for (Page page : pages) {
            exportPage(page, zout);
        }
        // theme and javascripts
        exportCSSAndJs(zout);
        // make it readable offline
        ByteArrayOutputStream manifestOut = new ByteArrayOutputStream();
        write(manifest, manifestOut);
        String manifestXml = new String(manifestOut.toByteArray());
        String indexSrc = sectionFilename(sections.get(0));
        CPOfflineReadableManager.getInstance().makeCPOfflineReadable(manifestXml, indexSrc, zout);
    } catch (Exception e) {
        log.error("", e);
    }
}
Also used : Binder(org.olat.modules.portfolio.Binder) ManifestType(org.olat.imscp.xml.manifest.ManifestType) ShieldOutputStream(org.olat.core.util.io.ShieldOutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) Page(org.olat.modules.portfolio.Page) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AssessmentSection(org.olat.modules.portfolio.AssessmentSection) Section(org.olat.modules.portfolio.Section) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException) IOException(java.io.IOException)

Example 4 with ShieldOutputStream

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

the class ForumDownloadResource 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())) {
        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));
        ZipEntry test = new ZipEntry(secureLabel + ".docx");
        zout.putNextEntry(test);
        Map<File, DocReference> attachments = exportForum(zout);
        zout.closeEntry();
        if (attachments != null && attachments.size() > 0) {
            for (Map.Entry<File, DocReference> attachmentEntry : attachments.entrySet()) {
                File attachment = attachmentEntry.getKey();
                DocReference ref = attachmentEntry.getValue();
                zout.putNextEntry(new ZipEntry("attachments/" + ref.getFilename()));
                Files.copy(attachment.toPath(), new ShieldOutputStream(zout));
                zout.closeEntry();
            }
        }
    } catch (Exception e) {
        log.error("", e);
    }
}
Also used : ShieldOutputStream(org.olat.core.util.io.ShieldOutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) File(java.io.File) Map(java.util.Map) DocReference(org.olat.core.util.openxml.DocReference)

Example 5 with ShieldOutputStream

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

the class QTI21ExportProcessor method assembleTest.

public void assembleTest(List<QuestionItemFull> fullItems, ZipOutputStream zout) {
    try {
        QtiSerializer qtiSerializer = qtiService.qtiSerializer();
        // imsmanifest
        ManifestBuilder manifest = ManifestBuilder.createAssessmentTestBuilder();
        // assessment test
        AssessmentTest assessmentTest = AssessmentTestFactory.createAssessmentTest("Assessment test from pool", "Section");
        String assessmentTestFilename = assessmentTest.getIdentifier() + ".xml";
        manifest.appendAssessmentTest(assessmentTestFilename);
        // make a section
        AssessmentSection section = assessmentTest.getTestParts().get(0).getAssessmentSections().get(0);
        // assessment items
        for (QuestionItemFull qitem : fullItems) {
            File resourceDirectory = qpoolFileStorage.getDirectory(qitem.getDirectory());
            File itemFile = new File(resourceDirectory, qitem.getRootFilename());
            String itemFilename = itemFile.getName();
            String container = qitem.getKey().toString();
            String containedFilename = container + "/" + itemFilename;
            ResolvedAssessmentItem resolvedAssessmentItem = qtiService.loadAndResolveAssessmentItemForCopy(itemFile.toURI(), resourceDirectory);
            ZipUtil.addFileToZip(containedFilename, itemFile, zout);
            AssessmentTestFactory.appendAssessmentItem(section, containedFilename);
            manifest.appendAssessmentItem(containedFilename);
            ManifestMetadataBuilder metadata = manifest.getResourceBuilderByHref(containedFilename);
            metadata.appendMetadataFrom(qitem, resolvedAssessmentItem, locale);
            // write materials
            try {
                Files.walkFileTree(resourceDirectory.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(".") && !itemFilename.equals(filename)) {
                            String relPath = resourceDirectory.toPath().relativize(file).toString();
                            ZipUtil.addFileToZip(container + "/" + relPath, file, zout);
                        }
                        return FileVisitResult.CONTINUE;
                    }
                });
            } catch (IOException e) {
                log.error("", e);
            }
        }
        zout.putNextEntry(new ZipEntry(assessmentTestFilename));
        qtiSerializer.serializeJqtiObject(assessmentTest, new ShieldOutputStream(zout));
        zout.closeEntry();
        zout.putNextEntry(new ZipEntry("imsmanifest.xml"));
        manifest.write(new ShieldOutputStream(zout));
        zout.closeEntry();
    } catch (IOException | URISyntaxException 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) URISyntaxException(java.net.URISyntaxException) ManifestMetadataBuilder(org.olat.ims.qti21.model.xml.ManifestMetadataBuilder) AssessmentTest(uk.ac.ed.ph.jqtiplus.node.test.AssessmentTest) QuestionItemFull(org.olat.modules.qpool.QuestionItemFull) ShieldOutputStream(org.olat.core.util.io.ShieldOutputStream) QtiSerializer(uk.ac.ed.ph.jqtiplus.serialization.QtiSerializer) ResolvedAssessmentItem(uk.ac.ed.ph.jqtiplus.resolution.ResolvedAssessmentItem) AssessmentSection(uk.ac.ed.ph.jqtiplus.node.test.AssessmentSection) File(java.io.File) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

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