Search in sources :

Example 11 with MCRAccessException

use of org.mycore.access.MCRAccessException in project mycore by MyCoRe-Org.

the class MCRMetsMods2IIIFConverter method convert.

public MCRIIIFManifest convert() {
    MCRIIIFManifest manifest = new MCRIIIFManifest();
    // root chapter ^= manifest metadata
    LogicalStructMap logicalStructMap = (LogicalStructMap) mets.getStructMap("LOGICAL");
    LogicalDiv divContainer = logicalStructMap.getDivContainer();
    List<MCRIIIFMetadata> metadata = extractMedataFromLogicalDiv(mets, divContainer);
    manifest.metadata = metadata;
    manifest.setId(this.identifier);
    PhysicalStructMap physicalStructMap = (PhysicalStructMap) mets.getStructMap("PHYSICAL");
    PhysicalDiv physicalDivContainer = physicalStructMap.getDivContainer();
    String id = physicalDivContainer.getId();
    MCRIIIFSequence sequence = new MCRIIIFSequence(id);
    List<PhysicalSubDiv> children = physicalDivContainer.getChildren();
    MCRIIIFImageImpl imageImpl = MCRIIIFImageImpl.getInstance(getImageImplName());
    MCRIIIFImageProfile profile = imageImpl.getProfile();
    profile.setId(MCRIIIFImageUtil.getProfileLink(imageImpl));
    sequence.canvases = children.stream().map(physicalSubDiv -> {
        String order = physicalSubDiv.asElement().getAttributeValue("ORDER");
        String orderLabel = physicalSubDiv.getOrderLabel();
        String contentids = physicalSubDiv.getContentids();
        String label = Stream.of(order, orderLabel, contentids).filter(o -> o != null && !o.isEmpty()).collect(Collectors.joining(" - "));
        String identifier = this.physicalIdentifierMap.get(physicalSubDiv);
        try {
            MCRIIIFImageInformation information = imageImpl.getInformation(identifier);
            MCRIIIFCanvas canvas = new MCRIIIFCanvas(identifier, label, information.width, information.height);
            MCRIIIFAnnotation annotation = new MCRIIIFAnnotation(identifier, canvas);
            canvas.images.add(annotation);
            MCRIIIFResource resource = new MCRIIIFResource(information.getId(), MCRDCMIType.Image);
            resource.setWidth(information.width);
            resource.setHeight(information.height);
            MCRIIIFService service = new MCRIIIFService(information.getId(), profile.getContext());
            service.profile = MCRIIIFImageProfile.IIIF_PROFILE_2_0;
            resource.setService(service);
            annotation.setResource(resource);
            return canvas;
        } catch (MCRIIIFImageNotFoundException | MCRIIIFImageProvidingException e) {
            throw new MCRException("Error while providing ImageInfo for " + identifier, e);
        } catch (MCRAccessException e) {
            LOGGER.warn("User has no access to {}", identifier);
            return null;
        }
    }).filter(Objects::nonNull).collect(Collectors.toList());
    manifest.sequences.add(sequence);
    List<MCRIIIFRange> complete = new ArrayList<>();
    processDivContainer(complete, divContainer);
    manifest.structures.addAll(complete);
    manifest.setLabel(metadata.stream().filter(m -> m.getLabel().equals("title")).findFirst().get().getStringValue().get());
    return manifest;
}
Also used : PhysicalDiv(org.mycore.mets.model.struct.PhysicalDiv) MCRIIIFCanvas(org.mycore.iiif.presentation.model.basic.MCRIIIFCanvas) MCRIIIFImageUtil(org.mycore.iiif.image.MCRIIIFImageUtil) MCRIIIFImageImpl(org.mycore.iiif.image.impl.MCRIIIFImageImpl) MCRConstants(org.mycore.common.MCRConstants) MCRIIIFImageProvidingException(org.mycore.iiif.image.impl.MCRIIIFImageProvidingException) MCRIIIFImageProfile(org.mycore.iiif.image.model.MCRIIIFImageProfile) HashMap(java.util.HashMap) MCRIIIFSequence(org.mycore.iiif.presentation.model.basic.MCRIIIFSequence) MCRException(org.mycore.common.MCRException) ArrayList(java.util.ArrayList) Document(org.jdom2.Document) MCRIIIFService(org.mycore.iiif.presentation.model.attributes.MCRIIIFService) FileGrp(org.mycore.mets.model.files.FileGrp) Map(java.util.Map) MCRDCMIType(org.mycore.iiif.presentation.model.attributes.MCRDCMIType) MCRIIIFResource(org.mycore.iiif.presentation.model.attributes.MCRIIIFResource) MCRAccessException(org.mycore.access.MCRAccessException) File(org.mycore.mets.model.files.File) PhysicalStructMap(org.mycore.mets.model.struct.PhysicalStructMap) MCRIIIFAnnotation(org.mycore.iiif.presentation.model.additional.MCRIIIFAnnotation) MCRIIIFViewingHint(org.mycore.iiif.presentation.model.attributes.MCRIIIFViewingHint) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MCRIIIFManifest(org.mycore.iiif.presentation.model.basic.MCRIIIFManifest) Mets(org.mycore.mets.model.Mets) Collectors(java.util.stream.Collectors) MCRIIIFRange(org.mycore.iiif.presentation.model.basic.MCRIIIFRange) LogicalDiv(org.mycore.mets.model.struct.LogicalDiv) MDTYPE(org.mycore.mets.model.struct.MDTYPE) PhysicalSubDiv(org.mycore.mets.model.struct.PhysicalSubDiv) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Logger(org.apache.logging.log4j.Logger) MCRIIIFImageNotFoundException(org.mycore.iiif.image.impl.MCRIIIFImageNotFoundException) MCRIIIFImageInformation(org.mycore.iiif.image.model.MCRIIIFImageInformation) LogicalStructMap(org.mycore.mets.model.struct.LogicalStructMap) DmdSec(org.mycore.mets.model.sections.DmdSec) MdWrap(org.mycore.mets.model.struct.MdWrap) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) MCRIIIFMetadata(org.mycore.iiif.presentation.model.attributes.MCRIIIFMetadata) PhysicalStructMap(org.mycore.mets.model.struct.PhysicalStructMap) ArrayList(java.util.ArrayList) MCRIIIFSequence(org.mycore.iiif.presentation.model.basic.MCRIIIFSequence) PhysicalSubDiv(org.mycore.mets.model.struct.PhysicalSubDiv) MCRIIIFRange(org.mycore.iiif.presentation.model.basic.MCRIIIFRange) LogicalStructMap(org.mycore.mets.model.struct.LogicalStructMap) LogicalDiv(org.mycore.mets.model.struct.LogicalDiv) MCRIIIFCanvas(org.mycore.iiif.presentation.model.basic.MCRIIIFCanvas) MCRIIIFMetadata(org.mycore.iiif.presentation.model.attributes.MCRIIIFMetadata) MCRIIIFService(org.mycore.iiif.presentation.model.attributes.MCRIIIFService) MCRIIIFImageImpl(org.mycore.iiif.image.impl.MCRIIIFImageImpl) MCRException(org.mycore.common.MCRException) MCRIIIFManifest(org.mycore.iiif.presentation.model.basic.MCRIIIFManifest) MCRIIIFImageInformation(org.mycore.iiif.image.model.MCRIIIFImageInformation) MCRIIIFResource(org.mycore.iiif.presentation.model.attributes.MCRIIIFResource) MCRAccessException(org.mycore.access.MCRAccessException) PhysicalDiv(org.mycore.mets.model.struct.PhysicalDiv) MCRIIIFImageProfile(org.mycore.iiif.image.model.MCRIIIFImageProfile) MCRIIIFAnnotation(org.mycore.iiif.presentation.model.additional.MCRIIIFAnnotation) Objects(java.util.Objects)

