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;
}
}
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;
}
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);
}
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;
}
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;
}
Aggregations