コード例 #1
0
ファイル: CheckSpamProcess.java プロジェクト: tiennv90/SVN
 protected void activate(final ComponentContext context) {
   // check if the key is valid
   logger.info("verifying key for spam check");
   boolean isValidKey = akismetService.isVerifiedKey();
   if (isValidKey) {
     logger.info("Key for Akismet is valid");
   } else {
     logger.warn("Akismet key is not valid! Spam check is not possible with this key.");
   }
 }
コード例 #2
0
ファイル: CheckSpamProcess.java プロジェクト: tiennv90/SVN
  /** @see WorkflowProcess#execute(WorkItem, WorkflowSession, MetaDataMap) */
  public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap args)
      throws WorkflowException {
    final Session session = workflowSession.getSession();
    final WorkflowData data = workItem.getWorkflowData();
    String path = null;
    String type = data.getPayloadType();
    try {
      if (type.equals(TYPE_JCR_PATH) && data.getPayload() != null) {
        String payloadData = (String) data.getPayload();
        if (session.itemExists(payloadData)) {
          path = payloadData;
        }
      }

      if (path != null) {
        final Node userGeneratedNode = (Node) session.getItem(path);

        if (userGeneratedNode.hasProperty(SLING_RESOURCE_TYPE)
            && userGeneratedNode
                .getProperty(SLING_RESOURCE_TYPE)
                .getString()
                .equals(COMMENT_RESOURCE_TYPE)) {

          logger.debug("Spam check for comment node at " + userGeneratedNode.getPath());
          String ipAddress = null;
          if (userGeneratedNode.hasProperty("ip")) {
            ipAddress = userGeneratedNode.getProperty("ip").getString();
            logger.debug("Spam check for comment node with ip " + ipAddress);
          } else {
            setIsSpam(userGeneratedNode, false, session);
            logger.info("Comment has no IP address, setting isSpam to false");
            return;
          }
          // if the ip is localhost we set isspam to false as well
          if (ipAddress.equalsIgnoreCase("127.0.0.1")
              || ipAddress.equalsIgnoreCase("localhost")
              || ipAddress.equalsIgnoreCase("0:0:0:0:0:0:0:1")) {
            setIsSpam(userGeneratedNode, false, session);
            logger.info("Comment IP address is localhost, setting isSpam to false");
            return;
          }

          String userAgent = null;
          if (userGeneratedNode.hasProperty("userAgent")) {
            userAgent = userGeneratedNode.getProperty("userAgent").getString();
            logger.debug("Spam check for comment node with userAgent " + userAgent);
          } else {
            setIsSpam(userGeneratedNode, false, session);
            logger.info("Comment has no user agent, setting isSpam to false");
            return;
          }

          String referrer = null;
          if (userGeneratedNode.hasProperty("referer")) {
            referrer = userGeneratedNode.getProperty("referer").getString();
            logger.debug("Spam check for comment node with referrer " + referrer);
          }

          String permalink = null;
          try {
            ResourceResolver resourceResolver =
                jcrResourceResolverFactory.getResourceResolver(session);
            Resource resource = resourceResolver.getResource(path);
            BlogManager blogManager = resource.getResourceResolver().adaptTo(BlogManager.class);
            Blog entry = blogManager.getBlog(path);
            permalink = entry.getFullUrl();
            logger.debug("Spam check for comment node with permalink " + permalink);
          } catch (Exception e) {
            logger.error("Exception " + e.toString() + " getting permalink");
          }

          String author = null;
          if (userGeneratedNode.hasProperty("userIdentifier")) {
            author = userGeneratedNode.getProperty("userIdentifier").getString();
            logger.debug("Spam check for comment node with permalink " + author);
          }
          String authorEmail = null;
          if (userGeneratedNode.hasProperty("email")) {
            authorEmail = userGeneratedNode.getProperty("email").getString();
            logger.debug("Spam check for comment node with author " + authorEmail);
          }
          String authorURL = null;
          if (userGeneratedNode.hasProperty("url")) {
            authorURL = userGeneratedNode.getProperty("url").getString();
            logger.debug("Spam check for comment node with authorURL " + authorURL);
          }
          String commentText = null;
          if (userGeneratedNode.hasProperty("jcr:description")) {
            commentText = userGeneratedNode.getProperty("jcr:description").getString();
            logger.debug("Spam check for comment node with commentText " + commentText);
          }

          boolean isSpam =
              akismetService.commentCheck(
                  ipAddress,
                  userAgent,
                  referrer,
                  permalink,
                  "comment",
                  author,
                  authorEmail,
                  authorURL,
                  commentText,
                  null);

          setIsSpam(userGeneratedNode, isSpam, session);

          logger.info("Spam check result " + isSpam + " for comment: " + workItem.toString());

        } else if (userGeneratedNode.hasProperty(SLING_RESOURCE_TYPE)
            && userGeneratedNode
                .getProperty(SLING_RESOURCE_TYPE)
                .getString()
                .equals(TRACKBACK_RESOURCE_TYPE)) { // for
          // trackbacks

          String ipAddress = null;
          if (userGeneratedNode.hasProperty("ip")) {
            ipAddress = userGeneratedNode.getProperty("ip").getString();
            logger.debug("Spam check for trackback node with ip " + ipAddress);
          }

          String authorURL = null;
          if (userGeneratedNode.hasProperty("url")) {
            authorURL = userGeneratedNode.getProperty("url").getString();
            logger.debug("Spam check for trackback node with authorURL " + authorURL);
          }

          String permalink = null;
          try {
            ResourceResolver resourceResolver =
                jcrResourceResolverFactory.getResourceResolver(session);
            Resource resource = resourceResolver.getResource(path);
            BlogManager blogManager = resource.getResourceResolver().adaptTo(BlogManager.class);
            Blog entry = blogManager.getBlog(path);
            permalink = entry.getFullUrl();
            logger.debug("Spam check for trackback node with permalink " + permalink);
          } catch (Exception e) {
            logger.error("Exception " + e.toString() + " getting permalink");
          }

          /*
           * it is not really clear from the akismet api how to map
           * the trackback's title and blog-name to the api call. will
           * try with the code below.
           */

          String author = null;
          if (userGeneratedNode.hasProperty("blogName")) {
            author = userGeneratedNode.getProperty("blogName").getString();
            logger.debug("Spam check for trackback node with blogName " + author);
          }

          // prepending the title to the excerpt
          String excerpt = "";
          if (userGeneratedNode.hasProperty("excerpt")) {
            excerpt = userGeneratedNode.getProperty("excerpt").getString();
            logger.debug("Spam check for trackback node with excerpt " + excerpt);
          }
          if (userGeneratedNode.hasProperty("jcr:title")) {
            excerpt = userGeneratedNode.getProperty("jcr:title").getString() + " " + excerpt;
            logger.debug(
                "Spam check for trackback node with title "
                    + userGeneratedNode.hasProperty("jcr:title"));
          }

          boolean isSpam =
              akismetService.commentCheck(
                  ipAddress,
                  null,
                  null,
                  permalink,
                  "trackback",
                  author,
                  null,
                  authorURL,
                  excerpt,
                  null);

          setIsSpam(userGeneratedNode, isSpam, session);

          logger.info("Spam check result " + isSpam + " for trackback: " + workItem.toString());

        } else {
          logger.warn(
              "Cannot check for spam because item is not a comment or a trackback. Workitem: "
                  + workItem.toString());
        }
      } else {
        logger.warn(
            "Cannot check for spam because path is null for this workitem: " + workItem.toString());
      }
    } catch (RepositoryException e) {
      throw new WorkflowException(e);
    }
  }