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(), doc.getSpace(), doc.getName());
    Link pageLink = objectFactory.createLink();
    pageLink.setHref(pageUri);
    pageLink.setRel(Relations.PAGE);
    comment.getLinks().add(pageLink);

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

    HttpServletRequest httpServletRequest =
        (HttpServletRequest) Context.getCurrent().getAttributes().get(Constants.HTTP_REQUEST);

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