Search in sources :

Example 76 with MCRObjectID

use of org.mycore.datamodel.metadata.MCRObjectID in project mycore by MyCoRe-Org.

the class MCRMetsLock method isLocked.

/**
 * Checks if a Derivate is locked
 * @param derivateIdString the derivate id
 * @return true if the derivate is locked
 */
public static synchronized boolean isLocked(String derivateIdString) {
    MCRObjectID derivateId = MCRObjectID.getInstance(derivateIdString);
    if (MCRMetsLock.metsAccessSessionTable.containsKey(derivateId)) {
        String lastAccessID = MCRMetsLock.metsAccessSessionTable.get(derivateId);
        MCRSession lastSession = MCRSessionMgr.getSession(lastAccessID);
        LOGGER.debug(MessageFormat.format("{0} is locked : {1}", derivateIdString, lastSession != null));
        return lastSession != null;
    } else {
        LOGGER.debug(MessageFormat.format("{0} is not locked", derivateIdString));
        return false;
    }
}
Also used : MCRSession(org.mycore.common.MCRSession) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID)

Example 77 with MCRObjectID

use of org.mycore.datamodel.metadata.MCRObjectID in project mycore by MyCoRe-Org.

the class MCRIView2Commands method forAllDerivatesOfObject.

private static List<String> forAllDerivatesOfObject(String objectID, String batchCommandSyntax) {
    MCRObjectID mcrobjid;
    try {
        mcrobjid = MCRObjectID.getInstance(objectID);
    } catch (Exception e) {
        LOGGER.error("The object ID {} is wrong", objectID);
        return null;
    }
    List<MCRObjectID> derivateIds = MCRMetadataManager.getDerivateIds(mcrobjid, 0, TimeUnit.MILLISECONDS);
    if (derivateIds == null) {
        LOGGER.error("Object does not exist: {}", mcrobjid);
    }
    ArrayList<String> cmds = new ArrayList<>(derivateIds.size());
    for (MCRObjectID derId : derivateIds) {
        cmds.add(MessageFormat.format(batchCommandSyntax, derId));
    }
    return cmds;
}
Also used : ArrayList(java.util.ArrayList) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRException(org.mycore.common.MCRException) JDOMException(org.jdom2.JDOMException) IOException(java.io.IOException)

Example 78 with MCRObjectID

use of org.mycore.datamodel.metadata.MCRObjectID in project mycore by MyCoRe-Org.

the class MCRJWPlayerResource method getSources.

@GET
@Path("{derivateId}/{path: .+}/sources.json")
@Produces({ "application/javascript" })
public String getSources(@PathParam("derivateId") String derivateId, @PathParam("path") String path) throws URISyntaxException, IOException {
    MCRObjectID derivate = MCRJerseyUtil.getID(derivateId);
    MCRJerseyUtil.checkDerivateReadPermission(derivate);
    try {
        MCRMediaSourceProvider formatter = new MCRMediaSourceProvider(derivateId, path, Optional.ofNullable(request.getHeader("User-Agent")), () -> Arrays.stream(Optional.ofNullable(request.getQueryString()).orElse("").split("&")).filter(p -> !p.startsWith("callback=")).toArray(String[]::new));
        return toJson(formatter.getSources());
    } catch (NoSuchFileException e) {
        LogManager.getLogger().warn("Could not find video file.", e);
        throw new WebApplicationException(Status.NOT_FOUND);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) MCRMediaSourceProvider(org.mycore.media.video.MCRMediaSourceProvider) NoSuchFileException(java.nio.file.NoSuchFileException) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 79 with MCRObjectID

use of org.mycore.datamodel.metadata.MCRObjectID in project mycore by MyCoRe-Org.

the class MCRThumbnailResource method getThumbnail.

private Response getThumbnail(String documentId, int size, String ext) {
    List<MCRObjectID> derivateIds = MCRMetadataManager.getDerivateIds(MCRJerseyUtil.getID(documentId), 1, TimeUnit.MINUTES);
    for (MCRObjectID derivateId : derivateIds) {
        if (MCRAccessManager.checkPermissionForReadingDerivate(derivateId.toString())) {
            String nameOfMainFile = MCRMetadataManager.retrieveMCRDerivate(derivateId).getDerivate().getInternals().getMainDoc();
            if (nameOfMainFile != null && !nameOfMainFile.equals("")) {
                MCRPath mainFile = MCRPath.getPath(derivateId.toString(), '/' + nameOfMainFile);
                try {
                    String mimeType = Files.probeContentType(mainFile);
                    String generators = MCRConfiguration.instance().getString("MCR.Media.Thumbnail.Generators");
                    for (String generator : generators.split(",")) {
                        Class<MCRThumbnailGenerator> classObject = (Class<MCRThumbnailGenerator>) Class.forName(generator);
                        Constructor<MCRThumbnailGenerator> constructor = classObject.getConstructor();
                        MCRThumbnailGenerator thumbnailGenerator = constructor.newInstance();
                        if (thumbnailGenerator.matchesFileType(mimeType, mainFile)) {
                            FileTime lastModified = Files.getLastModifiedTime(mainFile);
                            Date lastModifiedDate = new Date(lastModified.toMillis());
                            Response.ResponseBuilder resp = request.evaluatePreconditions(lastModifiedDate);
                            if (resp == null) {
                                Optional<BufferedImage> thumbnail = thumbnailGenerator.getThumbnail(mainFile, size);
                                if (thumbnail.isPresent()) {
                                    CacheControl cc = new CacheControl();
                                    cc.setMaxAge((int) TimeUnit.DAYS.toSeconds(1));
                                    String type = "image/png";
                                    if ("jpg".equals(ext) || "jpeg".equals(ext)) {
                                        type = "image/jpeg";
                                    }
                                    return Response.ok(thumbnail.get()).cacheControl(cc).lastModified(lastModifiedDate).type(type).build();
                                }
                                return Response.status(Response.Status.NOT_FOUND).build();
                            }
                            return resp.build();
                        }
                    }
                } catch (IOException | ReflectiveOperationException e) {
                    throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
                }
            }
        }
    }
    return Response.status(Response.Status.NOT_FOUND).build();
}
Also used : MCRThumbnailGenerator(org.mycore.media.services.MCRThumbnailGenerator) WebApplicationException(javax.ws.rs.WebApplicationException) FileTime(java.nio.file.attribute.FileTime) IOException(java.io.IOException) Date(java.util.Date) BufferedImage(java.awt.image.BufferedImage) Response(javax.ws.rs.core.Response) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) CacheControl(javax.ws.rs.core.CacheControl) MCRPath(org.mycore.datamodel.niofs.MCRPath)

