Search in sources :

Example 26 with LockResult

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

the class RepositoryEntryResource method getRepoFileById.

/**
 * Download the export zip file of a repository entry.
 * @response.representation.mediaType multipart/form-data
 * @response.representation.doc Download the resource file
 * @response.representation.200.mediaType application/zip
 * @response.representation.200.doc Download the repository entry as export zip file
 * @response.representation.200.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_REPOENTRYVO}
 * @response.representation.401.doc The roles of the authenticated user are not sufficient
 * @response.representation.404.doc The resource could not found
 * @response.representation.406.doc Download of this resource is not possible
 * @response.representation.409.doc The resource is locked
 * @param repoEntryKey
 * @param request The HTTP request
 * @return
 */
@GET
@Path("file")
@Produces({ "application/zip", MediaType.APPLICATION_OCTET_STREAM })
public Response getRepoFileById(@PathParam("repoEntryKey") String repoEntryKey, @Context HttpServletRequest request) {
    RepositoryEntry re = lookupRepositoryEntry(repoEntryKey);
    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();
    OLATResource ores = OLATResourceManager.getInstance().findResourceable(re.getOlatResource());
    if (ores == null)
        return Response.serverError().status(Status.NOT_FOUND).build();
    Identity identity = getIdentity(request);
    boolean isAuthor = RestSecurityHelper.isAuthor(request);
    boolean isOwner = repositoryManager.isOwnerOfRepositoryEntry(identity, re);
    if (!(isAuthor | isOwner))
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    boolean canDownload = re.getCanDownload() && typeToDownload.supportsDownload();
    if (!canDownload)
        return Response.serverError().status(Status.NOT_ACCEPTABLE).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) {
                repositoryService.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) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 27 with LockResult

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

the class CatalogWebService method addCatalogEntry.

/**
 * Adds a catalog entry under the path specified in the URL.
 * @response.representation.qname {http://www.example.com}catalogEntryVO
 * @response.representation.mediaType application/xml, application/json
 * @response.representation.doc The catalog entry
 * @response.representation.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_CATALOGENTRYVO}
 * @response.representation.200.qname {http://www.example.com}catalogEntryVO
 * @response.representation.200.mediaType application/xml, application/json
 * @response.representation.200.doc The list of 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 entryVo The catalog entry
 * @param httpRquest The HTTP request
 * @param uriInfo The URI informations
 * @return The response
 */
@PUT
@Path("{path:.*}")
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response addCatalogEntry(@PathParam("path") List<PathSegment> path, CatalogEntryVO entryVo, @Context HttpServletRequest httpRequest, @Context UriInfo uriInfo) {
    if (!isAuthor(httpRequest)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    Long parentKey = getCatalogEntryKeyFromPath(path);
    if (parentKey == null) {
        return Response.serverError().status(Status.NOT_ACCEPTABLE).build();
    }
    CatalogEntry parent = catalogManager.loadCatalogEntry(parentKey);
    if (parent == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    int type = guessType(entryVo);
    if (type == CatalogEntry.TYPE_NODE && !canAdminSubTree(parent, httpRequest)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    RepositoryEntry re = null;
    if (entryVo.getRepositoryEntryKey() != null) {
        re = RepositoryManager.getInstance().lookupRepositoryEntry(entryVo.getRepositoryEntryKey());
        if (re == null) {
            return Response.serverError().status(Status.NOT_FOUND).build();
        }
    }
    Identity id = getUserRequest(httpRequest).getIdentity();
    LockResult lock = CoordinatorManager.getInstance().getCoordinator().getLocker().acquireLock(lockRes, id, LOCK_TOKEN);
    if (!lock.isSuccess()) {
        return getLockedResponse(lock, httpRequest);
    }
    CatalogEntry ce = null;
    try {
        ce = catalogManager.createCatalogEntry();
        ce.setType(guessType(entryVo));
        ce.setName(entryVo.getName());
        ce.setDescription(entryVo.getDescription());
        ce.setOwnerGroup(BaseSecurityManager.getInstance().createAndPersistSecurityGroup());
        if (re != null) {
            ce.setRepositoryEntry(re);
        }
        catalogManager.addCatalogEntry(parent, ce);
    } catch (Exception e) {
        throw new WebApplicationException(e);
    } finally {
        CoordinatorManager.getInstance().getCoordinator().getLocker().releaseLock(lock);
    }
    CatalogEntryVO newEntryVo = link(get(ce), uriInfo);
    return Response.ok(newEntryVo).build();
}
Also used : LockResult(org.olat.core.util.coordinate.LockResult) WebApplicationException(javax.ws.rs.WebApplicationException) CatalogEntryVO(org.olat.restapi.support.vo.CatalogEntryVO) CatalogEntry(org.olat.repository.CatalogEntry) RepositoryEntry(org.olat.repository.RepositoryEntry) Identity(org.olat.core.id.Identity) WebApplicationException(javax.ws.rs.WebApplicationException) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT)

Example 28 with LockResult

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

the class ClusterLocker method acquireLock.

@Override
public LockResult acquireLock(final OLATResourceable ores, final Identity requestor, final String locksubkey) {
    final String asset = OresHelper.createStringRepresenting(ores, locksubkey);
    LockResult res = syncer.doInSync(ores, new SyncerCallback<LockResult>() {

        @Override
        public LockResult execute() {
            LockResultImpl lres;
            LockImpl li = clusterLockManager.findLock(asset);
            if (li == null) {
                // fine, we can lock it
                li = clusterLockManager.createLockImpl(asset, requestor);
                clusterLockManager.saveLock(li);
                LockEntry le = new LockEntry(li.getAsset(), li.getCreationDate().getTime(), li.getOwner());
                lres = new LockResultImpl(true, le);
            } else {
                // already locked by a user.
                // if that user is us, we can reacquire it
                LockEntry le = new LockEntry(li.getAsset(), li.getCreationDate().getTime(), li.getOwner());
                if (requestor.getName().equals(li.getOwner().getName())) {
                    // that's us -> success (asset, owner is the same, and we leave creationdate to when the lock was originally acquired, not when it was reacquired.
                    lres = new LockResultImpl(true, le);
                } else {
                    lres = new LockResultImpl(false, le);
                }
            }
            return lres;
        }
    });
    return res;
}
Also used : LockResultImpl(org.olat.core.util.coordinate.LockResultImpl) LockResult(org.olat.core.util.coordinate.LockResult) LockEntry(org.olat.core.util.coordinate.LockEntry)

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