Exemplo n.º 1
0
  @Override
  public void updateFile(
      long companyId, long repositoryId, String fileName, String versionLabel, InputStream is)
      throws PortalException, SystemException {

    Session session = null;

    try {
      session = JCRFactoryUtil.createSession();

      Workspace workspace = session.getWorkspace();

      VersionManager versionManager = workspace.getVersionManager();

      Node rootNode = getRootNode(session, companyId);

      Node repositoryNode = getFolderNode(rootNode, repositoryId);

      if (fileName.contains(StringPool.SLASH)) {
        String path = fileName.substring(0, fileName.lastIndexOf(StringPool.SLASH));

        fileName = fileName.substring(path.length() + 1);

        repositoryNode = getFolderNode(repositoryNode, path);
      }

      Node fileNode = repositoryNode.getNode(fileName);

      Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);

      versionManager.checkout(contentNode.getPath());

      contentNode.setProperty(JCRConstants.JCR_MIME_TYPE, "text/plain");

      ValueFactory valueFactory = session.getValueFactory();

      Binary binary = valueFactory.createBinary(is);

      contentNode.setProperty(JCRConstants.JCR_DATA, binary);

      contentNode.setProperty(JCRConstants.JCR_LAST_MODIFIED, Calendar.getInstance());

      session.save();

      Version version = versionManager.checkin(contentNode.getPath());

      VersionHistory versionHistory = versionManager.getVersionHistory(contentNode.getPath());

      versionHistory.addVersionLabel(
          version.getName(), versionLabel, PropsValues.DL_STORE_JCR_MOVE_VERSION_LABELS);
    } catch (PathNotFoundException pnfe) {
      throw new NoSuchFileException(
          "{fileName=" + fileName + ", versionLabel=" + versionLabel + "}");
    } catch (RepositoryException re) {
      throw new SystemException(re);
    } finally {
      JCRFactoryUtil.closeSession(session);
    }
  }
Exemplo n.º 2
0
  @Override
  public void addFile(long companyId, long repositoryId, String fileName, InputStream is)
      throws PortalException, SystemException {

    Session session = null;

    try {
      session = JCRFactoryUtil.createSession();

      Workspace workspace = session.getWorkspace();

      VersionManager versionManager = workspace.getVersionManager();

      Node rootNode = getRootNode(session, companyId);

      Node repositoryNode = getFolderNode(rootNode, repositoryId);

      if (fileName.contains(StringPool.SLASH)) {
        String path = fileName.substring(0, fileName.lastIndexOf(StringPool.SLASH));

        fileName = fileName.substring(path.length() + 1);

        repositoryNode = getFolderNode(repositoryNode, path);
      }

      if (repositoryNode.hasNode(fileName)) {
        throw new DuplicateFileException(fileName);
      } else {
        Node fileNode = repositoryNode.addNode(fileName, JCRConstants.NT_FILE);

        Node contentNode = fileNode.addNode(JCRConstants.JCR_CONTENT, JCRConstants.NT_RESOURCE);

        contentNode.addMixin(JCRConstants.MIX_VERSIONABLE);
        contentNode.setProperty(JCRConstants.JCR_MIME_TYPE, "text/plain");

        ValueFactory valueFactory = session.getValueFactory();

        Binary binary = valueFactory.createBinary(is);

        contentNode.setProperty(JCRConstants.JCR_DATA, binary);

        contentNode.setProperty(JCRConstants.JCR_LAST_MODIFIED, Calendar.getInstance());

        session.save();

        Version version = versionManager.checkin(contentNode.getPath());

        VersionHistory versionHistory = versionManager.getVersionHistory(contentNode.getPath());

        versionHistory.addVersionLabel(version.getName(), VERSION_DEFAULT, false);
      }
    } catch (RepositoryException re) {
      throw new SystemException(re);
    } finally {
      JCRFactoryUtil.closeSession(session);
    }
  }
