Search in sources :

Example 51 with MCRJDOMContent

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

the class MCRDFGLinkServlet method getMetsSource.

/**
 * Returns the mets document wrapped in a {@link MCRContent} object.
 *
 * @param job
 * @param useExistingMets
 * @return
 * @throws Exception
 */
private static MCRContent getMetsSource(MCRServletJob job, boolean useExistingMets, String derivate) throws Exception {
    MCRPath metsFile = MCRPath.getPath(derivate, "/mets.xml");
    try {
        job.getRequest().setAttribute("XSL.derivateID", derivate);
        job.getRequest().setAttribute("XSL.objectID", MCRLinkTableManager.instance().getSourceOf(derivate).iterator().next());
    } catch (Exception x) {
        LOGGER.warn("Unable to set \"XSL.objectID\" attribute to current request", x);
    }
    boolean metsExists = Files.exists(metsFile);
    if (metsExists && useExistingMets) {
        MCRContent content = new MCRPathContent(metsFile);
        content.setDocType("mets");
        return content;
    } else {
        Document mets = MCRMETSGeneratorFactory.create(metsFile.getParent()).generate().asDocument();
        return new MCRJDOMContent(mets);
    }
}
Also used : MCRPathContent(org.mycore.common.content.MCRPathContent) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) MCRPath(org.mycore.datamodel.niofs.MCRPath) Document(org.jdom2.Document) MCRContent(org.mycore.common.content.MCRContent) MCRPersistenceException(org.mycore.common.MCRPersistenceException)

Example 52 with MCRJDOMContent

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

the class MCRMETSServlet method getMetsSource.

/**
 * Returns the mets document wrapped in a {@link MCRContent} object.
 */
static MCRContent getMetsSource(MCRServletJob job, boolean useExistingMets, String derivate) throws Exception {
    MCRPath metsPath = MCRPath.getPath(derivate, "/mets.xml");
    try {
        job.getRequest().setAttribute("XSL.derivateID", derivate);
        String objectid = MCRLinkTableManager.instance().getSourceOf(derivate).iterator().next();
        if (objectid == null || objectid.length() == 0) {
            MCRDerivate derObj = MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(derivate));
            MCRObjectID ownerID = derObj.getOwnerID();
            objectid = ownerID.toString();
        }
        job.getRequest().setAttribute("XSL.objectID", objectid);
    } catch (Exception x) {
        LOGGER.warn("Unable to set \"XSL.objectID\" attribute to current request", x);
    }
    boolean metsExists = Files.exists(metsPath);
    if (metsExists && useExistingMets) {
        MCRContent content = new MCRPathContent(metsPath);
        content.setDocType("mets");
        return content;
    } else {
        Document mets = MCRMETSGeneratorFactory.create(MCRPath.getPath(derivate, "/")).generate().asDocument();
        if (!metsExists && STORE_METS_ON_GENERATE) {
            MCRMetsSave.saveMets(mets, MCRObjectID.getInstance(derivate));
        }
        return new MCRJDOMContent(mets);
    }
}
Also used : MCRPathContent(org.mycore.common.content.MCRPathContent) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPath(org.mycore.datamodel.niofs.MCRPath) Document(org.jdom2.Document) MCRContent(org.mycore.common.content.MCRContent) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 53 with MCRJDOMContent

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

the class MCRStoreBrowserRequest method doGetPost.

@Override
protected void doGetPost(MCRServletJob job) throws Exception {
    String pathInfo = job.getRequest().getPathInfo();
    MCRStoreBrowserRequest sbr = new MCRStoreBrowserRequest(pathInfo);
    Document xml = sbr.buildResponseXML();
    getLayoutService().doLayout(job.getRequest(), job.getResponse(), new MCRJDOMContent(xml));
}
Also used : MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) Document(org.jdom2.Document)

Example 54 with MCRJDOMContent

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

the class MCRRestAPIObjectsHelper method showMCRObject.

