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;
}
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();
}
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();
}
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();
}
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();
}
Aggregations