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