use of org.olat.core.util.coordinate.LockResult in project OpenOLAT by OpenOLAT.
the class CatalogWebService method addOwner.
/**
* Add an owner of the local sub tree
* @response.representation.200.qname {http://www.example.com}userVO
* @response.representation.200.mediaType application/xml, application/json
* @response.representation.200.doc The catalog entry
* @response.representation.200.example {@link org.olat.user.restapi.Examples#SAMPLE_USERVOes}
* @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 identityKey The id of the user
* @param httpRquest The HTTP request
* @return The response
*/
@PUT
@Path("{path:.*}/owners/{identityKey}")
public Response addOwner(@PathParam("path") List<PathSegment> path, @PathParam("identityKey") Long identityKey, @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 (!isAuthor(httpRequest) && !canAdminSubTree(ce, httpRequest)) {
return Response.serverError().status(Status.UNAUTHORIZED).build();
}
BaseSecurity securityManager = BaseSecurityManager.getInstance();
Identity identity = securityManager.loadIdentityByKey(identityKey, false);
if (identity == 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);
}
try {
SecurityGroup sg = ce.getOwnerGroup();
if (sg == null) {
ce.setOwnerGroup(securityManager.createAndPersistSecurityGroup());
DBFactory.getInstance().intermediateCommit();
}
securityManager.addIdentityToSecurityGroup(identity, ce.getOwnerGroup());
} catch (Exception e) {
throw new WebApplicationException(e);
} finally {
CoordinatorManager.getInstance().getCoordinator().getLocker().releaseLock(lock);
}
return Response.ok().build();
}
use of org.olat.core.util.coordinate.LockResult in project OpenOLAT by OpenOLAT.
the class CatalogWebService method removeOwner.
/**
* Remove an owner of the local sub tree
* @response.representation.200.qname {http://www.example.com}userVO
* @response.representation.200.mediaType application/xml, application/json
* @response.representation.200.doc The catalog entry
* @response.representation.200.example {@link org.olat.user.restapi.Examples#SAMPLE_USERVOes}
* @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 identityKey The id of the user
* @param httpRquest The HTTP request
* @return The response
*/
@DELETE
@Path("{path:.*}/owners/{identityKey}")
public Response removeOwner(@PathParam("path") List<PathSegment> path, @PathParam("identityKey") Long identityKey, @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 (!isAuthor(httpRequest) && !canAdminSubTree(ce, httpRequest)) {
return Response.serverError().status(Status.UNAUTHORIZED).build();
}
BaseSecurity securityManager = BaseSecurityManager.getInstance();
Identity identity = securityManager.loadIdentityByKey(identityKey, false);
if (identity == null) {
return Response.ok().build();
}
SecurityGroup sg = ce.getOwnerGroup();
if (sg == null) {
return Response.ok().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 {
securityManager.removeIdentityFromSecurityGroup(identity, ce.getOwnerGroup());
} catch (Exception e) {
throw new WebApplicationException(e);
} finally {
CoordinatorManager.getInstance().getCoordinator().getLocker().releaseLock(lock);
}
return Response.ok().build();
}
use of org.olat.core.util.coordinate.LockResult in project OpenOLAT by OpenOLAT.
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);
}
}
use of org.olat.core.util.coordinate.LockResult in project OpenOLAT by OpenOLAT.
the class LockTest method testCreateDeleteAcquire.
@Test
public void testCreateDeleteAcquire() {
// some setup
List<Identity> identities = new ArrayList<Identity>();
for (int i = 0; i < MAX_COUNT + MAX_USERS_MORE; i++) {
Identity i1 = JunitTestHelper.createAndPersistIdentityAsRndUser("lock-");
identities.add(i1);
}
dbInstance.closeSession();
Identity ident = identities.get(0);
Identity ident2 = identities.get(1);
OLATResourceable ores = OresHelper.createOLATResourceableInstanceWithoutCheck(LockTest.class.getName(), new Long(123456789));
// ------------------ test the clusterlockmanager ----------------------
// create a lock
String asset = OresHelper.createStringRepresenting(ores, "locktest");
LockImpl li = clusterLockManager.createLockImpl(asset, ident);
clusterLockManager.saveLock(li);
dbInstance.closeSession();
// find it
LockImpl l2 = clusterLockManager.findLock(asset);
assertNotNull(l2);
assertEquals(li.getKey(), l2.getKey());
// delete it
int deletedLock = clusterLockManager.deleteLock(asset, ident);
dbInstance.closeSession();
Assert.assertEquals(1, deletedLock);
// may not find it again
LockImpl l3 = clusterLockManager.findLock(asset);
assertNull(l3);
// ------------------ test the clusterlocker ----------------------
// access the cluster locker explicitely
Locker cl = clusterCoordinator.getLocker();
// acquire
LockResult res1 = cl.acquireLock(ores, ident, "abc");
assertTrue(res1.isSuccess());
dbInstance.closeSession();
// reacquire same identity (get from db)
LockResult res11 = cl.acquireLock(ores, ident, "abc");
long lock1Ac = res11.getLockAquiredTime();
assertTrue(res11.isSuccess());
assertTrue(lock1Ac > 0);
dbInstance.closeSession();
// acquire by another identity must fail
LockResult res2 = cl.acquireLock(ores, ident2, "abc");
assertFalse(res2.isSuccess());
dbInstance.closeSession();
// reacquire same identity
LockResult res3 = cl.acquireLock(ores, ident, "abc");
assertTrue(res3.isSuccess());
dbInstance.closeSession();
// make sure it is not locked anymore
boolean lo3 = cl.isLocked(ores, "abc");
assertTrue(lo3);
// test the admin
List<LockEntry> entries = cl.adminOnlyGetLockEntries();
assertEquals(1, entries.size());
LockEntry le = entries.get(0);
// must be original owner
assertEquals(le.getOwner().getName(), ident.getName());
// release lock
cl.releaseLock(res3);
dbInstance.closeSession();
// test the admin
entries = cl.adminOnlyGetLockEntries();
assertEquals(0, entries.size());
// make sure it is not locked anymore
boolean lo = cl.isLocked(ores, "abc");
assertFalse(lo);
}
use of org.olat.core.util.coordinate.LockResult in project openolat by klemens.
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;
}
Aggregations