private void applyJsonToAuthorizable(URL url, Authorizable authorizable, Session session)
     throws IOException, RepositoryException {
   String jsonString = IOUtils.readFully(url.openStream(), "UTF-8");
   if (jsonString != null) {
     Map<String, Object[]> postprocessParameters = new HashMap<String, Object[]>();
     try {
       JSONObject jsonObject = new JSONObject(jsonString);
       Iterator<String> keys = jsonObject.keys();
       while (keys.hasNext()) {
         String key = keys.next();
         Object jsonValue = jsonObject.get(key);
         if (key.startsWith(SlingPostConstants.RP_PREFIX)) {
           postprocessParameters.put(key, new Object[] {jsonValue});
         } else {
           Value value = JcrResourceUtil.createValue(jsonValue, session);
           authorizable.setProperty(key, value);
         }
       }
     } catch (JSONException e) {
       LOGGER.error("Faulty JSON at " + url, e);
     }
     try {
       authorizablePostProcessService.process(
           authorizable, session, ModificationType.CREATE, postprocessParameters);
     } catch (Exception e) {
       LOGGER.error("Could not configure default authorizable " + authorizable.getID(), e);
     }
   }
 }
  @Test
  public void testWriteSiteInfo() throws JSONException, RepositoryException, IOException {
    Node siteNode = new MockNode("/sites/foo");
    SiteService siteService = mock(SiteService.class);
    when(siteService.getMemberCount(siteNode)).thenReturn(11);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Writer w = new PrintWriter(baos);
    JSONWriter write = new JSONWriter(w);

    FileUtils.writeSiteInfo(siteNode, write, siteService);
    w.flush();

    String s = baos.toString("UTF-8");
    JSONObject o = new JSONObject(s);
    assertEquals("11", o.get("member-count"));
    assertEquals(siteNode.getPath(), o.get("jcr:path"));
  }
 public void setForHero(
     Elements heroElements, Node heroPanelLarge, String locale, Map<String, String> urlMap) {
   try {
     Value[] panelPropertiest = null;
     Property panelNodesProperty =
         heroPanelLarge.hasProperty("panelNodes")
             ? heroPanelLarge.getProperty("panelNodes")
             : null;
     if (panelNodesProperty.isMultiple()) {
       panelPropertiest = panelNodesProperty.getValues();
     }
     int i = 0;
     Node heroPanelNode = null;
     for (Element ele : heroElements) {
       if (panelPropertiest != null && i <= panelPropertiest.length) {
         String propertyVal = panelPropertiest[i].getString();
         if (StringUtils.isNotBlank(propertyVal)) {
           JSONObject jsonObj = new JSONObject(propertyVal);
           if (jsonObj.has("panelnode")) {
             String panelNodeProperty = jsonObj.get("panelnode").toString();
             heroPanelNode =
                 heroPanelLarge.hasNode(panelNodeProperty)
                     ? heroPanelLarge.getNode(panelNodeProperty)
                     : null;
           }
         }
         i++;
       } else {
         sb.append("<li>No heropanel Node found.</li>");
       }
       int imageSrcEmptyCount = 0;
       heroPanelTranslate(heroPanelNode, ele, locale, urlMap, imageSrcEmptyCount);
     }
   } catch (Exception e) {
   }
 }
 private void assertItem(JSONObject jsonObject, String path, String title) throws JSONException {
   assertEquals(path, jsonObject.get("value"));
   assertEquals(title, jsonObject.get("text"));
 }
