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