@Override protected void doPost(SlingHttpServletRequest request,SlingHttpServletResponse response) throws ServletException, IOException {
  final String blogPath=request.getParameter(BLOG_PATH_PARAMETER);
  if (!recaptchaService.getEnabled() || recaptchaService.validate(request)) {
    final String author=request.getParameter(AUTHOR_PARAMETER);
    final String comment=request.getParameter(COMMENT_PARAMETER);
    final String commentPath=request.getParameter(COMMENT_PATH_PARAMETER);
    try {
      resolver=resourceResolverFactory.getAdministrativeResourceResolver(null);
      String parentPath=StringUtils.isNotBlank(commentPath) ? commentPath : blogPath.replace(PublickConstants.BLOG_PATH,PublickConstants.COMMENTS_PATH);
      JcrResourceUtil.createPath(parentPath,JcrResourceConstants.NT_SLING_ORDERED_FOLDER,JcrResourceConstants.NT_SLING_ORDERED_FOLDER,resolver.adaptTo(Session.class),true);
      String nodeName=getCommentName(parentPath);
      Map<String,Object> properties=new HashMap<String,Object>();
      properties.put(PublickConstants.COMMENT_PROPERTY_AUTHOR,author);
      properties.put(PublickConstants.COMMENT_PROPERTY_COMMENT,comment);
      properties.put(JcrConstants.JCR_PRIMARYTYPE,PublickConstants.NODE_TYPE_COMMENT);
      properties.put(PublickConstants.COMMENT_PROPERTY_USER_AGENT,httpService.getUserAgent(request));
      properties.put(PublickConstants.COMMENT_PROPERTY_USER_IP,httpService.getIPAddress(request));
      properties.put(PublickConstants.COMMENT_PROPERTY_REFERRER,httpService.getReferrer(request));
      properties.put(PublickConstants.COMMENT_PROPERTY_DISPLAY,true);
      properties.put(PublickConstants.COMMENT_PROPERTY_EDITED,false);
      Resource commentResource=resolver.create(resolver.getResource(parentPath),nodeName,properties);
      Node commentNode=commentResource.adaptTo(Node.class);
      commentNode.addMixin(NodeType.MIX_CREATED);
      commentNode.addMixin(NodeType.MIX_REFERENCEABLE);
      resolver.commit();
      akismetService.isSpam(commentResource);
    }
 catch (    LoginException e) {
      LOGGER.error("Could not login",e);
    }
catch (    RepositoryException e) {
      LOGGER.error("Could not create comment node",e);
    }
catch (    Exception e) {
      LOGGER.error("Could not create comment node",e);
    }
  }
  response.sendRedirect(blogPath + ".html");
}

@ Override protected void doPost ( SlingHttpServletRequest request , SlingHttpServletResponse response ) throws ServletException , IOException { final String blogPath = request . getParameter ( BLOG_PATH_PARAMETER ) ; if ( ! recaptchaService . getEnabled ( ) || recaptchaService . validate ( request ) ) { final String author = request . getParameter ( AUTHOR_PARAMETER ) ; final String comment = request . getParameter ( COMMENT_PARAMETER ) ; final String commentPath = request . getParameter ( COMMENT_PATH_PARAMETER ) ; try { resolver = resourceResolverFactory . getAdministrativeResourceResolver ( null ) ; String parentPath = StringUtils . isNotBlank ( commentPath ) ? commentPath : blogPath . replace ( PublickConstants . BLOG_PATH , PublickConstants . COMMENTS_PATH ) ; JcrResourceUtil . createPath ( parentPath , JcrResourceConstants . NT_SLING_ORDERED_FOLDER , JcrResourceConstants . NT_SLING_ORDERED_FOLDER , resolver . adaptTo ( Session . class ) , true ) ; String nodeName = getCommentName ( parentPath ) ; Map < String , Object > properties = new HashMap < String , Object > ( ) ; properties . put ( PublickConstants . COMMENT_PROPERTY_AUTHOR , author ) ; properties . put ( PublickConstants . COMMENT_PROPERTY_COMMENT , comment ) ; properties . put ( JcrConstants . JCR_PRIMARYTYPE , PublickConstants . NODE_TYPE_COMMENT ) ; properties . put ( PublickConstants . COMMENT_PROPERTY_USER_AGENT , httpService . getUserAgent ( request ) ) ; properties . put ( PublickConstants . COMMENT_PROPERTY_USER_IP , httpService . getIPAddress ( request ) ) ; properties . put ( PublickConstants . COMMENT_PROPERTY_REFERRER , httpService . getReferrer ( request ) ) ; properties . put ( PublickConstants . COMMENT_PROPERTY_DISPLAY , true ) ; properties . put ( PublickConstants . COMMENT_PROPERTY_EDITED , false ) ; Resource commentResource = resolver . create ( resolver . getResource ( parentPath ) , nodeName , properties ) ; Node commentNode = commentResource . adaptTo ( Node . class ) ; commentNode . addMixin ( NodeType . MIX_CREATED ) ; commentNode . addMixin ( NodeType . MIX_REFERENCEABLE ) ; resolver . commit ( ) ; akismetService . isSpam ( commentResource ) ; } catch ( LoginException e ) { LOGGER . error ( "Could not login" , e ) ; } catch ( RepositoryException e ) { LOGGER . error ( "Could not create comment node" , e ) ; } catch ( Exception e ) { LOGGER . error ( "Could not create comment node" , e ) ; } } response . sendRedirect ( blogPath + ".html" ) ; }
save the comment to the jcr . get or create the comment node structure to mirror the blog post node structure . create the comment node under the structure and save the author , comment , and date . currently redirects back to the same page . verifies against the recaptcha service . the next version will be an asynchronous post .
