Search in sources :

Example 1 with ItemReference

use of org.eclipse.che.api.project.shared.dto.ItemReference in project che by eclipse.

the class ProjectService method search.

@GET
@Path("/search/{path:.*}")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Search for resources", notes = "Search for resources applying a number of search filters as query parameters", response = ItemReference.class, responseContainer = "List")
@ApiResponses({ @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 403, message = "User not authorized to call this operation"), @ApiResponse(code = 404, message = "Not found"), @ApiResponse(code = 409, message = "Conflict error"), @ApiResponse(code = 500, message = "Internal Server Error") })
public List<ItemReference> search(@ApiParam(value = "Path to resource, i.e. where to search?", required = true) @PathParam("path") String path, @ApiParam(value = "Resource name") @QueryParam("name") String name, @ApiParam(value = "Search keywords") @QueryParam("text") String text, @ApiParam(value = "Maximum items to display. If this parameter is dropped, there are no limits") @QueryParam("maxItems") @DefaultValue("-1") int maxItems, @ApiParam(value = "Skip count") @QueryParam("skipCount") int skipCount) throws NotFoundException, ForbiddenException, ConflictException, ServerException {
    final Searcher searcher;
    try {
        searcher = projectManager.getSearcher();
    } catch (NotFoundException e) {
        LOG.warn(e.getLocalizedMessage());
        return Collections.emptyList();
    }
    if (skipCount < 0) {
        throw new ConflictException(String.format("Invalid 'skipCount' parameter: %d.", skipCount));
    }
    final QueryExpression expr = new QueryExpression().setPath(path.startsWith("/") ? path : ('/' + path)).setName(name).setText(text).setMaxItems(maxItems).setSkipCount(skipCount);
    final SearchResult result = searcher.search(expr);
    final List<SearchResultEntry> searchResultEntries = result.getResults();
    final List<ItemReference> items = new ArrayList<>(searchResultEntries.size());
    final FolderEntry root = projectManager.getProjectsRoot();
    for (SearchResultEntry searchResultEntry : searchResultEntries) {
        final VirtualFileEntry child = root.getChild(searchResultEntry.getFilePath());
        if (child != null && child.isFile()) {
            items.add(injectFileLinks(asDto((FileEntry) child)));
        }
    }
    return items;
}
Also used : ItemReference(org.eclipse.che.api.project.shared.dto.ItemReference) ConflictException(org.eclipse.che.api.core.ConflictException) Searcher(org.eclipse.che.api.vfs.search.Searcher) ArrayList(java.util.ArrayList) NotFoundException(org.eclipse.che.api.core.NotFoundException) SearchResult(org.eclipse.che.api.vfs.search.SearchResult) QueryExpression(org.eclipse.che.api.vfs.search.QueryExpression) SearchResultEntry(org.eclipse.che.api.vfs.search.SearchResultEntry) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with ItemReference

use of org.eclipse.che.api.project.shared.dto.ItemReference in project che by eclipse.

the class ProjectServiceTest method testSearchByNameAndText.

@SuppressWarnings("unchecked")
@Test
public void testSearchByNameAndText() throws Exception {
    RegisteredProject myProject = pm.getProject("my_project");
    myProject.getBaseFolder().createFolder("a/b").createFile("test.txt", "test".getBytes(Charset.defaultCharset()));
    myProject.getBaseFolder().createFolder("x/y").createFile("test.txt", "test".getBytes(Charset.defaultCharset()));
    myProject.getBaseFolder().createFolder("c").createFile("test", "test".getBytes(Charset.defaultCharset()));
    ContainerResponse response = launcher.service(GET, "http://localhost:8080/api/project/search/my_project?text=test&name=test.txt", "http://localhost:8080/api", null, null, null);
    assertEquals(response.getStatus(), 200, "Error: " + response.getEntity());
    List<ItemReference> result = (List<ItemReference>) response.getEntity();
    assertEquals(result.size(), 2);
    assertEqualsNoOrder(new Object[] { result.get(0).getPath(), result.get(1).getPath() }, new Object[] { "/my_project/a/b/test.txt", "/my_project/x/y/test.txt" });
}
Also used : ItemReference(org.eclipse.che.api.project.shared.dto.ItemReference) ContainerResponse(org.everrest.core.impl.ContainerResponse) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Test(org.testng.annotations.Test)

Example 3 with ItemReference

use of org.eclipse.che.api.project.shared.dto.ItemReference in project che by eclipse.

the class ProjectServiceTest method testSearchWordWithAnyEnding.

