Search in sources :

Example 16 with FileVO

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

the class ForumWebService method getAttachments.

private FileVO[] getAttachments(Message mess, UriInfo uriInfo) {
    VFSContainer container = fom.getMessageContainer(mess.getForum().getKey(), mess.getKey());
    List<FileVO> attachments = new ArrayList<FileVO>();
    for (VFSItem item : container.getItems(new SystemItemFilter())) {
        UriBuilder attachmentUri = uriInfo.getBaseUriBuilder().path("repo").path("forums").path(mess.getForum().getKey().toString()).path("posts").path(mess.getKey().toString()).path("attachments").path(format(item.getName()));
        String uri = attachmentUri.build().toString();
        if (item instanceof VFSLeaf) {
            attachments.add(new FileVO("self", uri, item.getName(), ((VFSLeaf) item).getSize()));
        } else {
            attachments.add(new FileVO("self", uri, item.getName()));
        }
    }
    FileVO[] attachmentArr = new FileVO[attachments.size()];
    attachmentArr = attachments.toArray(attachmentArr);
    return attachmentArr;
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) VFSContainer(org.olat.core.util.vfs.VFSContainer) FileVO(org.olat.restapi.support.vo.FileVO) ArrayList(java.util.ArrayList) VFSItem(org.olat.core.util.vfs.VFSItem) SystemItemFilter(org.olat.core.util.vfs.filters.SystemItemFilter) UriBuilder(javax.ws.rs.core.UriBuilder)

Example 17 with FileVO

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

the class ForumTest method testGetAttachment.

@Test
public void testGetAttachment() throws IOException, URISyntaxException {
    RestConnection conn = new RestConnection();
    // set a attachment
    VFSContainer container = forumManager.getMessageContainer(m1.getForum().getKey(), m1.getKey());
    InputStream portraitIn = CoursesElementsTest.class.getResourceAsStream("portrait.jpg");
    assertNotNull(portraitIn);
    VFSLeaf attachment = container.createChildLeaf(UUID.randomUUID().toString().replace("-", "") + ".jpg");
    FileUtils.bcopy(portraitIn, attachment.getOutputStream(false), "");
    assertTrue(conn.login("administrator", "openolat"));
    URI uri = getForumUriBuilder().path("posts").path(m1.getKey().toString()).path("attachments").build();
    HttpGet method = conn.createGet(uri, MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(method);
    assertEquals(200, response.getStatusLine().getStatusCode());
    List<FileVO> files = parseFileArray(response.getEntity().getContent());
    assertNotNull(files);
    FileVO attachmentVO = null;
    for (FileVO file : files) {
        if (attachment.getName().equals(file.getTitle())) {
            attachmentVO = file;
        }
    }
    assertNotNull(attachmentVO);
    URI downloadURI = new URI(attachmentVO.getHref());
    HttpGet download = conn.createGet(downloadURI, MediaType.APPLICATION_JSON, true);
    HttpResponse downloadResponse = conn.execute(download);
    assertEquals(200, downloadResponse.getStatusLine().getStatusCode());
    // String contentType = downloadResponse.getEntity().getContentType().getValue();
    // doesn't work with grizzly assertEquals("image/jpeg", contentType);
    conn.shutdown();
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) 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) URI(java.net.URI) Test(org.junit.Test)

Example 18 with FileVO

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

the class ForumTest method testUploadAttachmentAndRename.

@Test
public void testUploadAttachmentAndRename() throws IOException, URISyntaxException {
    RestConnection conn = new RestConnection();
    assertTrue(conn.login(id1.getName(), "A6B7C8"));
    URI uri = getForumUriBuilder().path("posts").path(m1.getKey().toString()).queryParam("authorKey", id1.getKey()).queryParam("title", "New message with attachment ").queryParam("body", "A very interesting response in Thread-1 with an attachment").build();
    HttpPut method = conn.createPut(uri, MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(method);
    assertEquals(200, response.getStatusLine().getStatusCode());
    MessageVO message = conn.parse(response, MessageVO.class);
    assertNotNull(message);
    // attachment
    URL portraitUrl = CoursesElementsTest.class.getResource("portrait.jpg");
    assertNotNull(portraitUrl);
    File portrait = new File(portraitUrl.toURI());
    // upload portrait
    URI attachUri = getForumUriBuilder().path("posts").path(m1.getKey().toString()).path("attachments").build();
    HttpPost attachMethod = conn.createPost(attachUri, MediaType.APPLICATION_JSON);
    conn.addMultipart(attachMethod, "portrait.jpg", portrait);
    HttpResponse attachCode = conn.execute(attachMethod);
    assertEquals(200, attachCode.getStatusLine().getStatusCode());
    EntityUtils.consume(attachCode.getEntity());
    // upload portrait a second time
    URI attach2Uri = getForumUriBuilder().path("posts").path(m1.getKey().toString()).path("attachments").build();
    HttpPost attach2Method = conn.createPost(attach2Uri, MediaType.APPLICATION_JSON);
    conn.addMultipart(attach2Method, "portrait.jpg", portrait);
    HttpResponse attach2Code = conn.execute(attach2Method);
    assertEquals(200, attach2Code.getStatusLine().getStatusCode());
    EntityUtils.consume(attach2Code.getEntity());
    // load the attachments
    URI loadUri = getForumUriBuilder().path("posts").path(m1.getKey().toString()).path("attachments").build();
    HttpGet loadMethod = conn.createGet(loadUri, MediaType.APPLICATION_JSON, true);
    HttpResponse loadResponse = conn.execute(loadMethod);
    assertEquals(200, loadResponse.getStatusLine().getStatusCode());
    InputStream loadBody = loadResponse.getEntity().getContent();
    List<FileVO> files = parseFileArray(loadBody);
    assertNotNull(files);
    assertEquals(2, files.size());
    conn.shutdown();
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) 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) MessageVO(org.olat.modules.fo.restapi.MessageVO) File(java.io.File) HttpPut(org.apache.http.client.methods.HttpPut) URL(java.net.URL) Test(org.junit.Test)

Example 19 with FileVO

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

the class SharedFolderTest method getFiles.

/**
 * Check simple GET for the directory.
 *
 * @throws IOException
 * @throws URISyntaxException
 */
@Test
public void getFiles() throws IOException, URISyntaxException {
    RestConnection conn = new RestConnection();
    Assert.assertTrue(conn.login("administrator", "openolat"));
    Identity owner = JunitTestHelper.createAndPersistIdentityAsRndUser("shared-owner-");
    RepositoryEntry sharedFolder = new SharedFolderHandler().createResource(owner, "Shared 2", "Shared files", null, Locale.ENGLISH);
    VFSContainer container = SharedFolderManager.getInstance().getNamedSharedFolder(sharedFolder, true);
    copyFileInResourceFolder(container, "portrait.jpg", "2_");
    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("2_portrait.jpg"));
    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 20 with FileVO

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

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)

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