Search in sources :

Example 26 with RepositoryEntryVO

use of org.olat.restapi.support.vo.RepositoryEntryVO in project OpenOLAT by OpenOLAT.

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 27 with RepositoryEntryVO

use of org.olat.restapi.support.vo.RepositoryEntryVO in project OpenOLAT by OpenOLAT.

the class RepositoryEntryResource method replaceResource.

/**
 * Replace a resource in the repository and update its display name. The implementation is
 * limited to CP.
 * @response.representation.mediaType multipart/form-data
 * @response.representation.doc Import the resource file
 * @response.representation.200.qname {http://www.example.com}repositoryEntryVO
 * @response.representation.200.mediaType application/xml, application/json
 * @response.representation.200.doc Replace the resource and return the updated 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 repoEntryKey The key or soft key of the repository entry
 * @param filename The name of the file
 * @param file The file input stream
 * @param displayname The display name
 * @param request The HTTP request
 * @return
 */
@POST
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Consumes({ MediaType.MULTIPART_FORM_DATA })
public Response replaceResource(@PathParam("repoEntryKey") String repoEntryKey, @Context HttpServletRequest request) {
    if (!RestSecurityHelper.isAuthor(request)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    MultipartReader reader = null;
    try {
        final RepositoryEntry re = lookupRepositoryEntry(repoEntryKey);
        if (re == null) {
            return Response.serverError().status(Status.NOT_FOUND).build();
        }
        reader = new MultipartReader(request);
        File tmpFile = reader.getFile();
        String displayname = reader.getValue("displayname");
        String location = reader.getValue("location");
        String authors = reader.getValue("authors");
        String description = reader.getValue("description");
        String externalId = reader.getValue("externalId");
        String externalRef = reader.getValue("externalRef");
        String managedFlags = reader.getValue("managedFlags");
        Identity identity = RestSecurityHelper.getUserRequest(request).getIdentity();
        RepositoryEntry replacedRe;
        if (tmpFile == null) {
            replacedRe = repositoryManager.setDescriptionAndName(re, displayname, description, location, authors, externalId, externalRef, managedFlags, re.getLifecycle());
        } else {
            long length = tmpFile.length();
            if (length == 0) {
                return Response.serverError().status(Status.NO_CONTENT).build();
            }
            replacedRe = replaceFileResource(identity, re, tmpFile);
            if (replacedRe == null) {
                return Response.serverError().status(Status.NOT_FOUND).build();
            } else {
                replacedRe = repositoryManager.setDescriptionAndName(replacedRe, displayname, description, location, authors, externalId, externalRef, managedFlags, replacedRe.getLifecycle());
            }
        }
        RepositoryEntryVO vo = ObjectFactory.get(replacedRe);
        return Response.ok(vo).build();
    } catch (Exception e) {
        log.error("Error while importing a file", e);
    } finally {
        MultipartReader.closeQuietly(reader);
    }
    return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build();
}
Also used : RepositoryEntryVO(org.olat.restapi.support.vo.RepositoryEntryVO) RepositoryEntry(org.olat.repository.RepositoryEntry) Identity(org.olat.core.id.Identity) RestSecurityHelper.getIdentity(org.olat.restapi.security.RestSecurityHelper.getIdentity) File(java.io.File) MultipartReader(org.olat.restapi.support.MultipartReader) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes)

Example 28 with RepositoryEntryVO

use of org.olat.restapi.support.vo.RepositoryEntryVO in project OpenOLAT by OpenOLAT.

the class ObjectFactory method get.

public static RepositoryEntryVO get(RepositoryEntry entry) {
    RepositoryEntryVO vo = new RepositoryEntryVO();
    vo.setKey(entry.getKey());
    vo.setSoftkey(entry.getSoftkey());
    vo.setResourcename(entry.getResourcename());
    vo.setDisplayname(entry.getDisplayname());
    vo.setDescription(entry.getDescription());
    vo.setAuthors(entry.getAuthors());
    vo.setLocation(entry.getLocation());
    vo.setResourceableId(entry.getResourceableId());
    vo.setResourceableTypeName(entry.getResourceableTypeName());
    OLATResource resource = entry.getOlatResource();
    if (resource != null) {
        vo.setOlatResourceKey(resource.getKey());
        vo.setOlatResourceId(resource.getResourceableId());
        vo.setOlatResourceTypeName(resource.getResourceableTypeName());
    }
    vo.setExternalId(entry.getExternalId());
    vo.setExternalRef(entry.getExternalRef());
    vo.setManagedFlags(entry.getManagedFlagsString());
    if (entry.getLifecycle() != null) {
        vo.setLifecycle(new RepositoryEntryLifecycleVO(entry.getLifecycle()));
    }
    return vo;
}
Also used : RepositoryEntryVO(org.olat.restapi.support.vo.RepositoryEntryVO) OLATResource(org.olat.resource.OLATResource) RepositoryEntryLifecycleVO(org.olat.restapi.support.vo.RepositoryEntryLifecycleVO)

