Exemple #1
0
 /**
  * A real node-function (using the node argument). Returns the next newer node of same type. Also
  * a nice example on the difference between core and bridge.
  */
 public Object successor() {
   if (node == null) throw new IllegalArgumentException("successor is a node-function");
   if (cloud != null) {
     log.debug("Using bridge (security restrictions will be honoured)");
     NodeManager nm = node.getNodeManager();
     NodeQuery q = nm.createQuery();
     StepField field = q.getStepField(nm.getField("number"));
     q.setConstraint(
         q.createConstraint(
             field, FieldCompareConstraint.GREATER, Integer.valueOf(node.getNumber())));
     q.addSortOrder(field, SortOrder.ORDER_ASCENDING);
     q.setMaxNumber(1);
     NodeIterator i = nm.getList(q).nodeIterator();
     return i.hasNext() ? i.nextNode() : null;
   } else {
     log.debug("Using core.");
     throw new UnsupportedOperationException("Core implementation was dropped. See source code.");
     /* This is how it would go with core objects
     MMObjectBuilder builder = MMBase.getMMBase().getBuilder(node.getNodeManager().getName());
     NodeSearchQuery query = new NodeSearchQuery(builder);
     StepField field = query.getField(builder.getField("number"));
     BasicFieldValueConstraint cons = new BasicFieldValueConstraint(field, node.getNumber());
     cons.setOperator(FieldCompareConstraint.GREATER);
     query.setConstraint(cons);
     query.addSortOrder(field);
     query.setMaxNumber(1);
     try {
         java.util.Iterator<MMObjectNode> i = builder.getNodes(query).iterator();
         return i.hasNext() ?  i.next() : null;
     } catch (Exception e) {
         return null;
     }
     */
   }
 }
  /** Recursively outputs the contents of the given node. */
  private static void dump(Node node) throws RepositoryException {
    // First output the node path
    System.out.println(node.getPath());
    // Skip the virtual (and large!) jcr:system subtree
    if (node.getName().equals("jcr:system")) {
      return;
    }

    // Then output the properties
    PropertyIterator properties = node.getProperties();
    while (properties.hasNext()) {
      Property property = properties.nextProperty();
      if (property.getDefinition().isMultiple()) {
        // A multi-valued property, print all values
        Value[] values = property.getValues();
        for (int i = 0; i < values.length; i++) {
          System.out.println(property.getPath() + " = " + values[i].getString());
        }
      } else {
        // A single-valued property
        System.out.println(property.getPath() + " = " + property.getString());
      }
    }

    // Finally output all the child nodes recursively
    NodeIterator nodes = node.getNodes();
    while (nodes.hasNext()) {
      dump(nodes.nextNode());
    }
  }
  public VNodeDefinition(Node node) throws RepositoryException {
    name = node.getProperty(JCR_NODETYPENAME).getString();

    // do properties
    properties = new HashMap<String, VPropertyDefinitionI>();
    childSuggestions = new HashMap<String, String>();
    NodeIterator nodeIterator = node.getNodes();
    while (nodeIterator.hasNext()) {
      Node definitionNode = nodeIterator.nextNode();
      String nodeType = definitionNode.getProperty(AbstractProperty.JCR_PRIMARYTYPE).getString();

      // do a property
      if (NT_PROPERTYDEFINITION.equals(nodeType)) {
        String propertyName = "*"; // default to wildcard name
        if (definitionNode.hasProperty(JCR_NAME)) {

          // only add non-autogenerated properties
          if (!definitionNode.getProperty(JCR_AUTOCREATED).getBoolean()) {
            propertyName = definitionNode.getProperty(JCR_NAME).getString();
            properties.put(propertyName, new VPropertyDefinition(definitionNode));
          }
        } else {
          // property with no name means this node can accept custom properties
          canAddProperties = true;
        }
      }

      // do a child suggestion
      if (NT_CHILDNODEDEFINITION.equals(nodeType)) {
        String childName = "*";
        // only do well-defined childnodedefinitions with the following 2 jcr properties
        if (definitionNode.hasProperty(JCR_NAME)
            && definitionNode.hasProperty(JCR_DEFAULTPRIMARYTYPE)) {
          childSuggestions.put(
              definitionNode.getProperty(JCR_NAME).getString(),
              definitionNode.getProperty(JCR_DEFAULTPRIMARYTYPE).getString());
        }
      }
    }

    // do supertypes
    supertypes = new HashSet<String>();
    if (node.hasProperty(JCR_SUPERTYPES)) {
      for (Value value : node.getProperty(JCR_SUPERTYPES).getValues()) {
        supertypes.add(value.getString());
      }
    }

    // set mixin status
    isMixin = node.hasProperty(JCR_ISMIXIN) && node.getProperty(JCR_ISMIXIN).getBoolean();
  }
 void unregisterNewNode(JCRNodeWrapper node) {
   if (!newNodes.isEmpty()) {
     newNodes.remove(node.getPath());
     try {
       if (node.hasNodes()) {
         NodeIterator it = node.getNodes();
         while (it.hasNext()) {
           unregisterNewNode((JCRNodeWrapper) it.next());
         }
       }
     } catch (RepositoryException e) {
       logger.warn("Error unregistering new nodes", e);
     }
   }
 }