public static Response showMCRObject(String pathParamId, String queryParamStyle, UriInfo info, HttpServletRequest request) throws MCRRestAPIException {
    MCRObject mcrObj = retrieveMCRObject(pathParamId);
    Document doc = mcrObj.createXML();
    Element eStructure = doc.getRootElement().getChild("structure");
    if (queryParamStyle != null && !MCRRestAPIObjects.STYLE_DERIVATEDETAILS.equals(queryParamStyle)) {
        throw new MCRRestAPIException(Response.Status.BAD_REQUEST, new MCRRestAPIError(MCRRestAPIError.CODE_WRONG_PARAMETER, "The value of parameter {style} is not allowed.", "Allowed values for {style} parameter are: " + MCRRestAPIObjects.STYLE_DERIVATEDETAILS));
    }
    if (MCRRestAPIObjects.STYLE_DERIVATEDETAILS.equals(queryParamStyle) && eStructure != null) {
        Element eDerObjects = eStructure.getChild("derobjects");
        if (eDerObjects != null) {
            for (Element eDer : eDerObjects.getChildren("derobject")) {
                String derID = eDer.getAttributeValue("href", MCRConstants.XLINK_NAMESPACE);
                try {
                    MCRDerivate der = MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(derID));
                    eDer.addContent(der.createXML().getRootElement().detach());
                    // <mycorederivate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink" xsi:noNamespaceSchemaLocation="datamodel-derivate.xsd" ID="cpr_derivate_00003760" label="display_image" version="1.3">
                    // <derivate display="true">
                    eDer = eDer.getChild("mycorederivate").getChild("derivate");
                    Document docContents = listDerivateContentAsXML(MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(derID)), "/", -1, info);
                    if (docContents.hasRootElement()) {
                        eDer.addContent(docContents.getRootElement().detach());
                    }
                } catch (MCRException e) {
                    eDer.addContent(new Comment("Error: Derivate not found."));
                } catch (IOException e) {
                    eDer.addContent(new Comment("Error: Derivate content could not be listed: " + e.getMessage()));
                }
            }
        }
    }
    StringWriter sw = new StringWriter();
    XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
    try {
        String filter_id = MCRConfiguration.instance().getString("MCR.RestAPI.v1.Filter.XML", "");
        if (filter_id.length() > 0) {
            MCRContentTransformer trans = MCRContentTransformerFactory.getTransformer(filter_id);
            Document filtered_doc = trans.transform(new MCRJDOMContent(doc)).asXML();
            outputter.output(filtered_doc, sw);
        } else {
            outputter.output(doc, sw);
        }
    } catch (SAXException | JDOMException e) {
        throw new MCRRestAPIException(Response.Status.INTERNAL_SERVER_ERROR, new MCRRestAPIError(MCRRestAPIError.CODE_INTERNAL_ERROR, "Unable to transform MCRContent to XML document", e.getMessage()));
    } catch (IOException e) {
        throw new MCRRestAPIException(Response.Status.INTERNAL_SERVER_ERROR, new MCRRestAPIError(MCRRestAPIError.CODE_INTERNAL_ERROR, "Unable to retrieve/transform MyCoRe object", e.getMessage()));
    }
    String authHeader = MCRJSONWebTokenUtil.createJWTAuthorizationHeader(MCRJSONWebTokenUtil.retrieveAuthenticationToken(request));
    return Response.ok(sw.toString()).type("application/xml").header(HEADER_NAME_AUTHORIZATION, authHeader).build();
}
Also used : Comment(org.jdom2.Comment) XMLOutputter(org.jdom2.output.XMLOutputter) MCRRestAPIException(org.mycore.restapi.v1.errors.MCRRestAPIException) MCRException(org.mycore.common.MCRException) Element(org.jdom2.Element) MCRRestAPIError(org.mycore.restapi.v1.errors.MCRRestAPIError) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) IOException(java.io.IOException) Document(org.jdom2.Document) JDOMException(org.jdom2.JDOMException) SAXException(org.xml.sax.SAXException) MCRObject(org.mycore.datamodel.metadata.MCRObject) StringWriter(java.io.StringWriter) MCRContentTransformer(org.mycore.common.content.transformer.MCRContentTransformer)

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