Search in sources :

Example 91 with MCRException

use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.

the class MCRObjectCommands method mergeDerivatesOfObject.

@MCRCommand(syntax = "merge derivates of object {0}", help = "Retrieves the MCRObject with the MCRObjectID {0} and if it has more then one MCRDerivate, then all" + " Files will be copied to the first Derivate and all other will be deleted.", order = 190)
public static void mergeDerivatesOfObject(String id) {
    MCRObjectID objectID = MCRObjectID.getInstance(id);
    if (!MCRMetadataManager.exists(objectID)) {
        LOGGER.error("The object with the id {} does not exist!", id);
        return;
    }
    MCRObject object = MCRMetadataManager.retrieveMCRObject(objectID);
    List<MCRMetaLinkID> derivateLinkIDs = object.getStructure().getDerivates();
    List<MCRObjectID> derivateIDs = derivateLinkIDs.stream().map(MCRMetaLinkID::getXLinkHrefID).collect(Collectors.toList());
    if (derivateIDs.size() <= 1) {
        LOGGER.error("The object with the id {} has no Derivates to merge!", id);
        return;
    }
    String mainID = derivateIDs.get(0).toString();
    MCRPath mainDerivateRootPath = MCRPath.getPath(mainID, "/");
    derivateIDs.stream().skip(1).forEach(derivateID -> {
        LOGGER.info("Merge {} into {}...", derivateID, mainID);
        MCRPath copyRootPath = MCRPath.getPath(derivateID.toString(), "/");
        try {
            MCRTreeCopier treeCopier = new MCRTreeCopier(copyRootPath, mainDerivateRootPath);
            Files.walkFileTree(copyRootPath, treeCopier);
            Files.walkFileTree(copyRootPath, MCRRecursiveDeleter.instance());
            MCRMetadataManager.deleteMCRDerivate(derivateID);
        } catch (IOException | MCRAccessException e) {
            throw new MCRException(e);
        }
    });
}
Also used : MCRTreeCopier(org.mycore.datamodel.niofs.utils.MCRTreeCopier) MCRException(org.mycore.common.MCRException) MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRAccessException(org.mycore.access.MCRAccessException) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) IOException(java.io.IOException) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 92 with MCRException

use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.

the class MCRObjectCommands method deleteFromTo.

/**
 * Delete MCRObject's form ID to ID from the datastore.
 *
 * @param IDfrom
 *            the start ID for deleting the MCRObjects
 * @param IDto
 *            the stop ID for deleting the MCRObjects
 * @return list of delete commands
 */
@MCRCommand(syntax = "delete object from {0} to {1}", help = "Removes MCRObjects in the number range between the MCRObjectID {0} and {1}.", order = 30)
public static List<String> deleteFromTo(String IDfrom, String IDto) {
    MCRObjectID from = MCRObjectID.getInstance(IDfrom);
    MCRObjectID to = MCRObjectID.getInstance(IDto);
    int from_i = from.getNumberAsInteger();
    int to_i = to.getNumberAsInteger();
    if (from_i > to_i) {
        throw new MCRException("The from-to-interval is false.");
    }
    List<String> cmds = new ArrayList<>(to_i - from_i);
    for (int i = from_i; i < to_i + 1; i++) {
        String id = MCRObjectID.formatID(from.getProjectId(), from.getTypeId(), i);
        if (MCRMetadataManager.exists(MCRObjectID.getInstance(id))) {
            cmds.add("delete object " + id);
        }
    }
    return cmds;
}
Also used : MCRException(org.mycore.common.MCRException) ArrayList(java.util.ArrayList) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 93 with MCRException

use of org.mycore.common.MCRException 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 94 with MCRException

use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.

the class MCRRealmFactory method loadRealms.

/**
 */