Exemple #5
0
 public RDFNode lookup(Resource identity, String sourceValue) {
   if (!_model.contains(identity, Vertere.lookup)) {
     return null;
   }
   Resource lookupResource = _model.getProperty(identity, Vertere.lookup).getResource();
   NodeIterator listObjectsOfProperty =
       _model.listObjectsOfProperty(lookupResource, Vertere.lookup_entry);
   while (listObjectsOfProperty.hasNext()) {
     RDFNode entry = listObjectsOfProperty.next();
     Resource asResource = entry.asResource();
     String key = _model.getProperty(asResource, Vertere.lookup_key).getString();
     if (sourceValue.equals(key)) {
       return _model.getProperty(asResource, Vertere.lookup_value).getObject();
     }
   }
   return null;
 }
 public static void buildDefinitions(Session session) {
   log.info("started building node definitions");
   String nodeName = "";
   try {
     allNodes = new HashMap<String, VNodeDefinition>();
     Node rootNode = session.getNode("/jcr:system/jcr:nodeTypes");
     NodeIterator nodeIterator = rootNode.getNodes();
     while (nodeIterator.hasNext()) {
       Node node = nodeIterator.nextNode();
       nodeName = node.getName();
       VNodeDefinition vNodeDefinition = new VNodeDefinition(node);
       customizeDefinition(vNodeDefinition); // possibly customize this definition
       allNodes.put(nodeName, vNodeDefinition);
     }
     log.info("finished building nodes");
   } catch (RepositoryException re) {
     log.warn("Could not build node definitions, died at " + nodeName, re);
   } finally {
     if (session != null) session.logout();
   }
 }
Exemple #7
0
  public static void validate(@Nullable Node original, Node n) {
    if (original != null) {
      validate(null, original);
    }

    if (Null.red) {
      trouble(original, n, new RuntimeException("red null!"));
    }

    if (Null.left != Null || Null.right != Null || Null.value != Null || Null.key != null) {
      trouble(original, n, new RuntimeException("corrupted null!"));
    }

    if (n.red) {
      trouble(original, n, new RuntimeException("red root!"));
    }

    NodeStack s = new NodeStack();
    int blackCount = -1;
    for (NodeIterator it = new NodeIterator(s, n); it.hasNext(); ) {
      int index = it.stack.index;
      Node x = it.next();

      if (x.key instanceof Node) {
        trouble(original, n, new RuntimeException("node key!"));
      }

      if (x.key == null) {
        trouble(original, n, new RuntimeException("null key!"));
      }

      if (x.value instanceof Node) {
        validate(original, (Node) x.value);
      }

      if (x.red && (x.left.red || x.right.red)) {
        trouble(original, n, new RuntimeException("red node has red child(ren)!"));
      }

      if (x.left == Node.Null && x.right == Node.Null) {
        int count = 0;
        for (int i = 0; i < index; ++i) {
          if (!s.array[i].red) {
            ++count;
          }
        }
        if (!x.red) {
          ++count;
        }

        if (blackCount == -1) {
          blackCount = count;
        } else if (count != blackCount) {
          trouble(
              original,
              n,
              new RuntimeException("inconsistent number of black nodes per paths to leaves!"));
        }
      }
    }
  }
