Search in sources :

Example 26 with MCRJDOMContent

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));
}
Also used : Element(org.jdom2.Element) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent)

Example 27 with MCRJDOMContent

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);
        }
    }
}
Also used : MCRSession(org.mycore.common.MCRSession) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) Document(org.jdom2.Document)

Example 28 with MCRJDOMContent

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;
}
Also used : MCRObjectStructure(org.mycore.datamodel.metadata.MCRObjectStructure) MCRObject(org.mycore.datamodel.metadata.MCRObject) Element(org.jdom2.Element) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) MCRAccessException(org.mycore.access.MCRAccessException) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPersistenceException(org.mycore.common.MCRPersistenceException)

Example 29 with MCRJDOMContent

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));
}
Also used : Element(org.jdom2.Element) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) Document(org.jdom2.Document)

Example 30 with MCRJDOMContent

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\""));
}
Also used : MCRStringContent(org.mycore.common.content.MCRStringContent) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) FileObject(org.apache.commons.vfs2.FileObject) Document(org.jdom2.Document) Test(org.junit.Test)

Aggregations

MCRJDOMContent (org.mycore.common.content.MCRJDOMContent)54 Document (org.jdom2.Document)33 Element (org.jdom2.Element)28 Test (org.junit.Test)21 MCRContent (org.mycore.common.content.MCRContent)20 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)6 IOException (java.io.IOException)5 File (java.io.File)4 MCRParameterCollector (org.mycore.common.xsl.MCRParameterCollector)4 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)4 MCRObject (org.mycore.datamodel.metadata.MCRObject)4 Date (java.util.Date)3 JDOMException (org.jdom2.JDOMException)3 MCRContentTransformer (org.mycore.common.content.transformer.MCRContentTransformer)3 MCRPath (org.mycore.datamodel.niofs.MCRPath)3 FileObject (org.apache.commons.vfs2.FileObject)2 XMLOutputter (org.jdom2.output.XMLOutputter)2 MCRException (org.mycore.common.MCRException)2 MCRPersistenceException (org.mycore.common.MCRPersistenceException)2 MCRFileContent (org.mycore.common.content.MCRFileContent)2