Search in sources :

Example 71 with MCRObjectID

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

the class MetsResource method get.

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/crud/{derivateId}")
public String get(@PathParam("derivateId") String derivateId) {
    MCRObjectID derivateIdObject = MCRObjectID.getInstance(derivateId);
    checkDerivateExists(derivateIdObject);
    checkDerivateAccess(derivateIdObject, MCRAccessManager.PERMISSION_READ);
    MCRPath rootPath = MCRPath.getPath(derivateId, "/");
    if (!Files.isDirectory(rootPath)) {
        throw new WebApplicationException(Response.Status.NOT_FOUND);
    }
    MCRPath metsPath = MCRPath.getPath(derivateId, METS_XML_PATH);
    try {
        return MCRSimpleModelJSONConverter.toJSON(MCRXMLSimpleModelConverter.fromXML(getMetsDocument(metsPath)));
    } catch (Exception e) {
        throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRException(org.mycore.common.MCRException) JDOMException(org.jdom2.JDOMException) IOException(java.io.IOException) WebApplicationException(javax.ws.rs.WebApplicationException) Path(javax.ws.rs.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 72 with MCRObjectID

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

the class MetsResource method save.

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/crud/{derivateId}")
public String save(@PathParam("derivateId") String derivateId, String data) {
    MCRObjectID derivateIdObject = MCRObjectID.getInstance(derivateId);
    checkDerivateExists(derivateIdObject);
    checkDerivateAccess(derivateIdObject, MCRAccessManager.PERMISSION_WRITE);
    MCRMetsSimpleModel model = MCRJSONSimpleModelConverter.toSimpleModel(data);
    Document document = MCRSimpleModelXMLConverter.toXML(model);
    XMLOutputter o = new XMLOutputter();
    try (OutputStream out = Files.newOutputStream(MCRPath.getPath(derivateId, METS_XML_PATH), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
        o.output(document, out);
    } catch (IOException e) {
        throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
    }
    return "{ \"success\": true }";
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) WebApplicationException(javax.ws.rs.WebApplicationException) MCRMetsSimpleModel(org.mycore.mets.model.simple.MCRMetsSimpleModel) OutputStream(java.io.OutputStream) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) IOException(java.io.IOException) Document(org.jdom2.Document) Path(javax.ws.rs.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes)

Example 73 with MCRObjectID

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

the class MetsResource method startEditor.

@GET
@Path("/editor/start/{derivateId}")
@Produces(MediaType.TEXT_HTML)
public String startEditor(@PathParam("derivateId") String derivateId) {
    MCRObjectID derivateIdObject = MCRObjectID.getInstance(derivateId);
    checkDerivateExists(derivateIdObject);
    checkDerivateAccess(derivateIdObject, MCRAccessManager.PERMISSION_WRITE);
    InputStream resourceAsStream = MetsResource.class.getClassLoader().getResourceAsStream("mets-editor.html");
    try {
        StringWriter writer = new StringWriter();
        IOUtils.copy(resourceAsStream, writer, Charset.forName("UTF-8"));
        String htmlTemplate = writer.toString();
        // add additional javascript code
        String js = MCRConfiguration.instance().getString("MCR.Mets.Editor.additional.javascript", null);
        if (js != null && !js.isEmpty()) {
            htmlTemplate = htmlTemplate.replace("<link rel=\"additionalJS\" />", js);
        }
        // replace variables
        htmlTemplate = htmlTemplate.replaceAll("\\{baseURL\\}", MCRFrontendUtil.getBaseURL()).replaceAll("\\{derivateID\\}", derivateId);
        return htmlTemplate;
    } catch (IOException e) {
        throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
    }
}
Also used : StringWriter(java.io.StringWriter) WebApplicationException(javax.ws.rs.WebApplicationException) InputStream(java.io.InputStream) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) IOException(java.io.IOException) Path(javax.ws.rs.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 74 with MCRObjectID

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

the class MCRMETSServlet method doGetPost.

@Override
protected void doGetPost(MCRServletJob job) throws Exception {
    HttpServletRequest request = job.getRequest();
    HttpServletResponse response = job.getResponse();
    LOGGER.info(request.getPathInfo());
    String derivate = getOwnerID(request.getPathInfo());
    MCRPath rootPath = MCRPath.getPath(derivate, "/");
    if (!Files.isDirectory(rootPath)) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND, MessageFormat.format("Derivate {0} does not exist.", derivate));
        return;
    }
    request.setAttribute("XSL.derivateID", derivate);
    Collection<String> linkList = MCRLinkTableManager.instance().getSourceOf(derivate);
    if (linkList.isEmpty()) {
        MCRDerivate derivate2 = MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(derivate));
        MCRObjectID ownerID = derivate2.getOwnerID();
        if (ownerID != null && ownerID.toString().length() != 0) {
            linkList.add(ownerID.toString());
        } else {
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageFormat.format("Derivate {0} is not linked with a MCRObject. Please contact an administrator.", derivate));
            return;
        }
    }
    request.setAttribute("XSL.objectID", linkList.iterator().next());
    response.setContentType("text/xml");
    long lastModified = Files.getLastModifiedTime(rootPath).toMillis();
    MCRFrontendUtil.writeCacheHeaders(response, (long) CACHE_TIME, lastModified, useExpire);
    long start = System.currentTimeMillis();
    MCRContent metsContent = getMetsSource(job, useExistingMets(request), derivate);
    MCRLayoutService.instance().doLayout(request, response, metsContent);
    LOGGER.info("Generation of code by {} took {} ms", this.getClass().getSimpleName(), System.currentTimeMillis() - start);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) 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 75 with MCRObjectID

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

the class MCRMetsLock method doLock.

/**
 * Locks a Derivate with the current SessionId
 * @param derivateIdString the Derivate to lock
 * @return True if the derivate is locked. False if the derivate could not be locked.
 */
public static synchronized boolean doLock(String derivateIdString) {
    MCRObjectID derivateId = MCRObjectID.getInstance(derivateIdString);
    if (isLocked(derivateIdString) && MCRMetsLock.metsAccessSessionTable.get(derivateId) != MCRSessionMgr.getCurrentSessionID()) {
        LOGGER.info(MessageFormat.format("Could not lock {0}, because its already locked.", derivateIdString));
        return false;
    } else {
        LOGGER.info(MessageFormat.format("{0} is now locked", derivateIdString));
        MCRMetsLock.metsAccessSessionTable.put(derivateId, MCRSessionMgr.getCurrentSessionID());
        return true;
    }
}
Also used : MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID)

Aggregations

MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)144 IOException (java.io.IOException)37 MCRObject (org.mycore.datamodel.metadata.MCRObject)32 MCRException (org.mycore.common.MCRException)30 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)30 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)29 MCRPath (org.mycore.datamodel.niofs.MCRPath)25 MCRAccessException (org.mycore.access.MCRAccessException)22 Document (org.jdom2.Document)20 MCRPersistenceException (org.mycore.common.MCRPersistenceException)18 MCRMetaLinkID (org.mycore.datamodel.metadata.MCRMetaLinkID)16 JDOMException (org.jdom2.JDOMException)15 MCRBase (org.mycore.datamodel.metadata.MCRBase)15 SAXException (org.xml.sax.SAXException)15 Date (java.util.Date)14 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)13 MCRSession (org.mycore.common.MCRSession)11 MCRContent (org.mycore.common.content.MCRContent)11 MCRPersistentIdentifierException (org.mycore.pi.exceptions.MCRPersistentIdentifierException)11 URISyntaxException (java.net.URISyntaxException)10