Exemple #8
0
  @GET
  @Path("/{type}")
  @Produces({Utils.MEDIA_TYPE_APPLICATION_HAL_PLUS_JSON, MediaType.APPLICATION_JSON})
  public Object getByType(
      @PathParam("workspace") String workspace,
      @PathParam("language") String language,
      @PathParam("type") String type,
      @QueryParam("nameContains") List<String> nameConstraints,
      @QueryParam("orderBy") String orderBy,
      @QueryParam("limit") int limit,
      @QueryParam("offset") int offset,
      @QueryParam("depth") int depth,
      @Context UriInfo context) {

    if (API.isQueryDisabled()) {
      APIExceptionMapper.LOGGER.debug("Types endpoint is disabled. Attempted query on " + type);
      return Response.status(Response.Status.NOT_FOUND).build();
    }

    final String unescapedNodetype = Names.unescape(type);
    if (API.excludedNodeTypes.contains(unescapedNodetype)) {
      return Response.status(Response.Status.FORBIDDEN)
          .entity("'" + unescapedNodetype + "' is not available for querying.")
          .build();
    }

    Session session = null;

    try {

      session = getSession(workspace, language);
      final QueryObjectModelFactory qomFactory =
          session.getWorkspace().getQueryManager().getQOMFactory();
      final ValueFactory valueFactory = session.getValueFactory();
      final Selector selector = qomFactory.selector(unescapedNodetype, SELECTOR_NAME);

      // language constraint: either jcr:language doesn't exist or jcr:language is current language
      Constraint constraint =
          qomFactory.or(
              qomFactory.not(qomFactory.propertyExistence(SELECTOR_NAME, Constants.JCR_LANGUAGE)),
              stringComparisonConstraint(
                  qomFactory.propertyValue(SELECTOR_NAME, Constants.JCR_LANGUAGE),
                  language,
                  qomFactory,
                  valueFactory));

      // if we have passed "nameContains" query parameters, only return nodes which name contains
      // the specified terms
      if (nameConstraints != null && !nameConstraints.isEmpty()) {
        for (String name : nameConstraints) {
          final Comparison likeConstraint =
              qomFactory.comparison(
                  qomFactory.nodeLocalName(SELECTOR_NAME),
                  QueryObjectModelFactory.JCR_OPERATOR_LIKE,
                  qomFactory.literal(
                      valueFactory.createValue("%" + name + "%", PropertyType.STRING)));
          constraint = qomFactory.and(constraint, likeConstraint);
        }
      }

      Ordering[] orderings = null;
      // ordering deactivated because it currently doesn't work, probably due to a bug in
      // QueryServiceImpl
      if (Utils.exists(orderBy)) {
        if ("desc".equalsIgnoreCase(orderBy)) {
          orderings =
              new Ordering[] {qomFactory.descending(qomFactory.nodeLocalName(SELECTOR_NAME))};
        } else {
          orderings =
              new Ordering[] {qomFactory.ascending(qomFactory.nodeLocalName(SELECTOR_NAME))};
        }
      }

      final QueryObjectModel query =
          qomFactory.createQuery(
              selector,
              constraint,
              orderings,
              new Column[] {qomFactory.column(SELECTOR_NAME, null, null)});
      if (limit > 0) {
        query.setLimit(limit);
      }
      query.setOffset(offset);

      final QueryResult queryResult = query.execute();

      final NodeIterator nodes = queryResult.getNodes();
      final List<JSONNode> result = new LinkedList<JSONNode>();
      final Filter filter = Utils.getFilter(context);
      while (nodes.hasNext()) {
        final Node resultNode = nodes.nextNode();
        if (filter.acceptChild(resultNode)) {
          JSONNode node = getFactory().createNode(resultNode, filter, depth);
          result.add(node);
        }
      }

      return Response.ok(result).build();
    } catch (Exception e) {
      throw new APIException(e);
    } finally {
      closeSession(session);
    }
  }
  /**
   * Get JSON map for a given resource by applying the river settings
   *
   * @param rs resource being processed
   * @param properties properties to be indexed
   * @param model model returned by the indexing query
   * @param getPropLabel if set to true all URI property values will be indexed as their label. The
   *     label is taken as the value of one of the properties set in {@link #uriDescriptionList}.
   * @return map of properties to be indexed for res
   */
  private Map<String, ArrayList<String>> getJsonMap(
      Resource rs, Set<Property> properties, Model model, boolean getPropLabel) {
    Map<String, ArrayList<String>> jsonMap = new HashMap<String, ArrayList<String>>();
    ArrayList<String> results = new ArrayList<String>();

    if (addUriForResource) {
      results.add("\"" + rs.toString() + "\"");
      jsonMap.put("http://www.w3.org/1999/02/22-rdf-syntax-ns#about", results);
    }

    Set<String> rdfLanguages = new HashSet<String>();

    for (Property prop : properties) {
      NodeIterator niter = model.listObjectsOfProperty(rs, prop);
      String property = prop.toString();
      results = new ArrayList<String>();

      String lang;
      String currValue;

      while (niter.hasNext()) {
        RDFNode node = niter.next();
        currValue = getStringForResult(node, getPropLabel);
        if (addLanguage) {
          if (node.isLiteral()) {
            lang = node.asLiteral().getLanguage();
            if (!lang.isEmpty()) {
              rdfLanguages.add("\"" + lang + "\"");
            }
          }
        }

        String shortValue = currValue;

        int currLen = currValue.length();
        // Unquote string
        if (currLen > 1) shortValue = currValue.substring(1, currLen - 1);

        // If either whiteMap does contains shortValue
        // or blackMap contains the value
        // skip adding it to the index
        boolean whiteMapCond =
            whiteMap.containsKey(property) && !whiteMap.get(property).contains(shortValue);
        boolean blackMapCond =
            blackMap.containsKey(property) && blackMap.get(property).contains(shortValue);

        if (whiteMapCond || blackMapCond) {
          continue;
        }

        if (normalizeObj.containsKey(shortValue)) {
          results.add("\"" + normalizeObj.get(shortValue) + "\"");
        } else {
          results.add(currValue);
        }
      }

      // Do not index empty properties
      if (results.isEmpty()) continue;

      if (normalizeProp.containsKey(property)) {
        property = normalizeProp.get(property);
        if (jsonMap.containsKey(property)) {
          jsonMap.get(property).addAll(results);
        } else {
          jsonMap.put(property, results);
        }
      } else {
        jsonMap.put(property, results);
      }
    }

    if (addLanguage) {
      if (rdfLanguages.isEmpty() && !language.isEmpty()) rdfLanguages.add(language);
      if (!rdfLanguages.isEmpty()) jsonMap.put("language", new ArrayList<String>(rdfLanguages));
    }

    for (Map.Entry<String, String> it : normalizeMissing.entrySet()) {
      if (!jsonMap.containsKey(it.getKey())) {
        ArrayList<String> res = new ArrayList<String>();
        res.add("\"" + it.getValue() + "\"");
        jsonMap.put(it.getKey(), res);
      }
    }

    return jsonMap;
  }