/**
  * Used to get Full form of URL from the Given Short Page URL
  *
  * @param pageFullPath
  * @return {@link String}
  * @throws Exception
  */
 public static String getFullURLPath(String shortUrlPath) throws Exception {
   ResourceResolver resourceResolver = null;
   try {
     Bundle bndl = FrameworkUtil.getBundle(ResourceResolverFactory.class);
     BundleContext bundleContext = bndl.getBundleContext();
     ServiceReference ref =
         bundleContext.getServiceReference(ResourceResolverFactory.class.getName());
     ResourceResolverFactory resolverFactory =
         (ResourceResolverFactory) bundleContext.getService(ref);
     resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);
     Resource resource = resourceResolver.resolve(shortUrlPath);
     if (null != resource) {
       return java.net.URLDecoder.decode(resource.getPath(), "UTF-8");
     } else {
       if (LOGGER.isDebugEnabled()) {
         LOGGER.debug("Resource doesn't exists..." + shortUrlPath);
       }
     }
   } catch (Exception e) {
     LOGGER.error(
         " Error while getting Full URL for the path :"
             + shortUrlPath
             + " and the error message is :",
         e);
   } finally {
     resourceResolver.close();
   }
   return shortUrlPath;
 }
  public void handleEvent(final Event event) {
    if (disabled) return;

    SolrClient solr = getSolrClient(core);

    PageEvent pageEvent = PageEvent.fromEvent(event);
    if (pageEvent == null) return;

    ResourceResolver resourceResolver = null;
    try {
      resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);
      for (Iterator<PageModification> iter = pageEvent.getModifications(); iter.hasNext(); )
        handlePageModification(iter.next(), solr, resourceResolver);
    } catch (Exception e) {
      LOG.error("Could not get ResourceResolver instance or handle page modification", e);
      return;
    } finally {
      if (resourceResolver != null && resourceResolver.isLive()) resourceResolver.close();
    }
  }
 /**
  * Used for getting the short url from the long url
  *
  * @param url
  * @return
  */
 public static String getShortUrl(String url) {
   if (url != null) {
     ResourceResolver resourceResolver = null;
     try {
       Bundle bndl = FrameworkUtil.getBundle(ResourceResolverFactory.class);
       BundleContext bundleContext = bndl.getBundleContext();
       ServiceReference ref =
           bundleContext.getServiceReference(ResourceResolverFactory.class.getName());
       ResourceResolverFactory resolverFactory =
           (ResourceResolverFactory) bundleContext.getService(ref);
       resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);
       url = resourceResolver.map(url);
       url = java.net.URLDecoder.decode(url, "UTF-8");
       return url;
     } catch (Exception e) {
       LOGGER.error(
           " Error while getting Short URL for the path :" + url + " and the error message is :",
           e);
     } finally {
       resourceResolver.close();
     }
   }
   return url;
 }
  /**
   * This method renders the given request URI using the Sling request processing and stores the
   * result at the request's location relative to the cache root folder. The request is processed in
   * the context of the given user's session.
   *
   * @param uri The request URI including selectors and extensions
   * @param configCacheRoot The cache root folder
   * @param admin The admin session used to store the result in the cache
   * @param session The user's session
   * @return <code>true</code> if the cache was updated
   */
  protected boolean renderResource(
      String uri, String configCacheRoot, Session admin, Session session)
      throws RepositoryException, ServletException, IOException {
    String cachePath = configCacheRoot + getTargetPath(uri);

    ResourceResolver resolver = null;
    try {
      resolver = createResolver(session.getUserID());

      // render resource
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      HttpServletRequest request = createRequest(uri);
      HttpServletResponse response = requestResponseFactory.createResponse(out);

      slingServlet.processRequest(request, response, resolver);
      response.getWriter().flush();

      // compare md5 checksum with cache
      String md5 = requestResponseFactory.getMD5(response);
      String md5Path = cachePath + "/" + JcrConstants.JCR_CONTENT + "/" + MD5_HASH_PROPERTY;

      if (!admin.propertyExists(md5Path) || !admin.getProperty(md5Path).getString().equals(md5)) {
        log.info("MD5 hash missing or not equal, updating content sync cache: {}", cachePath);

        JcrUtil.createPath(cachePath, "sling:Folder", "nt:file", admin, false);

        Node cacheContentNode =
            JcrUtil.createPath(cachePath + "/jcr:content", "nt:resource", admin);
        if (needsUtf8Encoding(response)) {
          cacheContentNode.setProperty(
              JcrConstants.JCR_DATA,
              admin
                  .getValueFactory()
                  .createBinary(
                      IOUtils.toInputStream(out.toString(response.getCharacterEncoding()))));
        } else {
          cacheContentNode.setProperty(
              JcrConstants.JCR_DATA,
              admin.getValueFactory().createBinary(new ByteArrayInputStream(out.toByteArray())));
        }
        cacheContentNode.setProperty(JcrConstants.JCR_LASTMODIFIED, Calendar.getInstance());
        if (response.getContentType() != null) {
          cacheContentNode.setProperty(JcrConstants.JCR_MIMETYPE, response.getContentType());
        }
        if (response.getCharacterEncoding() != null) {
          cacheContentNode.setProperty(JcrConstants.JCR_ENCODING, response.getCharacterEncoding());
        }

        cacheContentNode.addMixin(NT_MD5_HASH);
        cacheContentNode.setProperty(MD5_HASH_PROPERTY, md5);

        admin.save();

        return true;
      } else {
        log.info("Skipping update of content sync cache: {}", uri);

        return false;
      }
    } catch (LoginException e) {
      log.error("Creating resource resolver for resource rendering failed: ", e);
      return false;
    } finally {
      if (resolver != null) {
        resolver.close();
      }

      if (admin.hasPendingChanges()) {
        admin.refresh(false);
      }
    }
  }
  @Override
  protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response)
      throws ServletException, IOException {
    HtmlResponse htmlResponse = null;
    ResourceResolver adminResolver = null;
    Session adminSession = null;

    try {
      String email = request.getParameter(REQ_PRM_USERNAME);
      String password = request.getParameter(REQ_PRM_PASSWORD);
      String inviteKey = request.getParameter(REQ_PRM_INVITEKEY);
      String acceptStatus = request.getParameter(REQ_PRM_ACCEPT_STATUS);

      adminResolver = getAdminResolver();
      adminSession = adminResolver.adaptTo(Session.class);

      InvitationToken invToken =
          ccInvitationService.getInvitationTokenByTokenKey(inviteKey, adminResolver);
      if (invToken != null && invToken.isValid() && inviteKey.equals(invToken.getKey())) {

        // Gets user account if user is already configured. Gets null
        // otherwise.
        Resource configResource = CCUtils.getAccountResourceByUserEmail(adminResolver, email);
        if (configResource == null) { // Create configuration if not
          // present. For first time login

          // encrypt the password
          if (!crypto.isProtected(password)) {
            password = crypto.protect(password);
          }
          AccessToken token = null;
          try {
            token = imsService.getAccessToken(email, password);
          } catch (Exception e) {
            log.error(e.getMessage(), e);
            htmlResponse =
                HtmlStatusResponseHelper.createStatusResponse(false, "Invalid Credentials");
          }
          if (token != null) { // succesful login
            String configName = "invited_" + email;
            configName = configName.replace("@", "_at_").replaceAll("\\.", "_");
            PageManager pageManager = pageManagerFactory.getPageManager(adminResolver);
            pageManager.create(CC_CONFIG_ROOT, configName, configName, CC_CONFIG_PAGE_TEMPLATE);

            Node configNode = adminSession.getNode(CC_CONFIG_ROOT + "/" + configName);
            Node contentNode = configNode.getNode("jcr:content");
            contentNode.setProperty(CreativeCloudAccountConfig.PROPERTY_USERNAME, email);
            contentNode.setProperty("sling:resourceType", CreativeCloudAccountConfig.RESOURCE_TYPE);

            contentNode.setProperty(
                CreativeCloudAccountConfig.PROPERTY_ACCESS_TOKEN, token.getAccessToken());
            contentNode.setProperty(
                CreativeCloudAccountConfig.PROPERTY_REFRESH_TOKEN, token.getRefreshToken());
            contentNode.setProperty(
                CreativeCloudAccountConfig.PROPERTY_TOKEN_EXPIRES, token.getExpiresIn());
            contentNode.setProperty(CreativeCloudAccountConfig.PROPERTY_PASSWORD, password);

            Node pollConfigNode = contentNode.addNode(CreativeCloudAccountConfig.NN_POLLCONFIG);
            pollConfigNode.setProperty(
                CreativeCloudAccountConfig.PROPERTY_INTERVAL, importer.getMinimumInterval());
            pollConfigNode.setProperty(CreativeCloudAccountConfig.PROPERTY_ENABLED, true);

            configResource = adminResolver.getResource(contentNode.getPath());
            ccConfigService.initAccount(configResource);
          }
        } else {
          // Sets the jcr content node as the config node
          configResource = configResource.getChild("jcr:content");
        }
        if (acceptStatus != null && acceptStatus.equalsIgnoreCase("false")) {
          htmlResponse = HtmlStatusResponseHelper.createStatusResponse(true, "invitation declined");
        } else {
          String[] paths = invToken.getPaths();
          for (String path : paths) {
            // ccShareService.shareWithCCUser(configResource,
            // adminResolver.getResource(path));
            // Asynchronous sharing
            Object job =
                new CCShareInBackground(factory, configResource.getPath(), path, ccShareService);
            String jobName =
                CCShareInBackground.class.getName()
                    + "_"
                    + UUID.randomUUID().toString().substring(0, 8);
            scheduler.fireJobAt(jobName, job, null, new Date());
          }
          htmlResponse = HtmlStatusResponseHelper.createStatusResponse(true, "invitation accepted");
        }
        ccInvitationService.acceptInvitation(email, inviteKey, adminResolver);
        adminSession.save();

      } else {
        htmlResponse =
            HtmlStatusResponseHelper.createStatusResponse(
                false, "invitation expired or already accepted/declined");
      }
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      htmlResponse = HtmlStatusResponseHelper.createStatusResponse(false, e.getMessage());
      htmlResponse.setError(e);
    } finally {
      if (adminSession != null) {
        adminSession.logout();
      }
      if (adminResolver != null) {
        adminResolver.close();
      }
      assert htmlResponse != null;
      htmlResponse.send(response, true);
    }
  }
    public void run() {
      ResourceResolver resourceResolver = null;
      Resource configResource;
      long start = System.currentTimeMillis();
      int total = 0;
      boolean stopped = false;

      try {
        resourceResolver = resourceResolverFactory.getAdministrativeResourceResolver(null);
        configResource = resourceResolver.getResource(configPath);

        final Config config = configResource.adaptTo(Config.class);
        final Workspace workspace = config.getWorkspace();

        if (workspace.isStopped()) {
          return;
        }

        try {
          SyntheticWorkflowModel model =
              syntheticWorkflowRunner.getSyntheticWorkflowModel(
                  resourceResolver, config.getWorkflowModelId(), true);
          resourceResolver
              .adaptTo(Session.class)
              .getWorkspace()
              .getObservationManager()
              .setUserData("changedByWorkflowProcess");

          PayloadGroup payloadGroup = null;
          if (workspace.getActivePayloadGroups().size() > 0) {
            payloadGroup = workspace.getActivePayloadGroups().get(0);
          }

          while (payloadGroup != null) {
            List<Payload> payloads = workspace.getActivePayloads();

            if (payloads.size() == 0) {
              // payloads size is 0, so onboard next payload group
              payloadGroup = onboardNextPayloadGroup(workspace, payloadGroup);

              if (payloadGroup != null) {
                payloads = onboardNextPayloads(workspace, payloadGroup);
              }
            }

            // Safety check; if payloads comes in null then immediately break from loop as there is
            // no work to do
            if (payloads == null || payloads.size() == 0) {
              break;
            }

            for (Payload payload : payloads) {
              if (workspace.isStopping() || workspace.isStopped()) {
                stop(workspace);
                stopped = true;
                break;
              }

              try {
                if (config.isAutoThrottle()) {
                  // Wait before starting more work
                  throttledTaskRunner.waitForLowCpuAndLowMemory();
                }

                long processStart = System.currentTimeMillis();
                swr.execute(resourceResolver, payload.getPayloadPath(), model, false, false);
                complete(workspace, payload);
                log.info(
                    "Processed [ {} ] in {} ms",
                    payload.getPayloadPath(),
                    System.currentTimeMillis() - processStart);
              } catch (WorkflowException e) {
                fail(workspace, payload);
                log.warn("Synthetic Workflow could not process [ {} ]", payload.getPath(), e);
              } catch (Exception e) {
                // Complete call failed; consider it failed
                log.warn("Complete call on [ {} ] failed", payload.getPath(), e);
                fail(workspace, payload);
              }

              total++;
            } // end for

            workspace.commit();

            if (stopped) {
              log.info("Bulk Synthetic Workflow run has been stopped.");
              break;
            }
          } // end while

          // Stop check in case a STOP request is made that breaks the while loop
          if (!stopped) {
            complete(workspace);
          }

          log.info(
              "Grand total of [ {} ] payloads saved in {} ms",
              total,
              System.currentTimeMillis() - start);
        } catch (Exception e) {
          log.error("Error processing Bulk Synthetic Workflow execution.", e);
        }
      } catch (LoginException e) {
        log.error("Error processing Bulk Synthetic Workflow execution.", e);
      } finally {
        if (resourceResolver != null) {
          resourceResolver.close();
        }
      }
    }