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);
}
}
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();
}
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;
}
Aggregations