Example 12 with MCRAccessException

use of org.mycore.access.MCRAccessException in project mycore by MyCoRe-Org.

the class MCRSwordContainerHandler method deleteObject.

public void deleteObject(MCRObject object) throws SwordServerException {
    try {
        object.getStructure().getDerivates().stream().map(MCRMetaLinkID::getXLinkHrefID).forEach(id -> {
            try {
                MCRMetadataManager.deleteMCRDerivate(id);
            } catch (Exception e) {
                throw new MCRException(e);
            }
        });
        MCRMetadataManager.delete(object);
    } catch (MCRActiveLinkException | MCRAccessException | MCRException e) {
        Throwable ex = e;
        if (e instanceof MCRException && Optional.ofNullable(e.getCause()).map(Object::getClass).filter(MCRAccessException.class::isAssignableFrom).isPresent()) {
            // unwrapp
            ex = e.getCause();
        }
        throw new SwordServerException("Error while deleting Object.", ex);
    }
}
Also used : MCRException(org.mycore.common.MCRException) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) SwordServerException(org.swordapp.server.SwordServerException) MCRAccessException(org.mycore.access.MCRAccessException) MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRException(org.mycore.common.MCRException) SwordServerException(org.swordapp.server.SwordServerException) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) MCRAccessException(org.mycore.access.MCRAccessException)

