Search in sources :

Example 6 with MCRPathContent

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

the class MCRMetsIIIFPresentationImpl method getMets.

public Document getMets(String id) throws IOException, JDOMException, SAXException {
    String objectid = MCRLinkTableManager.instance().getSourceOf(id).iterator().next();
    MCRContentTransformer transformer = getTransformer();
    MCRParameterCollector parameter = new MCRParameterCollector();
    if (objectid != null && objectid.length() != 0) {
        MCRDerivate derObj = MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(id));
        MCRObjectID ownerID = derObj.getOwnerID();
        objectid = ownerID.toString();
        parameter.setParameter("objectID", objectid);
        parameter.setParameter("derivateID", id);
    }
    MCRPath metsPath = MCRPath.getPath(id, "mets.xml");
    if (!Files.exists(metsPath)) {
        throw new MCRException("File not found: " + id);
    }
    MCRPathContent source = new MCRPathContent(metsPath);
    MCRContent content = transformer instanceof MCRParameterizedTransformer ? ((MCRParameterizedTransformer) transformer).transform(source, parameter) : transformer.transform(source);
    return content.asXML();
}
Also used : MCRParameterCollector(org.mycore.common.xsl.MCRParameterCollector) MCRException(org.mycore.common.MCRException) MCRPathContent(org.mycore.common.content.MCRPathContent) MCRParameterizedTransformer(org.mycore.common.content.transformer.MCRParameterizedTransformer) MCRContentTransformer(org.mycore.common.content.transformer.MCRContentTransformer) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRContent(org.mycore.common.content.MCRContent)

Example 7 with MCRPathContent

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

the class MCRDFGLinkServlet method doGetPost.

@Override
protected void doGetPost(MCRServletJob job) throws Exception {
    HttpServletRequest request = job.getRequest();
    HttpServletResponse response = job.getResponse();
    String filePath = request.getParameter("file") == null ? "" : request.getParameter("file");
    String derivateID = request.getParameter("deriv") == null ? "" : request.getParameter("deriv");
    if (derivateID.equals("")) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Derivate is not set");
    }
    String encodedMetsURL = URLEncoder.encode(MCRServlet.getServletBaseURL() + "MCRMETSServlet/" + derivateID + "?XSL.Style=dfg", "UTF-8");
    LOGGER.info(request.getPathInfo());
    MCRPath rootPath = MCRPath.getPath(derivateID, "/");
    if (!Files.isDirectory(rootPath)) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND, MessageFormat.format("Derivate {0} does not exist.", derivateID));
        return;
    }
    request.setAttribute("XSL.derivateID", derivateID);
    Collection<String> linkList = MCRLinkTableManager.instance().getSourceOf(derivateID);
    if (linkList.isEmpty()) {
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageFormat.format("Derivate {0} is not linked with a MCRObject. Please contact an administrator.", derivateID));
        return;
    }
    // TODO: this seems very very wrong
    if (filePath == "") {
        MCRDerivate derivate = MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(derivateID));
        filePath = derivate.getDerivate().getInternals().getMainDoc();
    }
    MCRPath metsPath = (MCRPath) rootPath.resolve("mets.xml");
    int imageNumber = -2;
    if (Files.exists(metsPath)) {
        imageNumber = getOrderNumber(new MCRPathContent(metsPath).asXML(), filePath);
    } else {
        MCRContent metsContent = getMetsSource(job, useExistingMets(request), derivateID);
        imageNumber = getOrderNumber(metsContent.asXML(), filePath);
    }
    String dfgURL = "";
    switch(imageNumber) {
        case -1:
            response.sendError(HttpServletResponse.SC_CONFLICT, MessageFormat.format("Image \"{0}\" not found in the MCRDerivate. Please contact an administrator.", filePath));
            return;
        case -2:
            dfgURL = "http://dfg-viewer.de/show/?set[mets]=" + encodedMetsURL;
            break;
        default:
            dfgURL = "http://dfg-viewer.de/show/?set[mets]=" + encodedMetsURL + "&set[image]=" + imageNumber;
            break;
    }
    response.sendRedirect(dfgURL);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MCRPathContent(org.mycore.common.content.MCRPathContent) HttpServletResponse(javax.servlet.http.HttpServletResponse) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRContent(org.mycore.common.content.MCRContent)

Example 8 with MCRPathContent

use of org.mycore.common.content.MCRPathContent 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 9 with MCRPathContent

use of org.mycore.common.content.MCRPathContent 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)

Aggregations

MCRPathContent (org.mycore.common.content.MCRPathContent)9 MCRPath (org.mycore.datamodel.niofs.MCRPath)9 MCRContent (org.mycore.common.content.MCRContent)7 Document (org.jdom2.Document)5 IOException (java.io.IOException)4 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)3 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)3 TransformerException (javax.xml.transform.TransformerException)2 MCRJDOMContent (org.mycore.common.content.MCRJDOMContent)2 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)2 InputStream (java.io.InputStream)1 FileVisitResult (java.nio.file.FileVisitResult)1 Path (java.nio.file.Path)1 PathMatcher (java.nio.file.PathMatcher)1 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)1 FileTime (java.nio.file.attribute.FileTime)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Element (org.jdom2.Element)1