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;
}
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);
}
}
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);
}
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);
}
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);
}
Aggregations