use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.
the class MCRMETSServlet method getLastModified.
/*
* (non-Javadoc)
*
* @see
* org.mycore.frontend.servlets.MCRServlet#getLastModified(javax.servlet
* .http.HttpServletRequest)
*/
@Override
protected long getLastModified(HttpServletRequest request) {
String ownerID = getOwnerID(request.getPathInfo());
MCRSession session = MCRSessionMgr.getCurrentSession();
MCRPath metsPath = MCRPath.getPath(ownerID, "/mets.xml");
try {
session.beginTransaction();
try {
if (Files.exists(metsPath)) {
return Files.getLastModifiedTime(metsPath).toMillis();
} else if (Files.isDirectory(metsPath.getParent())) {
return Files.getLastModifiedTime(metsPath.getParent()).toMillis();
}
} catch (IOException e) {
LOGGER.warn("Error while retrieving last modified information from {}", metsPath, e);
}
return -1L;
} finally {
session.commitTransaction();
MCRSessionMgr.releaseCurrentSession();
// just created session for db transaction
session.close();
}
}
use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.
the class MCRRestAPIObjectsHelper method createXMLForSubdirectories.
private static void createXMLForSubdirectories(MCRPath mcrPath, Element currentElement, int currentDepth, int maxDepth) {
if (currentDepth < maxDepth) {
XPathExpression<Element> xp = XPathFactory.instance().compile("./children/child[@type='directory']", Filters.element());
for (Element e : xp.evaluate(currentElement)) {
String name = e.getChildTextNormalize("name");
try {
MCRPath pChild = (MCRPath) mcrPath.resolve(name);
Document doc = MCRPathXML.getDirectoryXML(pChild);
Element eChildren = doc.getRootElement().getChild("children");
if (eChildren != null) {
e.addContent(eChildren.detach());
createXMLForSubdirectories(pChild, e, currentDepth + 1, maxDepth);
}
} catch (IOException ex) {
// ignore
}
}
}
}
use of org.mycore.datamodel.niofs.MCRPath 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.niofs.MCRPath in project mycore by MyCoRe-Org.
the class MCRRestAPIObjectsHelper method listDerivateContentAsJson.
private static String listDerivateContentAsJson(MCRDerivate derObj, String path, int depth, UriInfo info) throws IOException {
StringWriter sw = new StringWriter();
MCRPath root = MCRPath.getPath(derObj.getId().toString(), "/");
root = MCRPath.toMCRPath(root.resolve(path));
if (depth == -1) {
depth = Integer.MAX_VALUE;
}
if (root != null) {
JsonWriter writer = new JsonWriter(sw);
Files.walkFileTree(root, EnumSet.noneOf(FileVisitOption.class), depth, new MCRJSONFileVisitor(writer, derObj.getOwnerID(), derObj.getId(), info));
writer.close();
}
return sw.toString();
}
use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.
the class MCRURNGranularRESTRegistrationServiceTest method fullRegister.
@Test
public void fullRegister() throws Exception {
new MockContentTypes();
new MockFrontendUtil();
new MockFiles();
new MockAccessManager();
new MockObjectDerivate();
new MockDerivate();
MockMetadataManager mockMetadataManager = new MockMetadataManager();
MCRDerivate derivate = new MCRDerivate();
MCRObjectID mcrObjectID = MCRPIUtils.getNextFreeID();
derivate.setId(mcrObjectID);
mockMetadataManager.put(mcrObjectID, derivate);
Function<MCRDerivate, Stream<MCRPath>> foo = deriv -> IntStream.iterate(0, i -> i + 1).mapToObj(i -> "/foo/" + UUID.randomUUID() + "_" + String.format(Locale.getDefault(), "%02d", i)).map(f -> MCRPath.getPath(derivate.getId().toString(), f)).limit(numOfDerivFiles);
String serviceID = "TestService";
MCRURNGranularRESTRegistrationService testService = new MCRURNGranularRESTRegistrationService(serviceID, foo);
testService.register(derivate, "", true);
timerTask();
List<MCRPIRegistrationInfo> registeredURNs = MCREntityManagerProvider.getEntityManagerFactory().createEntityManager().createNamedQuery("Get.PI.Created", MCRPIRegistrationInfo.class).setParameter("mcrId", mcrObjectID.toString()).setParameter("type", MCRDNBURN.TYPE).setParameter("service", serviceID).getResultList();
Assert.assertEquals("Wrong number of registered URNs: ", numOfDerivFiles + 1, registeredURNs.size());
}
Aggregations