Search in sources :

Example 6 with ItemReference

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

the class ResourceManager method search.

protected Promise<Resource[]> search(final Container container, String fileMask, String contentMask) {
    QueryExpression queryExpression = new QueryExpression();
    if (!isNullOrEmpty(contentMask)) {
        queryExpression.setText(contentMask);
    }
    if (!isNullOrEmpty(fileMask)) {
        queryExpression.setName(fileMask);
    }
    if (!container.getLocation().isRoot()) {
        queryExpression.setPath(container.getLocation().toString());
    }
    return ps.search(queryExpression).thenPromise(new Function<List<ItemReference>, Promise<Resource[]>>() {

        @Override
        public Promise<Resource[]> apply(final List<ItemReference> references) throws FunctionException {
            if (references.isEmpty()) {
                return promises.resolve(NO_RESOURCES);
            }
            int maxDepth = 0;
            final Path[] paths = new Path[references.size()];
            for (int i = 0; i < paths.length; i++) {
                final Path path = Path.valueOf(references.get(i).getPath());
                paths[i] = path;
                if (path.segmentCount() > maxDepth) {
                    maxDepth = path.segmentCount();
                }
            }
            return getRemoteResources(container, maxDepth, true).then(new Function<Resource[], Resource[]>() {

                @Override
                public Resource[] apply(Resource[] resources) throws FunctionException {
                    Resource[] filtered = NO_RESOURCES;
                    Path[] mutablePaths = paths;
                    outer: for (Resource resource : resources) {
                        if (resource.getResourceType() != FILE) {
                            continue;
                        }
                        for (int i = 0; i < mutablePaths.length; i++) {
                            Path path = mutablePaths[i];
                            if (path.segmentCount() == resource.getLocation().segmentCount() && path.equals(resource.getLocation())) {
                                Resource[] tmpFiltered = copyOf(filtered, filtered.length + 1);
                                tmpFiltered[filtered.length] = resource;
                                filtered = tmpFiltered;
                                //reduce the size of mutablePaths by removing already checked item
                                int size = mutablePaths.length;
                                int numMoved = mutablePaths.length - i - 1;
                                if (numMoved > 0) {
                                    arraycopy(mutablePaths, i + 1, mutablePaths, i, numMoved);
                                }
                                mutablePaths = copyOf(mutablePaths, --size);
                                continue outer;
                            }
                        }
                    }
                    return filtered;
                }
            });
        }
    });
}
Also used : Path(org.eclipse.che.ide.resource.Path) Resource(org.eclipse.che.ide.api.resources.Resource) FunctionException(org.eclipse.che.api.promises.client.FunctionException) ItemReference(org.eclipse.che.api.project.shared.dto.ItemReference) Promise(org.eclipse.che.api.promises.client.Promise) Function(org.eclipse.che.api.promises.client.Function) List(java.util.List) ArrayList(java.util.ArrayList) QueryExpression(org.eclipse.che.ide.api.project.QueryExpression)

Example 7 with ItemReference

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

the class ProjectServiceLinksInjectorTest method verifyFileLinks.

@Test
public void verifyFileLinks() throws Exception {
    ItemReference itemReference = DtoFactory.newDto(ItemReference.class);
    itemReference.withPath(FILE_PATH);
    ItemReference result = projectServiceLinksInjector.injectFileLinks(itemReference, serviceContext);
    assertEquals(3, result.getLinks().size());
    Link updateLink = result.getLink("update content");
    assertNotNull(updateLink);
    assertEquals("localhost:8080/project/file/project1/folder/file", updateLink.getHref());
    assertEquals(HttpMethod.PUT, updateLink.getMethod());
    assertEquals(LINK_REL_UPDATE_CONTENT, updateLink.getRel());
    assertEquals(null, updateLink.getProduces());
    assertEquals(MediaType.WILDCARD, updateLink.getConsumes());
    Link getContentLink = result.getLink("get content");
    assertNotNull(getContentLink);
    assertEquals("localhost:8080/project/file/project1/folder/file", getContentLink.getHref());
    assertEquals(HttpMethod.GET, getContentLink.getMethod());
    assertEquals(LINK_REL_GET_CONTENT, getContentLink.getRel());
    assertEquals(APPLICATION_JSON, getContentLink.getProduces());
    Link deleteLink = result.getLink("delete");
    assertNotNull(deleteLink);
    assertEquals("localhost:8080/project/project1/folder/file", deleteLink.getHref());
    assertEquals(HttpMethod.DELETE, deleteLink.getMethod());
    assertEquals(LINK_REL_DELETE, deleteLink.getRel());
}
Also used : ItemReference(org.eclipse.che.api.project.shared.dto.ItemReference) Link(org.eclipse.che.api.core.rest.shared.dto.Link) Test(org.testng.annotations.Test)

