Search in sources :

Example 6 with MCRFileContent

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

the class MCRRealmFactory method getRealms.

private static Document getRealms() throws JDOMException, TransformerException, SAXException, IOException {
    if (realmsFile == null) {
        return MCRSourceContent.getInstance(realmsURI.toASCIIString()).asXML();
    }
    if (!realmsFile.exists() || realmsFile.length() == 0) {
        LOGGER.info("Creating {}...", realmsFile.getAbsolutePath());
        MCRSourceContent realmsContent = MCRSourceContent.getInstance(RESOURCE_REALMS_URI);
        realmsContent.sendTo(realmsFile);
    }
    updateLastModified();
    return MCRXMLParserFactory.getNonValidatingParser().parseXML(new MCRFileContent(realmsFile));
}
Also used : MCRSourceContent(org.mycore.common.content.MCRSourceContent) MCRFileContent(org.mycore.common.content.MCRFileContent)

Example 7 with MCRFileContent

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

the class MCRAccessCommands method getRuleFromFile.

private static Element getRuleFromFile(String fileName) throws Exception {
    if (!checkFilename(fileName)) {
        LOGGER.warn("Wrong file format or file doesn't exist");
        return null;
    }
    Document ruleDom = MCRXMLParserFactory.getParser().parseXML(new MCRFileContent(fileName));
    Element rule = ruleDom.getRootElement();
    if (!rule.getName().equals("condition")) {
        LOGGER.warn("ROOT element is not valid, a valid rule would be for example:");
        LOGGER.warn("<condition format=\"xml\"><boolean operator=\"true\" /></condition>");
        return null;
    }
    return rule;
}
Also used : MCRFileContent(org.mycore.common.content.MCRFileContent) Element(org.jdom2.Element) Document(org.jdom2.Document)

Example 8 with MCRFileContent

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

the class MCRUserCommands method encryptPasswordsInXMLFile.

/**
 * A given XML file containing user data with cleartext passwords must be converted prior to loading the user data
 * into the system. This method reads all user objects in the given XML file, encrypts the passwords and writes them
 * back to a file with name original-file-name_encrypted.xml.
 *
 * @param oldFile
 *            the filename of the user data input
 * @param newFile
 *            the filename of the user data output (encrypted passwords)
 */
@MCRCommand(syntax = "encrypt passwords in user xml file {0} to file {1}", help = "A migration tool to change old plain text password entries to encrpted entries.", order = 40)
public static void encryptPasswordsInXMLFile(String oldFile, String newFile) throws SAXParseException, IOException {
    File inputFile = getCheckedFile(oldFile);
    if (inputFile == null) {
        return;
    }
    LOGGER.info("Reading file {} ...", inputFile.getAbsolutePath());
    Document doc = MCRXMLParserFactory.getNonValidatingParser().parseXML(new MCRFileContent(inputFile));
    Element rootelm = doc.getRootElement();
    MCRUser mcrUser = MCRUserTransformer.buildMCRUser(rootelm);
    if (mcrUser == null) {
        throw new MCRException("These data do not correspond to a user.");
    }
    MCRUserManager.updatePasswordHashToSHA256(mcrUser, mcrUser.getPassword());
    FileOutputStream outFile = new FileOutputStream(newFile);
    saveToXMLFile(mcrUser, outFile);
}
Also used : MCRException(org.mycore.common.MCRException) MCRFileContent(org.mycore.common.content.MCRFileContent) Element(org.jdom2.Element) FileOutputStream(java.io.FileOutputStream) Document(org.jdom2.Document) File(java.io.File) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 9 with MCRFileContent

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

the class MCRUserCommands method getMCRUserFromFile.

private static MCRUser getMCRUserFromFile(String filename) throws SAXParseException, IOException {
    File inputFile = getCheckedFile(filename);
    if (inputFile == null) {
        return null;
    }
    LOGGER.info("Reading file {} ...", inputFile.getAbsolutePath());
    Document doc = MCRXMLParserFactory.getNonValidatingParser().parseXML(new MCRFileContent(inputFile));
    return MCRUserTransformer.buildMCRUser(doc.getRootElement());
}
Also used : MCRFileContent(org.mycore.common.content.MCRFileContent) Document(org.jdom2.Document) File(java.io.File)

Example 10 with MCRFileContent

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

the class MCRUserCommands method addRole.

/**
 * Loads XML from a user and looks for roles currently not present in the system and creates them.
 *
 * @param fileName
 *            a valid user XML file
 */
@MCRCommand(syntax = "import role from file {0}", help = "Imports a role from file, if that role does not exist", order = 90)
public static void addRole(String fileName) throws SAXParseException, IOException {
    LOGGER.info("Reading file {} ...", fileName);
    Document doc = MCRXMLParserFactory.getNonValidatingParser().parseXML(new MCRFileContent(fileName));
    MCRRole role = MCRRoleTransformer.buildMCRRole(doc.getRootElement());
    if (MCRRoleManager.getRole(role.getName()) == null) {
        MCRRoleManager.addRole(role);
    } else {
        LOGGER.info("Role {} does already exist.", role.getName());
    }
}
Also used : MCRFileContent(org.mycore.common.content.MCRFileContent) Document(org.jdom2.Document) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Aggregations

MCRFileContent (org.mycore.common.content.MCRFileContent)12 File (java.io.File)8 Document (org.jdom2.Document)6 Element (org.jdom2.Element)4 MCRContent (org.mycore.common.content.MCRContent)3 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)3 FileOutputStream (java.io.FileOutputStream)2 URL (java.net.URL)2 Test (org.junit.Test)2 MCRException (org.mycore.common.MCRException)2 MCRJDOMContent (org.mycore.common.content.MCRJDOMContent)2 MCRURLContent (org.mycore.common.content.MCRURLContent)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 MCRAccessInterface (org.mycore.access.MCRAccessInterface)1 MCRSourceContent (org.mycore.common.content.MCRSourceContent)1