Search in sources :

Example 76 with MCRPath

use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.

the class MCRSwordUtil method getZippedDerivateMediaResource.

public static MediaResource getZippedDerivateMediaResource(String object) {
    final Path tempFile;
    try {
        tempFile = Files.createTempFile("swordv2_", ".temp.zip");
    } catch (IOException e) {
        throw new MCRException("Could not create temp file!", e);
    }
    try (final OutputStream tempFileStream = Files.newOutputStream(tempFile)) {
        final ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(tempFileStream);
        zipOutputStream.setLevel(Deflater.BEST_COMPRESSION);
        final MCRPath root = MCRPath.getPath(object, "/");
        addDirectoryToZip(zipOutputStream, root);
        zipOutputStream.close();
    } catch (IOException e) {
        throw new MCRException(e);
    }
    MediaResource resultRessource;
    InputStream is;
    try {
        is = new MCRDeleteFileOnCloseFilterInputStream(Files.newInputStream(tempFile), tempFile);
        resultRessource = new MediaResource(is, MCRSwordConstants.MIME_TYPE_APPLICATION_ZIP, UriRegistry.PACKAGE_SIMPLE_ZIP);
    } catch (IOException e) {
        throw new MCRException("could not read from temp file!", e);
    }
    return resultRessource;
}
Also used : Path(java.nio.file.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRException(org.mycore.common.MCRException) DigestInputStream(java.security.DigestInputStream) InputStream(java.io.InputStream) ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) OutputStream(java.io.OutputStream) ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) MediaResource(org.swordapp.server.MediaResource) IOException(java.io.IOException) MCRPath(org.mycore.datamodel.niofs.MCRPath)

Example 77 with MCRPath

use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.

the class MCRDOIRegistrationService method getMediaList.

/**
 * Builds a list with with right content types and media urls assigned of a specific Object
 * @param obj the object
 * @return a list of entrys Media-Type, URL
 */
public List<Map.Entry<String, URI>> getMediaList(MCRObject obj) {
    List<Map.Entry<String, URI>> entryList = new ArrayList<>();
    Optional<MCRObjectID> derivateIdOptional = MCRMetadataManager.getDerivateIds(obj.getId(), 1, TimeUnit.MINUTES).stream().findFirst();
    derivateIdOptional.ifPresent(derivateId -> {
        MCRDerivate derivate = MCRMetadataManager.retrieveMCRDerivate(derivateId);
        String mainDoc = derivate.getDerivate().getInternals().getMainDoc();
        MCRPath mainDocumentPath = MCRPath.getPath(derivateId.toString(), mainDoc);
        try {
            String contentType = Optional.ofNullable(MCRContentTypes.probeContentType(mainDocumentPath)).orElse("application/octet-stream");
            entryList.add(new AbstractMap.SimpleEntry<>(contentType, new URI(this.registerURL + MCRXMLFunctions.encodeURIPath("/servlets/MCRFileNodeServlet/" + derivateId + "/" + mainDoc))));
        } catch (IOException | URISyntaxException e) {
            LOGGER.error("Error while detecting the file to register!", e);
        }
    });
    return entryList;
}
Also used : ArrayList(java.util.ArrayList) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) AbstractMap(java.util.AbstractMap) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPath(org.mycore.datamodel.niofs.MCRPath)

Example 78 with MCRPath

use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.

the class MCRMetsIIIFPresentationImpl method getMets.

public Document getMets(String id) throws IOException, JDOMException, SAXException {
    String objectid = MCRLinkTableManager.instance().getSourceOf(id).iterator().next();
    MCRContentTransformer transformer = getTransformer();
    MCRParameterCollector parameter = new MCRParameterCollector();
    if (objectid != null && objectid.length() != 0) {
        MCRDerivate derObj = MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(id));
        MCRObjectID ownerID = derObj.getOwnerID();
        objectid = ownerID.toString();
        parameter.setParameter("objectID", objectid);
        parameter.setParameter("derivateID", id);
    }
    MCRPath metsPath = MCRPath.getPath(id, "mets.xml");
    if (!Files.exists(metsPath)) {
        throw new MCRException("File not found: " + id);
    }
    MCRPathContent source = new MCRPathContent(metsPath);
    MCRContent content = transformer instanceof MCRParameterizedTransformer ? ((MCRParameterizedTransformer) transformer).transform(source, parameter) : transformer.transform(source);
    return content.asXML();
}
Also used : MCRParameterCollector(org.mycore.common.xsl.MCRParameterCollector) MCRException(org.mycore.common.MCRException) MCRPathContent(org.mycore.common.content.MCRPathContent) MCRParameterizedTransformer(org.mycore.common.content.transformer.MCRParameterizedTransformer) MCRContentTransformer(org.mycore.common.content.transformer.MCRContentTransformer) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRContent(org.mycore.common.content.MCRContent)

