Search in sources :

Example 1 with FileVO

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();
}
Also used : Response(javax.ws.rs.core.Response) VFSLeaf(org.olat.core.util.vfs.VFSLeaf) VFSContainer(org.olat.core.util.vfs.VFSContainer) FileVO(org.olat.restapi.support.vo.FileVO) VFSItem(org.olat.core.util.vfs.VFSItem) List(java.util.List) SystemItemFilter(org.olat.core.util.vfs.filters.SystemItemFilter) UriBuilder(javax.ws.rs.core.UriBuilder) Date(java.util.Date)

Example 2 with FileVO

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

Example 3 with FileVO

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();
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) InputStream(java.io.InputStream) File64VO(org.olat.restapi.support.vo.File64VO) HttpGet(org.apache.http.client.methods.HttpGet) VFSContainer(org.olat.core.util.vfs.VFSContainer) HttpResponse(org.apache.http.HttpResponse) VFSItem(org.olat.core.util.vfs.VFSItem) URI(java.net.URI) HttpPut(org.apache.http.client.methods.HttpPut) BufferedImage(java.awt.image.BufferedImage) ForumManager(org.olat.modules.fo.manager.ForumManager) ReplyVO(org.olat.modules.fo.restapi.ReplyVO) FileVO(org.olat.restapi.support.vo.FileVO) MessageVO(org.olat.modules.fo.restapi.MessageVO) Test(org.junit.Test)

Example 4 with FileVO

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();
}
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 5 with FileVO

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