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