use of org.mycore.common.content.MCRJDOMContent in project mycore by MyCoRe-Org.
the class MCRXSLInfoServlet method buildOutput.
private void buildOutput(MCRServletJob job) throws IOException, TransformerException, SAXException {
Element output = new Element("stylesheets");
for (Entry<String, Stylesheet> entry : stylesheets.entrySet()) {
Stylesheet stylesheet = entry.getValue();
output.addContent(stylesheet.buildXML());
}
getLayoutService().doLayout(job.getRequest(), job.getResponse(), new MCRJDOMContent(output));
}
use of org.mycore.common.content.MCRJDOMContent in project mycore by MyCoRe-Org.
the class MCRErrorServlet method generateErrorPage.
protected void generateErrorPage(HttpServletRequest request, HttpServletResponse response, String msg, Throwable ex, Integer statusCode, Class<? extends Throwable> exceptionType, String requestURI, String servletName) throws IOException, TransformerException, SAXException {
boolean exceptionThrown = ex != null;
LOGGER.log(exceptionThrown ? Level.ERROR : Level.WARN, MessageFormat.format("{0}: Error {1} occured. The following message was given: {2}", requestURI, statusCode, msg), ex);
String style = MCRFrontendUtil.getProperty(request, "XSL.Style").filter("xml"::equals).orElse("default");
request.setAttribute("XSL.Style", style);
Document errorDoc = buildErrorPage(msg, statusCode, requestURI, exceptionType, servletName, ex);
final String requestAttr = "MCRErrorServlet.generateErrorPage";
if (!response.isCommitted() && request.getAttribute(requestAttr) == null) {
if (statusCode != null) {
response.setStatus(statusCode);
} else {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
request.setAttribute(requestAttr, msg);
boolean currentSessionActive = MCRSessionMgr.hasCurrentSession();
boolean sessionFromRequest = setCurrentSession(request);
MCRSession session = null;
try {
session = MCRSessionMgr.getCurrentSession();
boolean openTransaction = session.isTransactionActive();
if (!openTransaction) {
session.beginTransaction();
}
try {
setWebAppBaseURL(session, request);
LAYOUT_SERVICE.doLayout(request, response, new MCRJDOMContent(errorDoc));
} finally {
if (!openTransaction)
session.commitTransaction();
}
} finally {
if (exceptionThrown || !currentSessionActive) {
MCRSessionMgr.releaseCurrentSession();
}
if (!sessionFromRequest) {
// new session created for transaction
session.close();
}
}
} else {
if (request.getAttribute(requestAttr) != null) {
LOGGER.warn("Could not send error page. Generating error page failed. The original message:\n{}", request.getAttribute(requestAttr));
} else {
LOGGER.warn("Could not send error page. Response allready commited. The following message was given:\n{}", msg);
}
}
}
use of org.mycore.common.content.MCRJDOMContent in project mycore by MyCoRe-Org.
the class MCRUpdateDerivateServlet method updateDerivateXML.
/**
* Updates derivate xml in the persistence backend
* @param editorSubmission
* MyCoRe derivate as XML
* @return
* MCRObjectID of the MyCoRe object
* @throws SAXParseException
* @throws MCRAccessException
*/
private MCRObjectID updateDerivateXML(Document editorSubmission) throws SAXParseException, IOException, MCRAccessException {
MCRObjectID objectID;
Element root = editorSubmission.getRootElement();
root.setAttribute("noNamespaceSchemaLocation", "datamodel-derivate.xsd", XSI_NAMESPACE);
root.addNamespaceDeclaration(XLINK_NAMESPACE);
root.addNamespaceDeclaration(XSI_NAMESPACE);
byte[] xml = new MCRJDOMContent(editorSubmission).asByteArray();
MCRDerivate der = new MCRDerivate(xml, true);
MCRObjectID derivateID = der.getId();
// store entry of derivate xlink:title in object
objectID = der.getDerivate().getMetaLink().getXLinkHrefID();
MCRObject obj = MCRMetadataManager.retrieveMCRObject(objectID);
MCRObjectStructure structure = obj.getStructure();
MCRMetaLinkID linkID = structure.getDerivateLink(derivateID);
linkID.setXLinkTitle(der.getLabel());
try {
MCRMetadataManager.update(obj);
MCRMetadataManager.update(der);
} catch (MCRPersistenceException | MCRAccessException e) {
throw new MCRPersistenceException("Can't store label of derivate " + derivateID + " in derivate list of object " + objectID + ".", e);
}
return objectID;
}
use of org.mycore.common.content.MCRJDOMContent in project mycore by MyCoRe-Org.
the class MCRLoginServlet method listRealms.
private void listRealms(HttpServletRequest req, HttpServletResponse res) throws IOException, TransformerException, SAXException {
String redirectURL = getReturnURL(req);
Document realmsDoc = MCRRealmFactory.getRealmsDocument();
Element realms = realmsDoc.getRootElement();
addCurrentUserInfo(realms);
List<Element> realmList = realms.getChildren(REALM_URL_PARAMETER);
for (Element realm : realmList) {
String realmID = realm.getAttributeValue("id");
Element login = realm.getChild("login");
if (login != null)
login.setAttribute("url", MCRRealmFactory.getRealm(realmID).getLoginURL(redirectURL));
}
getLayoutService().doLayout(req, res, new MCRJDOMContent(realmsDoc));
}
use of org.mycore.common.content.MCRJDOMContent in project mycore by MyCoRe-Org.
the class MCRFileStoreTest method repairMetadata.
@Test
public void repairMetadata() throws Exception {
MCRFileCollection col = getStore().create();
Document xml1 = col.getMetadata().clone();
col.repairMetadata();
Document xml2 = col.getMetadata().clone();
assertTrue(equals(xml1, xml2));
MCRDirectory dir = col.createDir("foo");
xml1 = col.getMetadata().clone();
assertFalse(equals(xml1, xml2));
dir.delete();
xml1 = col.getMetadata().clone();
assertTrue(equals(xml1, xml2));
MCRDirectory dir2 = col.createDir("dir");
MCRFile file1 = col.createFile("test1.txt");
file1.setContent(new MCRStringContent("Test 1"));
MCRFile readme = dir2.createFile("readme.txt");
readme.setContent(new MCRStringContent("Hallo Welt!"));
MCRFile file3 = col.createFile("test2.txt");
file3.setContent(new MCRStringContent("Test 2"));
file3.setLabel("de", "Die Testdatei");
xml2 = col.getMetadata().clone();
col.repairMetadata();
xml1 = col.getMetadata().clone();
assertTrue(equals(xml1, xml2));
file3.clearLabels();
xml2 = col.getMetadata().clone();
col.fo.getChild("mcrdata.xml").delete();
col = getStore().retrieve(col.getID());
xml1 = col.getMetadata().clone();
assertTrue(equals(xml1, xml2));
col.fo.getChild("test1.txt").delete();
FileObject tmp = col.fo.resolveFile("test3.txt");
tmp.createFile();
new MCRStringContent("Hallo Welt!").sendTo(tmp);
col.repairMetadata();
String xml3 = new MCRJDOMContent(col.getMetadata()).asString();
assertFalse(xml3.contains("name=\"test1.txt\""));
assertTrue(xml3.contains("name=\"test3.txt\""));
}
Aggregations