use of org.olat.restapi.support.vo.RepositoryEntryVO in project openolat by klemens.
the class RepositoryEntriesResource method searchEntries.
/**
* Search for repository entries, possible search attributes are name, author and type
* @response.representation.mediaType multipart/form-data
* @response.representation.doc Search for repository entries
* @response.representation.200.qname {http://www.example.com}repositoryEntryVO
* @response.representation.200.mediaType application/xml, application/json
* @response.representation.200.doc Search for repository entries
* @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
* @param type Filter by the file resource type of the repository entry
* @param author Filter by the author's username
* @param name Filter by name of repository entry
* @param myEntries Only search entries the requester owns
* @param httpRequest The HTTP request
* @return
*/
@GET
@Path("search")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response searchEntries(@QueryParam("type") String type, @QueryParam("author") @DefaultValue("*") String author, @QueryParam("name") @DefaultValue("*") String name, @QueryParam("myentries") @DefaultValue("false") boolean myEntries, @Context HttpServletRequest httpRequest) {
RepositoryManager rm = RepositoryManager.getInstance();
try {
List<RepositoryEntry> reposFound = new ArrayList<RepositoryEntry>();
Identity identity = getIdentity(httpRequest);
boolean restrictedType = type != null && !type.isEmpty();
// list of courses open for everybody
Roles roles = getRoles(httpRequest);
if (myEntries) {
List<RepositoryEntry> lstRepos = rm.queryByOwner(identity, restrictedType ? new String[] { type } : null);
boolean restrictedName = !name.equals("*");
boolean restrictedAuthor = !author.equals("*");
if (restrictedName | restrictedAuthor) {
// filter by search conditions
for (RepositoryEntry re : lstRepos) {
boolean nameOk = restrictedName ? re.getDisplayname().toLowerCase().contains(name.toLowerCase()) : true;
boolean authorOk = restrictedAuthor ? re.getInitialAuthor().toLowerCase().equals(author.toLowerCase()) : true;
if (nameOk & authorOk)
reposFound.add(re);
}
} else {
if (!lstRepos.isEmpty())
reposFound.addAll(lstRepos);
}
} else {
List<String> types = new ArrayList<String>(1);
if (restrictedType)
types.add(type);
SearchRepositoryEntryParameters params = new SearchRepositoryEntryParameters(name, author, null, restrictedType ? types : null, identity, roles, null);
List<RepositoryEntry> lstRepos = rm.genericANDQueryWithRolesRestriction(params, 0, -1, false);
if (!lstRepos.isEmpty())
reposFound.addAll(lstRepos);
}
int i = 0;
RepositoryEntryVO[] reVOs = new RepositoryEntryVO[reposFound.size()];
for (RepositoryEntry re : reposFound) {
reVOs[i++] = ObjectFactory.get(re);
}
return Response.ok(reVOs).build();
} catch (Exception e) {
throw new WebApplicationException(e);
}
}
use of org.olat.restapi.support.vo.RepositoryEntryVO in project openolat by klemens.
the class RepositoryEntryResource method getById.
/**
* get a resource in the repository
* @response.representation.200.qname {http://www.example.com}repositoryEntryVO
* @response.representation.200.mediaType application/xml, application/json
* @response.representation.200.doc Get the repository resource
* @response.representation.200.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_REPOENTRYVO}
* @response.representation.404.doc The repository entry not found
* @param repoEntryKey The key or soft key of the repository entry
* @param request The REST request
* @return
*/
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response getById(@PathParam("repoEntryKey") String repoEntryKey, @Context Request request) {
RepositoryEntry re = lookupRepositoryEntry(repoEntryKey);
if (re == null) {
return Response.serverError().status(Status.NOT_FOUND).build();
}
Date lastModified = re.getLastModified();
Response.ResponseBuilder response;
if (lastModified == null) {
EntityTag eTag = ObjectFactory.computeEtag(re);
response = request.evaluatePreconditions(eTag);
if (response == null) {
RepositoryEntryVO vo = ObjectFactory.get(re);
response = Response.ok(vo).tag(eTag).lastModified(lastModified);
}
} else {
EntityTag eTag = ObjectFactory.computeEtag(re);
response = request.evaluatePreconditions(lastModified, eTag);
if (response == null) {
RepositoryEntryVO vo = ObjectFactory.get(re);
response = Response.ok(vo).tag(eTag).lastModified(lastModified);
}
}
return response.build();
}
use of org.olat.restapi.support.vo.RepositoryEntryVO in project openolat by klemens.
the class RepositoryEntryResource method updateEntry.
@POST
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response updateEntry(@PathParam("repoEntryKey") String repoEntryKey, RepositoryEntryVO vo, @Context HttpServletRequest request) {
if (!RestSecurityHelper.isAuthor(request)) {
return Response.serverError().status(Status.UNAUTHORIZED).build();
}
final RepositoryEntry re = lookupRepositoryEntry(repoEntryKey);
if (re == null) {
return Response.serverError().status(Status.NOT_FOUND).build();
}
RepositoryEntryLifecycle lifecycle = null;
RepositoryEntryLifecycleVO lifecycleVo = vo.getLifecycle();
if (lifecycleVo != null) {
RepositoryEntryLifecycleDAO lifecycleDao = CoreSpringFactory.getImpl(RepositoryEntryLifecycleDAO.class);
if (lifecycleVo.getKey() != null) {
lifecycle = lifecycleDao.loadById(lifecycleVo.getKey());
if (lifecycle.isPrivateCycle()) {
// check date
String fromStr = lifecycleVo.getValidFrom();
String toStr = lifecycleVo.getValidTo();
String label = lifecycleVo.getLabel();
String softKey = lifecycleVo.getSoftkey();
Date from = ObjectFactory.parseDate(fromStr);
Date to = ObjectFactory.parseDate(toStr);
lifecycle.setLabel(label);
lifecycle.setSoftKey(softKey);
lifecycle.setValidFrom(from);
lifecycle.setValidTo(to);
}
} else {
String fromStr = lifecycleVo.getValidFrom();
String toStr = lifecycleVo.getValidTo();
String label = lifecycleVo.getLabel();
String softKey = lifecycleVo.getSoftkey();
Date from = ObjectFactory.parseDate(fromStr);
Date to = ObjectFactory.parseDate(toStr);
lifecycle = lifecycleDao.create(label, softKey, true, from, to);
}
}
RepositoryEntry reloaded = repositoryManager.setDescriptionAndName(re, vo.getDisplayname(), vo.getDescription(), vo.getLocation(), vo.getAuthors(), vo.getExternalId(), vo.getExternalRef(), vo.getManagedFlags(), lifecycle);
RepositoryEntryVO rvo = ObjectFactory.get(reloaded);
return Response.ok(rvo).build();
}
use of org.olat.restapi.support.vo.RepositoryEntryVO in project OpenOLAT by OpenOLAT.
the class RepositoryEntriesResource method putResource.
/**
* Import a resource in the repository
* @response.representation.mediaType multipart/form-data
* @response.representation.doc The file, its name and the resourcename
* @response.representation.200.qname {http://www.example.com}repositoryEntryVO
* @response.representation.200.mediaType application/xml, application/json
* @response.representation.200.doc Import the resource and return the repository entry
* @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
* @param filename The name of the imported file
* @param file The file input stream
* @param resourcename The name of the resource
* @param displayname The display name
* @param softkey The soft key (can be null)
* @param request The HTTP request
* @return
*/
@PUT
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Consumes({ MediaType.MULTIPART_FORM_DATA })
public Response putResource(@Context HttpServletRequest request) {
if (!isAuthor(request)) {
return Response.serverError().status(Status.UNAUTHORIZED).build();
}
MultipartReader partsReader = null;
try {
Identity identity = RestSecurityHelper.getUserRequest(request).getIdentity();
partsReader = new MultipartReader(request);
File tmpFile = partsReader.getFile();
long length = tmpFile.length();
if (length > 0) {
Long accessRaw = partsReader.getLongValue("access");
int access = accessRaw != null ? accessRaw.intValue() : RepositoryEntry.ACC_OWNERS;
String softkey = partsReader.getValue("softkey");
String resourcename = partsReader.getValue("resourcename");
String displayname = partsReader.getValue("displayname");
RepositoryEntry re = importFileResource(identity, tmpFile, resourcename, displayname, softkey, access);
RepositoryEntryVO vo = ObjectFactory.get(re);
return Response.ok(vo).build();
}
return Response.serverError().status(Status.NO_CONTENT).build();
} catch (Exception e) {
log.error("Error while importing a file", e);
} finally {
MultipartReader.closeQuietly(partsReader);
}
return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build();
}
use of org.olat.restapi.support.vo.RepositoryEntryVO in project OpenOLAT by OpenOLAT.
the class RepositoryEntriesResource method searchEntries.
/**
* Search for repository entries, possible search attributes are name, author and type
* @response.representation.mediaType multipart/form-data
* @response.representation.doc Search for repository entries
* @response.representation.200.qname {http://www.example.com}repositoryEntryVO
* @response.representation.200.mediaType application/xml, application/json
* @response.representation.200.doc Search for repository entries
* @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
* @param type Filter by the file resource type of the repository entry
* @param author Filter by the author's username
* @param name Filter by name of repository entry
* @param myEntries Only search entries the requester owns
* @param httpRequest The HTTP request
* @return
*/
@GET
@Path("search")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response searchEntries(@QueryParam("type") String type, @QueryParam("author") @DefaultValue("*") String author, @QueryParam("name") @DefaultValue("*") String name, @QueryParam("myentries") @DefaultValue("false") boolean myEntries, @Context HttpServletRequest httpRequest) {
RepositoryManager rm = RepositoryManager.getInstance();
try {
List<RepositoryEntry> reposFound = new ArrayList<RepositoryEntry>();
Identity identity = getIdentity(httpRequest);
boolean restrictedType = type != null && !type.isEmpty();
// list of courses open for everybody
Roles roles = getRoles(httpRequest);
if (myEntries) {
List<RepositoryEntry> lstRepos = rm.queryByOwner(identity, restrictedType ? new String[] { type } : null);
boolean restrictedName = !name.equals("*");
boolean restrictedAuthor = !author.equals("*");
if (restrictedName | restrictedAuthor) {
// filter by search conditions
for (RepositoryEntry re : lstRepos) {
boolean nameOk = restrictedName ? re.getDisplayname().toLowerCase().contains(name.toLowerCase()) : true;
boolean authorOk = restrictedAuthor ? re.getInitialAuthor().toLowerCase().equals(author.toLowerCase()) : true;
if (nameOk & authorOk)
reposFound.add(re);
}
} else {
if (!lstRepos.isEmpty())
reposFound.addAll(lstRepos);
}
} else {
List<String> types = new ArrayList<String>(1);
if (restrictedType)
types.add(type);
SearchRepositoryEntryParameters params = new SearchRepositoryEntryParameters(name, author, null, restrictedType ? types : null, identity, roles, null);
List<RepositoryEntry> lstRepos = rm.genericANDQueryWithRolesRestriction(params, 0, -1, false);
if (!lstRepos.isEmpty())
reposFound.addAll(lstRepos);
}
int i = 0;
RepositoryEntryVO[] reVOs = new RepositoryEntryVO[reposFound.size()];
for (RepositoryEntry re : reposFound) {
reVOs[i++] = ObjectFactory.get(re);
}
return Response.ok(reVOs).build();
} catch (Exception e) {
throw new WebApplicationException(e);
}
}
Aggregations