public void execute(WorkItem item, WorkflowSession session, MetaDataMap args)
      throws WorkflowException {
    WorkflowData workflowData = item.getWorkflowData();
    log.info("==================== <> ======================");
    Iterator props = args.entrySet().iterator();
    String pathToArchive = args.get("PROCESS_ARGS", String.class);
    if (pathToArchive != null)
      if (workflowData.getPayloadType().equals(TYPE_JCR_PATH)) {
        String path = workflowData.getPayload().toString();
        try {
          Node node = (Node) session.getSession().getItem(path + "/jcr:content");

          String orderBy = node.getProperty("orderby").getString();
          log.info("----------orderby: " + orderBy);
          Node nodeToOrder = (Node) session.getSession().getItem(pathToArchive);
          Iterator<Node> iterator = nodeToOrder.getNodes();
          List<Node> nodes = copyIterator(iterator);

          Collections.sort(nodes, new NodeComparator(orderBy));

          for (Node orderNode : nodes) {
            session
                .getSession()
                .getWorkspace()
                .move(
                    pathToArchive + "/" + orderNode.getName(),
                    pathToArchive + "/" + orderNode.getName());
          }

        } catch (RepositoryException e) {
          throw new WorkflowException(e.getMessage(), e);
        }
      }
  }
  @Override
  public void execute(WorkItem workItem, WorkflowSession workflowSession, String[] strings)
      throws Exception {
    log.info("executing workflow");

    WorkflowData workflowData = workItem.getWorkflowData();
    String payload = workflowData.getPayload().toString();
    log.info("payload " + payload);

    // find agent id
    if (strings.length == 0) {
      throw new Exception("Agent name is not set in arguments for workflow process step");
    }

    final String agentId = StringUtils.trim(strings[0]);
    log.info("agent id " + agentId);

    // filter agent
    ReplicationOptions opts = new ReplicationOptions();
    opts.setFilter(
        new AgentFilter() {
          public boolean isIncluded(final Agent agent) {
            return agentId.equals(agent.getId());
          }
        });

    // activate page
    Session session = null;
    try {
      session = repository.loginAdministrative(null);
      replicator.replicate(session, ReplicationActionType.ACTIVATE, payload, opts);
    } finally {
      if (session != null) {
        session.logout();
      }
    }
  }
  public Workflow startWorkflow(
      final WorkflowModel model, final WorkflowData data, final Map<String, Object> metaData)
      throws WorkflowException {
    final Date now = Calendar.getInstance().getTime();
    final MetaDataMap metaDataMap = new SimpleMetaDataMap();
    metaDataMap.putAll(metaData);
    Workflow wf =
        new Workflow() {

          public String getId() {
            return now.toString();
          }

          public List<WorkItem> getWorkItems() {
            return null;
          }

          public List<WorkItem> getWorkItems(WorkItemFilter filter) {
            return null;
          }

          public WorkflowModel getWorkflowModel() {
            return model;
          }

          public boolean isActive() {
            return true;
          }

          public String getState() {
            return null;
          }

          public String getInitiator() {
            return session.getUserID();
          }

          public Date getTimeStarted() {
            return now;
          }

          public Date getTimeEnded() {
            return now;
          }

          public WorkflowData getWorkflowData() {
            return data;
          }

          public Dictionary<String, String> getMetaData() {
            return null;
          }

          public MetaDataMap getMetaDataMap() {
            return metaDataMap;
          }
        };

    List<Workflow> all =
        payloadMap.containsKey(data.getPayload())
            ? payloadMap.get(data.getPayload())
            : new ArrayList<Workflow>();
    all.add(wf);
    this.payloadMap.put(data.getPayload(), all);
    return wf;
  }
Exemple #4
0
  /** @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);
    }
  }