Example 29 with RepositoryEntryVO

use of org.olat.restapi.support.vo.RepositoryEntryVO in project OpenOLAT by OpenOLAT.

the class RepositoryEntriesTest method testImportWiki.

@Test
public void testImportWiki() throws IOException, URISyntaxException {
    URL cpUrl = RepositoryEntriesTest.class.getResource("wiki-demo.zip");
    assertNotNull(cpUrl);
    File cp = new File(cpUrl.toURI());
    RestConnection conn = new RestConnection();
    assertTrue(conn.login("administrator", "openolat"));
    URI request = UriBuilder.fromUri(getContextURI()).path("repo/entries").build();
    HttpPut method = conn.createPut(request, MediaType.APPLICATION_JSON, true);
    HttpEntity entity = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE).addBinaryBody("file", cp, ContentType.APPLICATION_OCTET_STREAM, cp.getName()).addTextBody("filename", "wiki-demo.zip").addTextBody("resourcename", "Wiki demo").addTextBody("displayname", "Wiki demo").build();
    method.setEntity(entity);
    HttpResponse response = conn.execute(method);
    assertTrue(response.getStatusLine().getStatusCode() == 200 || response.getStatusLine().getStatusCode() == 201);
    RepositoryEntryVO vo = conn.parse(response, RepositoryEntryVO.class);
    assertNotNull(vo);
    Long key = vo.getKey();
    RepositoryEntry re = RepositoryManager.getInstance().lookupRepositoryEntry(key);
    assertNotNull(re);
    assertNotNull(re.getOlatResource());
    assertEquals("Wiki demo", re.getDisplayname());
    log.info(re.getOlatResource().getResourceableTypeName());
    conn.shutdown();
}
Also used : RepositoryEntryVO(org.olat.restapi.support.vo.RepositoryEntryVO) HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) RepositoryEntry(org.olat.repository.RepositoryEntry) File(java.io.File) URI(java.net.URI) URL(java.net.URL) HttpPut(org.apache.http.client.methods.HttpPut) Test(org.junit.Test)

Example 30 with RepositoryEntryVO

use of org.olat.restapi.support.vo.RepositoryEntryVO in project OpenOLAT by OpenOLAT.

the class RepositoryEntriesTest method testGetEntry_managed.

@Test
public void testGetEntry_managed() throws IOException, URISyntaxException {
    RepositoryEntry re = createRepository("Test GET repo entry");
    re.setManagedFlagsString("all");
    re = dbInstance.getCurrentEntityManager().merge(re);
    dbInstance.commitAndCloseSession();
    RestConnection conn = new RestConnection();
    assertTrue(conn.login("administrator", "openolat"));
    URI request = UriBuilder.fromUri(getContextURI()).path("repo").path("entries").queryParam("managed", "true").build();
    HttpGet method = conn.createGet(request, MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(method);
    assertEquals(200, response.getStatusLine().getStatusCode());
    List<RepositoryEntryVO> entryVoes = parseRepoArray(response.getEntity().getContent());
    Assert.assertNotNull(entryVoes);
    Assert.assertFalse(entryVoes.isEmpty());
    // only repo entries with managed flags
    for (RepositoryEntryVO entryVo : entryVoes) {
        Assert.assertNotNull(entryVo.getManagedFlags());
        Assert.assertTrue(entryVo.getManagedFlags().length() > 0);
    }
    conn.shutdown();
}
Also used : RepositoryEntryVO(org.olat.restapi.support.vo.RepositoryEntryVO) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) RepositoryEntry(org.olat.repository.RepositoryEntry) URI(java.net.URI) Test(org.junit.Test)

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