Search in sources :

Example 1 with File64VO

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

the class ForumWebService method replyToPost.

private Response replyToPost(Long messageKey, ReplyVO reply, Long authorKey, HttpServletRequest httpRequest, UriInfo uriInfo) {
    Identity identity = getIdentity(httpRequest);
    if (identity == null) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    Identity author;
    if (isAdmin(httpRequest)) {
        if (authorKey == null) {
            author = identity;
        } else {
            author = getMessageAuthor(authorKey, httpRequest);
        }
    } else {
        if (authorKey == null) {
            author = identity;
        } else if (authorKey.equals(identity.getKey())) {
            author = identity;
        } else {
            return Response.serverError().status(Status.UNAUTHORIZED).build();
        }
    }
    // 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();
    }
    // creating the thread (a message without a parent message)
    Message newMessage = fom.createMessage(forum, author, false);
    newMessage.setTitle(reply.getTitle());
    newMessage.setBody(reply.getBody());
    fom.replyToMessage(newMessage, mess);
    if (reply.getAttachments() != null) {
        for (File64VO attachment : reply.getAttachments()) {
            byte[] fileAsBytes = Base64.decodeBase64(attachment.getFile());
            InputStream in = new ByteArrayInputStream(fileAsBytes);
            attachToPost(newMessage, attachment.getFilename(), in, httpRequest);
        }
    }
    MessageVO vo = new MessageVO(newMessage);
    vo.setAttachments(getAttachments(newMessage, uriInfo));
    return Response.ok(vo).build();
}
Also used : Message(org.olat.modules.fo.Message) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) File64VO(org.olat.restapi.support.vo.File64VO) Identity(org.olat.core.id.Identity) RestSecurityHelper.getIdentity(org.olat.restapi.security.RestSecurityHelper.getIdentity)

Example 2 with File64VO

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

the class ForumTest method testUploadAttachmentWithFile64VO.

@Test
public void testUploadAttachmentWithFile64VO() 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
    InputStream portraitStream = CoursesElementsTest.class.getResourceAsStream("portrait.jpg");
    assertNotNull(portraitStream);
    // upload portrait
    URI attachUri = getForumUriBuilder().path("posts").path(message.getKey().toString()).path("attachments").build();
    HttpPut attachMethod = conn.createPut(attachUri, MediaType.APPLICATION_JSON, true);
    attachMethod.addHeader("Content-Type", MediaType.APPLICATION_JSON);
    byte[] portraitBytes = IOUtils.toByteArray(portraitStream);
    byte[] portrait64 = Base64.encodeBase64(portraitBytes, true);
    File64VO fileVo = new File64VO();
    fileVo.setFile(new String(portrait64));
    fileVo.setFilename("portrait64vo.jpg");
    conn.addJsonEntity(attachMethod, fileVo);
    HttpResponse attachCode = conn.execute(attachMethod);
    assertEquals(200, attachCode.getStatusLine().getStatusCode());
    // check if the file exists
    ForumManager fm = ForumManager.getInstance();
    VFSContainer container = fm.getMessageContainer(message.getForumKey(), message.getKey());
    VFSItem uploadedFile = container.resolve("portrait64vo.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);
    conn.shutdown();
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) InputStream(java.io.InputStream) File64VO(org.olat.restapi.support.vo.File64VO) 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) MessageVO(org.olat.modules.fo.restapi.MessageVO) Test(org.junit.Test)

Example 3 with File64VO

use of org.olat.restapi.support.vo.File64VO 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 File64VO

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

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 5 with File64VO

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

the class ForumTest method testUploadAttachmentWithFile64VO.

@Test
public void testUploadAttachmentWithFile64VO() 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
    InputStream portraitStream = CoursesElementsTest.class.getResourceAsStream("portrait.jpg");
    assertNotNull(portraitStream);
    // upload portrait
    URI attachUri = getForumUriBuilder().path("posts").path(message.getKey().toString()).path("attachments").build();
    HttpPut attachMethod = conn.createPut(attachUri, MediaType.APPLICATION_JSON, true);
    attachMethod.addHeader("Content-Type", MediaType.APPLICATION_JSON);
    byte[] portraitBytes = IOUtils.toByteArray(portraitStream);
    byte[] portrait64 = Base64.encodeBase64(portraitBytes, true);
    File64VO fileVo = new File64VO();
    fileVo.setFile(new String(portrait64));
    fileVo.setFilename("portrait64vo.jpg");
    conn.addJsonEntity(attachMethod, fileVo);
    HttpResponse attachCode = conn.execute(attachMethod);
    assertEquals(200, attachCode.getStatusLine().getStatusCode());
    // check if the file exists
    ForumManager fm = ForumManager.getInstance();
    VFSContainer container = fm.getMessageContainer(message.getForumKey(), message.getKey());
    VFSItem uploadedFile = container.resolve("portrait64vo.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);
    conn.shutdown();
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) InputStream(java.io.InputStream) File64VO(org.olat.restapi.support.vo.File64VO) 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) MessageVO(org.olat.modules.fo.restapi.MessageVO) Test(org.junit.Test)

Aggregations

InputStream (java.io.InputStream)6 File64VO (org.olat.restapi.support.vo.File64VO)6 BufferedImage (java.awt.image.BufferedImage)4 URI (java.net.URI)4 HttpResponse (org.apache.http.HttpResponse)4 HttpPut (org.apache.http.client.methods.HttpPut)4 Test (org.junit.Test)4 VFSContainer (org.olat.core.util.vfs.VFSContainer)4 VFSItem (org.olat.core.util.vfs.VFSItem)4 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)4 ForumManager (org.olat.modules.fo.manager.ForumManager)4 MessageVO (org.olat.modules.fo.restapi.MessageVO)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileInputStream (java.io.FileInputStream)2 HttpGet (org.apache.http.client.methods.HttpGet)2 Identity (org.olat.core.id.Identity)2 Message (org.olat.modules.fo.Message)2 ReplyVO (org.olat.modules.fo.restapi.ReplyVO)2 RestSecurityHelper.getIdentity (org.olat.restapi.security.RestSecurityHelper.getIdentity)2 FileVO (org.olat.restapi.support.vo.FileVO)2