use of org.xwiki.rest.model.jaxb.Tag 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.Tag in project xwiki-platform by xwiki.
the class PageTagsResourceImpl method setTags.
@Override
public Response setTags(String wikiName, String spaceName, String pageName, Boolean minorRevision, Tags tags) throws XWikiRestException {
try {
DocumentInfo documentInfo = getDocumentInfo(wikiName, spaceName, pageName, null, null, true, false);
Document doc = documentInfo.getDocument();
if (!doc.hasAccessLevel("edit", Utils.getXWikiUser(componentManager))) {
throw new WebApplicationException(Status.UNAUTHORIZED);
}
List<String> tagNames = new ArrayList<String>();
for (Tag tag : tags.getTags()) {
tagNames.add(tag.getName());
}
XWikiDocument xwikiDocument = Utils.getXWiki(componentManager).getDocument(doc.getDocumentReference(), Utils.getXWikiContext(componentManager));
BaseObject xwikiObject = xwikiDocument.getObject("XWiki.TagClass", 0);
if (xwikiObject == null) {
int objectNumber = xwikiDocument.createNewObject("XWiki.TagClass", Utils.getXWikiContext(componentManager));
xwikiObject = xwikiDocument.getObject("XWiki.TagClass", objectNumber);
if (xwikiObject == null) {
throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
}
// We must initialize all the fields to an empty value in order to correctly create the object
BaseClass xwikiClass = Utils.getXWiki(componentManager).getClass(xwikiObject.getClassName(), Utils.getXWikiContext(componentManager));
for (Object propertyNameObject : xwikiClass.getPropertyNames()) {
String propertyName = (String) propertyNameObject;
xwikiObject.set(propertyName, "", Utils.getXWikiContext(componentManager));
}
}
xwikiObject.set("tags", tagNames, Utils.getXWikiContext(componentManager));
doc.save("", Boolean.TRUE.equals(minorRevision));
return Response.status(Status.ACCEPTED).entity(tags).build();
} catch (XWikiException e) {
throw new XWikiRestException(e);
}
}
use of org.xwiki.rest.model.jaxb.Tag 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.Tag 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.Tag 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);
}
Aggregations