Search in sources :

Example 41 with MCRJDOMContent

use of org.mycore.common.content.MCRJDOMContent in project mycore by MyCoRe-Org.

the class MCREditorOutValidator method generateValidMyCoReObject.

/**
 * tries to generate a valid MCRObject as JDOM Document.
 *
 * @return MCRObject
 */
public Document generateValidMyCoReObject() throws JDOMException, SAXParseException, IOException {
    MCRObject obj;
    // load the JDOM object
    XPathFactory.instance().compile("/mycoreobject/*/*/*/@editor.output", Filters.attribute()).evaluate(input).forEach(Attribute::detach);
    try {
        byte[] xml = new MCRJDOMContent(input).asByteArray();
        obj = new MCRObject(xml, true);
    } catch (SAXParseException e) {
        XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
        LOGGER.warn("Failure while parsing document:\n{}", xout.outputString(input));
        throw e;
    }
    Date curTime = new Date();
    obj.getService().setDate("modifydate", curTime);
    // return the XML tree
    input = obj.createXML();
    return input;
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) MCRObject(org.mycore.datamodel.metadata.MCRObject) Attribute(org.jdom2.Attribute) SAXParseException(org.xml.sax.SAXParseException) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) MCRMetaISO8601Date(org.mycore.datamodel.metadata.MCRMetaISO8601Date) Date(java.util.Date) MCRMetaHistoryDate(org.mycore.datamodel.metadata.MCRMetaHistoryDate)

Example 42 with MCRJDOMContent

use of org.mycore.common.content.MCRJDOMContent in project mycore by MyCoRe-Org.

the class MCRUserServlet method listUsers.

/**
 * Handles MCRUserServlet?search={pattern}, which is an optional parameter.
 * Searches for users matching the pattern in user name or real name and outputs
 * the list of results using users.xsl. The search pattern may contain * and ?
 * wildcard characters. The property MCR.user2.Users.MaxResults (default 100) specifies
 * the maximum number of users to return. When there are more hits, just the
 * number of results is returned.
 *
 * When current user is not admin, the search pattern will be ignored and only all
 * the users the current user is owner of will be listed.
 */
private void listUsers(HttpServletRequest req, HttpServletResponse res) throws Exception {
    MCRUser currentUser = MCRUserManager.getCurrentUser();
    List<MCRUser> ownUsers = MCRUserManager.listUsers(currentUser);
    boolean hasAdminPermission = MCRAccessManager.checkPermission(MCRUser2Constants.USER_ADMIN_PERMISSION);
    boolean allowed = hasAdminPermission || MCRAccessManager.checkPermission(MCRUser2Constants.USER_CREATE_PERMISSION) || !ownUsers.isEmpty();
    if (!allowed) {
        String msg = MCRTranslation.translate("component.user2.UserServlet.noCreatePermission");
        res.sendError(HttpServletResponse.SC_FORBIDDEN, msg);
        return;
    }
    Element users = new Element("users");
    List<MCRUser> results = null;
    if (hasAdminPermission) {
        String search = req.getParameter("search");
        if ((search == null) || search.trim().length() == 0)
            search = null;
        if (search != null) {
            users.setAttribute("search", search);
            search = "*" + search + "*";
        }
        LOGGER.info("search users like {}", search);
        int max = MCRConfiguration.instance().getInt(MCRUser2Constants.CONFIG_PREFIX + "Users.MaxResults", 100);
        int num = MCRUserManager.countUsers(search, null, search);
        if ((num < max) && (num > 0))
            results = MCRUserManager.listUsers(search, null, search);
        users.setAttribute("num", String.valueOf(num));
        users.setAttribute("max", String.valueOf(max));
    } else {
        LOGGER.info("list owned users of {} {}", currentUser.getUserName(), currentUser.getRealmID());
        results = ownUsers;
    }
    if (results != null)
        for (MCRUser user : results) {
            Element u = MCRUserTransformer.buildBasicXML(user).detachRootElement();
            addString(u, "realName", user.getRealName());
            addString(u, "eMail", user.getEMailAddress());
            users.addContent(u);
        }
    getLayoutService().doLayout(req, res, new MCRJDOMContent(users));
}
Also used : Element(org.jdom2.Element) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent)

Example 43 with MCRJDOMContent

use of org.mycore.common.content.MCRJDOMContent in project mycore by MyCoRe-Org.

the class MCRViewerResource method getContent.

