use of org.olat.restapi.support.vo.FileVO in project OpenOLAT by OpenOLAT.
the class VFSWebservice method get.
protected Response get(List<PathSegment> path, UriInfo uriInfo, Request request) {
VFSItem vItem = resolveFile(path);
if (vItem == null) {
return Response.serverError().status(Status.NOT_FOUND).build();
} else if (vItem instanceof VFSContainer) {
VFSContainer directory = (VFSContainer) vItem;
List<VFSItem> items = directory.getItems(new SystemItemFilter());
int count = 0;
FileVO[] links = new FileVO[items.size()];
for (VFSItem item : items) {
UriBuilder builder = uriInfo.getAbsolutePathBuilder();
String uri = builder.path(normalize(item.getName())).build().toString();
if (item instanceof VFSLeaf) {
links[count++] = new FileVO("self", uri, item.getName(), ((VFSLeaf) item).getSize());
} else {
links[count++] = new FileVO("self", uri, item.getName());
}
}
return Response.ok(links).build();
} else if (vItem instanceof VFSLeaf) {
VFSLeaf leaf = (VFSLeaf) vItem;
Date lastModified = new Date(leaf.getLastModified());
Response.ResponseBuilder response = request.evaluatePreconditions(lastModified);
if (response == null) {
String mimeType = WebappHelper.getMimeType(leaf.getName());
if (mimeType == null) {
mimeType = MediaType.APPLICATION_OCTET_STREAM;
}
response = Response.ok(leaf.getInputStream(), mimeType).lastModified(lastModified).cacheControl(cc);
}
return response.build();
}
return Response.serverError().status(Status.BAD_REQUEST).build();
}
use of org.olat.restapi.support.vo.FileVO in project OpenOLAT by OpenOLAT.
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();
}
use of org.olat.restapi.support.vo.FileVO in project OpenOLAT by OpenOLAT.
the class ForumTest method testReplyWithTwoAttachments.
@Test
public void testReplyWithTwoAttachments() throws IOException, URISyntaxException {
RestConnection conn = new RestConnection();
assertTrue(conn.login(id1.getName(), "A6B7C8"));
ReplyVO vo = new ReplyVO();
vo.setTitle("Reply with attachment");
vo.setBody("Reply with attachment body");
File64VO[] files = new File64VO[2];
// upload portrait
InputStream portraitStream = CoursesElementsTest.class.getResourceAsStream("portrait.jpg");
assertNotNull(portraitStream);
byte[] portraitBytes = IOUtils.toByteArray(portraitStream);
byte[] portrait64 = Base64.encodeBase64(portraitBytes, true);
files[0] = new File64VO("portrait64.jpg", new String(portrait64));
// upload single page
InputStream indexStream = ForumTest.class.getResourceAsStream("singlepage.html");
assertNotNull(indexStream);
byte[] indexBytes = IOUtils.toByteArray(indexStream);
byte[] index64 = Base64.encodeBase64(indexBytes, true);
files[1] = new File64VO("singlepage64.html", new String(index64));
vo.setAttachments(files);
URI uri = getForumUriBuilder().path("posts").path(m1.getKey().toString()).build();
HttpPut method = conn.createPut(uri, MediaType.APPLICATION_JSON, true);
conn.addJsonEntity(method, vo);
method.addHeader("Accept-Language", "en");
HttpResponse response = conn.execute(method);
assertEquals(200, response.getStatusLine().getStatusCode());
MessageVO message = conn.parse(response, MessageVO.class);
assertNotNull(message);
assertNotNull(message.getAttachments());
assertEquals(2, message.getAttachments().length);
for (FileVO attachment : message.getAttachments()) {
String title = attachment.getTitle();
assertNotNull(title);
String href = attachment.getHref();
URI attachmentUri = new URI(href);
HttpGet getAttachment = conn.createGet(attachmentUri, "*/*", true);
HttpResponse attachmentCode = conn.execute(getAttachment);
assertEquals(200, attachmentCode.getStatusLine().getStatusCode());
EntityUtils.consume(attachmentCode.getEntity());
}
// check if the file exists
ForumManager fm = ForumManager.getInstance();
VFSContainer container = fm.getMessageContainer(message.getForumKey(), message.getKey());
VFSItem uploadedFile = container.resolve("portrait64.jpg");
assertNotNull(uploadedFile);
assertTrue(uploadedFile instanceof VFSLeaf);
// check if the image is still an image
VFSLeaf uploadedImage = (VFSLeaf) uploadedFile;
InputStream uploadedStream = uploadedImage.getInputStream();
BufferedImage image = ImageIO.read(uploadedStream);
FileUtils.closeSafely(uploadedStream);
assertNotNull(image);
// check if the single page exists
VFSItem uploadedPage = container.resolve("singlepage64.html");
assertNotNull(uploadedPage);
assertTrue(uploadedPage instanceof VFSLeaf);
conn.shutdown();
}
use of org.olat.restapi.support.vo.FileVO in project OpenOLAT by OpenOLAT.
the class UserMgmtTest method testUserBCCourseNodeFolder.
@Test
public void testUserBCCourseNodeFolder() throws IOException, URISyntaxException {
RestConnection conn = new RestConnection();
assertTrue(conn.login(id1.getName(), "A6B7C8"));
URI uri = UriBuilder.fromUri(getContextURI()).path("users").path(id1.getKey().toString()).path("folders").path("course").path(demoCourse.getResourceableId().toString()).path(demoBCCourseNode.getIdent()).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> folders = parseFileArray(body);
assertNotNull(folders);
assertFalse(folders.isEmpty());
// private and public
assertEquals(1, folders.size());
FileVO singlePage = folders.get(0);
assertEquals("singlepage.html", singlePage.getTitle());
conn.shutdown();
}
use of org.olat.restapi.support.vo.FileVO in project openolat by klemens.
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();
}
Aggregations