Example 13 with MCRAccessException

use of org.mycore.access.MCRAccessException in project mycore by MyCoRe-Org.

the class MCRPackerServlet method doGetPost.

@Override
protected void doGetPost(MCRServletJob job) throws IOException {
    String packer = job.getRequest().getParameter("packer");
    if (packer == null || packer.isEmpty()) {
        try {
            job.getResponse().sendError(HttpServletResponse.SC_BAD_REQUEST, "No or invalid 'packer' parameter!");
        } catch (IOException e) {
            LOGGER.error("Error while sending request error to client!", e);
            return;
        }
    }
    Map<String, String> jobParameters = resolveJobParameters(job);
    try {
        MCRJob mcrJob = MCRPackerManager.startPacking(jobParameters);
        if (mcrJob == null) {
            job.getResponse().sendError(HttpServletResponse.SC_BAD_REQUEST, "No packer parameter!");
        }
    } catch (MCRAccessException e) {
        job.getResponse().sendError(HttpServletResponse.SC_UNAUTHORIZED, e.getMessage());
    } catch (MCRUsageException e) {
        job.getResponse().sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid Parameters: " + e.getMessage());
    }
    if (jobParameters.containsKey("redirect")) {
        String redirect = jobParameters.get("redirect");
        job.getResponse().sendRedirect(job.getResponse().encodeRedirectURL(redirect));
    }
}
Also used : MCRUsageException(org.mycore.common.MCRUsageException) MCRAccessException(org.mycore.access.MCRAccessException) MCRJob(org.mycore.services.queuedjob.MCRJob) IOException(java.io.IOException)

Example 14 with MCRAccessException

use of org.mycore.access.MCRAccessException in project mycore by MyCoRe-Org.

the class MCRMetadataManager method update.

/**
 * Updates the derivate or creates it if it does not exist yet.
 *
 * @throws MCRPersistenceException
 *                if a persistence problem is occurred
 * @throws MCRAccessException
 *                if write permission to object or derivate is missing
 */
