Search in sources :

Example 66 with MCRObject

use of org.mycore.datamodel.metadata.MCRObject in project mycore by MyCoRe-Org.

the class MCRMODSPURLPersistentIdentifierMetadataManager method insertIdentifier.

@Override
public void insertIdentifier(MCRPersistentUniformResourceLocator identifier, MCRBase obj, String additional) throws MCRPersistentIdentifierException {
    MCRObject object = checkObject(obj);
    MCRMODSWrapper wrapper = new MCRMODSWrapper(object);
    wrapper.setElement("identifier", "type", "purl", identifier.asString()).orElseThrow(() -> new MCRException("Could not insert purl into mods document!"));
}
Also used : MCRException(org.mycore.common.MCRException) MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRMODSWrapper(org.mycore.mods.MCRMODSWrapper)

Example 67 with MCRObject

use of org.mycore.datamodel.metadata.MCRObject in project mycore by MyCoRe-Org.

the class MCRMODSURNPersistentIdentifierMetadataManager method getIdentifier.

@Override
public Optional<MCRPersistentIdentifier> getIdentifier(MCRBase obj, String additional) throws MCRPersistentIdentifierException {
    MCRObject object = checkObject(obj);
    MCRMODSWrapper wrapper = new MCRMODSWrapper(object);
    Element element = wrapper.getElement(MODS_IDENTIFIER_TYPE_URN);
    if (element == null) {
        return Optional.empty();
    }
    String urnText = element.getTextNormalize();
    return new MCRDNBURNParser().parse(urnText).filter(Objects::nonNull).map(MCRPersistentIdentifier.class::cast);
}
Also used : MCRObject(org.mycore.datamodel.metadata.MCRObject) Element(org.jdom2.Element) MCRMODSWrapper(org.mycore.mods.MCRMODSWrapper) MCRDNBURNParser(org.mycore.pi.urn.MCRDNBURNParser) MCRPersistentIdentifier(org.mycore.pi.MCRPersistentIdentifier)

Example 68 with MCRObject

use of org.mycore.datamodel.metadata.MCRObject in project mycore by MyCoRe-Org.

the class MCRRestAPIObjectsHelper method listContents.

/**
 * lists derivate content (file listing)
 * @param info - the Jersey UriInfo Object
 * @param request - the HTTPServletRequest object
 * @param mcrObjID - the MyCoRe Object ID
 * @param mcrDerID - the MyCoRe Derivate ID
 * @param format - the output format ('xml'|'json')
 * @param path - the sub path of a directory inside the derivate
 * @param depth - the level of subdirectories to be returned
 * @return a Jersey Response object
 * @throws MCRRestAPIException
 */
public static Response listContents(UriInfo info, HttpServletRequest httpRequest, Request request, String mcrObjID, String mcrDerID, String format, String path, int depth) throws MCRRestAPIException {
    if (!format.equals(MCRRestAPIObjects.FORMAT_JSON) && !format.equals(MCRRestAPIObjects.FORMAT_XML)) {
        throw new MCRRestAPIException(Response.Status.BAD_REQUEST, new MCRRestAPIError(MCRRestAPIError.CODE_WRONG_PARAMETER, "The syntax of format parameter is wrong.", "Allowed values for format are 'json' or 'xml'."));
    }
    MCRObject mcrObj = retrieveMCRObject(mcrObjID);
    MCRDerivate derObj = retrieveMCRDerivate(mcrObj, mcrDerID);
    String authHeader = MCRJSONWebTokenUtil.createJWTAuthorizationHeader(MCRJSONWebTokenUtil.retrieveAuthenticationToken(httpRequest));
    try {
        MCRPath root = MCRPath.getPath(derObj.getId().toString(), "/");
        BasicFileAttributes readAttributes = Files.readAttributes(root, BasicFileAttributes.class);
        Date lastModified = new Date(readAttributes.lastModifiedTime().toMillis());
        ResponseBuilder responseBuilder = request.evaluatePreconditions(lastModified);
        if (responseBuilder != null) {
            return responseBuilder.header(HEADER_NAME_AUTHORIZATION, authHeader).build();
        }
        switch(format) {
            case MCRRestAPIObjects.FORMAT_XML:
                Document docOut = listDerivateContentAsXML(derObj, path, depth, info);
                try (StringWriter sw = new StringWriter()) {
                    XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
                    xout.output(docOut, sw);
                    return response(sw.toString(), "application/xml", lastModified, authHeader);
                } catch (IOException e) {
                    throw new MCRRestAPIException(Response.Status.INTERNAL_SERVER_ERROR, new MCRRestAPIError(MCRRestAPIError.CODE_INTERNAL_ERROR, GENERAL_ERROR_MSG, e.getMessage()));
                }
            case MCRRestAPIObjects.FORMAT_JSON:
                if (MCRRestAPIObjects.FORMAT_JSON.equals(format)) {
                    String result = listDerivateContentAsJson(derObj, path, depth, info);
                    return response(result, "application/json", lastModified, authHeader);
                }
            default:
                throw new MCRRestAPIException(Response.Status.INTERNAL_SERVER_ERROR, new MCRRestAPIError(MCRRestAPIError.CODE_INTERNAL_ERROR, "Unexepected program flow termination.", "Please contact a developer!"));
        }
    } catch (IOException e) {
        throw new MCRRestAPIException(Response.Status.INTERNAL_SERVER_ERROR, new MCRRestAPIError(MCRRestAPIError.CODE_INTERNAL_ERROR, "Unexepected program flow termination.", e.getMessage()));
    }
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) MCRRestAPIException(org.mycore.restapi.v1.errors.MCRRestAPIException) MCRRestAPIError(org.mycore.restapi.v1.errors.MCRRestAPIError) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) IOException(java.io.IOException) Document(org.jdom2.Document) Date(java.util.Date) MCRObjectIDDate(org.mycore.datamodel.common.MCRObjectIDDate) MCRObject(org.mycore.datamodel.metadata.MCRObject) StringWriter(java.io.StringWriter) MCRPath(org.mycore.datamodel.niofs.MCRPath) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 69 with MCRObject

