use of org.mycore.datamodel.metadata.MCRDerivate 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);
}
}
use of org.mycore.datamodel.metadata.MCRDerivate in project mycore by MyCoRe-Org.
the class MCRServiceFlagEventHandler method updateDerivateState.
protected static void updateDerivateState(MCRObjectID derID, MCRCategoryID state) {
MCRDerivate derivate = MCRMetadataManager.retrieveMCRDerivate(derID);
MCRCategoryID oldState = derivate.getService().getState();
if (!state.equals(oldState)) {
derivate.getService().setState(state);
MCRMetadataManager.updateMCRDerivateXML(derivate);
}
}
use of org.mycore.datamodel.metadata.MCRDerivate 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()));
}
}
use of org.mycore.datamodel.metadata.MCRDerivate 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();
}
use of org.mycore.datamodel.metadata.MCRDerivate in project mycore by MyCoRe-Org.
the class MCRRestAPIObjectsHelper method listDerivates.
/**
* returns a list of derivate objects
* @param info - the injected Jersey URIInfo object
*
* @param mcrObjID - the MyCoRe Object ID
* @param format - the output format ('xml'|'json')
* @param sort - the sort criteria
*
* @return a Jersey response object
* @throws MCRRestAPIException
*
* @see org.mycore.restapi.v1.MCRRestAPIObjects#listDerivates(UriInfo, HttpServletRequest, String, String, String)
*/
public static Response listDerivates(UriInfo info, HttpServletRequest request, String mcrObjID, String format, String sort) throws MCRRestAPIException {
List<MCRRestAPIError> errors = new ArrayList<>();
MCRRestAPISortObject sortObj = null;
try {
sortObj = createSortObject(sort);
} catch (MCRRestAPIException rae) {
errors.addAll(rae.getErrors());
}
if (format.equals(MCRRestAPIObjects.FORMAT_JSON) || format.equals(MCRRestAPIObjects.FORMAT_XML)) {
// ok
} else {
errors.add(new MCRRestAPIError(MCRRestAPIError.CODE_WRONG_PARAMETER, "The Parameter format is wrong.", "Allowed values for format are 'json' or 'xml'."));
}
if (errors.size() > 0) {
throw new MCRRestAPIException(Status.BAD_REQUEST, errors);
}
// Parameters are checked - continue to retrieve data
List<MCRObjectIDDate> objIdDates = retrieveMCRObject(mcrObjID).getStructure().getDerivates().stream().map(MCRMetaLinkID::getXLinkHrefID).filter(MCRMetadataManager::exists).map(id -> new MCRObjectIDDate() {
long lastModified;
{
try {
lastModified = MCRXMLMetadataManager.instance().getLastModified(id);
} catch (IOException e) {
lastModified = 0;
LOGGER.error("Exception while getting last modified of {}", id, e);
}
}
@Override
public String getId() {
return id.toString();
}
@Override
public Date getLastModified() {
return new Date(lastModified);
}
}).sorted(new MCRRestAPISortObjectComparator(sortObj)::compare).collect(Collectors.toList());
String authHeader = MCRJSONWebTokenUtil.createJWTAuthorizationHeader(MCRJSONWebTokenUtil.retrieveAuthenticationToken(request));
// output as XML
if (MCRRestAPIObjects.FORMAT_XML.equals(format)) {
Element eDerObjects = new Element("derobjects");
Document docOut = new Document(eDerObjects);
eDerObjects.setAttribute("numFound", Integer.toString(objIdDates.size()));
for (MCRObjectIDDate oid : objIdDates) {
Element eDerObject = new Element("derobject");
eDerObject.setAttribute("ID", oid.getId());
MCRDerivate der = MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(oid.getId()));
String mcrID = der.getDerivate().getMetaLink().getXLinkHref();
eDerObject.setAttribute("metadata", mcrID);
if (der.getLabel() != null) {
eDerObject.setAttribute("label", der.getLabel());
}
eDerObject.setAttribute("lastModified", SDF_UTC.format(oid.getLastModified()));
eDerObject.setAttribute("href", info.getAbsolutePathBuilder().path(oid.getId()).build().toString());
eDerObjects.addContent(eDerObject);
}
try {
StringWriter sw = new StringWriter();
XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
xout.output(docOut, sw);
return Response.ok(sw.toString()).type("application/xml; charset=UTF-8").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()));
}
}
// output as JSON
if (MCRRestAPIObjects.FORMAT_JSON.equals(format)) {
StringWriter sw = new StringWriter();
try {
JsonWriter writer = new JsonWriter(sw);
writer.setIndent(" ");
writer.beginObject();
writer.name("numFound").value(objIdDates.size());
writer.name("mycoreobjects");
writer.beginArray();
for (MCRObjectIDDate oid : objIdDates) {
writer.beginObject();
writer.name("ID").value(oid.getId());
MCRDerivate der = MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(oid.getId()));
String mcrID = der.getDerivate().getMetaLink().getXLinkHref();
writer.name("metadata").value(mcrID);
if (der.getLabel() != null) {
writer.name("label").value(der.getLabel());
}
writer.name("lastModified").value(SDF_UTC.format(oid.getLastModified()));
writer.name("href").value(info.getAbsolutePathBuilder().path(oid.getId()).build().toString());
writer.endObject();
}
writer.endArray();
writer.endObject();
writer.close();
return Response.ok(sw.toString()).type("application/json; charset=UTF-8").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()));
}
}
throw new MCRRestAPIException(Response.Status.INTERNAL_SERVER_ERROR, new MCRRestAPIError(MCRRestAPIError.CODE_INTERNAL_ERROR, "Unexepected program flow termination.", "Please contact a developer!"));
}
Aggregations