Search in sources :

Example 21 with MCRException

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

the class MCRAccessControlSystem method checkPermission.

@Override
public boolean checkPermission(Element rule) {
    MCRSession session = MCRSessionMgr.getCurrentSession();
    String ruleStr = getNormalizedRuleString(rule);
    MCRAccessRule accessRule = new MCRAccessRule(null, "System", new Date(), ruleStr, "");
    try {
        return accessRule.checkAccess(session.getUserInformation().getUserID(), new Date(), new MCRIPAddress(session.getCurrentIP()));
    } catch (MCRException | UnknownHostException e) {
        // only return true if access is allowed, we dont know this
        LOGGER.debug("Error while checking rule.", e);
        return false;
    }
}
Also used : MCRException(org.mycore.common.MCRException) MCRSession(org.mycore.common.MCRSession) UnknownHostException(java.net.UnknownHostException) Date(java.util.Date)

Example 22 with MCRException

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

the class MCRDerivateCommands method exportAllDerivatesOfProject.

/**
 * The command look for all derivates starts with project name in the
 * application and build export commands.
 *
 * @param dirname
 *            the filename to store the object
 * @param style
 *            the type of the stylesheet
 * @return a list of export commands for derivates with project name
 */
@MCRCommand(syntax = "export all derivates of project {0} to directory {1} with {2}", help = "Stores all derivates of project {0} to the directory {1} with the stylesheet mcr_{2}-derivate.xsl. For {2} save is the default.", order = 110)
public static List<String> exportAllDerivatesOfProject(String project, String dirname, String style) {
    // check dirname
    File dir = new File(dirname);
    if (dir.isFile()) {
        throw new MCRException(dirname + " is not a dirctory.");
    }
    List<String> ids = MCRXMLMetadataManager.instance().listIDsOfType("derivate");
    List<String> cmds = new ArrayList<>(ids.size());
    for (String id : ids) {
        if (!id.startsWith(project))
            continue;
        cmds.add("export derivate " + id + " to directory " + dirname + " with " + style);
    }
    return cmds;
}
Also used : MCRException(org.mycore.common.MCRException) ArrayList(java.util.ArrayList) File(java.io.File) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 23 with MCRException

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

the class MCRDerivateCommands method exportDerivate.

/**
 * @param dir
 * @param trans
 * @param nid
 * @throws FileNotFoundException
 * @throws TransformerException
 * @throws IOException
 */
private static void exportDerivate(File dir, Transformer trans, String nid) throws TransformerException, IOException {
    // store the XML file
    Document xml = null;
    MCRDerivate obj;
    MCRObjectID derivateID = MCRObjectID.getInstance(nid);
    try {
        obj = MCRMetadataManager.retrieveMCRDerivate(derivateID);
        String path = obj.getDerivate().getInternals().getSourcePath();
        // reset from the absolute to relative path, for later reload
        LOGGER.info("Old Internal Path ====>{}", path);
        obj.getDerivate().getInternals().setSourcePath(nid);
        LOGGER.info("New Internal Path ====>{}", nid);
        // add ACL's
        Collection<String> l = ACCESS_IMPL.getPermissionsForID(nid);
        for (String permission : l) {
            Element rule = ACCESS_IMPL.getRule(nid, permission);
            obj.getService().addRule(permission, rule);
        }
        // build JDOM
        xml = obj.createXML();
    } catch (MCRException ex) {
        LOGGER.warn("Could not read {}, continue with next ID", nid);
        return;
    }
    File xmlOutput = new File(dir, derivateID + ".xml");
    FileOutputStream out = new FileOutputStream(xmlOutput);
    dir = new File(dir, derivateID.toString());
    if (trans != null) {
        trans.setParameter("dirname", dir.getPath());
        StreamResult sr = new StreamResult(out);
        trans.transform(new org.jdom2.transform.JDOMSource(xml), sr);
    } else {
        new org.jdom2.output.XMLOutputter().output(xml, out);
        out.flush();
        out.close();
    }
    LOGGER.info("Object {} stored under {}.", nid, xmlOutput);
    // store the derivate file under dirname
    if (!dir.isDirectory()) {
        dir.mkdir();
    }
    MCRPath rootPath = MCRPath.getPath(derivateID.toString(), "/");
    Files.walkFileTree(rootPath, new MCRTreeCopier(rootPath, dir.toPath()));
    LOGGER.info("Derivate {} saved under {} and {}.", nid, dir, xmlOutput);
}
Also used : MCRTreeCopier(org.mycore.datamodel.niofs.utils.MCRTreeCopier) MCRException(org.mycore.common.MCRException) StreamResult(javax.xml.transform.stream.StreamResult) Element(org.jdom2.Element) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) Document(org.jdom2.Document) FileOutputStream(java.io.FileOutputStream) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPath(org.mycore.datamodel.niofs.MCRPath) File(java.io.File)