Exemplo n.º 3
0
  @Override
  public void deleteFile(long companyId, long repositoryId, String fileName, String versionLabel)
      throws PortalException, SystemException {

    Session session = null;

    try {
      session = JCRFactoryUtil.createSession();

      Workspace workspace = session.getWorkspace();

      VersionManager versionManager = workspace.getVersionManager();

      Node rootNode = getRootNode(session, companyId);

      Node repositoryNode = getFolderNode(rootNode, repositoryId);

      Node fileNode = repositoryNode.getNode(fileName);

      Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);

      VersionHistory versionHistory = versionManager.getVersionHistory(contentNode.getPath());

      if (!versionHistory.hasVersionLabel(versionLabel)) {
        throw new NoSuchFileException(
            "{fileName=" + fileName + ", versionLabel=" + versionLabel + "}");
      }

      Version version = versionHistory.getVersionByLabel(versionLabel);

      versionManager.restore(version.getPredecessors()[0], true);

      versionHistory.removeVersion(version.getName());

      session.save();
    } catch (PathNotFoundException pnfe) {
      throw new NoSuchFileException(
          "{fileName=" + fileName + ", versionLabel=" + versionLabel + "}");
    } catch (RepositoryException re) {
      throw new SystemException(re);
    } finally {
      JCRFactoryUtil.closeSession(session);
    }
  }
  @Test
  public void testGet()
      throws VersionException, UnsupportedRepositoryOperationException, InvalidItemStateException,
          LockException, RepositoryException, IOException, ServletException {
    SlingHttpServletRequest request = createNiceMock(SlingHttpServletRequest.class);
    SlingHttpServletResponse response = createNiceMock(SlingHttpServletResponse.class);
    Resource resource = createNiceMock(Resource.class);
    Node node = createNiceMock(Node.class);
    Version version = createNiceMock(Version.class);
    PropertyIterator propertyIterator = createNiceMock(PropertyIterator.class);
    VersionHistory versionHistory = createNiceMock(VersionHistory.class);
    VersionIterator versionIterator = createNiceMock(VersionIterator.class);
    Node versionNode = createNiceMock(Node.class);
    Session session = createNiceMock(Session.class);
    Workspace workspace = createNiceMock(Workspace.class);
    VersionManager versionManager = createNiceMock(VersionManager.class);

    EasyMock.expect(request.getResource()).andReturn(resource).anyTimes();
    EasyMock.expect(resource.adaptTo(Node.class)).andReturn(node).anyTimes();
    PrintWriter pw = new PrintWriter(new ByteArrayOutputStream());
    EasyMock.expect(response.getWriter()).andReturn(pw).anyTimes();

    EasyMock.expect(node.getSession()).andReturn(session);
    EasyMock.expect(session.getWorkspace()).andReturn(workspace);
    EasyMock.expect(workspace.getVersionManager()).andReturn(versionManager);
    EasyMock.expect(versionManager.getVersionHistory("/foo")).andReturn(versionHistory);
    EasyMock.expect(node.getPath()).andReturn("/foo").anyTimes();
    EasyMock.expect(versionHistory.getAllVersions()).andReturn(versionIterator);
    EasyMock.expect(versionIterator.getSize()).andReturn(2L);
    EasyMock.expect(versionIterator.hasNext()).andReturn(true);
    EasyMock.expect(versionIterator.nextVersion()).andReturn(version);
    EasyMock.expect(versionIterator.hasNext()).andReturn(true);
    EasyMock.expect(versionIterator.nextVersion()).andReturn(version);

    EasyMock.expect(version.getName()).andReturn("NameVersioNode").anyTimes();
    EasyMock.expect(version.getNode(JcrConstants.JCR_FROZENNODE)).andReturn(versionNode).anyTimes();
    EasyMock.expect(version.getProperties()).andReturn(propertyIterator).anyTimes();
    EasyMock.expect(propertyIterator.hasNext()).andReturn(false).anyTimes();

    replay();

    listVersionsServlet.doGet(request, response);
    verify();
  }
Exemplo n.º 5
0
  protected Node getFileContentNode(
      Session session, long companyId, long repositoryId, String fileName, String versionLabel)
      throws PortalException, SystemException {

    Node contentNode = null;

    try {
      Workspace workspace = session.getWorkspace();

      VersionManager versionManager = workspace.getVersionManager();

      Node rootNode = getRootNode(session, companyId);

      Node repositoryNode = getFolderNode(rootNode, repositoryId);

      Node fileNode = repositoryNode.getNode(fileName);

      contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);

      if (Validator.isNotNull(versionLabel)) {
        VersionHistory versionHistory = versionManager.getVersionHistory(contentNode.getPath());

        if (!versionHistory.hasVersionLabel(versionLabel)) {
          throw new NoSuchFileException(
              "{fileName=" + fileName + ", versionLabel=" + versionLabel + "}");
        }

        Version version = versionHistory.getVersionByLabel(versionLabel);

        contentNode = version.getNode(JCRConstants.JCR_FROZEN_NODE);
      }
    } catch (PathNotFoundException pnfe) {
      throw new NoSuchFileException(
          "{fileName=" + fileName + ", versionLabel=" + versionLabel + "}");
    } catch (RepositoryException re) {
      throw new SystemException(re);
    }

    return contentNode;
  }
