use of org.xwiki.rest.model.jaxb.PageSummary in project xwiki-platform by xwiki.
the class WikiPagesResourceImpl method getPages.
@Override
public Pages getPages(String wikiName, Integer start, String name, String space, String author, Integer number) throws XWikiRestException {
String database = Utils.getXWikiContext(componentManager).getWikiId();
Pages pages = objectFactory.createPages();
/* This try is just needed for executing the finally clause. */
try {
Map<String, String> filters = new HashMap<String, String>();
if (!name.equals("")) {
filters.put("name", name);
}
if (!space.equals("")) {
filters.put("space", Utils.getLocalSpaceId(parseSpaceSegments(space)));
}
if (!author.equals("")) {
filters.put("author", author);
}
/* Build the query */
Formatter f = new Formatter();
f.format("select doc from XWikiDocument as doc");
if (filters.keySet().size() > 0) {
f.format(" where (");
int i = 0;
for (String param : filters.keySet()) {
if (param.equals("name")) {
f.format(" upper(doc.fullName) like :name ");
}
if (param.equals("space")) {
f.format(" upper(doc.space) like :space ");
}
if (param.equals("author")) {
f.format(" upper(doc.contentAuthor) like :author ");
}
i++;
if (i < filters.keySet().size()) {
f.format(" and ");
}
}
f.format(")");
}
String queryString = f.toString();
/* Execute the query by filling the parameters */
List<Object> queryResult = null;
try {
Query query = queryManager.createQuery(queryString, Query.XWQL).setLimit(number).setOffset(start);
for (String param : filters.keySet()) {
query.bindValue(param, String.format("%%%s%%", filters.get(param).toUpperCase()));
}
queryResult = query.execute();
} catch (QueryException e) {
throw new XWikiRestException(e);
}
/* Get the results and populate the returned representation */
for (Object object : queryResult) {
XWikiDocument xwikiDocument = (XWikiDocument) object;
xwikiDocument.setDatabase(wikiName);
Document doc = new Document(xwikiDocument, Utils.getXWikiContext(componentManager));
/*
* We manufacture page summaries in place because we don't have all the data for calling the
* DomainObjectFactory method (doing so would require to retrieve an actual Document)
*/
PageSummary pageSummary = objectFactory.createPageSummary();
pageSummary.setId(doc.getPrefixedFullName());
pageSummary.setFullName(doc.getFullName());
pageSummary.setWiki(wikiName);
pageSummary.setSpace(doc.getSpace());
pageSummary.setName(doc.getName());
pageSummary.setTitle(doc.getTitle());
pageSummary.setParent(doc.getParent());
URL absoluteUrl = Utils.getXWikiContext(componentManager).getURLFactory().createExternalURL(doc.getSpace(), doc.getName(), "view", null, null, Utils.getXWikiContext(componentManager));
pageSummary.setXwikiAbsoluteUrl(absoluteUrl.toString());
pageSummary.setXwikiRelativeUrl(Utils.getXWikiContext(componentManager).getURLFactory().getURL(absoluteUrl, Utils.getXWikiContext(componentManager)));
String pageUri = Utils.createURI(uriInfo.getBaseUri(), PageResource.class, doc.getWiki(), Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName()).toString();
Link pageLink = objectFactory.createLink();
pageLink.setHref(pageUri);
pageLink.setRel(Relations.PAGE);
pageSummary.getLinks().add(pageLink);
pages.getPageSummaries().add(pageSummary);
}
} finally {
Utils.getXWikiContext(componentManager).setWikiId(database);
}
return pages;
}
use of org.xwiki.rest.model.jaxb.PageSummary in project xwiki-platform by xwiki.
the class PageResourceTest method getFirstPage.
private Page getFirstPage() throws Exception {
GetMethod getMethod = executeGet(getFullUri(WikisResource.class));
Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
Wikis wikis = (Wikis) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
Assert.assertTrue(wikis.getWikis().size() > 0);
Wiki wiki = wikis.getWikis().get(0);
// Get a link to an index of spaces (http://localhost:8080/xwiki/rest/wikis/xwiki/spaces)
Link spacesLink = getFirstLinkByRelation(wiki, Relations.SPACES);
Assert.assertNotNull(spacesLink);
getMethod = executeGet(spacesLink.getHref());
Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
Spaces spaces = (Spaces) this.unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
Assert.assertTrue(spaces.getSpaces().size() > 0);
Space space = null;
for (final Space s : spaces.getSpaces()) {
if (this.space.equals(s.getName())) {
space = s;
break;
}
}
// get the pages list for the space
// eg: http://localhost:8080/xwiki/rest/wikis/xwiki/spaces/Main/pages
Link pagesInSpace = getFirstLinkByRelation(space, Relations.PAGES);
Assert.assertNotNull(pagesInSpace);
getMethod = executeGet(pagesInSpace.getHref());
Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
Pages pages = (Pages) this.unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
Assert.assertTrue(pages.getPageSummaries().size() > 0);
Link pageLink = null;
for (final PageSummary ps : pages.getPageSummaries()) {
if (this.pageName.equals(ps.getName())) {
pageLink = getFirstLinkByRelation(ps, Relations.PAGE);
Assert.assertNotNull(pageLink);
break;
}
}
Assert.assertNotNull(pageLink);
getMethod = executeGet(pageLink.getHref());
Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
Page page = (Page) this.unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
return page;
}
use of org.xwiki.rest.model.jaxb.PageSummary in project xwiki-platform by xwiki.
the class AbstractHttpTest method getPagesInfo.
protected String getPagesInfo(Pages pages) {
StringBuffer sb = new StringBuffer();
sb.append(String.format("Pages: %d\n", pages.getPageSummaries().size()));
for (PageSummary pageSummary : pages.getPageSummaries()) {
sb.append(String.format("* %s\n", pageSummary.getFullName()));
}
return sb.toString();
}
use of org.xwiki.rest.model.jaxb.PageSummary in project xwiki-platform by xwiki.
the class PageResourceTest method testGETPageChildren.
@Test
public void testGETPageChildren() throws Exception {
GetMethod getMethod = executeGet(buildURI(PageChildrenResource.class, getWiki(), this.spaces, this.pageName).toString());
Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
Pages pages = (Pages) this.unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
Assert.assertTrue(pages.getPageSummaries().size() > 0);
for (PageSummary pageSummary : pages.getPageSummaries()) {
checkLinks(pageSummary);
}
}
use of org.xwiki.rest.model.jaxb.PageSummary in project xwiki-platform by xwiki.
the class TagsResourceTest method testRepresentation.
@Override
@Test
public void testRepresentation() throws Exception {
String tagName = UUID.randomUUID().toString();
createPageIfDoesntExist(TestConstants.TEST_SPACE_NAME, TestConstants.TEST_PAGE_NAME, "Test");
GetMethod getMethod = executeGet(buildURI(PageResource.class, getWiki(), TestConstants.TEST_SPACE_NAME, TestConstants.TEST_PAGE_NAME).toString());
Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
Tags tags = objectFactory.createTags();
Tag tag = objectFactory.createTag();
tag.setName(tagName);
tags.getTags().add(tag);
PutMethod putMethod = executePutXml(buildURI(PageTagsResource.class, getWiki(), TestConstants.TEST_SPACE_NAME, TestConstants.TEST_PAGE_NAME).toString(), tags, TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_ACCEPTED, putMethod.getStatusCode());
getMethod = executeGet(buildURI(PageTagsResource.class, getWiki(), TestConstants.TEST_SPACE_NAME, TestConstants.TEST_PAGE_NAME).toString());
Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
tags = (Tags) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
boolean found = false;
for (Tag t : tags.getTags()) {
if (tagName.equals(t.getName())) {
found = true;
break;
}
}
Assert.assertTrue(found);
getMethod = executeGet(buildURI(TagsResource.class, getWiki()).toString());
Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
tags = (Tags) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
found = false;
for (Tag t : tags.getTags()) {
if (tagName.equals(t.getName())) {
found = true;
break;
}
}
Assert.assertTrue(found);
getMethod = executeGet(buildURI(PagesForTagsResource.class, getWiki(), tagName).toString());
Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
Pages pages = (Pages) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
found = false;
for (PageSummary pageSummary : pages.getPageSummaries()) {
if (pageSummary.getFullName().equals(String.format("%s.%s", TestConstants.TEST_SPACE_NAME.get(0), TestConstants.TEST_PAGE_NAME))) {
found = true;
}
}
Assert.assertTrue(found);
getMethod = executeGet(buildURI(PageResource.class, getWiki(), TestConstants.TEST_SPACE_NAME, TestConstants.TEST_PAGE_NAME).toString());
Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
Page page = (Page) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
Link tagsLink = getFirstLinkByRelation(page, Relations.TAGS);
Assert.assertNotNull(tagsLink);
}
Aggregations