Example 80 with MCRObjectID

use of org.mycore.datamodel.metadata.MCRObjectID in project mycore by MyCoRe-Org.

the class MCRUpdateMetsOnDerivateChangeEventHandler method checkUpdateMets.

/**
 * Checks if the mets.xml should be updated.
 *
 * @param evt the mcr event
 * @param file the file which was changed
 * @param attrs the file attributes
 * @return true if the mets shoud be updated, otherwise false
 */
protected boolean checkUpdateMets(MCREvent evt, Path file, BasicFileAttributes attrs) {
    // don't update if no MCRPath
    if (!(file instanceof MCRPath)) {
        return false;
    }
    // don't update if mets.xml is deleted
    Path fileName = file.getFileName();
    if (fileName != null && fileName.toString().equals(mets)) {
        return false;
    }
    MCRPath mcrPath = MCRPath.toMCRPath(file);
    String derivateId = mcrPath.getOwner();
    // don't update if mets.xml does not exist
    if (Files.notExists(MCRPath.getPath(derivateId, '/' + mets))) {
        return false;
    }
    // don't update if derivate or mycore object is marked for deletion
    MCRObjectID mcrDerivateId = MCRObjectID.getInstance(derivateId);
    MCRDerivate mcrDerivate = MCRMetadataManager.retrieveMCRDerivate(mcrDerivateId);
    return !MCRMarkManager.instance().isMarkedForDeletion(mcrDerivate);
}
Also used : MCRPath(org.mycore.datamodel.niofs.MCRPath) Path(java.nio.file.Path) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPath(org.mycore.datamodel.niofs.MCRPath)

Aggregations

MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)144 IOException (java.io.IOException)37 MCRObject (org.mycore.datamodel.metadata.MCRObject)32 MCRException (org.mycore.common.MCRException)30 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)30 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)29 MCRPath (org.mycore.datamodel.niofs.MCRPath)25 MCRAccessException (org.mycore.access.MCRAccessException)22 Document (org.jdom2.Document)20 MCRPersistenceException (org.mycore.common.MCRPersistenceException)18 MCRMetaLinkID (org.mycore.datamodel.metadata.MCRMetaLinkID)16 JDOMException (org.jdom2.JDOMException)15 MCRBase (org.mycore.datamodel.metadata.MCRBase)15 SAXException (org.xml.sax.SAXException)15 Date (java.util.Date)14 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)13 MCRSession (org.mycore.common.MCRSession)11 MCRContent (org.mycore.common.content.MCRContent)11 MCRPersistentIdentifierException (org.mycore.pi.exceptions.MCRPersistentIdentifierException)11 URISyntaxException (java.net.URISyntaxException)10