Exemplo n.º 6
0
  public void updateFile(long companyId, long repositoryId, String fileName, String newFileName)
      throws PortalException, SystemException {

    Session session = null;

    try {
      session = JCRFactoryUtil.createSession();

      Workspace workspace = session.getWorkspace();

      VersionManager versionManager = workspace.getVersionManager();

      Node rootNode = getRootNode(session, companyId);

      Node repositoryNode = getFolderNode(rootNode, repositoryId);

      if (fileName.contains(StringPool.SLASH)) {
        String path = fileName.substring(0, fileName.lastIndexOf(StringPool.SLASH));

        fileName = fileName.substring(path.length() + 1);

        repositoryNode = getFolderNode(repositoryNode, path);
      }

      Node fileNode = repositoryNode.getNode(fileName);

      Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);

      Node newFileNode = repositoryNode.addNode(newFileName, JCRConstants.NT_FILE);

      Node newContentNode = newFileNode.addNode(JCRConstants.JCR_CONTENT, JCRConstants.NT_RESOURCE);

      VersionHistory versionHistory = versionManager.getVersionHistory(contentNode.getPath());

      String[] versionLabels = versionHistory.getVersionLabels();

      for (int i = (versionLabels.length - 1); i >= 0; i--) {
        Version version = versionHistory.getVersionByLabel(versionLabels[i]);

        Node frozenContentNode = version.getNode(JCRConstants.JCR_FROZEN_NODE);

        if (i == (versionLabels.length - 1)) {
          newContentNode.addMixin(JCRConstants.MIX_VERSIONABLE);
        } else {
          versionManager.checkout(newContentNode.getPath());
        }

        newContentNode.setProperty(JCRConstants.JCR_MIME_TYPE, "text/plain");

        copyBinaryProperty(frozenContentNode, newContentNode, JCRConstants.JCR_DATA);

        newContentNode.setProperty(JCRConstants.JCR_LAST_MODIFIED, Calendar.getInstance());

        session.save();

        Version newVersion = versionManager.checkin(newContentNode.getPath());

        VersionHistory newVersionHistory =
            versionManager.getVersionHistory(newContentNode.getPath());

        newVersionHistory.addVersionLabel(
            newVersion.getName(), versionLabels[i], PropsValues.DL_STORE_JCR_MOVE_VERSION_LABELS);
      }

      fileNode.remove();

      session.save();
    } catch (PathNotFoundException pnfe) {
      throw new NoSuchFileException(fileName);
    } catch (RepositoryException re) {
      throw new SystemException(re);
    } finally {
      JCRFactoryUtil.closeSession(session);
    }
  }
Exemplo n.º 7
0
  @Override
  public void deleteFile(long companyId, long repositoryId, String fileName)
      throws PortalException, SystemException {

    Session session = null;

    // A bug in Jackrabbit requires us to create a dummy node and delete the
    // version tree manually to successfully delete a file

    // Create a dummy node

    try {
      session = JCRFactoryUtil.createSession();

      Workspace workspace = session.getWorkspace();

      VersionManager versionManager = workspace.getVersionManager();

      Node rootNode = getRootNode(session, companyId);

      Node repositoryNode = getFolderNode(rootNode, repositoryId);

      Node fileNode = repositoryNode.getNode(fileName);

      Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);

      versionManager.checkout(contentNode.getPath());

      contentNode.setProperty(JCRConstants.JCR_MIME_TYPE, "text/plain");
      contentNode.setProperty(JCRConstants.JCR_DATA, "");
      contentNode.setProperty(JCRConstants.JCR_LAST_MODIFIED, Calendar.getInstance());

      session.save();

      Version version = versionManager.checkin(contentNode.getPath());

      VersionHistory versionHistory = versionManager.getVersionHistory(contentNode.getPath());

      versionHistory.addVersionLabel(version.getName(), "0.0", false);
    } catch (PathNotFoundException pnfe) {
      throw new NoSuchFileException(fileName);
    } catch (RepositoryException re) {
      throw new SystemException(re);
    } finally {
      JCRFactoryUtil.closeSession(session);
    }

    // Delete version tree

    try {
      session = JCRFactoryUtil.createSession();

      Workspace workspace = session.getWorkspace();

      VersionManager versionManager = workspace.getVersionManager();

      Node rootNode = getRootNode(session, companyId);

      Node repositoryNode = getFolderNode(rootNode, repositoryId);

      Node fileNode = repositoryNode.getNode(fileName);

      Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);

      VersionHistory versionHistory = versionManager.getVersionHistory(contentNode.getPath());

      VersionIterator itr = versionHistory.getAllVersions();

      while (itr.hasNext()) {
        Version version = itr.nextVersion();

        if (itr.getPosition() == itr.getSize()) {
          break;
        } else {
          if (!StringUtils.equals(JCRConstants.JCR_ROOT_VERSION, version.getName())) {

            versionHistory.removeVersion(version.getName());
          }
        }
      }

      session.save();
    } catch (PathNotFoundException pnfe) {
      throw new NoSuchFileException(fileName);
    } catch (RepositoryException re) {
      throw new SystemException(re);
    } finally {
      JCRFactoryUtil.closeSession(session);
    }

    // Delete file

    try {
      session = JCRFactoryUtil.createSession();

      Node rootNode = getRootNode(session, companyId);

      Node repositoryNode = getFolderNode(rootNode, repositoryId);

      Node fileNode = repositoryNode.getNode(fileName);

      fileNode.remove();

      session.save();
    } catch (PathNotFoundException pnfe) {
      throw new NoSuchFileException(fileName);
    } catch (RepositoryException re) {
      throw new SystemException(re);
    } finally {
      JCRFactoryUtil.closeSession(session);
    }
  }