Example 24 with MCRException

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

the class MCRObjectCommands method exportMCRObject.

/**
 * The method read a MCRObject and use the transformer to write the data to a file. They are any steps to handel
 * errors and save the damaged data.
 * <ul>
 * <li>Read data for object ID in the MCRObject, add ACL's and store it as checked and transformed XML. Return true.
 * </li>
 * <li>If it can't find a transformer instance (no script file found) it store the checked data with ACL's native in
 * the file. Warning and return true.</li>
 * <li>If it get an exception while build the MCRObject, it try to read the XML blob and stor it without check and
 * ACL's to the file. Warning and return true.</li>
 * <li>If it get an exception while store the native data without check, ACĂ–'s and transformation it return a
 * warning and false.</li>
 * </ul>
 *
 * @param dir
 *            the file instance to store
 * @param trans
 *            the XML transformer
 * @param nid
 *            the MCRObjectID
 * @return true if the store was okay (see description), else return false
 * @throws TransformerException
 * @throws IOException
 * @throws MCRException
 * @throws SAXParseException
 */
private static boolean exportMCRObject(File dir, Transformer trans, String nid) throws TransformerException, IOException, MCRException, SAXParseException {
    MCRContent content;
    try {
        // if object do'snt exist - no exception is catched!
        content = MCRXMLMetadataManager.instance().retrieveContent(MCRObjectID.getInstance(nid));
    } catch (MCRException ex) {
        return false;
    }
    File xmlOutput = new File(dir, nid + ".xml");
    if (trans != null) {
        FileOutputStream out = new FileOutputStream(xmlOutput);
        StreamResult sr = new StreamResult(out);
        Document doc = MCRXMLParserFactory.getNonValidatingParser().parseXML(content);
        trans.transform(new org.jdom2.transform.JDOMSource(doc), sr);
    } else {
        content.sendTo(xmlOutput);
    }
    LOGGER.info("Object {} saved to {}.", nid, xmlOutput.getCanonicalPath());
    return true;
}
Also used : JDOMSource(org.jdom2.transform.JDOMSource) MCRException(org.mycore.common.MCRException) StreamResult(javax.xml.transform.stream.StreamResult) FileOutputStream(java.io.FileOutputStream) Document(org.jdom2.Document) MCRContent(org.mycore.common.content.MCRContent) File(java.io.File)

Example 25 with MCRException

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

the class MCRDerivateCommands method exportAllDerivates.

/**
 * The command look for all derivates in the application and build export
 * commands.
 *
 * @param dirname
 *            the filename to store the object
 * @param style
 *            the type of the stylesheet
 * @return a list of export commands for each derivate
 */
@MCRCommand(syntax = "export all derivates to directory {0} with {1}", help = "Stores all derivates to the directory {0} with the stylesheet mcr_{1}-derivate.xsl. For {1} save is the default.", order = 100)
public static List<String> exportAllDerivates(String dirname, String style) {
    // check dirname
    File dir = new File(dirname);
    if (dir.isFile()) {
        throw new MCRException(dirname + " is not a dirctory.");
    }
    List<String> ids = MCRXMLMetadataManager.instance().listIDsOfType("derivate");
    List<String> cmds = new ArrayList<>(ids.size());
    for (String id : ids) {
        cmds.add("export derivate " + id + " to directory " + dirname + " with " + style);
    }
    return cmds;
}
Also used : MCRException(org.mycore.common.MCRException) ArrayList(java.util.ArrayList) File(java.io.File) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

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