@Override public void close() { if (isValid()) { abort(); } }
private void closeContext(Context context) { if (context != null && context.isValid()) { context.abort(); } }
/** * Abort all of the contexts held by this class. No changes will * be written to the database */ public void abort() { // abort both contexts if (context != null && context.isValid()) { context.abort(); } if (authenticatorContext != null && authenticatorContext.isValid()) { authenticatorContext.abort(); } }
public static void abortContext(HttpServletRequest request) { Context context = (Context) request.getAttribute(DSPACE_CONTEXT); if (context != null && context.isValid()) { context.abort(); } }
/** * Abort all of the contexts held by this class. No changes will * be written to the database */ public void abort() { // abort both contexts if (context != null && context.isValid()) { context.abort(); } if (authenticatorContext != null && authenticatorContext.isValid()) { authenticatorContext.abort(); } }
/** * Process finally statement. It will print message to logger error stream * and abort DSpace context, if was not properly ended. * * @param context Context which must be aborted. * @throws WebApplicationException This exception is thrown for user of REST API. */ protected void processFinally(org.dspace.core.Context context) throws WebApplicationException { if ((context != null) && (context.isValid())) { context.abort(); log.error("Something get wrong. Aborting context in finally statement."); throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR); } }
/** * Process exception, print message to logger error stream and abort DSpace * context. * * @param message Message, which will be printed to error stream. * @param context Context which must be aborted. * @throws WebApplicationException This exception is throw for user of REST api. */ protected static void processException(String message, org.dspace.core.Context context) throws WebApplicationException { if ((context != null) && (context.isValid())) { context.abort(); } log.error(message); throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR); }
/** * Abort all of the contexts held by this class. No changes will * be written to the database */ public void abort() { // abort both contexts if (context != null && context.isValid()) { context.abort(); } if (authenticatorContext != null && authenticatorContext.isValid()) { authenticatorContext.abort(); } }
/** * Returns the context object used in the current curation thread. * This is primarily a utility method to allow tasks access to the context when necessary. * <p> * If the context is null or not set, then this just returns * a brand new Context object representing an Anonymous User. * * @return curation thread's Context object (or a new, anonymous Context if no curation Context exists) * @throws SQLException An exception that provides information on a database access error or other errors. */ public static Context curationContext() throws SQLException { // Return curation context or new context if undefined/invalid Context curCtx = curationCtx.get(); if (curCtx == null || !curCtx.isValid()) { //Create a new context (represents an Anonymous User) curCtx = new Context(); //Save it to current execution thread curationCtx.set(curCtx); } return curCtx; }
/** * Check if a context exists for this request, if so complete the context. * * @param request * The request object */ public static void completeContext(HttpServletRequest request) throws ServletException { Context context = (Context) request.getAttribute(DSPACE_CONTEXT); if (context != null && context.isValid()) { try { context.complete(); } catch (SQLException e) { throw new ServletException(e); } } }
/** * Commit the primary context held by this class, and abort the authenticated * user's context if it is different. This ensures that only changes written * through the appropriate user's context is persisted, and all other * operations are flushed. You should, in general, not try to commit the contexts directly * when using the sword api. * * @throws DSpaceSwordException can be thrown by the internals of the DSpace SWORD implementation */ public void commit() throws DSpaceSwordException { try { // commit the primary context if (context != null && context.isValid()) { context.complete(); } // the secondary context is for filtering permissions by only, and is // never committed, so we abort here if (authenticatorContext != null && authenticatorContext.isValid()) { authenticatorContext.abort(); } } catch (SQLException e) { throw new DSpaceSwordException(e); } } }
/** * Commit the primary context held by this class, and abort the authenticated * user's context if it is different. This ensures that only changes written * through the appropriate user's context is persisted, and all other * operations are flushed. You should, in general, not try to commit the contexts directly * when using the SWORD API. * * @throws DSpaceSWORDException on database error. */ public void commit() throws DSpaceSWORDException { try { // commit the primary context if (context != null && context.isValid()) { context.complete(); } // the secondary context is for filtering permissions by only, and is // never committed, so we abort here if (authenticatorContext != null && authenticatorContext.isValid()) { authenticatorContext.abort(); } } catch (SQLException e) { throw new DSpaceSWORDException(e); } } }
/** * Authenticate the given service document request. This extracts the appropriate * information from the request and forwards to the appropriate authentication * method * * @param auth authentication credentials * @return SWORD context * @throws DSpaceSwordException can be thrown by the internals of the DSpace SWORD implementation * @throws SwordError SWORD error per SWORD spec * @throws SwordAuthException thrown if unable to authenticate */ public SwordContext authenticate(AuthCredentials auth) throws DSpaceSwordException, SwordError, SwordAuthException { Context context = this.constructContext(); SwordContext sc; try { sc = this.authenticate(context, auth); } catch (DSpaceSwordException | SwordError | RuntimeException | SwordAuthException e) { if (context != null && context.isValid()) { context.abort(); } throw e; } return sc; }
public void initGroups() { // After every migrate, ensure default Groups are setup correctly. Context context = null; try { context = new Context(); context.turnOffAuthorisationSystem(); // While it's not really a formal "registry", we need to ensure the // default, required Groups exist in the DSpace database groupService.initDefaultGroupNames(context); context.restoreAuthSystemState(); // Commit changes and close context context.complete(); } catch (Exception e) { log.error("Error attempting to add/update default DSpace Groups", e); throw new RuntimeException(e); } finally { // Clean up our context, if it still exists & it was never completed if (context != null && context.isValid()) { context.abort(); } } }
if (!isValid()) { log.info("complete() was called on a closed Context object. No changes to commit.");
/** * Authenticate the deposit request. * * @param deposit deposit request * @return a SWORDContext object containing the relevant users * @throws SWORDException passed through. * @throws SWORDErrorException on generic SWORD exception * @throws SWORDAuthenticationException Thrown if the authentication fails */ public SWORDContext authenticate(Deposit deposit) throws SWORDException, SWORDErrorException, SWORDAuthenticationException { Context context = this.constructContext(deposit.getIPAddress()); SWORDContext sc = null; try { sc = this.authenticate(context, deposit); } catch (SWORDException | SWORDErrorException | RuntimeException | SWORDAuthenticationException e) { if (context != null && context.isValid()) { context.abort(); } throw e; } return sc; }
/** * Authenticate the given service document request. This extracts * the appropriate information from the request and forwards to the * appropriate authentication method. * * @param request service document request * @return authenticated SWORD context * @throws SWORDException passed through. * @throws SWORDErrorException on generic SWORD exception * @throws SWORDAuthenticationException Thrown if the authentication fails */ public SWORDContext authenticate(ServiceDocumentRequest request) throws SWORDException, SWORDErrorException, SWORDAuthenticationException { Context context = this.constructContext(request.getIPAddress()); SWORDContext sc; try { sc = this.authenticate(context, request); } catch (SWORDException | SWORDErrorException | RuntimeException | SWORDAuthenticationException e) { if (context != null && context.isValid()) { context.abort(); } throw e; } return sc; }
/** * Authenticate the given ATOM document request. This extracts the * appropriate information from the request, and forwards to the * appropriate authentication method. * * @param request ATOM document request * @return a SWORDContext object containing the relevant users * @throws SWORDException passed through. * @throws SWORDErrorException on generic SWORD exception * @throws SWORDAuthenticationException Thrown if the authentication fails */ public SWORDContext authenticate(AtomDocumentRequest request) throws SWORDException, SWORDErrorException, SWORDAuthenticationException { Context context = this.constructContext(request.getIPAddress()); SWORDContext sc = null; try { sc = this.authenticate(context, request); } catch (SWORDException | SWORDErrorException | SWORDAuthenticationException | RuntimeException e) { if (context != null && context.isValid()) { context.abort(); } throw e; } return sc; }
public void initializeSiteObject() { // After every migrate, ensure default Site is setup correctly. Context context = null; try { context = new Context(); context.turnOffAuthorisationSystem(); // While it's not really a formal "registry", we need to ensure the // default, required Groups exist in the DSpace database if (siteService.findSite(context) == null) { siteService.createSite(context); } context.restoreAuthSystemState(); // Commit changes and close context context.complete(); } catch (Exception e) { log.error("Error attempting to add/update default DSpace Groups", e); } finally { // Clean up our context, if it still exists & it was never completed if (context != null && context.isValid()) { context.abort(); } } }