@Override
  public FedoraResource getVersionedAncestor() {

    try {
      if (!isFrozenResource()) {
        return null;
      }

      Node versionableFrozenNode = getNode();
      FedoraResource unfrozenResource = getUnfrozenResource();

      // traverse the frozen tree looking for a node whose unfrozen equivalent is versioned
      while (!unfrozenResource.isVersioned()) {

        if (versionableFrozenNode.getDepth() == 0) {
          return null;
        }

        // node in the frozen tree
        versionableFrozenNode = versionableFrozenNode.getParent();

        // unfrozen equivalent
        unfrozenResource = new FedoraResourceImpl(versionableFrozenNode).getUnfrozenResource();
      }

      return new FedoraResourceImpl(versionableFrozenNode);
    } catch (final RepositoryException e) {
      throw new RepositoryRuntimeException(e);
    }
  }
Example #2
0
 private JcrNodeModel getPhysicalNode(JcrNodeModel model) {
   Node node = model.getNode();
   if (node != null) {
     try {
       if (node.isNodeType("nt:version")) {
         Node frozen = node.getNode("jcr:frozenNode");
         String uuid = frozen.getProperty("jcr:frozenUuid").getString();
         try {
           Node docNode = node.getSession().getNodeByUUID(uuid);
           if (docNode.getDepth() > 0) {
             Node parent = docNode.getParent();
             if (parent.isNodeType(HippoNodeType.NT_HANDLE)) {
               return new JcrNodeModel(parent);
             }
           }
           return new JcrNodeModel(docNode);
         } catch (ItemNotFoundException infe) {
           // node doesn't exist anymore.  If it's a document, the handle
           // should still be available though.
           if (frozen.hasProperty(HippoNodeType.HIPPO_PATHS)) {
             Value[] ancestors = frozen.getProperty(HippoNodeType.HIPPO_PATHS).getValues();
             if (ancestors.length > 1) {
               uuid = ancestors[1].getString();
               return new JcrNodeModel(node.getSession().getNodeByUUID(uuid));
             }
           }
           throw infe;
         }
       }
     } catch (RepositoryException ex) {
       log.error(ex.getMessage());
     }
   }
   return model;
 }
  @Test
  public void noAclTest() throws RepositoryException {
    final String accessTo = "/dark/archive/sunshine";

    when(mockResource.getPath()).thenReturn(accessTo);
    when(mockResource.getContainer()).thenReturn(mockParentResource);
    when(mockResource.getTriples(anyObject(), eq(PropertiesRdfContext.class)))
        .thenReturn(new RdfStream());
    when(mockNode.getDepth()).thenReturn(1);

    when(mockParentResource.getNode()).thenReturn(mockParentNode);
    when(mockParentResource.getTriples(anyObject(), eq(PropertiesRdfContext.class)))
        .thenReturn(new RdfStream());
    when(mockParentNode.getDepth()).thenReturn(0);

    final Map<String, List<String>> roles = roleProvider.getRoles(mockNode, true);

    assertTrue("There should be no agents in the roles map", roles.isEmpty());
  }
  @Test
  public void acl01ParentTest() throws RepositoryException {
    final String agent = "smith123";
    final String accessTo = "/webacl_box1";
    final String acl = "/acls/01";
    final String auth = acl + "/authorization.ttl";

    when(mockResource.getPath()).thenReturn(accessTo);
    when(mockResource.getContainer()).thenReturn(mockParentResource);
    when(mockResource.getPath()).thenReturn(accessTo + "/foo");
    when(mockResource.getTriples(anyObject(), eq(PropertiesRdfContext.class)))
        .thenReturn(new RdfStream());
    when(mockNode.getDepth()).thenReturn(1);

    when(mockParentResource.getNode()).thenReturn(mockParentNode);
    when(mockParentResource.getPath()).thenReturn(accessTo);
    when(mockParentResource.getTriples(anyObject(), eq(PropertiesRdfContext.class)))
        .thenReturn(getResourceRdfStream(accessTo, acl));
    when(mockParentNode.getDepth()).thenReturn(0);

    when(mockProperty.getString()).thenReturn(acl);
    when(mockNodeService.find(mockSession, acl)).thenReturn(mockAclResource);
    when(mockAclResource.getPath()).thenReturn(acl);

    when(mockAuthorizationResource1.getTypes()).thenReturn(Arrays.asList(WEBAC_AUTHORIZATION));
    when(mockAuthorizationResource1.getPath()).thenReturn(auth);
    when(mockAuthorizationResource1.getTriples(anyObject(), eq(PropertiesRdfContext.class)))
        .thenReturn(getRdfStreamFromResource(auth, TTL));

    when(mockAclResource.getChildren())
        .thenReturn(Arrays.asList(mockAuthorizationResource1).iterator());

    final Map<String, List<String>> roles = roleProvider.getRoles(mockNode, true);

    assertEquals("There should be exactly one agent in the role map", 1, roles.size());
    assertEquals("The agent should have exactly two modes", 2, roles.get(agent).size());
    assertTrue(
        "The agent should be able to read", roles.get(agent).contains(WEBAC_MODE_READ_VALUE));
    assertTrue(
        "The agent should be able to write", roles.get(agent).contains(WEBAC_MODE_WRITE_VALUE));
  }