public static void update(final MCRDerivate mcrDerivate) throws MCRPersistenceException, MCRAccessException {
    MCRObjectID id = mcrDerivate.getId();
    // check deletion mark
    if (MCRMarkManager.instance().isMarkedForDeletion(id)) {
        return;
    }
    if (!MCRMetadataManager.exists(id)) {
        MCRMetadataManager.create(mcrDerivate);
        return;
    }
    if (!MCRAccessManager.checkPermission(id, PERMISSION_WRITE)) {
        throw MCRAccessException.missingPermission("Update derivate", id.toString(), PERMISSION_WRITE);
    }
    File fileSourceDirectory = null;
    if (mcrDerivate.getDerivate().getInternals() != null && mcrDerivate.getDerivate().getInternals().getSourcePath() != null) {
        fileSourceDirectory = new File(mcrDerivate.getDerivate().getInternals().getSourcePath());
        if (!fileSourceDirectory.exists()) {
            LOGGER.warn("{}: the directory {} was not found.", id, fileSourceDirectory);
            fileSourceDirectory = null;
        }
    }
    // get the old Item
    MCRDerivate old = MCRMetadataManager.retrieveMCRDerivate(id);
    // remove the old link to metadata
    MCRMetaLinkID oldLink = old.getDerivate().getMetaLink();
    MCRMetaLinkID newLink = mcrDerivate.getDerivate().getMetaLink();
    if (!oldLink.equals(newLink)) {
        MCRObjectID oldMetadataObjectID = oldLink.getXLinkHrefID();
        MCRObjectID newMetadataObjectID = newLink.getXLinkHrefID();
        if (!oldMetadataObjectID.equals(newLink.getXLinkHrefID())) {
            try {
                MCRMetadataManager.removeDerivateFromObject(oldMetadataObjectID, id);
            } catch (final MCRException e) {
                LOGGER.warn(e.getMessage(), e);
            }
        }
        // add the link to metadata
        final MCRMetaLinkID der = new MCRMetaLinkID("derobject", id, null, mcrDerivate.getLabel(), newLink.getXLinkRole());
        addOrUpdateDerivateToObject(newMetadataObjectID, der);
    }
    // update the derivate
    mcrDerivate.getService().setDate("createdate", old.getService().getDate("createdate"));
    if (!mcrDerivate.getService().isFlagTypeSet(MCRObjectService.FLAG_TYPE_CREATEDBY)) {
        for (String flagCreatedBy : old.getService().getFlags(MCRObjectService.FLAG_TYPE_CREATEDBY)) {
            mcrDerivate.getService().addFlag(MCRObjectService.FLAG_TYPE_CREATEDBY, flagCreatedBy);
        }
    }
    MCRMetadataManager.updateMCRDerivateXML(mcrDerivate);
    // update to IFS
    if (fileSourceDirectory != null) {
        Path sourcePath = fileSourceDirectory.toPath();
        MCRPath targetPath = MCRPath.getPath(id.toString(), "/");
        try {
            Files.walkFileTree(sourcePath, new MCRTreeCopier(sourcePath, targetPath));
        } catch (Exception exc) {
            throw new MCRPersistenceException("Unable to update IFS. Copy failed from " + sourcePath.toAbsolutePath() + " to target " + targetPath.toAbsolutePath(), exc);
        }
    }
}
Also used : Path(java.nio.file.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRTreeCopier(org.mycore.datamodel.niofs.utils.MCRTreeCopier) MCRException(org.mycore.common.MCRException) MCRPath(org.mycore.datamodel.niofs.MCRPath) File(java.io.File) MCRPersistenceException(org.mycore.common.MCRPersistenceException) MCRException(org.mycore.common.MCRException) JDOMException(org.jdom2.JDOMException) MCRAccessException(org.mycore.access.MCRAccessException) MCRPersistenceException(org.mycore.common.MCRPersistenceException) IOException(java.io.IOException) PersistenceException(javax.persistence.PersistenceException) SAXException(org.xml.sax.SAXException) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException)

Example 15 with MCRAccessException

use of org.mycore.access.MCRAccessException in project mycore by MyCoRe-Org.

the class MCRRoleResolver method resolve.

/* (non-Javadoc)
     * @see javax.xml.transform.URIResolver#resolve(java.lang.String, java.lang.String)
     */
@Override
public Source resolve(final String href, final String base) throws TransformerException {
    final String target = href.substring(href.indexOf(":") + 1);
    final String[] part = target.split(":");
    final String method = part[0];
    try {
        if ("getAssignableGroupsForUser".equals(method)) {
            return new JDOMSource(getAssignableGroupsForUser());
        }
    } catch (final MCRAccessException exc) {
        throw new TransformerException(exc);
    }
    throw new TransformerException(new IllegalArgumentException("Unknown method " + method + " in uri " + href));
}
Also used : MCRAccessException(org.mycore.access.MCRAccessException) JDOMSource(org.jdom2.transform.JDOMSource) TransformerException(javax.xml.transform.TransformerException)

Aggregations

MCRAccessException (org.mycore.access.MCRAccessException)23 MCRPersistenceException (org.mycore.common.MCRPersistenceException)12 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)12 IOException (java.io.IOException)11 MCRException (org.mycore.common.MCRException)9 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)9 MCRPath (org.mycore.datamodel.niofs.MCRPath)7 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)6 MCRObject (org.mycore.datamodel.metadata.MCRObject)6 Path (java.nio.file.Path)4 JDOMException (org.jdom2.JDOMException)4 MCRMetaLinkID (org.mycore.datamodel.metadata.MCRMetaLinkID)4 SAXException (org.xml.sax.SAXException)4 List (java.util.List)3 Stream (java.util.stream.Stream)3 MCRPersistentIdentifierException (org.mycore.pi.exceptions.MCRPersistentIdentifierException)3 SignedJWT (com.nimbusds.jwt.SignedJWT)2 File (java.io.File)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 URISyntaxException (java.net.URISyntaxException)2