Search in sources :

Example 1 with PageSummary

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;
}
Also used : PageResource(org.xwiki.rest.resources.pages.PageResource) Query(org.xwiki.query.Query) HashMap(java.util.HashMap) Formatter(java.util.Formatter) XWikiRestException(org.xwiki.rest.XWikiRestException) Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) URL(java.net.URL) Pages(org.xwiki.rest.model.jaxb.Pages) QueryException(org.xwiki.query.QueryException) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) PageSummary(org.xwiki.rest.model.jaxb.PageSummary) Link(org.xwiki.rest.model.jaxb.Link)

Example 2 with PageSummary

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;
}
Also used : Space(org.xwiki.rest.model.jaxb.Space) Pages(org.xwiki.rest.model.jaxb.Pages) Spaces(org.xwiki.rest.model.jaxb.Spaces) GetMethod(org.apache.commons.httpclient.methods.GetMethod) Wikis(org.xwiki.rest.model.jaxb.Wikis) Wiki(org.xwiki.rest.model.jaxb.Wiki) Page(org.xwiki.rest.model.jaxb.Page) PageSummary(org.xwiki.rest.model.jaxb.PageSummary) Link(org.xwiki.rest.model.jaxb.Link) WikisResource(org.xwiki.rest.resources.wikis.WikisResource)

Example 3 with PageSummary

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();
}
Also used : PageSummary(org.xwiki.rest.model.jaxb.PageSummary)

Example 4 with PageSummary

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);
    }
}
Also used : Pages(org.xwiki.rest.model.jaxb.Pages) GetMethod(org.apache.commons.httpclient.methods.GetMethod) PageSummary(org.xwiki.rest.model.jaxb.PageSummary) AbstractHttpTest(org.xwiki.test.rest.framework.AbstractHttpTest) Test(org.junit.Test)

Example 5 with 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);
}
Also used : PageResource(org.xwiki.rest.resources.pages.PageResource) PagesForTagsResource(org.xwiki.rest.resources.tags.PagesForTagsResource) PageTagsResource(org.xwiki.rest.resources.pages.PageTagsResource) TagsResource(org.xwiki.rest.resources.tags.TagsResource) PagesForTagsResource(org.xwiki.rest.resources.tags.PagesForTagsResource) Page(org.xwiki.rest.model.jaxb.Page) Pages(org.xwiki.rest.model.jaxb.Pages) GetMethod(org.apache.commons.httpclient.methods.GetMethod) PutMethod(org.apache.commons.httpclient.methods.PutMethod) Tag(org.xwiki.rest.model.jaxb.Tag) PageSummary(org.xwiki.rest.model.jaxb.PageSummary) Tags(org.xwiki.rest.model.jaxb.Tags) PageTagsResource(org.xwiki.rest.resources.pages.PageTagsResource) Link(org.xwiki.rest.model.jaxb.Link) AbstractHttpTest(org.xwiki.test.rest.framework.AbstractHttpTest) Test(org.junit.Test)

Aggregations

PageSummary (org.xwiki.rest.model.jaxb.PageSummary)7 Pages (org.xwiki.rest.model.jaxb.Pages)5 GetMethod (org.apache.commons.httpclient.methods.GetMethod)4 Link (org.xwiki.rest.model.jaxb.Link)4 Test (org.junit.Test)3 PageResource (org.xwiki.rest.resources.pages.PageResource)3 AbstractHttpTest (org.xwiki.test.rest.framework.AbstractHttpTest)3 Page (org.xwiki.rest.model.jaxb.Page)2 Document (com.xpn.xwiki.api.Document)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 URL (java.net.URL)1 Formatter (java.util.Formatter)1 HashMap (java.util.HashMap)1 PutMethod (org.apache.commons.httpclient.methods.PutMethod)1 Query (org.xwiki.query.Query)1 QueryException (org.xwiki.query.QueryException)1 XWikiRestException (org.xwiki.rest.XWikiRestException)1 Space (org.xwiki.rest.model.jaxb.Space)1 Spaces (org.xwiki.rest.model.jaxb.Spaces)1 Tag (org.xwiki.rest.model.jaxb.Tag)1