use of org.mycore.common.MCRSession in project mycore by MyCoRe-Org.
the class MCRTilingAction method run.
/**
* takes a {@link MCRTileJob} and tiles the referenced {@link MCRImage} instance.
*
* Also this updates tileJob properties of {@link MCRTileJob} in the database.
*/
public void run() {
tileJob.setStart(new Date());
MCRImage image;
Path tileDir = MCRIView2Tools.getTileDir();
try {
image = getMCRImage();
image.setTileDir(tileDir);
} catch (IOException e) {
LOGGER.error("Error while retrieving image for job: {}", tileJob, e);
return;
}
MCRSession mcrSession = MCRSessionMgr.getCurrentSession();
mcrSession.setUserInformation(MCRSystemUserInformation.getSystemUserInstance());
Transaction transaction = null;
try (Session session = MCRHIBConnection.instance().getSession()) {
MCRTileEventHandler tileEventHandler = new MCRTileEventHandler() {
Transaction transaction;
@Override
public void preImageReaderCreated() {
transaction = session.beginTransaction();
}
@Override
public void postImageReaderCreated() {
// beside tileJob, no write access so far
session.clear();
if (transaction.getStatus().isOneOf(TransactionStatus.ACTIVE)) {
transaction.commit();
}
}
};
try {
MCRTiledPictureProps picProps = image.tile(tileEventHandler);
tileJob.setFinished(new Date());
tileJob.setStatus(MCRJobState.FINISHED);
tileJob.setHeight(picProps.getHeight());
tileJob.setWidth(picProps.getWidth());
tileJob.setTiles(picProps.getTilesCount());
tileJob.setZoomLevel(picProps.getZoomlevel());
} catch (IOException e) {
LOGGER.error("IOException occured while tiling a queued picture", e);
throw e;
}
transaction = session.beginTransaction();
session.update(tileJob);
transaction.commit();
} catch (Exception e) {
LOGGER.error("Error while getting next tiling job.", e);
if (transaction != null && transaction.getStatus().isOneOf(TransactionStatus.ACTIVE)) {
transaction.rollback();
}
try {
Files.deleteIfExists(MCRImage.getTiledFile(tileDir, tileJob.getDerivate(), tileJob.getPath()));
} catch (IOException e1) {
LOGGER.error("Could not delete tile file after error!", e);
}
} finally {
MCRSessionMgr.releaseCurrentSession();
mcrSession.close();
}
}
use of org.mycore.common.MCRSession 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.common.MCRSession in project mycore by MyCoRe-Org.
the class MCRRestAPIUploadHelper method deleteDerivate.
/**
* deletes a whole derivate
* @param info - the Jersey UriInfo object
* @param request - the HTTPServletRequest object
* @param pathParamMcrObjID - the MyCoRe Object ID
* @param pathParamMcrDerID - the MyCoRe Derivate ID
* @return a Jersey Response Object
* @throws MCRRestAPIException
*/
public static Response deleteDerivate(UriInfo info, HttpServletRequest request, String pathParamMcrObjID, String pathParamMcrDerID) throws MCRRestAPIException {
Response response = Response.status(Status.INTERNAL_SERVER_ERROR).build();
SignedJWT signedJWT = MCRJSONWebTokenUtil.retrieveAuthenticationToken(request);
String base64Signature = request.getHeader("X-MyCoRe-RestAPI-Signature");
if (base64Signature == null) {
// ToDo error handling
}
try (MCRJPATransactionWrapper mtw = new MCRJPATransactionWrapper()) {
// MCRSession session = MCRServlet.getSession(request);
MCRSession session = MCRSessionMgr.getCurrentSession();
MCRUserInformation currentUser = session.getUserInformation();
session.setUserInformation(MCRUserManager.getUser(MCRJSONWebTokenUtil.retrieveUsernameFromAuthenticationToken(signedJWT)));
MCRObjectID objID = MCRObjectID.getInstance(pathParamMcrObjID);
MCRObjectID derID = MCRObjectID.getInstance(pathParamMcrDerID);
// MCRAccessManager.checkPermission() uses CACHE, which seems to be dirty from other calls????
MCRAccessManager.invalidPermissionCache(derID.toString(), PERMISSION_DELETE);
if (MCRAccessManager.checkPermission(derID.toString(), PERMISSION_DELETE)) {
try {
MCRMetadataManager.deleteMCRDerivate(derID);
} catch (MCRPersistenceException pe) {
// dir does not exist - do nothing
} catch (MCRAccessException e) {
LOGGER.error(e);
}
}
session.setUserInformation(currentUser);
response = Response.created(info.getBaseUriBuilder().path("v1/objects/" + objID + "/derivates").build()).type("application/xml; charset=UTF-8").header(HEADER_NAME_AUTHORIZATION, "Bearer " + MCRJSONWebTokenUtil.createJWTAuthorizationHeader(signedJWT)).build();
}
return response;
}
Aggregations