Exemplo n.º 5
0
  public void internalImportContent(
      ContentManager contentManager,
      JSONObject json,
      String path,
      boolean replaceProperties,
      AccessControlManager accessControlManager)
      throws JSONException, StorageClientException, AccessDeniedException {
    Iterator<String> keys = json.keys();
    Map<String, Object> properties = new HashMap<String, Object>();
    List<AclModification> modifications = Lists.newArrayList();
    while (keys.hasNext()) {

      String key = keys.next();
      if (!key.startsWith("jcr:")) {
        Object obj = json.get(key);

        String pathKey = getPathElement(key);
        Class<?> typeHint = getElementType(key);

        if (obj instanceof JSONObject) {
          if (key.endsWith("@grant")) {
            JSONObject acl = (JSONObject) obj;
            int bitmap = getPermissionBitMap(acl.getJSONArray("permission"));
            Operation op = getOperation(acl.getString("operation"));
            modifications.add(new AclModification(AclModification.grantKey(pathKey), bitmap, op));
          } else if (key.endsWith("@deny")) {
            JSONObject acl = (JSONObject) obj;
            int bitmap = getPermissionBitMap(acl.getJSONArray("permission"));
            Operation op = getOperation(acl.getString("operation"));
            modifications.add(new AclModification(AclModification.denyKey(pathKey), bitmap, op));
          } else if (key.endsWith("@Delete")) {
            StorageClientUtils.deleteTree(contentManager, path);
          } else {
            // need to do somethingwith delete here
            internalImportContent(
                contentManager,
                (JSONObject) obj,
                path + "/" + pathKey,
                replaceProperties,
                accessControlManager);
          }
        } else if (obj instanceof JSONArray) {
          if (key.endsWith("@Delete")) {
            properties.put(pathKey, new RemoveProperty());
          } else {
            // This represents a multivalued property
            JSONArray arr = (JSONArray) obj;
            properties.put(pathKey, getArray(arr, typeHint));
          }
        } else {
          if (key.endsWith("@Delete")) {
            properties.put(pathKey, new RemoveProperty());
          } else {
            properties.put(pathKey, getObject(obj, typeHint));
          }
        }
      }
    }
    Content content = contentManager.get(path);
    if (content == null) {
      contentManager.update(new Content(path, properties));
      LOGGER.info("Created Node {} {}", path, properties);
    } else {
      for (Entry<String, Object> e : properties.entrySet()) {
        if (replaceProperties || !content.hasProperty(e.getKey())) {
          LOGGER.info("Updated Node {} {} {} ", new Object[] {path, e.getKey(), e.getValue()});
          content.setProperty(e.getKey(), e.getValue());
        }
      }
      contentManager.update(content);
    }
    if (modifications.size() > 0) {
      accessControlManager.setAcl(
          Security.ZONE_CONTENT,
          path,
          modifications.toArray(new AclModification[modifications.size()]));
    }
  }
  private void migrateHero(
      Elements heroLargeElements,
      Node midSizeUpperLeftNode,
      String locale,
      Map<String, String> urlMap)
      throws PathNotFoundException, ValueFormatException, VersionException, LockException,
          ConstraintViolationException, RepositoryException, JSONException {
    Node heroLargeNode = null;
    Value[] panelPropertiest = null;
    if (midSizeUpperLeftNode.hasNode("hero_large")) {
      heroLargeNode = midSizeUpperLeftNode.getNode("hero_large");
      Property panelNodesProperty =
          heroLargeNode.hasProperty("panelNodes") ? heroLargeNode.getProperty("panelNodes") : null;
      if (panelNodesProperty.isMultiple()) {
        panelPropertiest = panelNodesProperty.getValues();
      }
    } else {
      log.debug(
          "<li>Node with name 'hero_large' doesn't exist under "
              + midSizeUpperLeftNode.getPath()
              + "</li>");
      log.debug(
          "Node with name 'hero_large' doesn't exist under " + midSizeUpperLeftNode.getPath());
    }

    if (heroLargeElements != null) {
      Element heroLargeElement = heroLargeElements.first();
      if (heroLargeElement != null) {
        int eleSize = heroLargeElement.select("div.frame").size();
        Elements heroLargeFrameElements = heroLargeElement.select("div.frame");
        Node heroPanelNode = null;
        if (heroLargeFrameElements != null) {
          if (eleSize != heroLargeNode.getNodes("heropanel*").getSize()) {
            sb.append(Constants.MISMATCH_IN_HERO_SLIDES);
            heroLargeElement.select("div.frame").first().remove();
            heroLargeFrameElements = heroLargeElement.select("div.frame");
          }
          int i = 0;
          for (Element ele : heroLargeFrameElements) {
            String heroPanelTitle = "";
            String heroPanelDescription = "";
            String heroPanelLinkText = "";
            String heroPanellinkUrl = "";
            Elements heroTitleElements = ele.getElementsByTag("h2");
            if (heroTitleElements != null) {
              Element heroTitleElement = heroTitleElements.first();
              if (heroTitleElement != null) {
                heroPanelTitle = heroTitleElement.text();
              } else {
                sb.append(Constants.HERO_CONTENT_HEADING_ELEMENT_DOESNOT_EXISTS);
                log.debug("No h2 first element found with in the class 'frame' of div.");
              }
            } else {
              sb.append(Constants.HERO_CONTENT_HEADING_ELEMENT_DOESNOT_EXISTS);
              log.debug("No h2 found with in the class 'frame' of div.");
            }
            Elements heroDescriptionElements = ele.getElementsByTag("p");
            if (heroDescriptionElements != null) {
              Element heroDescriptionElement = heroDescriptionElements.first();
              if (heroDescriptionElement != null) {
                heroPanelDescription = heroDescriptionElement.text();
              } else {
                sb.append(Constants.HERO_CONTENT_DESCRIPTION_ELEMENT_DOESNOT_EXISTS);
                log.debug("No p frist element found with in the class 'frame' of div.");
              }
            } else {
              sb.append(Constants.HERO_CONTENT_DESCRIPTION_ELEMENT_DOESNOT_EXISTS);
              log.debug("No p elemtn found with in the class 'frame' of div.");
            }
            Elements heroPanelLinkTextElements = ele.getElementsByTag("b");
            if (heroPanelLinkTextElements != null) {
              Element heroPanelLinkTextElement = heroPanelLinkTextElements.first();
              if (heroPanelLinkTextElement != null) {
                heroPanelLinkText = heroPanelLinkTextElement.text();
              } else {
                sb.append(Constants.HERO_CONTENT_ANCHOR_TEXT_IS_BLANK);
                log.debug("No b tags first elemtn found with in the class 'frame' of div.");
              }
            } else {
              sb.append(Constants.HERO_CONTENT_ANCHOR_TEXT_IS_BLANK);
              log.debug("No b tag found with the class 'frame' of div.");
            }
            Elements heroPanelLinkUrlElements = ele.getElementsByTag("a");
            if (heroPanelLinkUrlElements != null) {
              Element heroPanelLinkUrlElement = heroPanelLinkUrlElements.first();
              if (heroPanelLinkUrlElement != null) {
                heroPanellinkUrl = heroPanelLinkUrlElement.absUrl("href");
                if (heroPanellinkUrl.equals("")) {
                  heroPanellinkUrl = heroPanelLinkUrlElement.attr("href");
                }
                // Start extracting valid href
                log.debug("heroPanellinkUrl before migration : " + heroPanellinkUrl);
                heroPanellinkUrl =
                    FrameworkUtils.getLocaleReference(heroPanellinkUrl, urlMap, locale, sb);
                log.debug("heroPanellinkUrl after migration : " + heroPanellinkUrl);
                // End extracting valid href
              } else {
                sb.append(Constants.HERO_CONTENT_ANCHOR_LINK_IS_BLANK);
                log.debug("No anchor first element found with in the class 'frame' of div.");
              }
            } else {
              sb.append(Constants.HERO_CONTENT_ANCHOR_LINK_IS_BLANK);
              log.debug("No anchor element found with in the class 'frame' of div.");
            }
            // start image
            String heroImage = FrameworkUtils.extractImagePath(ele, sb);
            log.debug("heroImage path : " + heroImage);
            // end image
            log.debug("heroPanelTitle : " + heroPanelTitle);
            log.debug("heroPanelDescription : " + heroPanelDescription);
            log.debug("heroPanelLinkText : " + heroPanelLinkText);
            log.debug("heroPanellinkUrl : " + heroPanellinkUrl);

            if (panelPropertiest != null && i <= panelPropertiest.length) {
              String propertyVal = panelPropertiest[i].getString();
              if (StringUtils.isNotBlank(propertyVal)) {
                JSONObject jsonObj = new JSONObject(propertyVal);
                if (jsonObj.has("panelnode")) {
                  String panelNodeProperty = jsonObj.get("panelnode").toString();
                  heroPanelNode =
                      heroLargeNode.hasNode(panelNodeProperty)
                          ? heroLargeNode.getNode(panelNodeProperty)
                          : null;
                  log.debug("hero_node_Name : " + heroPanelNode.getName());
                }
              }
              i++;
            } else {
              sb.append(Constants.HERO_CONTENT_NODE_NOT_FOUND);
              log.debug("No list panelProperties found for the hero compoent order.");
            }

            if (heroPanelNode != null) {
              Node heroPanelPopUpNode = null;
              Elements lightBoxElements = ele.select("div.c50-image").select("a.c26v4-lightbox");
              if (lightBoxElements != null && !lightBoxElements.isEmpty()) {
                heroPanelPopUpNode = FrameworkUtils.getHeroPopUpNode(heroPanelNode);
              }
              if (StringUtils.isNotBlank(heroPanelTitle)) {
                heroPanelNode.setProperty("title", heroPanelTitle);
                if (heroPanelPopUpNode != null) {
                  heroPanelPopUpNode.setProperty("popupHeader", heroPanelTitle);
                } else {
                  if (lightBoxElements != null && lightBoxElements.size() != 0) {
                    sb.append("<li>Hero content video pop up node not found.</li>");
                    log.debug(
                        "No pop-up node found for the hero panel node " + heroPanelNode.getPath());
                  }
                }
              } else {
                sb.append(Constants.HERO_SLIDE_TITLE_NOT_FOUND);
                log.debug("Title is blank with in the 'frame' class of div.");
              }
              if (StringUtils.isNotBlank(heroPanelDescription)) {
                heroPanelNode.setProperty("description", heroPanelDescription);
              } else {
                sb.append(Constants.HERO_SLIDE_DESCRIPTION_NOT_FOUND);
                log.debug("Description is blank with in the 'frame' class of the div.");
              }
              if (StringUtils.isNotBlank(heroPanelLinkText)) {
                heroPanelNode.setProperty("linktext", heroPanelLinkText);
              } else {
                sb.append(Constants.HERO_SLIDE_DESCRIPTION_NOT_FOUND);
                log.debug("Link Text doesn't exists with in the class 'frame' of the div.");
              }
              if (StringUtils.isNotBlank(heroPanellinkUrl)) {
                heroPanelNode.setProperty("linkurl", heroPanellinkUrl);
              } else {
                sb.append(Constants.HERO_SLIDE_LINKURL_NOT_FOUND);
                log.debug("Link url doesn't exists with in the class 'frame' of the div.");
              }
              if (heroPanelNode.hasNode("image")) {
                Node imageNode = heroPanelNode.getNode("image");
                String fileReference =
                    imageNode.hasProperty("fileReference")
                        ? imageNode.getProperty("fileReference").getString()
                        : "";
                heroImage = FrameworkUtils.migrateDAMContent(heroImage, fileReference, locale, sb);
                log.debug("heroImage : " + heroImage);
                if (StringUtils.isNotBlank(heroImage)) {
                  imageNode.setProperty("fileReference", heroImage);
                }
              } else {
                sb.append(Constants.HERO_SLIDE_IMAGE_NODE_NOT_FOUND);
                log.debug("'image' node doesn't exists in " + heroPanelNode.getPath());
              }
            }
          }
        } else {
          log.debug("<li>Hero Large Frames/Panel Elements is not found</li>");
          log.debug("No div found with class 'frame'");
        }
      } else {
        sb.append(Constants.HERO_LARGE_COMPONENT_NOT_FOUND);
        log.debug("No first element found with class 'c50-pilot'");
      }
    } else {
      sb.append("<li>Hero Large component is not found on web publisher page</li>");
      log.debug("No element found with class 'c50-pilot'");
    }
  }