Search in sources :

Example 21 with RepositoryEntryVO

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);
    }
}
Also used : SearchRepositoryEntryParameters(org.olat.repository.model.SearchRepositoryEntryParameters) WebApplicationException(javax.ws.rs.WebApplicationException) ArrayList(java.util.ArrayList) RestSecurityHelper.getRoles(org.olat.restapi.security.RestSecurityHelper.getRoles) Roles(org.olat.core.id.Roles) RepositoryEntry(org.olat.repository.RepositoryEntry) WebApplicationException(javax.ws.rs.WebApplicationException) RepositoryEntryVO(org.olat.restapi.support.vo.RepositoryEntryVO) RepositoryManager(org.olat.repository.RepositoryManager) RestSecurityHelper.getIdentity(org.olat.restapi.security.RestSecurityHelper.getIdentity) Identity(org.olat.core.id.Identity) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 22 with RepositoryEntryVO

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();
}
Also used : Response(javax.ws.rs.core.Response) RepositoryEntryVO(org.olat.restapi.support.vo.RepositoryEntryVO) EntityTag(javax.ws.rs.core.EntityTag) RepositoryEntry(org.olat.repository.RepositoryEntry) Date(java.util.Date) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 23 with RepositoryEntryVO

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();
}
Also used : RepositoryEntryVO(org.olat.restapi.support.vo.RepositoryEntryVO) RepositoryEntryLifecycle(org.olat.repository.model.RepositoryEntryLifecycle) RepositoryEntry(org.olat.repository.RepositoryEntry) RepositoryEntryLifecycleVO(org.olat.restapi.support.vo.RepositoryEntryLifecycleVO) RepositoryEntryLifecycleDAO(org.olat.repository.manager.RepositoryEntryLifecycleDAO) Date(java.util.Date) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes)

Example 24 with RepositoryEntryVO

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();
}
Also used : RepositoryEntryVO(org.olat.restapi.support.vo.RepositoryEntryVO) RepositoryEntry(org.olat.repository.RepositoryEntry) RestSecurityHelper.getIdentity(org.olat.restapi.security.RestSecurityHelper.getIdentity) Identity(org.olat.core.id.Identity) File(java.io.File) MultipartReader(org.olat.restapi.support.MultipartReader) WebApplicationException(javax.ws.rs.WebApplicationException) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 25 with RepositoryEntryVO

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);
    }
}
Also used : SearchRepositoryEntryParameters(org.olat.repository.model.SearchRepositoryEntryParameters) WebApplicationException(javax.ws.rs.WebApplicationException) ArrayList(java.util.ArrayList) RestSecurityHelper.getRoles(org.olat.restapi.security.RestSecurityHelper.getRoles) Roles(org.olat.core.id.Roles) RepositoryEntry(org.olat.repository.RepositoryEntry) WebApplicationException(javax.ws.rs.WebApplicationException) RepositoryEntryVO(org.olat.restapi.support.vo.RepositoryEntryVO) RepositoryManager(org.olat.repository.RepositoryManager) RestSecurityHelper.getIdentity(org.olat.restapi.security.RestSecurityHelper.getIdentity) Identity(org.olat.core.id.Identity) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

RepositoryEntryVO (org.olat.restapi.support.vo.RepositoryEntryVO)42 RepositoryEntry (org.olat.repository.RepositoryEntry)36 URI (java.net.URI)26 HttpResponse (org.apache.http.HttpResponse)26 Test (org.junit.Test)24 File (java.io.File)16 HttpEntity (org.apache.http.HttpEntity)14 HttpPut (org.apache.http.client.methods.HttpPut)14 URL (java.net.URL)12 Produces (javax.ws.rs.Produces)12 HttpGet (org.apache.http.client.methods.HttpGet)10 Identity (org.olat.core.id.Identity)8 Date (java.util.Date)6 GET (javax.ws.rs.GET)6 WebApplicationException (javax.ws.rs.WebApplicationException)6 RestSecurityHelper.getIdentity (org.olat.restapi.security.RestSecurityHelper.getIdentity)6 RepositoryEntryLifecycleVO (org.olat.restapi.support.vo.RepositoryEntryLifecycleVO)6 Consumes (javax.ws.rs.Consumes)5 HttpPost (org.apache.http.client.methods.HttpPost)4 Roles (org.olat.core.id.Roles)4