Example 8 with ItemReference

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

the class ProjectServiceTest method testSearchFromWSRoot.

@SuppressWarnings("unchecked")
@Test
public void testSearchFromWSRoot() throws Exception {
    RegisteredProject myProject = pm.getProject("my_project");
    myProject.getBaseFolder().createFolder("a/b").createFile("test", "test".getBytes(Charset.defaultCharset()));
    myProject.getBaseFolder().createFolder("x/y").createFile("test", "test".getBytes(Charset.defaultCharset()));
    myProject.getBaseFolder().createFolder("c").createFile("test.txt", "test".getBytes(Charset.defaultCharset()));
    ContainerResponse response = launcher.service(GET, "http://localhost:8080/api/project/search/?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(), 1);
    Assert.assertTrue(result.get(0).getPath().equals("/my_project/c/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 9 with ItemReference

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

the class ProjectServiceTest method testSearchByName.

@SuppressWarnings("unchecked")
@Test
public void testSearchByName() throws Exception {
    RegisteredProject myProject = pm.getProject("my_project");
    myProject.getBaseFolder().createFolder("a/b").createFile("test.txt", "hello".getBytes(Charset.defaultCharset()));
    myProject.getBaseFolder().createFolder("x/y").createFile("test.txt", "test".getBytes(Charset.defaultCharset()));
    myProject.getBaseFolder().createFolder("c").createFile("exclude", "test".getBytes(Charset.defaultCharset()));
    ContainerResponse response = launcher.service(GET, "http://localhost:8080/api/project/search/my_project?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);
    Set<String> paths = new LinkedHashSet<>(2);
    for (ItemReference itemReference : result) {
        paths.add(itemReference.getPath());
    }
    Assert.assertTrue(paths.contains("/my_project/a/b/test.txt"));
    Assert.assertTrue(paths.contains("/my_project/x/y/test.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 10 with ItemReference

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

the class ProjectServiceTest method testSearchByTextWhenFileWasNotIndexed.

@SuppressWarnings("unchecked")
@Test
public void testSearchByTextWhenFileWasNotIndexed() throws Exception {
    RegisteredProject myProject = pm.getProject("my_project");
    myProject.getBaseFolder().createFolder("a/b").createFile("test.txt", "hello".getBytes(Charset.defaultCharset()));
    myProject.getBaseFolder().createFolder("x/y").createFile("__test.txt", "searchhit".getBytes(Charset.defaultCharset()));
    myProject.getBaseFolder().createFolder(EXCLUDE_SEARCH_PATH).createFile("_test", "searchhit".getBytes(Charset.defaultCharset()));
    ContainerResponse response = launcher.service(GET, "http://localhost:8080/api/project/search/my_project?text=searchhit", "http://localhost:8080/api", null, null, null);
    assertEquals(response.getStatus(), 200, "Error: " + response.getEntity());
    List<ItemReference> result = (List<ItemReference>) response.getEntity();
    assertEquals(result.size(), 1);
    Set<String> paths = new LinkedHashSet<>(1);
    paths.addAll(result.stream().map(ItemReference::getPath).collect(Collectors.toList()));
    Assert.assertTrue(paths.contains("/my_project/x/y/__test.txt"));
    Assert.assertFalse(paths.contains("/my_project/" + EXCLUDE_SEARCH_PATH + "/_test"));
}
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)

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