Example 79 with MCRPath

use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.

the class MCRMETSDefaultGenerator method structureMets.

private void structureMets(MCRPath dir, Set<MCRPath> ignoreNodes, FileSec fileSec, PhysicalDiv physicalDiv, LogicalDiv logicalDiv, StructLink structLink, int logOrder) throws IOException {
    SortedMap<MCRPath, BasicFileAttributes> files = new TreeMap<>(), directories = new TreeMap<>();
    fillFileMap(ignoreNodes, files, directories, dir);
    for (Map.Entry<MCRPath, BasicFileAttributes> file : files.entrySet()) {
        createStructure(dir, fileSec, physicalDiv, logicalDiv, structLink, file);
    }
    for (Map.Entry<MCRPath, BasicFileAttributes> directory : directories.entrySet()) {
        String dirName = directory.getKey().getFileName().toString();
        if (isInExcludedRootFolder(directory.getKey())) {
            structureMets(directory.getKey(), ignoreNodes, fileSec, physicalDiv, logicalDiv, structLink, logOrder);
        } else {
            LogicalDiv section = new LogicalDiv("log_" + Integer.toString(++logOrder), "section", dirName);
            logicalDiv.add(section);
            structureMets(directory.getKey(), ignoreNodes, fileSec, physicalDiv, section, structLink, logOrder);
        }
    }
}
Also used : LogicalDiv(org.mycore.mets.model.struct.LogicalDiv) MCRPath(org.mycore.datamodel.niofs.MCRPath) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap) Map(java.util.Map) PhysicalStructMap(org.mycore.mets.model.struct.PhysicalStructMap) TreeMap(java.util.TreeMap) LogicalStructMap(org.mycore.mets.model.struct.LogicalStructMap) SortedMap(java.util.SortedMap) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 80 with MCRPath

use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.

the class MCRMETSDefaultGenerator method fillFileMap.

private void fillFileMap(Set<MCRPath> ignoreNodes, SortedMap<MCRPath, BasicFileAttributes> files, SortedMap<MCRPath, BasicFileAttributes> directories, Path dir) throws IOException {
    try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(dir)) {
        for (Path child : dirStream) {
            MCRPath path = MCRPath.toMCRPath(child);
            if (ignoreNodes.contains(path)) {
                continue;
            }
            BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
            if (attrs.isDirectory()) {
                directories.put(path, attrs);
            } else {
                files.put(path, attrs);
            }
        }
    }
}
Also used : Path(java.nio.file.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRPath(org.mycore.datamodel.niofs.MCRPath) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Aggregations

MCRPath (org.mycore.datamodel.niofs.MCRPath)96 IOException (java.io.IOException)49 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)26 Path (java.nio.file.Path)25 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)22 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)22 Document (org.jdom2.Document)15 JDOMException (org.jdom2.JDOMException)15 MCRPersistenceException (org.mycore.common.MCRPersistenceException)14 MCRException (org.mycore.common.MCRException)13 MCRDirectory (org.mycore.datamodel.ifs.MCRDirectory)13 MCRAccessException (org.mycore.access.MCRAccessException)12 Files (java.nio.file.Files)11 Collectors (java.util.stream.Collectors)11 LogManager (org.apache.logging.log4j.LogManager)11 Logger (org.apache.logging.log4j.Logger)11 FileVisitResult (java.nio.file.FileVisitResult)10 NoSuchFileException (java.nio.file.NoSuchFileException)10 Date (java.util.Date)10 List (java.util.List)10