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);
     }
   }
 }
  /**
   * Extract the value of a MultiFieldPanel property into a list of maps. Will never return a null
   * map, but may return an empty one. Invalid property values are logged and skipped.
   *
   * @param resource the resource
   * @param name the property name
   * @return a list of maps.
   */
  @Function
  public static List<Map<String, String>> getMultiFieldPanelValues(Resource resource, String name) {
    ValueMap map = resource.adaptTo(ValueMap.class);
    List<Map<String, String>> results = new ArrayList<Map<String, String>>();
    if (map.containsKey(name)) {
      String[] values = map.get(name, new String[0]);
      for (String value : values) {

        try {
          JSONObject parsed = new JSONObject(value);
          Map<String, String> columnMap = new HashMap<String, String>();
          for (Iterator<String> iter = parsed.keys(); iter.hasNext(); ) {
            String key = iter.next();
            String innerValue = parsed.getString(key);
            columnMap.put(key, innerValue);
          }

          results.add(columnMap);

        } catch (JSONException e) {
          log.error(
              String.format("Unable to parse JSON in %s property of %s", name, resource.getPath()),
              e);
        }
      }
    }
    return results;
  }
Esempio n. 3
0
  /**
   * @param propertiesMap
   * @param queryOptions
   * @return
   * @throws JSONException
   * @throws MissingParameterException
   */
  private Map<String, String> processOptions(
      Map<String, String> propertiesMap, JSONObject queryOptions)
      throws JSONException, MissingParameterException {
    Collection<String> missingTerms;
    Map<String, String> options = Maps.newHashMap();
    if (queryOptions != null) {
      Iterator<String> keys = queryOptions.keys();
      while (keys.hasNext()) {
        String key = keys.next();
        String val = queryOptions.getString(key);
        missingTerms = templateService.missingTerms(propertiesMap, val);
        if (!missingTerms.isEmpty()) {
          throw new MissingParameterException(
              "Your request is missing parameters for the template: "
                  + StringUtils.join(missingTerms, ", "));
        }

        String processedVal = templateService.evaluateTemplate(propertiesMap, val);
        options.put(key, processedVal);
      }
    }
    return options;
  }
Esempio n. 4
0
 /** {@inheritDoc} */
 public Iterator<String> getPropertyNames() {
   return data.keys();
 }
Esempio 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()]));
    }
  }