throws WebApplicationException {
ContentRepository repository = getContentRepository(q.getSite(), false);
if (repository == null)
throw new WebApplicationException(Status.SERVICE_UNAVAILABLE);
SearchResult result = null;
Page pageByPath = null;
try {
if (q.getVersion() == Resource.WORK && q.getPath() != null) {
ResourceURI uri = new PageURIImpl(q.getSite(), q.getPath(), q.getVersion());
pageByPath = (Page) repository.get(uri);
int count = pageByPath != null ? 1 : 0;
result = new SearchResultImpl(q, count, count);
} else {
result = repository.find(q);
}
} catch (ContentRepositoryException e) {
throw new WebApplicationException(e);
}
StringBuffer buf = new StringBuffer("<pages ");
buf.append("hits=\"").append(result.getHitCount()).append("\" ");
buf.append("offset=\"").append(result.getOffset()).append("\" ");
if (q.getLimit() > 0)
buf.append("limit=\"").append(result.getLimit()).append("\" ");
buf.append("page=\"").append(result.getPage()).append("\" ");
buf.append("pagesize=\"").append(result.getPageSize()).append("\"");
buf.append(">");
if (pageByPath != null) {
String xml = pageByPath.toXml();
if (!details) {
xml = xml.replaceAll("<body>.*</body>", "");
xml = xml.replaceAll("<body/>", "");
}
buf.append(xml);
} else {
for (SearchResultItem item : result.getItems()) {
String xml = null;
if (details)
xml = ((PageSearchResultItemImpl) item).getResourceXml();
else
xml = ((PageSearchResultItemImpl) item).getPageHeaderXml();