protected MCRContent getContent(final HttpServletRequest req) throws Exception {
    // get derivate id from request object
    String derivate = MCRViewerConfiguration.getDerivate(req);
    if (derivate == null) {
        MCRJerseyUtil.throwException(Status.BAD_REQUEST, "Could not locate derivate identifer in path.");
    }
    // get mycore object id
    final MCRObjectID derivateID = MCRObjectID.getInstance(derivate);
    if (!MCRMetadataManager.exists(derivateID)) {
        String errorMessage = MCRTranslation.translate("component.viewer.MCRIViewClientServlet.object.not.found", derivateID);
        MCRJerseyUtil.throwException(Status.NOT_FOUND, errorMessage);
    }
    // check permission
    if (IVIEW_ACL_PROVDER != null && !IVIEW_ACL_PROVDER.checkAccess(req.getSession(), derivateID)) {
        String errorMessage = MCRTranslation.translate("component.viewer.MCRIViewClientServlet.noRights", derivateID);
        MCRJerseyUtil.throwException(Status.UNAUTHORIZED, errorMessage);
    }
    // build configuration object
    MCRViewerConfigurationStrategy configurationStrategy = MCRConfiguration.instance().getInstanceOf("MCR.Viewer.configuration.strategy", new MCRViewerDefaultConfigurationStrategy());
    MCRJDOMContent source = new MCRJDOMContent(buildResponseDocument(configurationStrategy.get(req)));
    MCRParameterCollector parameter = new MCRParameterCollector(req);
    MCRContentTransformer transformer = getContentTransformer(source.getDocType(), parameter);
    return transformer.transform(source);
}
Also used : MCRParameterCollector(org.mycore.common.xsl.MCRParameterCollector) MCRViewerConfigurationStrategy(org.mycore.viewer.configuration.MCRViewerConfigurationStrategy) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) MCRViewerDefaultConfigurationStrategy(org.mycore.viewer.configuration.MCRViewerDefaultConfigurationStrategy) MCRContentTransformer(org.mycore.common.content.transformer.MCRContentTransformer) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID)

Example 44 with MCRJDOMContent

use of org.mycore.common.content.MCRJDOMContent in project mycore by MyCoRe-Org.

the class MCRDerivateLinkServlet method doGetPost.

@Override
protected void doGetPost(MCRServletJob job) throws Exception {
    @SuppressWarnings("unchecked") Map<String, String[]> pMap = job.getRequest().getParameterMap();
    String webpage = pMap.get("subselect.webpage")[0];
    String mcrId = getSubParameterValueOfBrowserAddressParameter(webpage, "mcrid");
    String parentId = getSubParameterValueOfBrowserAddressParameter(webpage, "parentID");
    // create a new root element
    Element rootElement = new Element("derivateLinks-parentList");
    MCRObjectID objId = MCRObjectID.getInstance(mcrId);
    if (MCRMetadataManager.exists(objId)) {
        /* mcr object exists in datastore -> add all parent with their
             * derivates to the jdom tree */
        addParentsToElement(rootElement, objId);
    } else if (parentId != null && MCRMetadataManager.exists(MCRObjectID.getInstance(parentId))) {
        /* mcr object doesnt exists in datastore -> use the parent id
             * to create the content */
        Element firstParent = getMyCoReObjectElement(MCRObjectID.getInstance(parentId));
        if (firstParent != null) {
            rootElement.addContent(firstParent);
        }
        addParentsToElement(rootElement, MCRObjectID.getInstance(parentId));
    }
    // check if root element has content -> if not, show an error page
    if (rootElement.getContentSize() == 0) {
        job.getResponse().sendRedirect(job.getResponse().encodeRedirectURL(MCRFrontendUtil.getBaseURL() + derivateLinkErrorPage));
        return;
    }
    // set some important attributes to the root element
    rootElement.setAttribute("session", pMap.get("subselect.session")[0]);
    rootElement.setAttribute("varpath", pMap.get("subselect.varpath")[0]);
    rootElement.setAttribute("webpage", webpage);
    // transform & display the generated xml document
    getLayoutService().doLayout(job.getRequest(), job.getResponse(), new MCRJDOMContent(rootElement));
}
Also used : Element(org.jdom2.Element) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID)

Example 45 with MCRJDOMContent

use of org.mycore.common.content.MCRJDOMContent in project mycore by MyCoRe-Org.

the class MCRSolrTransformerInputDocumentFactory method getDocuments.

@Override
public Iterator<SolrInputDocument> getDocuments(Map<MCRObjectID, MCRContent> contentMap) throws IOException, SAXException {
    if (contentMap.isEmpty()) {
        return Collections.emptyIterator();
    }
    try {
        Document doc = getMergedDocument(contentMap);
        if (isJAXBTransformer) {
            MCRParameterCollector param = new MCRParameterCollector();
            @SuppressWarnings("unchecked") MCRXSL2JAXBTransformer<MCRSolrInputDocumentList> jaxbTransformer = (MCRXSL2JAXBTransformer<MCRSolrInputDocumentList>) transformer;
            MCRSolrInputDocumentList input = jaxbTransformer.getJAXBObject(new MCRJDOMContent(doc), param);
            return MCRSolrInputDocumentGenerator.getSolrInputDocuments(input.getDoc()).iterator();
        } else {
            MCRContent result = transformer.transform(new MCRJDOMContent(doc));
            return getSolrInputDocuments(result);
        }
    } catch (TransformerConfigurationException | JAXBException | JDOMException | ParserConfigurationException e) {
        throw new IOException(e);
    }
}
Also used : MCRParameterCollector(org.mycore.common.xsl.MCRParameterCollector) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) JAXBException(javax.xml.bind.JAXBException) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) IOException(java.io.IOException) Document(org.jdom2.Document) SolrInputDocument(org.apache.solr.common.SolrInputDocument) JDOMException(org.jdom2.JDOMException) MCRContent(org.mycore.common.content.MCRContent) MCRSolrInputDocumentList(org.mycore.solr.index.document.jaxb.MCRSolrInputDocumentList) MCRXSL2JAXBTransformer(org.mycore.common.content.transformer.MCRXSL2JAXBTransformer) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

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