private static void loadRealms() {
    Element root;
    try {
        root = getRealms().getRootElement();
    } catch (SAXException | JDOMException | TransformerException | IOException e) {
        throw new MCRException("Could not load realms from URI: " + realmsURI);
    }
    String localRealmID = root.getAttributeValue("local");
    HashMap<String, MCRRealm> realmsMap = new HashMap<>();
    HashMap<String, MCRUserAttributeMapper> attributeMapper = new HashMap<>();
    List<MCRRealm> realmsList = new ArrayList<>();
    List<Element> realms = root.getChildren("realm");
    for (Element child : realms) {
        String id = child.getAttributeValue("id");
        MCRRealm realm = new MCRRealm(id);
        List<Element> labels = child.getChildren("label");
        for (Element label : labels) {
            String text = label.getTextTrim();
            String lang = label.getAttributeValue("lang", Namespace.XML_NAMESPACE);
            realm.setLabel(lang, text);
        }
        realm.setPasswordChangeURL(child.getChildTextTrim("passwordChangeURL"));
        Element login = child.getChild("login");
        if (login != null) {
            realm.setLoginURL(login.getAttributeValue("url"));
            realm.setRedirectParameter(login.getAttributeValue("redirectParameter"));
            realm.setRealmParameter(login.getAttributeValue("realmParameter"));
        }
        Element createElement = child.getChild("create");
        if (createElement != null) {
            realm.setCreateURL(createElement.getAttributeValue("url"));
        }
        attributeMapper.put(id, MCRUserAttributeMapper.instance(child));
        realmsMap.put(id, realm);
        realmsList.add(realm);
        if (localRealmID.equals(id)) {
            localRealm = realm;
        }
    }
    MCRRealmFactory.realmsDocument = root.getDocument();
    MCRRealmFactory.realmsMap = realmsMap;
    MCRRealmFactory.realmsList = realmsList;
    MCRRealmFactory.attributeMapper = attributeMapper;
}
Also used : MCRException(org.mycore.common.MCRException) HashMap(java.util.HashMap) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) IOException(java.io.IOException) JDOMException(org.jdom2.JDOMException) SAXException(org.xml.sax.SAXException) TransformerException(javax.xml.transform.TransformerException)

Example 95 with MCRException

use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.

the class MCRDataciteClient method getDOIList.

public List<MCRDigitalObjectIdentifier> getDOIList() throws MCRPersistentIdentifierException {
    URI requestURI = getRequestURI("/doi");
    HttpGet get = new HttpGet(requestURI);
    try (CloseableHttpClient httpClient = getHttpClient()) {
        CloseableHttpResponse response = httpClient.execute(get);
        HttpEntity entity = response.getEntity();
        StatusLine statusLine = response.getStatusLine();
        switch(statusLine.getStatusCode()) {
            case HttpStatus.SC_OK:
                Scanner scanner = new Scanner(entity.getContent(), "UTF-8");
                List<MCRDigitalObjectIdentifier> doiList = new ArrayList<>();
                while (scanner.hasNextLine()) {
                    String line = scanner.nextLine();
                    Optional<MCRDigitalObjectIdentifier> parse = new MCRDOIParser().parse(line);
                    MCRDigitalObjectIdentifier doi = parse.orElseThrow(() -> new MCRException("Could not parse DOI from Datacite!"));
                    doiList.add(doi);
                }
                return doiList;
            case HttpStatus.SC_NO_CONTENT:
                return Collections.emptyList();
            default:
                throw new MCRDatacenterException(String.format(Locale.ENGLISH, "Unknown error while resolving all doi’s \n %d - %s", statusLine.getStatusCode(), statusLine.getReasonPhrase()));
        }
    } catch (IOException e) {
        throw new MCRDatacenterException("Unknown error while resolving all doi’s", e);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Scanner(java.util.Scanner) MCRDatacenterException(org.mycore.pi.exceptions.MCRDatacenterException) MCRException(org.mycore.common.MCRException) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URI(java.net.URI) StatusLine(org.apache.http.StatusLine) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Aggregations

MCRException (org.mycore.common.MCRException)131 IOException (java.io.IOException)39 Element (org.jdom2.Element)26 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)19 Document (org.jdom2.Document)18 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)18 File (java.io.File)15 MCRConfigurationException (org.mycore.common.config.MCRConfigurationException)12 MCRObject (org.mycore.datamodel.metadata.MCRObject)12 ArrayList (java.util.ArrayList)11 JDOMException (org.jdom2.JDOMException)11 MCRAccessException (org.mycore.access.MCRAccessException)11 MCRPath (org.mycore.datamodel.niofs.MCRPath)10 SAXException (org.xml.sax.SAXException)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)7 List (java.util.List)7 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)7 SAXParseException (org.xml.sax.SAXParseException)7 URI (java.net.URI)6 Path (java.nio.file.Path)6