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