Search in sources :

Example 1 with Tag

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;
}
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 Tag

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);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) XWikiRestException(org.xwiki.rest.XWikiRestException) ArrayList(java.util.ArrayList) Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) BaseObject(com.xpn.xwiki.objects.BaseObject) Tag(org.xwiki.rest.model.jaxb.Tag) XWikiException(com.xpn.xwiki.XWikiException)

Example 3 with Tag

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);
    }
}
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 4 with Tag

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);
}
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 5 with Tag

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

Aggregations

Tag (org.xwiki.rest.model.jaxb.Tag)8 Tags (org.xwiki.rest.model.jaxb.Tags)7 GetMethod (org.apache.commons.httpclient.methods.GetMethod)3 Test (org.junit.Test)3 XWikiRestException (org.xwiki.rest.XWikiRestException)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 XWikiException (com.xpn.xwiki.XWikiException)2 PutMethod (org.apache.commons.httpclient.methods.PutMethod)2 ObjectFactory (org.xwiki.rest.model.jaxb.ObjectFactory)2 Document (com.xpn.xwiki.api.Document)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 BaseObject (com.xpn.xwiki.objects.BaseObject)1 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)1 ArrayList (java.util.ArrayList)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 NameValuePair (org.apache.commons.httpclient.NameValuePair)1