Search in sources :

Example 66 with MCRException

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

the class MCRSwordUtil method addDirectoryToZip.

private static void addDirectoryToZip(ZipArchiveOutputStream zipOutputStream, Path directory) {
    MCRSession currentSession = MCRSessionMgr.getCurrentSession();
    final DirectoryStream<Path> paths;
    try {
        paths = Files.newDirectoryStream(directory);
    } catch (IOException e) {
        throw new MCRException(e);
    }
    paths.forEach(p -> {
        final boolean isDir = Files.isDirectory(p);
        final ZipArchiveEntry zipArchiveEntry;
        try {
            final String fileName = getFilename(p);
            LOGGER.info("Addding {} to zip file!", fileName);
            if (isDir) {
                addDirectoryToZip(zipOutputStream, p);
            } else {
                zipArchiveEntry = new ZipArchiveEntry(fileName);
                zipArchiveEntry.setSize(Files.size(p));
                zipOutputStream.putArchiveEntry(zipArchiveEntry);
                if (currentSession.isTransactionActive()) {
                    currentSession.commitTransaction();
                }
                Files.copy(p, zipOutputStream);
                currentSession.beginTransaction();
                zipOutputStream.closeArchiveEntry();
            }
        } catch (IOException e) {
            LOGGER.error("Could not add path {}", p);
        }
    });
}
Also used : Path(java.nio.file.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRException(org.mycore.common.MCRException) MCRSession(org.mycore.common.MCRSession) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) IOException(java.io.IOException)

Example 67 with MCRException

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

the class MCRRealm method getLoginURL.

/**
 * Returns the URL where users from this realm can login with redirect URL attached.
 * If this realm has a attribut <code>redirectParameter</code> defined this method returns
 * a complete login URL with <code>redirectURL</code> properly configured.
 * @param redirectURL URL where to redirect to after login succeeds.
 * @return the same as {@link #getLoginURL()} if <code>redirectParameter</code> is undefined for this realm
 */
public String getLoginURL(String redirectURL) {
    LinkedHashMap<String, String> parameter = new LinkedHashMap<>();
    String redirect = getRedirectParameter();
    if (redirect != null && redirectURL != null) {
        parameter.put(redirect, redirectURL);
    }
    String realmParameter = getRealmParameter();
    if (realmParameter != null) {
        parameter.put(realmParameter, getID());
    }
    if (parameter.isEmpty()) {
        return getLoginURL();
    }
    StringBuilder loginURL = new StringBuilder(getLoginURL());
    boolean firstParameter = !getLoginURL().contains("?");
    try {
        for (Entry<String, String> entry : parameter.entrySet()) {
            if (firstParameter) {
                loginURL.append('?');
                firstParameter = false;
            } else {
                loginURL.append('&');
            }
            loginURL.append(entry.getKey()).append('=').append(URLEncoder.encode(entry.getValue(), "UTF-8"));
        }
        return loginURL.toString();
    } catch (UnsupportedEncodingException e) {
        throw new MCRException(e);
    }
}
Also used : MCRException(org.mycore.common.MCRException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) LinkedHashMap(java.util.LinkedHashMap)

Example 68 with MCRException

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

the class MCRUserTransformer method getDocument.

private static Document getDocument(MCRUser user) {
    MCRJAXBContent<MCRUser> content = new MCRJAXBContent<>(JAXB_CONTEXT, user);
    try {
        Document userXML = content.asXML();
        sortAttributes(userXML);
        return userXML;
    } catch (SAXParseException | JDOMException | IOException e) {
        throw new MCRException("Exception while transforming MCRUser " + user.getUserID() + " to JDOM document.", e);
    }
}
Also used : MCRJAXBContent(org.mycore.common.content.MCRJAXBContent) MCRException(org.mycore.common.MCRException) SAXParseException(org.xml.sax.SAXParseException) MCRUser(org.mycore.user2.MCRUser) IOException(java.io.IOException) Document(org.jdom2.Document) JDOMException(org.jdom2.JDOMException)

Example 69 with MCRException

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

the class MCRClassificationBrowser2 method addLabel.

/**
 * Add label in current lang, otherwise default lang, optional with
 * description
 */
private void addLabel(HttpServletRequest req, MCRCategory child, Element category) {
    MCRLabel label = child.getCurrentLabel().orElseThrow(() -> new MCRException("Category " + child.getId() + " has no labels."));
    category.addContent(new Element("label").setText(label.getText()));
    // if true, add description
    boolean descr = Boolean.valueOf(req.getParameter("adddescription"));
    if (descr && (label.getDescription() != null)) {
        category.addContent(new Element("description").setText(label.getDescription()));
    }
}
Also used : MCRException(org.mycore.common.MCRException) Element(org.jdom2.Element) MCRLabel(org.mycore.datamodel.classifications2.MCRLabel)

Example 70 with MCRException

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

the class MCRFileImportExport method exportFiles.

/**
 * Exports all contents of the given MCRDirectory to the local filesystem, including all subdirectories and stored files. If the local object is a file, the
 * parent directory of that file will be used for exporting.
 *
 * @param local
 *            the local directory where to export the contents to
 * @param dir
 *            the directory thats contents should be exported
 */
public static void exportFiles(MCRDirectory dir, File local) throws MCRException {
    Objects.requireNonNull(dir, "internal directory is null");
    Objects.requireNonNull(local, "local file is null");
    String path = local.getPath();
    if (!local.canWrite()) {
        throw new MCRUsageException("Not writeable: " + path);
    }
    // If local is file, use its parent instead
    if (local.isFile()) {
        local = local.getParentFile();
    }
    MCRFilesystemNode[] children = dir.getChildren();
    for (MCRFilesystemNode element : children) {
        if (element instanceof MCRFile) {
            MCRFile internalFile = (MCRFile) element;
            String name = internalFile.getName();
            File localFile = new File(local, name);
            try {
                internalFile.getContentTo(localFile);
            } catch (Exception ex) {
                throw new MCRException("Can't get file content.", ex);
            }
        } else {
            MCRDirectory internalDir = (MCRDirectory) element;
            String name = internalDir.getName();
            File localDir = new File(local, name);
            if (!localDir.exists()) {
                localDir.mkdir();
            }
            exportFiles(internalDir, localDir);
        }
    }
}
Also used : MCRUsageException(org.mycore.common.MCRUsageException) MCRException(org.mycore.common.MCRException) File(java.io.File) MCRUsageException(org.mycore.common.MCRUsageException) IOException(java.io.IOException) MCRException(org.mycore.common.MCRException) FileNotFoundException(java.io.FileNotFoundException)

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