use of org.mycore.datamodel.niofs.MCRPath 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);
}
use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.
the class MCRIView2Commands method forAllImages.
private static List<String> forAllImages(String derivateID, String batchCommandSyntax) throws IOException {
if (!MCRIView2Tools.isDerivateSupported(derivateID)) {
LOGGER.info("Skipping tiling of derivate {} as it's main file is not supported by IView2.", derivateID);
return null;
}
MCRPath derivateRoot = MCRPath.getPath(derivateID, "/");
if (!Files.exists(derivateRoot)) {
throw new MCRException("Derivate " + derivateID + " does not exist or is not a directory!");
}
List<MCRPath> supportedFiles = getSupportedFiles(derivateRoot);
return supportedFiles.stream().map(image -> MessageFormat.format(batchCommandSyntax, derivateID, image.getOwnerRelativePath())).collect(Collectors.toList());
}
use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.
the class MCRIView2Commands method fixDeadEntries.
@MCRCommand(syntax = "fix dead tile jobs", help = "Deletes entries for files which dont exist anymore!")
public static void fixDeadEntries() {
EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
TypedQuery<MCRTileJob> allTileJobQuery = em.createNamedQuery("MCRTileJob.all", MCRTileJob.class);
List<MCRTileJob> tiles = allTileJobQuery.getResultList();
tiles.stream().filter(tj -> {
MCRPath path = MCRPath.getPath(tj.getDerivate(), tj.getPath());
return !Files.exists(path);
}).peek(tj -> LOGGER.info("Delete TileJob {}:{}", tj.getDerivate(), tj.getPath())).forEach(em::remove);
}
use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.
the class MCRPDFThumbnailServlet method getContent.
/* (non-Javadoc)
* @see org.mycore.frontend.servlets.MCRContentServlet#getContent(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
@Override
public MCRContent getContent(HttpServletRequest req, HttpServletResponse resp) throws IOException {
try {
ThumnailInfo thumbnailInfo = getThumbnailInfo(req.getPathInfo());
MCRPath pdfFile = MCRPath.getPath(thumbnailInfo.derivate, thumbnailInfo.filePath);
LOGGER.info("PDF file: {}", pdfFile);
if (Files.notExists(pdfFile)) {
resp.sendError(HttpServletResponse.SC_NOT_FOUND, MessageFormat.format("Could not find pdf file for {0}{1}", thumbnailInfo.derivate, thumbnailInfo.filePath));
return null;
}
String centerThumb = req.getParameter("centerThumb");
String imgSize = req.getParameter("ts");
// defaults to "yes"
boolean centered = !"no".equals(centerThumb);
int thumbnailSize = imgSize == null ? this.thumbnailSize : Integer.parseInt(imgSize);
BasicFileAttributes attrs = Files.readAttributes(pdfFile, BasicFileAttributes.class);
MCRContent imageContent = pdfTools.getThumnail(pdfFile, attrs, thumbnailSize, centered);
if (imageContent != null) {
resp.setHeader("Cache-Control", "max-age=" + MAX_AGE);
Date expires = new Date(System.currentTimeMillis() + MAX_AGE * 1000);
LOGGER.debug("Last-Modified: {}, expire on: {}", new Date(attrs.lastModifiedTime().toMillis()), expires);
resp.setDateHeader("Expires", expires.getTime());
}
return imageContent;
} finally {
LOGGER.debug("Finished sending {}", req.getPathInfo());
}
}
use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.
the class MCRThumbnailResource method getThumbnail.
private Response getThumbnail(String documentId, int size, String ext) {
List<MCRObjectID> derivateIds = MCRMetadataManager.getDerivateIds(MCRJerseyUtil.getID(documentId), 1, TimeUnit.MINUTES);
for (MCRObjectID derivateId : derivateIds) {
if (MCRAccessManager.checkPermissionForReadingDerivate(derivateId.toString())) {
String nameOfMainFile = MCRMetadataManager.retrieveMCRDerivate(derivateId).getDerivate().getInternals().getMainDoc();
if (nameOfMainFile != null && !nameOfMainFile.equals("")) {
MCRPath mainFile = MCRPath.getPath(derivateId.toString(), '/' + nameOfMainFile);
try {
String mimeType = Files.probeContentType(mainFile);
String generators = MCRConfiguration.instance().getString("MCR.Media.Thumbnail.Generators");
for (String generator : generators.split(",")) {
Class<MCRThumbnailGenerator> classObject = (Class<MCRThumbnailGenerator>) Class.forName(generator);
Constructor<MCRThumbnailGenerator> constructor = classObject.getConstructor();
MCRThumbnailGenerator thumbnailGenerator = constructor.newInstance();
if (thumbnailGenerator.matchesFileType(mimeType, mainFile)) {
FileTime lastModified = Files.getLastModifiedTime(mainFile);
Date lastModifiedDate = new Date(lastModified.toMillis());
Response.ResponseBuilder resp = request.evaluatePreconditions(lastModifiedDate);
if (resp == null) {
Optional<BufferedImage> thumbnail = thumbnailGenerator.getThumbnail(mainFile, size);
if (thumbnail.isPresent()) {
CacheControl cc = new CacheControl();
cc.setMaxAge((int) TimeUnit.DAYS.toSeconds(1));
String type = "image/png";
if ("jpg".equals(ext) || "jpeg".equals(ext)) {
type = "image/jpeg";
}
return Response.ok(thumbnail.get()).cacheControl(cc).lastModified(lastModifiedDate).type(type).build();
}
return Response.status(Response.Status.NOT_FOUND).build();
}
return resp.build();
}
}
} catch (IOException | ReflectiveOperationException e) {
throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
}
}
}
}
return Response.status(Response.Status.NOT_FOUND).build();
}
Aggregations