@SuppressWarnings("unchecked")
@Test
public void testSearchWordWithAnyEnding() throws Exception {
    String queryToSearch = "?text=" + "question" + URL_ENCODED_ASTERISK;
    RegisteredProject myProject = pm.getProject("my_project");
    myProject.getBaseFolder().createFolder("x/y").createFile("containsSearchText.txt", "To be or not to be that is the question".getBytes(Charset.defaultCharset()));
    myProject.getBaseFolder().createFolder("a/b").createFile("containsSearchTextAlso.txt", "Pay attention! To be or not to be that is the questionS".getBytes(Charset.defaultCharset()));
    myProject.getBaseFolder().createFolder("c").createFile("notContainsSearchText", "Pay attention! To be or to not be that is the questEon".getBytes(Charset.defaultCharset()));
    ContainerResponse response = launcher.service(GET, "http://localhost:8080/api/project/search/my_project" + queryToSearch, "http://localhost:8080/api", null, null, null);
    assertEquals(response.getStatus(), 200, "Error: " + response.getEntity());
    List<ItemReference> result = (List<ItemReference>) response.getEntity();
    assertEquals(result.size(), 2);
    Set<String> paths = new LinkedHashSet<>(2);
    paths.addAll(result.stream().map(ItemReference::getPath).collect(Collectors.toList()));
    Assert.assertTrue(paths.contains("/my_project/x/y/containsSearchText.txt"));
    Assert.assertTrue(paths.contains("/my_project/a/b/containsSearchTextAlso.txt"));
    Assert.assertFalse(paths.contains("/my_project/c/notContainsSearchText.txt"));
}
Also used : ItemReference(org.eclipse.che.api.project.shared.dto.ItemReference) LinkedHashSet(java.util.LinkedHashSet) ContainerResponse(org.everrest.core.impl.ContainerResponse) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Test(org.testng.annotations.Test)

Example 4 with ItemReference

use of org.eclipse.che.api.project.shared.dto.ItemReference in project che by eclipse.

the class ProjectServiceTest method testGetTreeWithDepthAndIncludeFilesNoFiles.

@Test
public void testGetTreeWithDepthAndIncludeFilesNoFiles() throws Exception {
    RegisteredProject myProject = pm.getProject("my_project");
    FolderEntry a = myProject.getBaseFolder().createFolder("a");
    a.createFolder("b/c");
    a.createFolder("x");
    ContainerResponse response = launcher.service(GET, "http://localhost:8080/api/project/tree/my_project/a?depth=100&includeFiles=true", "http://localhost:8080/api", null, null, null);
    assertEquals(response.getStatus(), 200, "Error: " + response.getEntity());
    TreeElement tree = (TreeElement) response.getEntity();
    ItemReference a_node = tree.getNode();
    assertEquals(a_node.getName(), "a");
    List<TreeElement> children = tree.getChildren();
    assertNotNull(children);
    Set<String> names = new LinkedHashSet<>(4);
    for (TreeElement subTree : children) {
        ItemReference _node = subTree.getNode();
        validateFolderLinks(_node);
        String name = _node.getName();
        names.add(name);
        for (TreeElement subSubTree : subTree.getChildren()) {
            ItemReference __node = subSubTree.getNode();
            validateFolderLinks(__node);
            names.add(name + "/" + __node.getName());
        }
    }
    Assert.assertTrue(names.contains("b"));
    Assert.assertTrue(names.contains("x"));
    Assert.assertTrue(names.contains("b/c"));
    Assert.assertFalse(names.contains("x/test.txt"));
}
Also used : ItemReference(org.eclipse.che.api.project.shared.dto.ItemReference) LinkedHashSet(java.util.LinkedHashSet) ContainerResponse(org.everrest.core.impl.ContainerResponse) TreeElement(org.eclipse.che.api.project.shared.dto.TreeElement) Test(org.testng.annotations.Test)

Example 5 with ItemReference

use of org.eclipse.che.api.project.shared.dto.ItemReference in project che by eclipse.

the class ProjectServiceTest method testCreateFolder.

@Test
public void testCreateFolder() throws Exception {
    ContainerResponse response = launcher.service(POST, "http://localhost:8080/api/project/folder/my_project/test", "http://localhost:8080/api", null, null, null);
    assertEquals(response.getStatus(), 201, "Error: " + response.getEntity());
    ItemReference fileItem = (ItemReference) response.getEntity();
    assertEquals(fileItem.getName(), "test");
    assertEquals(fileItem.getPath(), "/my_project/test");
    validateFolderLinks(fileItem);
    assertEquals(response.getHttpHeaders().getFirst("Location"), URI.create("http://localhost:8080/api/project/children/my_project/test"));
    VirtualFileEntry folder = pm.getProject("my_project").getBaseFolder().getChild("test");
    Assert.assertTrue(folder.isFolder());
}
Also used : ItemReference(org.eclipse.che.api.project.shared.dto.ItemReference) ContainerResponse(org.everrest.core.impl.ContainerResponse) Test(org.testng.annotations.Test)

Aggregations

ItemReference (org.eclipse.che.api.project.shared.dto.ItemReference)26 Test (org.testng.annotations.Test)22 ContainerResponse (org.everrest.core.impl.ContainerResponse)20 ArrayList (java.util.ArrayList)14 LinkedHashSet (java.util.LinkedHashSet)13 List (java.util.List)13 Collections.singletonList (java.util.Collections.singletonList)11 LinkedList (java.util.LinkedList)11 TreeElement (org.eclipse.che.api.project.shared.dto.TreeElement)4 Link (org.eclipse.che.api.core.rest.shared.dto.Link)2 UiHandler (com.google.gwt.uibinder.client.UiHandler)1 Command (com.google.gwt.user.client.Command)1 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 ConflictException (org.eclipse.che.api.core.ConflictException)1 NotFoundException (org.eclipse.che.api.core.NotFoundException)1 Function (org.eclipse.che.api.promises.client.Function)1