private void writeNode( SlingHttpServletRequest request, JSONWriter write, Aggregator aggregator, Row row) throws JSONException, RepositoryException { Session session = request.getResourceResolver().adaptTo(Session.class); Node node = RowUtils.getNode(row, session); Node siteNode = node; boolean foundSite = false; while (!siteNode.getPath().equals("/")) { if (siteService.isSite(siteNode)) { foundSite = true; break; } siteNode = siteNode.getParent(); } if (foundSite) { if (node.hasProperty(SLING_RESOURCE_TYPE_PROPERTY)) { String type = node.getProperty(SLING_RESOURCE_TYPE_PROPERTY).getString(); // From looking at the type we determine how we should represent this node. SearchResultProcessor processor = tracker.getSearchResultProcessorByType(type); if (processor != null) { write.object(); write.key("path"); write.value(node.getPath()); write.key("site"); siteSearchResultProcessor.writeNode(write, siteNode); write.key("type"); write.value(node.getProperty(SLING_RESOURCE_TYPE_PROPERTY).getString()); write.key("excerpt"); write.value(RowUtils.getDefaultExcerpt(row)); write.key("data"); processor.writeNode(request, write, aggregator, row); write.endObject(); } else { // No processor found, just dump the properties writeDefaultNode(write, aggregator, row, siteNode, session); } } else { // No type, just dump the properties writeDefaultNode(write, aggregator, row, siteNode, session); } } }
protected void simpleResultCountCheck(SearchResultProcessor processor) throws RepositoryException, JSONException { int itemCount = 12; QueryResult queryResult = createMock(QueryResult.class); RowIterator results = createMock(RowIterator.class); expect(queryResult.getRows()).andReturn(results); expect(results.getSize()).andReturn(500L).anyTimes(); Row dummyRow = createMock(Row.class); Value val = createMock(Value.class); expect(val.getString()).andReturn("").times(itemCount); expect(dummyRow.getValue("jcr:path")).andReturn(val).times(itemCount); expect(results.hasNext()).andReturn(true).anyTimes(); expect(results.nextRow()).andReturn(dummyRow).times(itemCount); SlingHttpServletRequest request = createMock(SlingHttpServletRequest.class); ResourceResolver resourceResolver = createMock(ResourceResolver.class); Session session = createMock(Session.class); Node resultNode = createMock(Node.class); expect(resultNode.getPath()).andReturn("/path/to/node").anyTimes(); expect(resultNode.getName()).andReturn("node").anyTimes(); expect(request.getResourceResolver()).andReturn(resourceResolver).times(itemCount); expect(resourceResolver.adaptTo(Session.class)).andReturn(session).times(itemCount); expect(session.getItem("")).andReturn(resultNode).times(itemCount); PropertyIterator propIterator = createMock(PropertyIterator.class); expect(propIterator.hasNext()).andReturn(false).anyTimes(); expect(resultNode.getProperties()).andReturn(propIterator).anyTimes(); replay(); JSONWriter write = new JSONWriter(new PrintWriter(new ByteArrayOutputStream())); write.array(); RowIterator iterator = queryResult.getRows(); int i = 0; while (iterator.hasNext() && i < itemCount) { processor.writeNode(request, write, null, iterator.nextRow()); i++; } write.endArray(); verify(); }