Search in sources :

Example 61 with MCRSession

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();
    }
}
Also used : MCRPath(org.mycore.datamodel.niofs.MCRPath) Path(java.nio.file.Path) MCRSession(org.mycore.common.MCRSession) Transaction(org.hibernate.Transaction) MCRTileEventHandler(org.mycore.imagetiler.MCRTileEventHandler) IOException(java.io.IOException) MCRTiledPictureProps(org.mycore.imagetiler.MCRTiledPictureProps) Date(java.util.Date) MCRImage(org.mycore.imagetiler.MCRImage) IOException(java.io.IOException) Session(org.hibernate.Session) MCRSession(org.mycore.common.MCRSession)

Example 62 with MCRSession

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();
    }
}
Also used : MCRSession(org.mycore.common.MCRSession) IOException(java.io.IOException) MCRPath(org.mycore.datamodel.niofs.MCRPath)

Example 63 with MCRSession

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;
}
Also used : Response(javax.ws.rs.core.Response) MCRSession(org.mycore.common.MCRSession) MCRAccessException(org.mycore.access.MCRAccessException) SignedJWT(com.nimbusds.jwt.SignedJWT) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRUserInformation(org.mycore.common.MCRUserInformation) MCRPersistenceException(org.mycore.common.MCRPersistenceException)

Aggregations

MCRSession (org.mycore.common.MCRSession)63 IOException (java.io.IOException)15 MCRUserInformation (org.mycore.common.MCRUserInformation)10 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)10 MCRPath (org.mycore.datamodel.niofs.MCRPath)6 SignedJWT (com.nimbusds.jwt.SignedJWT)5 Date (java.util.Date)5 EntityManager (javax.persistence.EntityManager)5 Path (java.nio.file.Path)4 Response (javax.ws.rs.core.Response)4 Test (org.junit.Test)4 MCRRestAPIException (org.mycore.restapi.v1.errors.MCRRestAPIException)4 SAXException (org.xml.sax.SAXException)4 UnknownHostException (java.net.UnknownHostException)3 Document (org.jdom2.Document)3 MCRAccessException (org.mycore.access.MCRAccessException)3 MCRException (org.mycore.common.MCRException)3 MCRPersistenceException (org.mycore.common.MCRPersistenceException)3 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)3 MCRRestAPIError (org.mycore.restapi.v1.errors.MCRRestAPIError)3