Search in sources :

Example 51 with MCRSession

use of org.mycore.common.MCRSession in project mycore by MyCoRe-Org.

the class MCRUploadViaFormServlet method handleUploadedFiles.

private void handleUploadedFiles(MCRUploadHandler handler, Collection<Part> files) throws Exception {
    int numFiles = (int) files.stream().map(Part::getSubmittedFileName).filter(Objects::nonNull).count();
    LOGGER.info("UploadHandler uploading {} file(s)", numFiles);
    handler.startUpload(numFiles);
    MCRSession session = MCRSessionMgr.getCurrentSession();
    session.commitTransaction();
    for (Part file : files) {
        try {
            handleUploadedFile(handler, file);
        } finally {
            file.delete();
        }
    }
    session.beginTransaction();
}
Also used : MCRSession(org.mycore.common.MCRSession) Part(javax.servlet.http.Part) Objects(java.util.Objects)

Example 52 with MCRSession

use of org.mycore.common.MCRSession in project mycore by MyCoRe-Org.

the class MCRContainerLoginFormServlet method think.

/* (non-Javadoc)
     * @see org.mycore.frontend.servlets.MCRServlet#think(org.mycore.frontend.servlets.MCRServletJob)
     */
@Override
protected void think(MCRServletJob job) throws Exception {
    MCRSession mcrSession = MCRSessionMgr.getCurrentSession();
    MCRUserInformation userInformation = mcrSession.getUserInformation();
    MCRLogin login = new MCRLogin(userInformation, getFormAction());
    login.getForm().getInput().addAll(Arrays.asList(getUserNameField(), getPasswordField()));
    job.getRequest().setAttribute(LOGIN_ATTR, new MCRJAXBContent<>(MCRLogin.getContext(), login));
}
Also used : MCRLogin(org.mycore.frontend.support.MCRLogin) MCRSession(org.mycore.common.MCRSession) MCRUserInformation(org.mycore.common.MCRUserInformation)

Example 53 with MCRSession

use of org.mycore.common.MCRSession in project mycore by MyCoRe-Org.

the class MCRLockServlet method render.

@Override
protected void render(MCRServletJob job, Exception ex) throws Exception {
    if (job.getResponse().isCommitted()) {
        LOGGER.info("Response allready committed");
        return;
    }
    if (ex != null) {
        throw ex;
    }
    HttpServletRequest req = job.getRequest();
    MCRObjectID objectId = (MCRObjectID) job.getRequest().getAttribute(OBJECT_ID_KEY);
    Action action = (Action) job.getRequest().getAttribute(ACTION_KEY);
    MCRSession lockingSession = MCRObjectIDLockTable.getLocker(objectId);
    if (MCRObjectIDLockTable.isLockedByCurrentSession(objectId) || action == Action.unlock) {
        String url = getProperty(job.getRequest(), PARAM_REDIRECT);
        if (url.startsWith("/")) {
            url = req.getContextPath() + url;
        }
        url = addQueryParameter(url, req);
        job.getResponse().sendRedirect(job.getResponse().encodeRedirectURL(url));
    } else {
        String errorI18N = getErrorI18N("error", "lockedBy", objectId.toString(), lockingSession.getUserInformation().getUserID());
        job.getResponse().sendError(HttpServletResponse.SC_CONFLICT, errorI18N);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MCRSession(org.mycore.common.MCRSession) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID)

Example 54 with MCRSession

use of org.mycore.common.MCRSession in project mycore by MyCoRe-Org.

the class MCRServlet method configureSession.

private void configureSession(MCRServletJob job) {
    MCRSession session = MCRSessionMgr.getCurrentSession();
    String c = getClass().getName();
    c = c.substring(c.lastIndexOf(".") + 1);
    String msg = c + " ip=" + MCRFrontendUtil.getRemoteAddr(job.getRequest()) + " mcr=" + session.getID() + " user=" + session.getUserInformation().getUserID();
    LOGGER.info(msg);
    MCRFrontendUtil.configureSession(session, job.getRequest(), job.getResponse());
}
Also used : MCRSession(org.mycore.common.MCRSession)

Example 55 with MCRSession

use of org.mycore.common.MCRSession in project mycore by MyCoRe-Org.

the class MCRServlet method processThinkPhase.

private Exception processThinkPhase(MCRServletJob job) {
    MCRSession session = MCRSessionMgr.getCurrentSession();
    try {
        if (getProperty(job.getRequest(), INITIAL_SERVLET_NAME_KEY).equals(getServletName())) {
            // current Servlet not called via RequestDispatcher
            session.beginTransaction();
        }
        configureSession(job);
        think(job);
        if (getProperty(job.getRequest(), INITIAL_SERVLET_NAME_KEY).equals(getServletName())) {
            // current Servlet not called via RequestDispatcher
            session.commitTransaction();
        }
    } catch (Exception ex) {
        if (getProperty(job.getRequest(), INITIAL_SERVLET_NAME_KEY).equals(getServletName())) {
            // current Servlet not called via RequestDispatcher
            LOGGER.warn("Exception occurred, performing database rollback.");
            session.rollbackTransaction();
        } else {
            LOGGER.warn("Exception occurred, cannot rollback database transaction right now.");
        }
        return ex;
    }
    return null;
}
Also used : MCRSession(org.mycore.common.MCRSession) TransformerException(javax.xml.transform.TransformerException) ServletException(javax.servlet.ServletException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

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