Search in sources :

Example 31 with FileVO

use of org.olat.restapi.support.vo.FileVO in project openolat by klemens.

the class SharedFolderTest method getFiles_participant.

/**
 * Participant can read the files and download them.
 *
 * @throws IOException
 * @throws URISyntaxException
 */
@Test
public void getFiles_participant() throws IOException, URISyntaxException {
    // a shared folder with a participant
    Identity owner = JunitTestHelper.createAndPersistIdentityAsRndUser("shared-owner-");
    Identity participant = JunitTestHelper.createAndPersistIdentityAsRndUser("shared-part-");
    RepositoryEntry sharedFolder = new SharedFolderHandler().createResource(owner, "Shared 2", "Shared files", null, Locale.ENGLISH);
    VFSContainer container = SharedFolderManager.getInstance().getNamedSharedFolder(sharedFolder, true);
    copyFileInResourceFolder(container, "portrait.jpg", "3_");
    repositoryEntryRelationDao.addRole(participant, sharedFolder, GroupRoles.participant.name());
    dbInstance.commitAndCloseSession();
    // participant want to see the file
    RestConnection conn = new RestConnection();
    Assert.assertTrue(conn.login(participant.getName(), "A6B7C8"));
    // check directories
    URI uri = UriBuilder.fromUri(getFolderURI(sharedFolder)).path("files").build();
    HttpGet method = conn.createGet(uri, MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(method);
    Assert.assertEquals(200, response.getStatusLine().getStatusCode());
    InputStream body = response.getEntity().getContent();
    List<FileVO> links = parseFileArray(body);
    Assert.assertNotNull(links);
    Assert.assertEquals(1, links.size());
    Assert.assertTrue(links.get(0).getHref().contains("3_portrait.jpg"));
    // download the file
    URI fileUri = UriBuilder.fromUri(getFolderURI(sharedFolder)).path("files").path("3_portrait.jpg").build();
    HttpGet fileMethod = conn.createGet(fileUri, "*/*", true);
    HttpResponse fileResponse = conn.execute(fileMethod);
    Assert.assertEquals(200, fileResponse.getStatusLine().getStatusCode());
    byte[] fileBytes = EntityUtils.toByteArray(fileResponse.getEntity());
    Assert.assertNotNull(fileBytes);
    Assert.assertTrue(fileBytes.length > 10);
    conn.shutdown();
}
Also used : SharedFolderHandler(org.olat.repository.handlers.SharedFolderHandler) InputStream(java.io.InputStream) VFSContainer(org.olat.core.util.vfs.VFSContainer) HttpGet(org.apache.http.client.methods.HttpGet) FileVO(org.olat.restapi.support.vo.FileVO) HttpResponse(org.apache.http.HttpResponse) RepositoryEntry(org.olat.repository.RepositoryEntry) Identity(org.olat.core.id.Identity) URI(java.net.URI) Test(org.junit.Test)

Example 32 with FileVO

use of org.olat.restapi.support.vo.FileVO in project openolat by klemens.

the class UserMgmtTest method testUserPersonalFolder.

@Test
public void testUserPersonalFolder() throws Exception {
    RestConnection conn = new RestConnection();
    assertTrue(conn.login(id1.getName(), "A6B7C8"));
    URI uri = UriBuilder.fromUri(getContextURI()).path("users").path(id1.getKey().toString()).path("folders").path("personal").build();
    HttpGet method = conn.createGet(uri, MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(method);
    assertEquals(200, response.getStatusLine().getStatusCode());
    InputStream body = response.getEntity().getContent();
    List<FileVO> files = parseFileArray(body);
    assertNotNull(files);
    assertFalse(files.isEmpty());
    // private and public
    assertEquals(2, files.size());
    conn.shutdown();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) FileVO(org.olat.restapi.support.vo.FileVO) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) Test(org.junit.Test)

Example 33 with FileVO

use of org.olat.restapi.support.vo.FileVO in project openolat by klemens.

the class VFSWebservice method createFileVO.

public static FileVO createFileVO(VFSItem item, UriInfo uriInfo) {
    UriBuilder builder = uriInfo.getAbsolutePathBuilder();
    String uri = builder.path(normalize(item.getName())).build().toString();
    FileVO link = new FileVO("self", uri, item.getName());
    if (item instanceof VFSLeaf) {
        link.setSize(((VFSLeaf) item).getSize());
    }
    return link;
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) FileVO(org.olat.restapi.support.vo.FileVO) UriBuilder(javax.ws.rs.core.UriBuilder)

Example 34 with FileVO

use of org.olat.restapi.support.vo.FileVO in project openolat by klemens.

the class ForumWebService method getAttachments.

/**
 * Retrieves the attachments of the message
 * @response.representation.200.mediaType application/xml, application/json
 * @response.representation.200.doc The links to the attachments
 * @response.representation.404.doc The message not found
 * @param messageKey The key of the message
 * @param uriInfo The URI information
 * @return The attachments
 */
@GET
@Path("posts/{messageKey}/attachments")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response getAttachments(@PathParam("messageKey") Long messageKey, @Context UriInfo uriInfo) {
    // load message
    Message mess = fom.loadMessage(messageKey);
    if (mess == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    if (!forum.equalsByPersistableKey(mess.getForum())) {
        return Response.serverError().status(Status.CONFLICT).build();
    }
    FileVO[] attachments = getAttachments(mess, uriInfo);
    return Response.ok(attachments).build();
}
Also used : Message(org.olat.modules.fo.Message) FileVO(org.olat.restapi.support.vo.FileVO) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

FileVO (org.olat.restapi.support.vo.FileVO)34 URI (java.net.URI)26 HttpResponse (org.apache.http.HttpResponse)26 Test (org.junit.Test)24 InputStream (java.io.InputStream)22 HttpGet (org.apache.http.client.methods.HttpGet)22 VFSContainer (org.olat.core.util.vfs.VFSContainer)14 ByteArrayInputStream (java.io.ByteArrayInputStream)10 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)10 HttpPut (org.apache.http.client.methods.HttpPut)8 UriBuilder (javax.ws.rs.core.UriBuilder)6 VFSItem (org.olat.core.util.vfs.VFSItem)6 Identity (org.olat.core.id.Identity)4 SystemItemFilter (org.olat.core.util.vfs.filters.SystemItemFilter)4 MessageVO (org.olat.modules.fo.restapi.MessageVO)4 RepositoryEntry (org.olat.repository.RepositoryEntry)4 SharedFolderHandler (org.olat.repository.handlers.SharedFolderHandler)4 BufferedImage (java.awt.image.BufferedImage)2 File (java.io.File)2 URL (java.net.URL)2