Search in sources :

Example 16 with LockResult

use of org.olat.core.util.coordinate.LockResult in project openolat by klemens.

the class CourseWebService method getRepoFileById.

/**
 * Export the course
 * @response.representation.200.mediaType application/zip
 * @response.representation.200.doc The course as a ZIP file
 * @response.representation.401.doc Not authorized to export the course
 * @response.representation.404.doc The course not found
 * @return It returns the <code>CourseVO</code> object representing the course.
 */
@GET
@Path("file")
@Produces({ "application/zip", MediaType.APPLICATION_OCTET_STREAM })
public Response getRepoFileById(@Context HttpServletRequest request) {
    RepositoryService rs = CoreSpringFactory.getImpl(RepositoryService.class);
    RepositoryEntry re = course.getCourseEnvironment().getCourseGroupManager().getCourseEntry();
    if (re == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    RepositoryHandler typeToDownload = RepositoryHandlerFactory.getInstance().getRepositoryHandler(re);
    if (typeToDownload == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    Identity identity = getIdentity(request);
    boolean canDownload = re.getCanDownload() && typeToDownload.supportsDownload();
    if (isAdmin(request) || RepositoryManager.getInstance().isOwnerOfRepositoryEntry(identity, re)) {
        canDownload = true;
    } else if (!isAuthor(request)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    if (!canDownload) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    OLATResource ores = OLATResourceManager.getInstance().findResourceable(re.getOlatResource());
    if (ores == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    boolean isAlreadyLocked = typeToDownload.isLocked(ores);
    LockResult lockResult = null;
    try {
        lockResult = typeToDownload.acquireLock(ores, identity);
        if (lockResult == null || (lockResult != null && lockResult.isSuccess() && !isAlreadyLocked)) {
            MediaResource mr = typeToDownload.getAsMediaResource(ores, false);
            if (mr != null) {
                rs.incrementDownloadCounter(re);
                // success
                return Response.ok(mr.getInputStream()).cacheControl(cc).build();
            } else {
                return Response.serverError().status(Status.NO_CONTENT).build();
            }
        } else {
            return Response.serverError().status(Status.CONFLICT).build();
        }
    } finally {
        if ((lockResult != null && lockResult.isSuccess() && !isAlreadyLocked)) {
            typeToDownload.releaseLock(lockResult);
        }
    }
}
Also used : LockResult(org.olat.core.util.coordinate.LockResult) OLATResource(org.olat.resource.OLATResource) MediaResource(org.olat.core.gui.media.MediaResource) RepositoryEntry(org.olat.repository.RepositoryEntry) RepositoryHandler(org.olat.repository.handlers.RepositoryHandler) Identity(org.olat.core.id.Identity) RestSecurityHelper.getIdentity(org.olat.restapi.security.RestSecurityHelper.getIdentity) RepositoryService(org.olat.repository.RepositoryService) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 17 with LockResult

use of org.olat.core.util.coordinate.LockResult in project openolat by klemens.

the class IQConfigurationController method doIQReference.

private void doIQReference(UserRequest urequest, RepositoryEntry re, boolean manualCorrection) {
    // repository search controller done
    if (re != null) {
        if (CoordinatorManager.getInstance().getCoordinator().getLocker().isLocked(re.getOlatResource(), null)) {
            LockResult lockResult = CoordinatorManager.getInstance().getCoordinator().getLocker().acquireLock(re.getOlatResource(), urequest.getIdentity(), null);
            String fullName = CoreSpringFactory.getImpl(UserManager.class).getUserDisplayName(lockResult.getOwner());
            showError("error.entry.locked", fullName);
            if (lockResult.isSuccess()) {
                // improbable concurrency security
                CoordinatorManager.getInstance().getCoordinator().getLocker().releaseLock(lockResult);
            }
        } else {
            if (editTestButton != null) {
                myContent.remove(editTestButton);
            }
            IQEditController.setIQReference(re, moduleConfiguration);
            String displayName = StringHelper.escapeHtml(re.getDisplayname());
            previewLink = LinkFactory.createCustomLink("command.preview.link", "command.preview", displayName, Link.NONTRANSLATED, myContent, this);
            previewLink.setIconLeftCSS("o_icon o_icon-fw o_icon_preview");
            previewLink.setCustomEnabledLinkCSS("o_preview");
            previewLink.setTitle(getTranslator().translate("command.preview"));
            previewButton = LinkFactory.createButtonSmall("command.preview", myContent, this);
            previewButton.setIconLeftCSS("o_icon o_icon-fw o_icon_preview");
            myContent.contextPut("dontRenderRepositoryButton", new Boolean(true));
            // If of type test, get min, max, cut - put in module config and push
            // to velocity
            boolean isOnyx = OnyxModule.isOnyxTest(re.getOlatResource());
            myContent.contextPut("isOnyx", Boolean.valueOf(isOnyx));
            if (isOnyx) {
                myContent.contextPut("onyxDisplayName", displayName);
                moduleConfiguration.set(IQEditController.CONFIG_KEY_TYPE_QTI, IQEditController.CONFIG_VALUE_QTI2);
            } else if (ImsQTI21Resource.TYPE_NAME.equals(re.getOlatResource().getResourceableTypeName())) {
                moduleConfiguration.set(IQEditController.CONFIG_KEY_TYPE_QTI, IQEditController.CONFIG_VALUE_QTI21);
                if (isEditable(urequest.getIdentity(), urequest.getUserSession().getRoles(), re)) {
                    editTestButton = LinkFactory.createButtonSmall("command.editRepFile", myContent, this);
                }
            } else {
                moduleConfiguration.set(IQEditController.CONFIG_KEY_TYPE_QTI, IQEditController.CONFIG_VALUE_QTI1);
                if (isEditable(urequest.getIdentity(), urequest.getUserSession().getRoles(), re)) {
                    editTestButton = LinkFactory.createButtonSmall("command.editRepFile", myContent, this);
                }
            }
            if (manualCorrection) {
                myContent.contextPut(IQEditController.CONFIG_CORRECTION_MODE, "manual");
            } else {
                myContent.contextPut(IQEditController.CONFIG_CORRECTION_MODE, "auto");
            }
            fireEvent(urequest, NodeEditController.NODECONFIG_CHANGED_EVENT);
        }
    }
}
Also used : LockResult(org.olat.core.util.coordinate.LockResult) UserManager(org.olat.user.UserManager)

Example 18 with LockResult

use of org.olat.core.util.coordinate.LockResult in project OpenOLAT by OpenOLAT.

the class CourseWebService method getRepoFileById.

/**
 * Export the course
 * @response.representation.200.mediaType application/zip
 * @response.representation.200.doc The course as a ZIP file
 * @response.representation.401.doc Not authorized to export the course
 * @response.representation.404.doc The course not found
 * @return It returns the <code>CourseVO</code> object representing the course.
 */
@GET
@Path("file")
@Produces({ "application/zip", MediaType.APPLICATION_OCTET_STREAM })
public Response getRepoFileById(@Context HttpServletRequest request) {
    RepositoryService rs = CoreSpringFactory.getImpl(RepositoryService.class);
    RepositoryEntry re = course.getCourseEnvironment().getCourseGroupManager().getCourseEntry();
    if (re == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    RepositoryHandler typeToDownload = RepositoryHandlerFactory.getInstance().getRepositoryHandler(re);
    if (typeToDownload == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    Identity identity = getIdentity(request);
    boolean canDownload = re.getCanDownload() && typeToDownload.supportsDownload();
    if (isAdmin(request) || RepositoryManager.getInstance().isOwnerOfRepositoryEntry(identity, re)) {
        canDownload = true;
    } else if (!isAuthor(request)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    if (!canDownload) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    OLATResource ores = OLATResourceManager.getInstance().findResourceable(re.getOlatResource());
    if (ores == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    boolean isAlreadyLocked = typeToDownload.isLocked(ores);
    LockResult lockResult = null;
    try {
        lockResult = typeToDownload.acquireLock(ores, identity);
        if (lockResult == null || (lockResult != null && lockResult.isSuccess() && !isAlreadyLocked)) {
            MediaResource mr = typeToDownload.getAsMediaResource(ores, false);
            if (mr != null) {
                rs.incrementDownloadCounter(re);
                // success
                return Response.ok(mr.getInputStream()).cacheControl(cc).build();
            } else {
                return Response.serverError().status(Status.NO_CONTENT).build();
            }
        } else {
            return Response.serverError().status(Status.CONFLICT).build();
        }
    } finally {
        if ((lockResult != null && lockResult.isSuccess() && !isAlreadyLocked)) {
            typeToDownload.releaseLock(lockResult);
        }
    }
}
Also used : LockResult(org.olat.core.util.coordinate.LockResult) OLATResource(org.olat.resource.OLATResource) MediaResource(org.olat.core.gui.media.MediaResource) RepositoryEntry(org.olat.repository.RepositoryEntry) RepositoryHandler(org.olat.repository.handlers.RepositoryHandler) Identity(org.olat.core.id.Identity) RestSecurityHelper.getIdentity(org.olat.restapi.security.RestSecurityHelper.getIdentity) RepositoryService(org.olat.repository.RepositoryService) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 19 with LockResult

use of org.olat.core.util.coordinate.LockResult in project OpenOLAT by OpenOLAT.

the class CatalogWebService method deleteCatalogEntry.

/**
 * Deletes the catalog entry with the path specified in the URL.
 * @response.representation.200.qname {http://www.example.com}catalogEntryVO
 * @response.representation.200.mediaType application/xml, application/json
 * @response.representation.200.doc The catalog entry
 * @response.representation.200.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_CATALOGENTRYVO}
 * @response.representation.401.doc Not authorized
 * @response.representation.404.doc The path could not be resolved to a valid catalog entry
 * @param path The path
 * @param httpRquest The HTTP request
 * @return The response
 */
@DELETE
@Path("{path:.*}")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response deleteCatalogEntry(@PathParam("path") List<PathSegment> path, @Context HttpServletRequest httpRequest) {
    Long key = getCatalogEntryKeyFromPath(path);
    if (key == null) {
        return Response.serverError().status(Status.NOT_ACCEPTABLE).build();
    }
    CatalogEntry ce = catalogManager.loadCatalogEntry(key);
    if (ce == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    if (!canAdminSubTree(ce, httpRequest)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    Identity id = getUserRequest(httpRequest).getIdentity();
    LockResult lock = CoordinatorManager.getInstance().getCoordinator().getLocker().acquireLock(lockRes, id, LOCK_TOKEN);
    if (!lock.isSuccess()) {
        return getLockedResponse(lock, httpRequest);
    }
    try {
        catalogManager.deleteCatalogEntry(ce);
    } catch (Exception e) {
        throw new WebApplicationException(e);
    } finally {
        CoordinatorManager.getInstance().getCoordinator().getLocker().releaseLock(lock);
    }
    return Response.ok().build();
}
Also used : LockResult(org.olat.core.util.coordinate.LockResult) WebApplicationException(javax.ws.rs.WebApplicationException) CatalogEntry(org.olat.repository.CatalogEntry) Identity(org.olat.core.id.Identity) WebApplicationException(javax.ws.rs.WebApplicationException) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Produces(javax.ws.rs.Produces)

Example 20 with LockResult

use of org.olat.core.util.coordinate.LockResult in project OpenOLAT by OpenOLAT.

the class FeedManagerImpl method acquireLock.

@Override
public LockResult acquireLock(OLATResourceable feed, Item item, Identity identity) {
    String key = itemKey(item, feed);
    if (key.length() >= OresHelper.ORES_TYPE_LENGTH) {
        key = Encoder.md5hash(key);
    }
    OLATResourceable itemResource = OresHelper.createOLATResourceableType(key);
    LockResult lockResult = coordinator.getLocker().acquireLock(itemResource, identity, key);
    return lockResult;
}
Also used : LockResult(org.olat.core.util.coordinate.LockResult) OLATResourceable(org.olat.core.id.OLATResourceable)

Aggregations

LockResult (org.olat.core.util.coordinate.LockResult)28 Identity (org.olat.core.id.Identity)16 Path (javax.ws.rs.Path)14 Produces (javax.ws.rs.Produces)10 WebApplicationException (javax.ws.rs.WebApplicationException)10 CatalogEntry (org.olat.repository.CatalogEntry)10 RepositoryEntry (org.olat.repository.RepositoryEntry)10 OLATResourceable (org.olat.core.id.OLATResourceable)6 RepositoryHandler (org.olat.repository.handlers.RepositoryHandler)6 OLATResource (org.olat.resource.OLATResource)6 Consumes (javax.ws.rs.Consumes)4 DELETE (javax.ws.rs.DELETE)4 GET (javax.ws.rs.GET)4 PUT (javax.ws.rs.PUT)4 BaseSecurity (org.olat.basesecurity.BaseSecurity)4 SecurityGroup (org.olat.basesecurity.SecurityGroup)4 MediaResource (org.olat.core.gui.media.MediaResource)4 LockEntry (org.olat.core.util.coordinate.LockEntry)4 RepositoryService (org.olat.repository.RepositoryService)4 RestSecurityHelper.getIdentity (org.olat.restapi.security.RestSecurityHelper.getIdentity)4