Search in sources :

Example 1 with Tags

use of org.xwiki.rest.model.jaxb.Tags in project xwiki-platform by xwiki.

the class FormUrlEncodedTagsReader method readFrom.

@Override
public Tags readFrom(Class<Tags> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
    ObjectFactory objectFactory = new ObjectFactory();
    Tags tags = objectFactory.createTags();
    Representation representation = new InputRepresentation(entityStream, org.restlet.data.MediaType.APPLICATION_WWW_FORM);
    Form form = new Form(representation);
    /*
         * If the form is empty then it might have happened that some filter has invalidated the entity stream. Try to
         * read data using getParameter()
         */
    if (form.getNames().isEmpty()) {
        HttpServletRequest httpServletRequest = ServletUtils.getRequest(Request.getCurrent());
        String text = httpServletRequest.getParameter(TAGS_FIELD_NAME);
        if (text != null) {
            String[] tagNames = text.split(" |,|\\|");
            for (String tagName : tagNames) {
                Tag tag = objectFactory.createTag();
                tag.setName(tagName);
                tags.getTags().add(tag);
            }
        }
        String[] tagNames = httpServletRequest.getParameterValues(TAG_FIELD_NAME);
        if (tagNames != null) {
            for (String tagName : tagNames) {
                Tag tag = objectFactory.createTag();
                tag.setName(tagName);
                tags.getTags().add(tag);
            }
        }
    } else {
        String text = form.getFirstValue(TAGS_FIELD_NAME);
        if (text != null) {
            String[] tagNames = text.split(" |,|\\|");
            for (String tagName : tagNames) {
                Tag tag = objectFactory.createTag();
                tag.setName(tagName);
                tags.getTags().add(tag);
            }
        }
        for (String tagName : form.getValuesArray(TAG_FIELD_NAME)) {
            Tag tag = objectFactory.createTag();
            tag.setName(tagName);
            tags.getTags().add(tag);
        }
    }
    return tags;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ObjectFactory(org.xwiki.rest.model.jaxb.ObjectFactory) InputRepresentation(org.restlet.representation.InputRepresentation) Form(org.restlet.data.Form) InputRepresentation(org.restlet.representation.InputRepresentation) Representation(org.restlet.representation.Representation) Tag(org.xwiki.rest.model.jaxb.Tag) Tags(org.xwiki.rest.model.jaxb.Tags)

Example 2 with Tags

use of org.xwiki.rest.model.jaxb.Tags in project xwiki-platform by xwiki.

the class PageTagsResourceImpl method getPageTags.

@Override
public Tags getPageTags(String wikiName, String spaceName, String pageName) throws XWikiRestException {
    try {
        String pageId = Utils.getPageId(wikiName, parseSpaceSegments(spaceName), pageName);
        List<String> tagNames = getTagsFromDocument(pageId);
        Tags tags = objectFactory.createTags();
        for (String tagName : tagNames) {
            Tag tag = objectFactory.createTag();
            tag.setName(tagName);
            String tagUri = Utils.createURI(uriInfo.getBaseUri(), PagesForTagsResource.class, wikiName, tagName).toString();
            Link tagLink = objectFactory.createLink();
            tagLink.setHref(tagUri);
            tagLink.setRel(Relations.TAG);
            tag.getLinks().add(tagLink);
            tags.getTags().add(tag);
        }
        return tags;
    } catch (XWikiException e) {
        throw new XWikiRestException(e);
    }
}
Also used : XWikiRestException(org.xwiki.rest.XWikiRestException) PagesForTagsResource(org.xwiki.rest.resources.tags.PagesForTagsResource) Tag(org.xwiki.rest.model.jaxb.Tag) Tags(org.xwiki.rest.model.jaxb.Tags) Link(org.xwiki.rest.model.jaxb.Link) XWikiException(com.xpn.xwiki.XWikiException)

Example 3 with Tags

use of org.xwiki.rest.model.jaxb.Tags in project xwiki-platform by xwiki.

the class TagsResourceTest method testPUTTagsFormUrlEncoded.

@Test
public void testPUTTagsFormUrlEncoded() throws Exception {
    createPageIfDoesntExist(TestConstants.TEST_SPACE_NAME, TestConstants.TEST_PAGE_NAME, "Test");
    String tagName = UUID.randomUUID().toString();
    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());
    NameValuePair[] nameValuePairs = new NameValuePair[1];
    nameValuePairs[0] = new NameValuePair("tags", tagName);
    PostMethod postMethod = executePostForm(String.format("%s?method=PUT", buildURI(PageTagsResource.class, getWiki(), TestConstants.TEST_SPACE_NAME, TestConstants.TEST_PAGE_NAME).toString()), nameValuePairs, TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
    Assert.assertEquals(getHttpMethodInfo(postMethod), HttpStatus.SC_ACCEPTED, postMethod.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 = (Tags) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    boolean found = false;
    for (Tag t : tags.getTags()) {
        if (tagName.equals(t.getName())) {
            found = true;
            break;
        }
    }
    Assert.assertTrue(found);
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) PostMethod(org.apache.commons.httpclient.methods.PostMethod) GetMethod(org.apache.commons.httpclient.methods.GetMethod) Tag(org.xwiki.rest.model.jaxb.Tag) PageTagsResource(org.xwiki.rest.resources.pages.PageTagsResource) Tags(org.xwiki.rest.model.jaxb.Tags) AbstractHttpTest(org.xwiki.test.rest.framework.AbstractHttpTest) Test(org.junit.Test)

Example 4 with Tags

use of org.xwiki.rest.model.jaxb.Tags in project xwiki-platform by xwiki.

the class TagsResourceTest method testPUTTagsWithTextPlain.

@Test
public void testPUTTagsWithTextPlain() throws Exception {
    createPageIfDoesntExist(TestConstants.TEST_SPACE_NAME, TestConstants.TEST_PAGE_NAME, "Test");
    String tagName = UUID.randomUUID().toString();
    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());
    PutMethod putMethod = executePut(buildURI(PageTagsResource.class, getWiki(), TestConstants.TEST_SPACE_NAME, TestConstants.TEST_PAGE_NAME).toString(), tagName, MediaType.TEXT_PLAIN, 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 = (Tags) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    boolean found = false;
    for (Tag t : tags.getTags()) {
        if (tagName.equals(t.getName())) {
            found = true;
            break;
        }
    }
    Assert.assertTrue(found);
}
Also used : GetMethod(org.apache.commons.httpclient.methods.GetMethod) PutMethod(org.apache.commons.httpclient.methods.PutMethod) Tag(org.xwiki.rest.model.jaxb.Tag) PageTagsResource(org.xwiki.rest.resources.pages.PageTagsResource) Tags(org.xwiki.rest.model.jaxb.Tags) AbstractHttpTest(org.xwiki.test.rest.framework.AbstractHttpTest) Test(org.junit.Test)

Example 5 with Tags

use of org.xwiki.rest.model.jaxb.Tags 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

Tag (org.xwiki.rest.model.jaxb.Tag)7 Tags (org.xwiki.rest.model.jaxb.Tags)7 GetMethod (org.apache.commons.httpclient.methods.GetMethod)3 Test (org.junit.Test)3 Link (org.xwiki.rest.model.jaxb.Link)3 PageTagsResource (org.xwiki.rest.resources.pages.PageTagsResource)3 PagesForTagsResource (org.xwiki.rest.resources.tags.PagesForTagsResource)3 AbstractHttpTest (org.xwiki.test.rest.framework.AbstractHttpTest)3 PutMethod (org.apache.commons.httpclient.methods.PutMethod)2 XWikiRestException (org.xwiki.rest.XWikiRestException)2 ObjectFactory (org.xwiki.rest.model.jaxb.ObjectFactory)2 XWikiException (com.xpn.xwiki.XWikiException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 NameValuePair (org.apache.commons.httpclient.NameValuePair)1 PostMethod (org.apache.commons.httpclient.methods.PostMethod)1 Form (org.restlet.data.Form)1 InputRepresentation (org.restlet.representation.InputRepresentation)1 Representation (org.restlet.representation.Representation)1 QueryException (org.xwiki.query.QueryException)1 Page (org.xwiki.rest.model.jaxb.Page)1