@Test public void testQueryWithPath() { dataSourceProperties.put("query", TEST_PATH + "variant"); dataSourceProperties.put("rootPath", TEST_PATH); dataSourceProperties.put("path", TEST_PATH); dataSourceProperties.put("filter", Filter.folderOrProductOrVariant); children.add(mockVariantResource()); children.add(mockFolderResource()); servlet.doGet(request, response); DataSource dataSource = (DataSource) request.getAttribute(DataSource.class.getName()); assertNotNull(dataSource); assertTrue(dataSource.iterator().hasNext()); assertTrue(dataSource.iterator().next().getName().startsWith("variant")); assertEquals(1, IteratorUtils.toList(dataSource.iterator()).size()); }
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) { final SlingScriptHelper sling = getScriptHelper(request); final ExpressionHelper ex = new ExpressionHelper(sling.getService(ExpressionResolver.class), request); final Config dsCfg = new Config(request.getResource().getChild(Config.DATASOURCE)); final CommerceBasePathsService cbps = sling.getService(CommerceBasePathsService.class); final String query = ex.getString(dsCfg.get("query", String.class)); final String parentPath; final String searchName; if (query != null) { final String rootPath = ex.getString(dsCfg.get("rootPath", cbps.getProductsBasePath())); final int slashIndex = query.lastIndexOf('/'); if (slashIndex < 0) { parentPath = rootPath; searchName = query.toLowerCase(); } else if (!query.startsWith(rootPath)) { parentPath = rootPath; searchName = null; } else if (slashIndex == query.length() - 1) { parentPath = query; searchName = null; } else { parentPath = query.substring(0, slashIndex + 1); searchName = query.substring(slashIndex + 1).toLowerCase(); } } else { parentPath = ex.getString(dsCfg.get("path", String.class)); searchName = null; } final Resource parent = request.getResourceResolver().getResource(parentPath); final DataSource ds; if (parent == null) { ds = EmptyDataSource.instance(); } else { final Integer offset = ex.get(dsCfg.get("offset", String.class), Integer.class); final Integer limit = ex.get(dsCfg.get("limit", String.class), Integer.class); final String itemRT = dsCfg.get("itemResourceType", String.class); final String filter = ex.getString(dsCfg.get("filter", String.class)); final Collection<Predicate> predicates = new ArrayList<>(2); predicates.add(Filter.getFilter(filter)); if (searchName != null) { final Pattern searchNamePattern = Pattern.compile(Pattern.quote(searchName), Pattern.CASE_INSENSITIVE); predicates.add(o -> searchNamePattern.matcher(((Resource) o).getName()).lookingAt()); } final Predicate predicate = PredicateUtils.allPredicate(predicates); final Transformer transformer = createTransformer(itemRT, predicate); @SuppressWarnings("unchecked") DataSource datasource = new AbstractDataSource() { public Iterator<Resource> iterator() { final Iterator<Resource> iterator; if (Filter.product.name().equals(filter)) { Transformer transformer = resource -> { if (Filter.product.evaluate(resource)) { return resource; } else { return ((Resource) resource).listChildren(); } }; iterator = new ObjectGraphIterator(parent, transformer); } else { iterator = parent.listChildren(); } FilterIterator filterIterator = new FilterIterator(iterator, predicate); PagingIterator<Resource> pagingIterator = new PagingIterator<>(filterIterator, offset, limit); return new TransformIterator(pagingIterator, transformer); } }; ds = datasource; } request.setAttribute(DataSource.class.getName(), ds); }
ChildrenDataSourceServlet extends SlingSafeMethodsServlet { protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) { final SlingScriptHelper sling = getScriptHelper(request); final ExpressionHelper ex = new ExpressionHelper(sling.getService(ExpressionResolver.class), request); final Config dsCfg = new Config(request.getResource().getChild(Config.DATASOURCE)); final CommerceBasePathsService cbps = sling.getService(CommerceBasePathsService.class); final String query = ex.getString(dsCfg.get("query", String.class)); final String parentPath; final String searchName; if (query != null) { final String rootPath = ex.getString(dsCfg.get("rootPath", cbps.getProductsBasePath())); final int slashIndex = query.lastIndexOf('/'); if (slashIndex < 0) { parentPath = rootPath; searchName = query.toLowerCase(); } else if (!query.startsWith(rootPath)) { parentPath = rootPath; searchName = null; } else if (slashIndex == query.length() - 1) { parentPath = query; searchName = null; } else { parentPath = query.substring(0, slashIndex + 1); searchName = query.substring(slashIndex + 1).toLowerCase(); } } else { parentPath = ex.getString(dsCfg.get("path", String.class)); searchName = null; } final Resource parent = request.getResourceResolver().getResource(parentPath); final DataSource ds; if (parent == null) { ds = EmptyDataSource.instance(); } else { final Integer offset = ex.get(dsCfg.get("offset", String.class), Integer.class); final Integer limit = ex.get(dsCfg.get("limit", String.class), Integer.class); final String itemRT = dsCfg.get("itemResourceType", String.class); final String filter = ex.getString(dsCfg.get("filter", String.class)); final Collection<Predicate> predicates = new ArrayList<>(2); predicates.add(Filter.getFilter(filter)); if (searchName != null) { final Pattern searchNamePattern = Pattern.compile(Pattern.quote(searchName), Pattern.CASE_INSENSITIVE); predicates.add(o -> searchNamePattern.matcher(((Resource) o).getName()).lookingAt()); } final Predicate predicate = PredicateUtils.allPredicate(predicates); final Transformer transformer = createTransformer(itemRT, predicate); @SuppressWarnings("unchecked") DataSource datasource = new AbstractDataSource() { public Iterator<Resource> iterator() { final Iterator<Resource> iterator; if (Filter.product.name().equals(filter)) { Transformer transformer = resource -> { if (Filter.product.evaluate(resource)) { return resource; } else { return ((Resource) resource).listChildren(); } }; iterator = new ObjectGraphIterator(parent, transformer); } else { iterator = parent.listChildren(); } FilterIterator filterIterator = new FilterIterator(iterator, predicate); PagingIterator<Resource> pagingIterator = new PagingIterator<>(filterIterator, offset, limit); return new TransformIterator(pagingIterator, transformer); } }; ds = datasource; } request.setAttribute(DataSource.class.getName(), ds); } }
ChildrenDataSourceServlet extends SlingSafeMethodsServlet { protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) { final SlingScriptHelper sling = getScriptHelper(request); final ExpressionHelper ex = new ExpressionHelper(sling.getService(ExpressionResolver.class), request); final Config dsCfg = new Config(request.getResource().getChild(Config.DATASOURCE)); final CommerceBasePathsService cbps = sling.getService(CommerceBasePathsService.class); final String query = ex.getString(dsCfg.get("query", String.class)); final String parentPath; final String searchName; if (query != null) { final String rootPath = ex.getString(dsCfg.get("rootPath", cbps.getProductsBasePath())); final int slashIndex = query.lastIndexOf('/'); if (slashIndex < 0) { parentPath = rootPath; searchName = query.toLowerCase(); } else if (!query.startsWith(rootPath)) { parentPath = rootPath; searchName = null; } else if (slashIndex == query.length() - 1) { parentPath = query; searchName = null; } else { parentPath = query.substring(0, slashIndex + 1); searchName = query.substring(slashIndex + 1).toLowerCase(); } } else { parentPath = ex.getString(dsCfg.get("path", String.class)); searchName = null; } final Resource parent = request.getResourceResolver().getResource(parentPath); final DataSource ds; if (parent == null) { ds = EmptyDataSource.instance(); } else { final Integer offset = ex.get(dsCfg.get("offset", String.class), Integer.class); final Integer limit = ex.get(dsCfg.get("limit", String.class), Integer.class); final String itemRT = dsCfg.get("itemResourceType", String.class); final String filter = ex.getString(dsCfg.get("filter", String.class)); final Collection<Predicate> predicates = new ArrayList<>(2); predicates.add(Filter.getFilter(filter)); if (searchName != null) { final Pattern searchNamePattern = Pattern.compile(Pattern.quote(searchName), Pattern.CASE_INSENSITIVE); predicates.add(o -> searchNamePattern.matcher(((Resource) o).getName()).lookingAt()); } final Predicate predicate = PredicateUtils.allPredicate(predicates); final Transformer transformer = createTransformer(itemRT, predicate); @SuppressWarnings("unchecked") DataSource datasource = new AbstractDataSource() { public Iterator<Resource> iterator() { final Iterator<Resource> iterator; if (Filter.product.name().equals(filter)) { Transformer transformer = resource -> { if (Filter.product.evaluate(resource)) { return resource; } else { return ((Resource) resource).listChildren(); } }; iterator = new ObjectGraphIterator(parent, transformer); } else { iterator = parent.listChildren(); } FilterIterator filterIterator = new FilterIterator(iterator, predicate); PagingIterator<Resource> pagingIterator = new PagingIterator<>(filterIterator, offset, limit); return new TransformIterator(pagingIterator, transformer); } }; ds = datasource; } request.setAttribute(DataSource.class.getName(), ds); }  }
ChildrenDataSourceServlet extends SlingSafeMethodsServlet { protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) { final SlingScriptHelper sling = getScriptHelper(request); final ExpressionHelper ex = new ExpressionHelper(sling.getService(ExpressionResolver.class), request); final Config dsCfg = new Config(request.getResource().getChild(Config.DATASOURCE)); final CommerceBasePathsService cbps = sling.getService(CommerceBasePathsService.class); final String query = ex.getString(dsCfg.get("query", String.class)); final String parentPath; final String searchName; if (query != null) { final String rootPath = ex.getString(dsCfg.get("rootPath", cbps.getProductsBasePath())); final int slashIndex = query.lastIndexOf('/'); if (slashIndex < 0) { parentPath = rootPath; searchName = query.toLowerCase(); } else if (!query.startsWith(rootPath)) { parentPath = rootPath; searchName = null; } else if (slashIndex == query.length() - 1) { parentPath = query; searchName = null; } else { parentPath = query.substring(0, slashIndex + 1); searchName = query.substring(slashIndex + 1).toLowerCase(); } } else { parentPath = ex.getString(dsCfg.get("path", String.class)); searchName = null; } final Resource parent = request.getResourceResolver().getResource(parentPath); final DataSource ds; if (parent == null) { ds = EmptyDataSource.instance(); } else { final Integer offset = ex.get(dsCfg.get("offset", String.class), Integer.class); final Integer limit = ex.get(dsCfg.get("limit", String.class), Integer.class); final String itemRT = dsCfg.get("itemResourceType", String.class); final String filter = ex.getString(dsCfg.get("filter", String.class)); final Collection<Predicate> predicates = new ArrayList<>(2); predicates.add(Filter.getFilter(filter)); if (searchName != null) { final Pattern searchNamePattern = Pattern.compile(Pattern.quote(searchName), Pattern.CASE_INSENSITIVE); predicates.add(o -> searchNamePattern.matcher(((Resource) o).getName()).lookingAt()); } final Predicate predicate = PredicateUtils.allPredicate(predicates); final Transformer transformer = createTransformer(itemRT, predicate); @SuppressWarnings("unchecked") DataSource datasource = new AbstractDataSource() { public Iterator<Resource> iterator() { final Iterator<Resource> iterator; if (Filter.product.name().equals(filter)) { Transformer transformer = resource -> { if (Filter.product.evaluate(resource)) { return resource; } else { return ((Resource) resource).listChildren(); } }; iterator = new ObjectGraphIterator(parent, transformer); } else { iterator = parent.listChildren(); } FilterIterator filterIterator = new FilterIterator(iterator, predicate); PagingIterator<Resource> pagingIterator = new PagingIterator<>(filterIterator, offset, limit); return new TransformIterator(pagingIterator, transformer); } }; ds = datasource; } request.setAttribute(DataSource.class.getName(), ds); }   }
ChildrenDataSourceServlet extends SlingSafeMethodsServlet { protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) { final SlingScriptHelper sling = getScriptHelper(request); final ExpressionHelper ex = new ExpressionHelper(sling.getService(ExpressionResolver.class), request); final Config dsCfg = new Config(request.getResource().getChild(Config.DATASOURCE)); final CommerceBasePathsService cbps = sling.getService(CommerceBasePathsService.class); final String query = ex.getString(dsCfg.get("query", String.class)); final String parentPath; final String searchName; if (query != null) { final String rootPath = ex.getString(dsCfg.get("rootPath", cbps.getProductsBasePath())); final int slashIndex = query.lastIndexOf('/'); if (slashIndex < 0) { parentPath = rootPath; searchName = query.toLowerCase(); } else if (!query.startsWith(rootPath)) { parentPath = rootPath; searchName = null; } else if (slashIndex == query.length() - 1) { parentPath = query; searchName = null; } else { parentPath = query.substring(0, slashIndex + 1); searchName = query.substring(slashIndex + 1).toLowerCase(); } } else { parentPath = ex.getString(dsCfg.get("path", String.class)); searchName = null; } final Resource parent = request.getResourceResolver().getResource(parentPath); final DataSource ds; if (parent == null) { ds = EmptyDataSource.instance(); } else { final Integer offset = ex.get(dsCfg.get("offset", String.class), Integer.class); final Integer limit = ex.get(dsCfg.get("limit", String.class), Integer.class); final String itemRT = dsCfg.get("itemResourceType", String.class); final String filter = ex.getString(dsCfg.get("filter", String.class)); final Collection<Predicate> predicates = new ArrayList<>(2); predicates.add(Filter.getFilter(filter)); if (searchName != null) { final Pattern searchNamePattern = Pattern.compile(Pattern.quote(searchName), Pattern.CASE_INSENSITIVE); predicates.add(o -> searchNamePattern.matcher(((Resource) o).getName()).lookingAt()); } final Predicate predicate = PredicateUtils.allPredicate(predicates); final Transformer transformer = createTransformer(itemRT, predicate); @SuppressWarnings("unchecked") DataSource datasource = new AbstractDataSource() { public Iterator<Resource> iterator() { final Iterator<Resource> iterator; if (Filter.product.name().equals(filter)) { Transformer transformer = resource -> { if (Filter.product.evaluate(resource)) { return resource; } else { return ((Resource) resource).listChildren(); } }; iterator = new ObjectGraphIterator(parent, transformer); } else { iterator = parent.listChildren(); } FilterIterator filterIterator = new FilterIterator(iterator, predicate); PagingIterator<Resource> pagingIterator = new PagingIterator<>(filterIterator, offset, limit); return new TransformIterator(pagingIterator, transformer); } }; ds = datasource; } request.setAttribute(DataSource.class.getName(), ds); }    }