Search in sources :

Example 1 with Comment

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

the class CommentsResourceTest method testPOSTComment.

@Test
public void testPOSTComment() throws Exception {
    String commentsUri = buildURI(CommentsResource.class, getWiki(), this.spaces, this.pageName).toString();
    GetMethod getMethod = executeGet(commentsUri);
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    Comments comments = (Comments) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    int numberOfComments = comments.getComments().size();
    Comment comment = objectFactory.createComment();
    comment.setText("Comment");
    PostMethod postMethod = executePostXml(commentsUri, comment, TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
    Assert.assertEquals(getHttpMethodInfo(postMethod), HttpStatus.SC_CREATED, postMethod.getStatusCode());
    getMethod = executeGet(commentsUri);
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    comments = (Comments) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    Assert.assertEquals(numberOfComments + 1, comments.getComments().size());
}
Also used : Comment(org.xwiki.rest.model.jaxb.Comment) PostMethod(org.apache.commons.httpclient.methods.PostMethod) Comments(org.xwiki.rest.model.jaxb.Comments) GetMethod(org.apache.commons.httpclient.methods.GetMethod) CommentsResource(org.xwiki.rest.resources.comments.CommentsResource) AbstractHttpTest(org.xwiki.test.rest.framework.AbstractHttpTest) Test(org.junit.Test)

Example 2 with Comment

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

the class CommentsResourceTest method testGETComment.

@Test
public void testGETComment() throws Exception {
    String commentsUri = buildURI(CommentsResource.class, getWiki(), this.spaces, this.pageName).toString();
    GetMethod getMethod = executeGet(commentsUri);
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    Comments comments = (Comments) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    for (Comment comment : comments.getComments()) {
        checkLinks(comment);
    }
}
Also used : Comment(org.xwiki.rest.model.jaxb.Comment) Comments(org.xwiki.rest.model.jaxb.Comments) GetMethod(org.apache.commons.httpclient.methods.GetMethod) CommentsResource(org.xwiki.rest.resources.comments.CommentsResource) AbstractHttpTest(org.xwiki.test.rest.framework.AbstractHttpTest) Test(org.junit.Test)

Example 3 with Comment

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

the class CommentsResourceImpl method postComment.

@Override
public Response postComment(String wikiName, String spaceName, String pageName, Comment comment) throws XWikiRestException {
    try {
        List<String> spaces = parseSpaceSegments(spaceName);
        DocumentInfo documentInfo = getDocumentInfo(wikiName, spaces, pageName, null, null, true, true);
        Document doc = documentInfo.getDocument();
        int id = doc.createNewObject("XWiki.XWikiComments");
        com.xpn.xwiki.api.Object commentObject = doc.getObject("XWiki.XWikiComments", id);
        commentObject.set("author", Utils.getXWikiUser(componentManager));
        commentObject.set("date", new Date());
        boolean save = false;
        if (comment.getHighlight() != null) {
            commentObject.set("highlight", comment.getHighlight());
            save = true;
        }
        if (comment.getText() != null) {
            commentObject.set("comment", comment.getText());
            save = true;
        }
        if (comment.getReplyTo() != null) {
            commentObject.set("replyto", comment.getReplyTo());
        }
        if (save) {
            doc.save();
            Comment createdComment = DomainObjectFactory.createComment(objectFactory, uriInfo.getBaseUri(), doc, commentObject, Utils.getXWikiApi(componentManager), false);
            return Response.created(Utils.createURI(uriInfo.getBaseUri(), CommentResource.class, wikiName, spaces, pageName, id)).entity(createdComment).build();
        }
        return null;
    } catch (XWikiException e) {
        throw new XWikiRestException(e);
    }
}
Also used : Comment(org.xwiki.rest.model.jaxb.Comment) XWikiRestException(org.xwiki.rest.XWikiRestException) Document(com.xpn.xwiki.api.Document) Date(java.util.Date) CommentResource(org.xwiki.rest.resources.comments.CommentResource) XWikiException(com.xpn.xwiki.XWikiException)

Example 4 with Comment

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

the class DomainObjectFactory method createComment.

public static Comment createComment(ObjectFactory objectFactory, URI baseUri, Document doc, com.xpn.xwiki.api.Object xwikiComment, XWiki xwikiApi, Boolean withPrettyNames) {
    Comment comment = objectFactory.createComment();
    comment.setId(xwikiComment.getNumber());
    com.xpn.xwiki.api.Property property = xwikiComment.getProperty("author");
    if (property != null) {
        comment.setAuthor((String) property.getValue());
        if (withPrettyNames) {
            comment.setAuthorName(xwikiApi.getUserName((String) property.getValue(), false));
        }
    }
    property = xwikiComment.getProperty("date");
    if (property != null) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime((Date) property.getValue());
        comment.setDate(calendar);
    }
    property = xwikiComment.getProperty("highlight");
    if (property != null) {
        comment.setHighlight((String) property.getValue());
    }
    property = xwikiComment.getProperty("comment");
    if (property != null) {
        comment.setText((String) property.getValue());
    }
    property = xwikiComment.getProperty("replyto");
    if (property != null) {
        comment.setReplyTo((Integer) property.getValue());
    }
    String pageUri = uri(baseUri, PageResource.class, doc.getWiki(), Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName());
    Link pageLink = objectFactory.createLink();
    pageLink.setHref(pageUri);
    pageLink.setRel(Relations.PAGE);
    comment.getLinks().add(pageLink);
    return comment;
}
Also used : Comment(org.xwiki.rest.model.jaxb.Comment) Calendar(java.util.Calendar) Link(org.xwiki.rest.model.jaxb.Link)

Example 5 with Comment

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

the class FormUrlEncodedCommentReader method readFrom.

@Override
public Comment readFrom(Class<Comment> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
    ObjectFactory objectFactory = new ObjectFactory();
    Comment comment = objectFactory.createComment();
    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());
        try {
            comment.setReplyTo(Integer.parseInt(httpServletRequest.getParameter(COMMENT_REPLYTO_FIELD_NAME)));
        } catch (NumberFormatException e) {
        // Just ignore, there won't be a reply to.
        }
        comment.setText(httpServletRequest.getParameter(COMMENT_TEXT_FIELD_NAME));
    } else {
        try {
            comment.setReplyTo(Integer.parseInt(form.getFirstValue(COMMENT_REPLYTO_FIELD_NAME)));
        } catch (NumberFormatException e) {
        // Just ignore, there won't be a reply to.
        }
        comment.setText(form.getFirstValue(COMMENT_TEXT_FIELD_NAME));
    }
    return comment;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Comment(org.xwiki.rest.model.jaxb.Comment) 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)

Aggregations

Comment (org.xwiki.rest.model.jaxb.Comment)6 GetMethod (org.apache.commons.httpclient.methods.GetMethod)2 Test (org.junit.Test)2 Comments (org.xwiki.rest.model.jaxb.Comments)2 ObjectFactory (org.xwiki.rest.model.jaxb.ObjectFactory)2 CommentsResource (org.xwiki.rest.resources.comments.CommentsResource)2 AbstractHttpTest (org.xwiki.test.rest.framework.AbstractHttpTest)2 XWikiException (com.xpn.xwiki.XWikiException)1 Document (com.xpn.xwiki.api.Document)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)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 XWikiRestException (org.xwiki.rest.XWikiRestException)1 Link (org.xwiki.rest.model.jaxb.Link)1 CommentResource (org.xwiki.rest.resources.comments.CommentResource)1