use of org.jdom2.Text 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);
}
use of org.jdom2.Text 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;
}
use of org.jdom2.Text in project mycore by MyCoRe-Org.
the class MCROAIDataProvider method addXSLStyle.
/**
* Add link to XSL stylesheet for displaying OAI response in web browser.
*/
private Document addXSLStyle(Document doc) {
String styleSheet = MCROAIAdapter.PREFIX + getServletName() + ".ResponseStylesheet";
String xsl = MCRConfiguration.instance().getString(styleSheet, "oai/oai2.xsl");
if (!xsl.isEmpty()) {
Map<String, String> pairs = new HashMap<>();
pairs.put("type", "text/xsl");
pairs.put("href", MCRFrontendUtil.getBaseURL() + xsl);
doc.addContent(0, new ProcessingInstruction("xml-stylesheet", pairs));
}
return doc;
}
use of org.jdom2.Text in project mycore by MyCoRe-Org.
the class MCRXSL2XMLTransformer method getDocument.
private Document getDocument(JDOMResult result) {
Document resultDoc = result.getDocument();
if (resultDoc == null) {
// Sometimes a transformation produces whitespace strings
// JDOM would produce a empty document if it detects those
// So we remove them, if they exists.
List<Content> transformResult = result.getResult();
int origSize = transformResult.size();
Iterator<Content> iterator = transformResult.iterator();
while (iterator.hasNext()) {
Content content = iterator.next();
if (content instanceof Text) {
String trimmedText = ((Text) content).getTextTrim();
if (trimmedText.length() == 0) {
iterator.remove();
}
}
}
if (transformResult.size() < origSize) {
JDOMFactory f = result.getFactory();
if (f == null) {
f = new DefaultJDOMFactory();
}
resultDoc = f.document(null);
resultDoc.setContent(transformResult);
}
}
return resultDoc;
}
use of org.jdom2.Text in project mycore by MyCoRe-Org.
the class MCRTransferPackageUtil method getMCRObjects.
/**
* Gets the list of mycore object identifiers from the given directory.
*
* @param targetDirectory directory where the *.tar was unpacked
* @return list of object which lies within the directory
*/
public static List<String> getMCRObjects(Path targetDirectory) throws JDOMException, IOException {
Path order = targetDirectory.resolve(MCRTransferPackage.IMPORT_CONFIG_FILENAME);
Document xml = new SAXBuilder().build(order.toFile());
Element config = xml.getRootElement();
XPathExpression<Text> exp = MCRConstants.XPATH_FACTORY.compile("order/object/text()", Filters.text());
return exp.evaluate(config).stream().map(Text::getText).collect(Collectors.toList());
}
Aggregations