Search in sources :

Example 11 with FileVO

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

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 12 with FileVO

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

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 13 with FileVO

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

the class GroupFoldersTest method testGetSubFolder.

@Test
public void testGetSubFolder() throws IOException, URISyntaxException {
    // create some sub folders
    CollaborationTools collabTools1 = CollaborationToolsFactory.getInstance().getOrCreateCollaborationTools(g1);
    String folderRelPath = collabTools1.getFolderRelPath();
    OlatRootFolderImpl folder = new OlatRootFolderImpl(folderRelPath, null);
    VFSContainer newFolder1 = folder.createChildContainer("New folder 1");
    if (newFolder1 == null) {
        newFolder1 = (VFSContainer) folder.resolve("New folder 1");
    }
    assertNotNull(newFolder1);
    VFSContainer newFolder11 = newFolder1.createChildContainer("New folder 1_1");
    if (newFolder11 == null) {
        newFolder11 = (VFSContainer) newFolder1.resolve("New folder 1_1");
    }
    assertNotNull(newFolder11);
    assertTrue(conn.login("rest-one", "A6B7C8"));
    // get root folder
    URI request0 = UriBuilder.fromUri(getContextURI()).path("/groups/" + g1.getKey() + "/folder/").build();
    HttpGet method0 = conn.createGet(request0, MediaType.APPLICATION_JSON, true);
    HttpResponse code0 = conn.execute(method0);
    assertEquals(200, code0.getStatusLine().getStatusCode());
    InputStream body0 = code0.getEntity().getContent();
    assertNotNull(body0);
    List<FileVO> fileVos0 = parseFileArray(body0);
    assertNotNull(fileVos0);
    assertEquals(1, fileVos0.size());
    // get sub folder
    URI request1 = UriBuilder.fromUri(getContextURI()).path("/groups/" + g1.getKey() + "/folder/New_folder_1").build();
    HttpGet method1 = conn.createGet(request1, MediaType.APPLICATION_JSON, true);
    HttpResponse code1 = conn.execute(method1);
    assertEquals(200, code1.getStatusLine().getStatusCode());
    InputStream body1 = code1.getEntity().getContent();
    assertNotNull(body1);
    List<FileVO> fileVos1 = parseFileArray(body1);
    assertNotNull(fileVos1);
    assertEquals(1, fileVos1.size());
    // get sub folder by link
    FileVO fileVO = fileVos1.get(0);
    URI fileUri = new URI(fileVO.getHref());
    HttpGet brutMethod = conn.createGet(fileUri, "*/*", true);
    brutMethod.addHeader("Accept", MediaType.APPLICATION_JSON);
    HttpResponse codeBrut = conn.execute(brutMethod);
    assertEquals(200, codeBrut.getStatusLine().getStatusCode());
    EntityUtils.consume(codeBrut.getEntity());
    // get sub sub folder
    URI request2 = UriBuilder.fromUri(getContextURI()).path("/groups/" + g1.getKey() + "/folder/New_folder_1/New_folder_1_1").build();
    HttpGet method2 = conn.createGet(request2, MediaType.APPLICATION_JSON, true);
    HttpResponse code2 = conn.execute(method2);
    assertEquals(200, code2.getStatusLine().getStatusCode());
    InputStream body2 = code2.getEntity().getContent();
    assertNotNull(body2);
    EntityUtils.consume(code2.getEntity());
    // get sub folder with end /
    URI request3 = UriBuilder.fromUri(getContextURI()).path("/groups/" + g1.getKey() + "/folder/New_folder_1/").build();
    HttpGet method3 = conn.createGet(request3, MediaType.APPLICATION_JSON, true);
    HttpResponse code3 = conn.execute(method3);
    assertEquals(200, code3.getStatusLine().getStatusCode());
    InputStream body3 = code3.getEntity().getContent();
    assertNotNull(body3);
    List<FileVO> fileVos3 = parseFileArray(body3);
    assertNotNull(fileVos3);
    assertEquals(1, fileVos3.size());
}
Also used : OlatRootFolderImpl(org.olat.core.commons.modules.bc.vfs.OlatRootFolderImpl) InputStream(java.io.InputStream) VFSContainer(org.olat.core.util.vfs.VFSContainer) HttpGet(org.apache.http.client.methods.HttpGet) CollaborationTools(org.olat.collaboration.CollaborationTools) FileVO(org.olat.restapi.support.vo.FileVO) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) Test(org.junit.Test)

Example 14 with FileVO

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

the class GroupFoldersTest method testCreateFoldersWithSpecialCharacter.

// @Test not working -> Jersey ignore the request and return 200 (why?)
public void testCreateFoldersWithSpecialCharacter() throws IOException, URISyntaxException {
    assertTrue(conn.login("rest-one", "A6B7C8"));
    URI request = UriBuilder.fromUri(getContextURI()).path("/groups/" + g1.getKey() + "/folder/New_folder_1/New_folder_1_1/New_folder_1 1 2").build();
    HttpPut method = conn.createPut(request, MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(method);
    assertEquals(200, response.getStatusLine().getStatusCode());
    FileVO file = conn.parse(response, FileVO.class);
    assertNotNull(file);
}
Also used : FileVO(org.olat.restapi.support.vo.FileVO) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) HttpPut(org.apache.http.client.methods.HttpPut)

Example 15 with FileVO

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

the class GroupFoldersTest method testCreateFoldersWithSpecialCharacter2.

@Test
public void testCreateFoldersWithSpecialCharacter2() throws IOException, URISyntaxException {
    assertTrue(conn.login("rest-one", "A6B7C8"));
    URI request = UriBuilder.fromUri(getContextURI()).path("/groups/" + g1.getKey() + "/folder/New_folder_1/New_folder_1_1/").build();
    HttpPut method = conn.createPut(request, MediaType.APPLICATION_JSON, true);
    HttpEntity entity = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE).addTextBody("foldername", "New folder 1 2 3").build();
    method.setEntity(entity);
    HttpResponse response = conn.execute(method);
    assertEquals(200, response.getStatusLine().getStatusCode());
    FileVO file = conn.parse(response, FileVO.class);
    assertNotNull(file);
    assertNotNull(file.getHref());
    assertNotNull(file.getTitle());
    assertEquals("New folder 1 2 3", file.getTitle());
}
Also used : HttpEntity(org.apache.http.HttpEntity) FileVO(org.olat.restapi.support.vo.FileVO) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) HttpPut(org.apache.http.client.methods.HttpPut) 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