Example #5
0
 /* Check the current node is eligible to add mix:versionable or not
  *
  * @param node the current node
  * @param nodetypes The list of node types have child nodes which are not add mix:versionaboe while enrolling.
  * @throws Exception the exception
  */
 public static boolean isMakeVersionable(Node node, String[] nodetypes) throws Exception {
   boolean isMakeVersionable = true;
   int deep = node.getDepth();
   for (int i = 0; i < deep; i++) {
     Node parent = node.getParent();
     for (String nodetype : nodetypes) {
       if (nodetype != null && nodetype.length() > 0 && parent.isNodeType(nodetype)) return false;
     }
     node = parent;
   }
   return isMakeVersionable;
 }
  @Test
  public void testCreateObjectWithHierarchy() throws Exception {
    when(mockNode.getParent()).thenReturn(mockParent);
    when(mockParent.getParent()).thenReturn(mockRoot);
    when(mockParent.isNew()).thenReturn(true);
    when(mockRoot.getNode("foo/bar")).thenReturn(mockNode);
    when(mockNode.getDepth()).thenReturn(1);
    when(mockNode.isNew()).thenReturn(true);

    final Node actual = getJcrNode(testObj.findOrCreate(mockSession, "/foo/bar"));
    assertEquals(mockNode, actual);
    verify(mockParent).addMixin(FedoraTypes.FEDORA_PAIRTREE);
  }
  @Before
  public void setUp() throws RepositoryException {

    roleProvider = new WebACRolesProvider();
    setField(roleProvider, "nodeService", mockNodeService);
    setField(roleProvider, "sessionFactory", mockSessionFactory);

    when(mockNodeService.cast(mockNode)).thenReturn(mockResource);
    when(mockNode.getSession()).thenReturn(mockSession);
    when(mockSessionFactory.getInternalSession()).thenReturn(mockSession);

    when(mockResource.getNode()).thenReturn(mockNode);
    when(mockNode.getDepth()).thenReturn(0);
  }
 public Set<Node> getReferrers(Node document) throws RepositoryException {
   Set<Node> referrers =
       new TreeSet<Node>(
           new Comparator<Node>() {
             public int compare(Node node1, Node node2) {
               try {
                 return node1.getIdentifier().compareTo(node2.getIdentifier());
               } catch (UnsupportedRepositoryOperationException ex) {
                 // cannot happen
                 return 0;
               } catch (RepositoryException ex) {
                 return 0;
               }
             }
           });
   if (!document.isNodeType(HippoNodeType.NT_DOCUMENT)) {
     return null;
   }
   document = ((HippoNode) document).getCanonicalNode();
   Node handle = document.getParent();
   if (!handle.isNodeType(HippoNodeType.NT_HANDLE)
       || !handle.isNodeType(HippoNodeType.NT_HARDHANDLE)) {
     return null;
   }
   String uuid = handle.getIdentifier();
   QueryManager queryManager = document.getSession().getWorkspace().getQueryManager();
   String statement = "//*[@hippo:docbase='" + uuid + "']";
   Query query = queryManager.createQuery(statement, Query.XPATH);
   QueryResult result = query.execute();
   for (NodeIterator iter = result.getNodes(); iter.hasNext(); ) {
     Node node = iter.nextNode();
     while (node != null && !node.isNodeType(HippoNodeType.NT_DOCUMENT)) {
       node = (node.getDepth() > 0 ? node.getParent() : null);
     }
     if (node != null) {
       if (node.isNodeType("hippostd:publishable")
           && node.hasProperty("hippostd:state")
           && node.getProperty("hippostd:state").getString().equals("published")) {
         referrers.add(node);
       }
     }
   }
   return referrers;
 }
  @Override
  public FedoraResource getContainer() {
    try {

      if (getNode().getDepth() == 0) {
        return null;
      }

      Node container = getNode().getParent();
      while (container.getDepth() > 0) {
        if (container.isNodeType(FEDORA_PAIRTREE)
            || container.isNodeType(FEDORA_NON_RDF_SOURCE_DESCRIPTION)) {
          container = container.getParent();
        } else {
          return nodeConverter.convert(container);
        }
      }

      return nodeConverter.convert(container);
    } catch (final RepositoryException e) {
      throw new RepositoryRuntimeException(e);
    }
  }
 /**
  * Exports <code>node</code> (or the subtree rooted at <code>node</code>) into an XML document by
  * invoking SAX events on <code>contentHandler</code>.
  *
  * @param node the node which should be exported. If <code>noRecursion</code> was set to <code>
  *     false</code> in the constructor, the entire subtree rooted at <code>node</code> will be
  *     exported.
  * @param contentHandler the SAX content handler for which SAX events will be invoked as the XML
  *     document is created.
  * @param skipBinary if <code>true</code>, indicates that binary properties should not be exported
  * @param noRecurse if<code>true</code>, indicates that only the given node should be exported,
  *     otherwise a recursive export and not any of its child nodes.
  * @throws SAXException if an exception occurs during generation of the XML document
  * @throws RepositoryException if an exception occurs accessing the content repository
  */
 @Override
 public void exportNode(
     Node node, ContentHandler contentHandler, boolean skipBinary, boolean noRecurse)
     throws RepositoryException, SAXException {
   exportNode(node, contentHandler, skipBinary, noRecurse, node.getDepth() == 0);
 }
  /**
   * Exports <code>node</code> (or the subtree rooted at <code>node</code>) into an XML document by
   * invoking SAX events on <code>contentHandler</code>.
   *
   * @param node the node which should be exported. If <code>noRecursion</code> was set to <code>
   *     false</code> in the constructor, the entire subtree rooted at <code>node</code> will be
   *     exported.
   * @param contentHandler the SAX content handler for which SAX events will be invoked as the XML
   *     document is created.
   * @param skipBinary if <code>true</code>, indicates that binary properties should not be exported
   * @param noRecurse if<code>true</code>, indicates that only the given node should be exported,
   *     otherwise a recursive export and not any of its child nodes.
   * @param isRoot true if the supplied node is the root node (supplied as an efficiency)
   * @throws SAXException if an exception occurs during generation of the XML document
   * @throws RepositoryException if an exception occurs accessing the content repository
   */
  protected void exportNode(
      Node node,
      ContentHandler contentHandler,
      boolean skipBinary,
      boolean noRecurse,
      boolean isRoot)
      throws RepositoryException, SAXException {

    // start the sv:node element for this JCR node
    AttributesImpl atts = new AttributesImpl();
    String nodeName = node.getName();
    if (isRoot && node.getDepth() == 0) {
      // This is the root node ...
      nodeName = "jcr:root";
    }
    atts.addAttribute(
        JcrSvLexicon.NAME.getNamespaceUri(),
        JcrSvLexicon.NAME.getLocalName(),
        getPrefixedName(JcrSvLexicon.NAME),
        PropertyType.nameFromValue(PropertyType.STRING),
        nodeName);

    startElement(contentHandler, JcrSvLexicon.NODE, atts);

    if (node instanceof JcrSharedNode) {
      // This is a shared node, and per Section 14.7 of the JCR 2.0 specification, they have to be
      // written out
      // in a special way ...

      // jcr:primaryType = nt:share ...
      emitProperty(
          JcrLexicon.PRIMARY_TYPE,
          PropertyType.NAME,
          JcrNtLexicon.SHARE,
          contentHandler,
          skipBinary);

      // jcr:uuid = UUID of shared node ...
      emitProperty(
          JcrLexicon.UUID, PropertyType.STRING, node.getIdentifier(), contentHandler, skipBinary);
    } else {

      // Output any special properties first (see Javadoc for SPECIAL_PROPERTY_NAMES for more
      // context)
      for (Name specialPropertyName : SPECIAL_PROPERTY_NAMES) {
        Property specialProperty = ((AbstractJcrNode) node).getProperty(specialPropertyName);

        if (specialProperty != null) {
          emitProperty(specialProperty, contentHandler, skipBinary);
        }
      }

      PropertyIterator properties = node.getProperties();
      while (properties.hasNext()) {
        exportProperty(properties.nextProperty(), contentHandler, skipBinary);
      }

      if (!noRecurse) {
        NodeIterator nodes = node.getNodes();
        while (nodes.hasNext()) {
          exportNode(nodes.nextNode(), contentHandler, skipBinary, noRecurse, false);
        }
      }
    }

    endElement(contentHandler, JcrSvLexicon.NODE);
  }