Exemplo n.º 8
0
  @SuppressWarnings("unchecked")
  @Test
  public void test()
      throws ServletException, IOException, UnsupportedRepositoryOperationException,
          RepositoryException {
    SlingHttpServletRequest request = createNiceMock(SlingHttpServletRequest.class);
    SlingHttpServletResponse response = createNiceMock(SlingHttpServletResponse.class);
    RequestPathInfo requestPathInfo = createNiceMock(RequestPathInfo.class);
    Resource resource = createNiceMock(Resource.class);
    Node node = createNiceMock(Node.class);
    VersionHistory versionHistory = createNiceMock(VersionHistory.class);
    Version version = createNiceMock(Version.class);
    Node frozenNode = createNiceMock(Node.class);
    Property resourceTypeProperty = createNiceMock(Property.class);
    Capture<Resource> capturedResource = new Capture<Resource>();
    RequestDispatcher requestDispatcher = createNiceMock(RequestDispatcher.class);
    Capture<ServletRequest> capturedRequest = new Capture<ServletRequest>();
    Capture<ServletResponse> capturedResponse = new Capture<ServletResponse>();
    VersionManager versionManager = createNiceMock(VersionManager.class);
    Session session = createNiceMock(Session.class);
    Workspace workspace = createNiceMock(Workspace.class);

    EasyMock.expect(request.getRequestPathInfo()).andReturn(requestPathInfo);
    EasyMock.expect(requestPathInfo.getSelectorString()).andReturn("version.,1.1,.tidy.json");
    EasyMock.expect(request.getResource()).andReturn(resource);
    EasyMock.expect(resource.adaptTo(Node.class)).andReturn(node);
    EasyMock.expect(node.getPath()).andReturn("/foo");
    EasyMock.expect(node.getSession()).andReturn(session);
    EasyMock.expect(session.getWorkspace()).andReturn(workspace);
    EasyMock.expect(workspace.getVersionManager()).andReturn(versionManager);
    EasyMock.expect(versionManager.getVersionHistory("/foo")).andReturn(versionHistory);
    EasyMock.expect(versionHistory.getVersion("1.1")).andReturn(version);
    EasyMock.expect(version.getNode(JcrConstants.JCR_FROZENNODE)).andReturn(frozenNode);
    EasyMock.expect(frozenNode.getPath()).andReturn("/testnode");
    EasyMock.expect(frozenNode.hasProperty(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY))
        .andReturn(true);
    EasyMock.expect(frozenNode.getProperty(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY))
        .andReturn(resourceTypeProperty);
    EasyMock.expect(resourceTypeProperty.getString()).andReturn("sakai/testing");
    EasyMock.expect(frozenNode.hasProperty(JcrResourceConstants.SLING_RESOURCE_SUPER_TYPE_PROPERTY))
        .andReturn(true);
    EasyMock.expect(frozenNode.getProperty(JcrResourceConstants.SLING_RESOURCE_SUPER_TYPE_PROPERTY))
        .andReturn(resourceTypeProperty);
    EasyMock.expect(resourceTypeProperty.getString()).andReturn("sakai/super-type-testing");
    EasyMock.expect(request.getRequestDispatcher(EasyMock.capture(capturedResource)))
        .andReturn(requestDispatcher);
    requestDispatcher.forward(
        EasyMock.capture(capturedRequest), EasyMock.capture(capturedResponse));
    EasyMock.expectLastCall();

    replay();
    getVersionServlet.doGet(request, response);

    Assert.assertTrue(capturedRequest.hasCaptured());
    Assert.assertTrue(capturedResource.hasCaptured());
    Assert.assertTrue(capturedResponse.hasCaptured());

    Resource cresource = capturedResource.getValue();

    Node capturedNode = cresource.adaptTo(Node.class);
    Assert.assertEquals(capturedNode, frozenNode);

    Map<String, String> map = cresource.adaptTo(Map.class);
    Assert.assertNotNull(map);

    verify();
  }