use of org.mycore.datamodel.metadata.MCRObject in project mycore by MyCoRe-Org.

the class MCRRestAPIObjectsHelper method showMCRDerivate.

public static Response showMCRDerivate(String pathParamMcrID, String pathParamDerID, UriInfo info, HttpServletRequest request) throws MCRRestAPIException {
    MCRObject mcrObj = retrieveMCRObject(pathParamMcrID);
    MCRDerivate derObj = retrieveMCRDerivate(mcrObj, pathParamDerID);
    try {
        Document doc = derObj.createXML();
        Document docContent = listDerivateContentAsXML(derObj, "/", -1, info);
        if (docContent != null && docContent.hasRootElement()) {
            doc.getRootElement().addContent(docContent.getRootElement().detach());
        }
        StringWriter sw = new StringWriter();
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        outputter.output(doc, sw);
        String authHeader = MCRJSONWebTokenUtil.createJWTAuthorizationHeader(MCRJSONWebTokenUtil.retrieveAuthenticationToken(request));
        return Response.ok(sw.toString()).type("application/xml").header(HEADER_NAME_AUTHORIZATION, authHeader).build();
    } catch (IOException e) {
        throw new MCRRestAPIException(Response.Status.INTERNAL_SERVER_ERROR, new MCRRestAPIError(MCRRestAPIError.CODE_INTERNAL_ERROR, GENERAL_ERROR_MSG, e.getMessage()));
    }
// return MCRRestAPIError.create(Response.Status.INTERNAL_SERVER_ERROR, "Unexepected program flow termination.",
// "Please contact a developer!").createHttpResponse();
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) MCRRestAPIException(org.mycore.restapi.v1.errors.MCRRestAPIException) MCRObject(org.mycore.datamodel.metadata.MCRObject) StringWriter(java.io.StringWriter) MCRRestAPIError(org.mycore.restapi.v1.errors.MCRRestAPIError) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) IOException(java.io.IOException) Document(org.jdom2.Document)

Example 70 with MCRObject

use of org.mycore.datamodel.metadata.MCRObject 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

MCRObject (org.mycore.datamodel.metadata.MCRObject)71 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)33 Document (org.jdom2.Document)18 Element (org.jdom2.Element)17 MCRException (org.mycore.common.MCRException)16 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)14 MCRMetaLinkID (org.mycore.datamodel.metadata.MCRMetaLinkID)14 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)12 IOException (java.io.IOException)11 MCRAccessException (org.mycore.access.MCRAccessException)11 MCRMODSWrapper (org.mycore.mods.MCRMODSWrapper)9 MCRPersistenceException (org.mycore.common.MCRPersistenceException)7 Date (java.util.Date)6 JDOMException (org.jdom2.JDOMException)6 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)6 MCRPersistentIdentifierException (org.mycore.pi.exceptions.MCRPersistentIdentifierException)6 SAXException (org.xml.sax.SAXException)6 URI (java.net.URI)5 URISyntaxException (java.net.URISyntaxException)5 ArrayList (java.util.ArrayList)5