@Test
  public void testVersionRemoval() throws Exception {
    DocumentModel folder = session.createDocumentModel("/", "folder", "Folder");
    folder = session.createDocument(folder);
    DocumentModel file = session.createDocumentModel("/folder", "file", "File");
    file = session.createDocument(file);
    DocumentModel proxy = session.publishDocument(file, folder);
    DocumentModel version = session.getLastDocumentVersion(file.getRef());
    session.save();

    // even Administrator/system cannot remove a version with a proxy
    try {
      session.removeDocument(version.getRef());
      fail("Admin should not be able to remove version");
    } catch (DocumentSecurityException e) {
      // ok
    }

    // remove the proxy first
    session.removeDocument(proxy.getRef());
    session.save();
    // we can now remove the version
    session.removeDocument(version.getRef());
    session.save();
  }
  @Test
  public void testRemoveSingleDocVersion() throws Exception {
    DocumentModel folder = new DocumentModelImpl("/", "folder#1", "Folder");
    folder = session.createDocument(folder);

    DocumentModel file = new DocumentModelImpl(folder.getPathAsString(), "file#1", "File");
    file = session.createDocument(file);

    checkVersions(file);

    file.setPropertyValue("file:filename", "A");
    file.putContextData(
        ScopeType.REQUEST, VersioningDocument.CREATE_SNAPSHOT_ON_SAVE_KEY, Boolean.TRUE);
    file = session.saveDocument(file);

    checkVersions(file, "0.1");

    DocumentModel lastversion = session.getLastDocumentVersion(file.getRef());
    assertNotNull(lastversion);

    assertTrue(lastversion.isVersion());
    session.removeDocument(lastversion.getRef());

    checkVersions(file);
  }
Example #3
0
  @Test
  public void testDefaultCreateTwiceFromSameBlob() throws Exception {
    // create doc
    File file = getTestFile("test-data/hello.doc");
    Blob input = Blobs.createBlob(file, "application/msword");

    DocumentModel doc =
        service.createDocumentFromBlob(
            coreSession, input, workspace.getPathAsString(), true, "test-data/hello.doc");
    DocumentRef docRef = doc.getRef();

    assertNotNull(doc);
    assertEquals("hello.doc", doc.getProperty("dublincore", "title"));
    assertEquals("hello.doc", doc.getProperty("file", "filename"));
    assertNotNull(doc.getProperty("file", "content"));

    List<DocumentModel> versions = coreSession.getVersions(docRef);
    assertEquals(0, versions.size());

    // create again with same file
    doc =
        service.createDocumentFromBlob(
            coreSession, input, workspace.getPathAsString(), true, "test-data/hello.doc");
    assertNotNull(doc);

    DocumentRef newDocRef = doc.getRef();
    assertEquals(docRef, newDocRef);
    assertEquals("hello.doc", doc.getProperty("dublincore", "title"));
    assertEquals("hello.doc", doc.getProperty("file", "filename"));
    assertNotNull(doc.getProperty("file", "content"));

    versions = coreSession.getVersions(docRef);
    assertEquals(1, versions.size());
  }
Example #4
0
  @Test
  public void testDefaultUpdateFromBlob() throws Exception {
    // create doc
    File file = getTestFile("test-data/hello.doc");
    Blob input = Blobs.createBlob(file, "application/msword");

    DocumentModel doc =
        service.createDocumentFromBlob(
            coreSession, input, workspace.getPathAsString(), true, "test-data/hello.doc");
    DocumentRef docRef = doc.getRef();

    assertNotNull(doc);
    assertEquals("hello.doc", doc.getProperty("dublincore", "title"));
    assertEquals("hello.doc", doc.getProperty("file", "filename"));
    assertNotNull(doc.getProperty("file", "content"));

    // update it with another file with same name
    doc =
        service.updateDocumentFromBlob(
            coreSession, input, workspace.getPathAsString(), "test-data/update/hello.doc");
    assertNotNull(doc);

    DocumentRef newDocRef = doc.getRef();
    assertEquals(docRef, newDocRef);
    assertEquals("hello.doc", doc.getProperty("dublincore", "title"));
    assertEquals("hello.doc", doc.getProperty("file", "filename"));
    assertNotNull(doc.getProperty("file", "content"));
  }
  @Test
  public void testReadAclAfterCopy() {
    DocumentModel folder1 = new DocumentModelImpl("/", "folder1", "Folder");
    folder1 = session.createDocument(folder1);
    DocumentModel folder2 = new DocumentModelImpl("/", "folder2", "Folder");
    folder2 = session.createDocument(folder2);
    DocumentModel doc = new DocumentModelImpl("/folder1", "doc", "File");
    doc = session.createDocument(doc);

    // set ACL on folder2
    ACL acl = new ACLImpl();
    acl.add(new ACE("Everyone", "Read", true));
    ACP acp = new ACPImpl();
    acp.addACL(acl);
    folder2.setACP(acp, true);
    session.save();

    // doc under folder1 cannot be read by joe
    try (CoreSession joeSession = openSessionAs("joe")) {
      DocumentModelList list = joeSession.query("SELECT * FROM File");
      assertEquals(0, list.size());
    }

    // copy doc under folder2
    session.copy(doc.getRef(), folder2.getRef(), "doccopy");
    session.save();

    // check doc copy now readable by joe
    try (CoreSession joeSession = openSessionAs("joe")) {
      DocumentModelList list = joeSession.query("SELECT * FROM File");
      assertEquals(1, list.size());
      assertEquals("doccopy", list.get(0).getName());
    }
  }
  @Test
  public void testCopy() {
    DocumentModel doc = session.createDocumentModel("/", "file", "File");
    doc = session.createDocument(doc);
    session.save();
    String versionSeriesId = doc.getVersionSeriesId();

    // copy
    DocumentModel copy =
        session.copy(doc.getRef(), session.getRootDocument().getRef(), "fileCopied");

    // check different version series id
    assertNotSame(versionSeriesId, copy.getVersionSeriesId());

    // create version and proxy
    DocumentModel folder = session.createDocumentModel("/", "folder", "Folder");
    folder = session.createDocument(folder);
    DocumentModel proxy = session.publishDocument(doc, folder);
    // check same version series id
    assertEquals(versionSeriesId, proxy.getVersionSeriesId());

    // copy proxy
    DocumentModel proxyCopy =
        session.copy(proxy.getRef(), session.getRootDocument().getRef(), "proxyCopied");
    // check same version series id
    assertEquals(versionSeriesId, proxyCopy.getVersionSeriesId());
  }
  @Test
  public void testPublishing() {
    DocumentModel folder = session.createDocumentModel("/", "folder", "Folder");
    folder = session.createDocument(folder);
    DocumentModel doc = session.createDocumentModel("/", "file", "File");
    doc = session.createDocument(doc);
    checkVersions(doc);

    // publish
    DocumentModel proxy = session.publishDocument(doc, folder);
    session.save();
    String versionSeriesId = doc.getVersionSeriesId();
    assertFalse(proxy.isVersion());
    assertTrue(proxy.isProxy());
    assertTrue(proxy.hasFacet(FacetNames.IMMUTABLE));
    assertTrue(proxy.isImmutable());
    assertEquals(versionSeriesId, proxy.getVersionSeriesId());
    assertNotSame(versionSeriesId, proxy.getId());
    assertEquals("0.1", proxy.getVersionLabel());
    assertNull(proxy.getCheckinComment());
    assertFalse(proxy.isMajorVersion());
    assertTrue(proxy.isLatestVersion());
    assertFalse(proxy.isLatestMajorVersion());

    checkVersions(doc, "0.1");
    VersionModel lastVersion = session.getLastVersion(doc.getRef());
    assertNotNull(lastVersion);
    assertEquals("0.1", lastVersion.getLabel());
    DocumentModel lastVersionDocument = session.getLastDocumentVersion(doc.getRef());
    assertNotNull(lastVersionDocument);
    assertEquals("file", lastVersionDocument.getName());
  }
  @Test
  public void testAutoCheckOut() throws Exception {
    DocumentModel doc = new DocumentModelImpl("/", "file", "File");
    doc.setPropertyValue("dc:title", "t0");
    doc = session.createDocument(doc);
    assertTrue(doc.isCheckedOut());
    session.checkIn(doc.getRef(), null, null);
    doc.refresh();
    assertFalse(doc.isCheckedOut());

    // auto-checkout
    doc.setPropertyValue("dc:title", "t1");
    doc = session.saveDocument(doc);
    assertTrue(doc.isCheckedOut());

    session.checkIn(doc.getRef(), null, null);
    doc.refresh();
    assertFalse(doc.isCheckedOut());

    // disable auto-checkout
    doc.setPropertyValue("dc:title", "t2");
    doc.putContextData(VersioningService.DISABLE_AUTO_CHECKOUT, Boolean.TRUE);
    doc = session.saveDocument(doc);
    assertFalse(doc.isCheckedOut());
    assertEquals("t2", doc.getPropertyValue("dc:title"));

    // can still be checked out normally afterwards
    doc.checkOut();
    assertTrue(doc.isCheckedOut());
    assertEquals("t2", doc.getPropertyValue("dc:title"));
  }
 protected void setPermission(
     DocumentModel doc, String userName, String permission, boolean isGranted) {
   ACP acp = session.getACP(doc.getRef());
   ACL localACL = acp.getOrCreateACL(ACL.LOCAL_ACL);
   localACL.add(new ACE(userName, permission, isGranted));
   session.setACP(doc.getRef(), acp, true);
   session.save();
 }
  @Test
  public void testGetParentDocuments() {

    setPermissionToAnonymous(EVERYTHING);

    DocumentModel root = session.getRootDocument();

    String name = "Workspaces#1";
    DocumentModel workspaces = new DocumentModelImpl(root.getPathAsString(), name, "Workspace");
    session.createDocument(workspaces);
    String name2 = "repositoryWorkspace2#";
    DocumentModel repositoryWorkspace =
        new DocumentModelImpl(workspaces.getPathAsString(), name2, "Workspace");
    repositoryWorkspace = session.createDocument(repositoryWorkspace);

    String name3 = "ws#3";
    DocumentModel ws1 =
        new DocumentModelImpl(repositoryWorkspace.getPathAsString(), name3, "Workspace");
    ws1 = session.createDocument(ws1);
    String name4 = "ws#4";
    DocumentModel ws2 = new DocumentModelImpl(ws1.getPathAsString(), name4, "Workspace");
    session.createDocument(ws2);

    if (session.isNegativeAclAllowed()) { // always false for Mem
      ACP acp = new ACPImpl();
      ACE denyRead = new ACE("test", READ, false);
      ACL acl = new ACLImpl();
      acl.setACEs(new ACE[] {denyRead});
      acp.addACL(acl);
      // TODO this produces a stack trace
      repositoryWorkspace.setACP(acp, true);
      ws1.setACP(acp, true);
    }

    session.save();

    List<DocumentModel> ws2ParentsUnderAdministrator = session.getParentDocuments(ws2.getRef());
    assertTrue(
        "list parents for"
            + ws2.getName()
            + "under "
            + session.getPrincipal().getName()
            + " is not empty:",
        !ws2ParentsUnderAdministrator.isEmpty());

    CoreSession testSession = openSessionAs("test");
    List<DocumentModel> ws2ParentsUnderTest = testSession.getParentDocuments(ws2.getRef());
    assertTrue(
        "list parents for"
            + ws2.getName()
            + "under "
            + testSession.getPrincipal().getName()
            + " is empty:",
        ws2ParentsUnderTest.isEmpty());
    closeSession(testSession);
  }
 protected void resetPermissions(DocumentModel doc, String userName) {
   ACP acp = session.getACP(doc.getRef());
   ACL localACL = acp.getOrCreateACL(ACL.LOCAL_ACL);
   Iterator<ACE> localACLIt = localACL.iterator();
   while (localACLIt.hasNext()) {
     ACE ace = localACLIt.next();
     if (userName.equals(ace.getUsername())) {
       localACLIt.remove();
     }
   }
   session.setACP(doc.getRef(), acp, true);
   session.save();
 }
  protected DocumentRef createDocumentModelWithSamplePermissions(String name) {
    DocumentModel root = session.getRootDocument();
    DocumentModel doc = new DocumentModelImpl(root.getPathAsString(), name, "Folder");
    doc = session.createDocument(doc);

    ACP acp = doc.getACP();
    ACL localACL = acp.getOrCreateACL();

    localACL.add(new ACE("joe_reader", READ, true));

    localACL.add(new ACE("joe_contributor", READ, true));
    localACL.add(new ACE("joe_contributor", WRITE_PROPERTIES, true));
    localACL.add(new ACE("joe_contributor", ADD_CHILDREN, true));

    localACL.add(new ACE("joe_localmanager", READ, true));
    localACL.add(new ACE("joe_localmanager", WRITE, true));
    localACL.add(new ACE("joe_localmanager", WRITE_SECURITY, true));

    acp.addACL(localACL);
    doc.setACP(acp, true);

    // add the permission to remove children on the root
    ACP rootACP = root.getACP();
    ACL rootACL = rootACP.getOrCreateACL();
    rootACL.add(new ACE("joe_localmanager", REMOVE_CHILDREN, true));
    rootACP.addACL(rootACL);
    root.setACP(rootACP, true);

    // make it visible for others
    session.save();
    return doc.getRef();
  }
  @Override
  protected void doIndexingWork(ElasticSearchIndexing esi, List<IndexingCommand> cmds) {
    if (cmds.isEmpty()) {
      return;
    }
    IndexingCommand cmd = cmds.get(0);
    DocumentModel doc = getDocument(cmd);
    if (doc == null) {
      return;
    }
    DocumentModelIterator iter = session.getChildrenIterator(doc.getRef());
    while (iter.hasNext()) {
      // Add a session save to process cache invalidation
      session.save();
      DocumentModel child = iter.next();

      IndexingCommand childCommand = cmd.clone(child);

      if (!esi.isAlreadyScheduled(childCommand)) {
        esi.indexNonRecursive(childCommand);
      }
      if (child.isFolder()) {
        ChildrenIndexingWorker subWorker = new ChildrenIndexingWorker(monitor, childCommand);
        WorkManager wm = Framework.getLocalService(WorkManager.class);
        wm.schedule(subWorker);
      }
    }
  }
  @Test
  public void testSaveRestoredVersionWithVersionAutoIncrement() {
    // check-in version 1.0, 2.0 and restore version 1.0
    DocumentModel doc = new DocumentModelImpl("/", "myfile", "File");
    doc = session.createDocument(doc);
    doc = session.saveDocument(doc);
    DocumentRef co = doc.getRef();
    DocumentRef ci1 = session.checkIn(co, VersioningOption.MAJOR, "first check-in");
    session.checkOut(co);
    maybeSleepToNextSecond();
    DocumentRef ci2 = session.checkIn(co, VersioningOption.MAJOR, "second check-in");
    waitForFulltextIndexing();
    maybeSleepToNextSecond();
    session.restoreToVersion(co, ci1);

    // save document with auto-increment should produce version 3.0
    doc = session.getDocument(co);
    assertEquals(doc.getVersionLabel(), "1.0");
    doc.getContextData()
        .putScopedValue(
            ScopeType.DEFAULT, VersioningService.VERSIONING_OPTION, VersioningOption.MAJOR);
    // mark as dirty - must change the value
    doc.setPropertyValue("dc:title", doc.getPropertyValue("dc:title") + " dirty");
    doc = session.saveDocument(doc);
    assertEquals(doc.getVersionLabel(), "3.0");
  }
  /**
   * Processes the version selection row.
   *
   * @param versionModelSelection the version model selection
   * @throws ClientException the client exception
   */
  protected final void processVersionSelectRow(PageSelection<VersionModel> versionModelSelection)
      throws ClientException {

    DocumentModel currentDocument = navigationContext.getCurrentDocument();
    if (currentDocument == null) {
      throw new ClientException(
          "Cannot process version select row since current document is null.");
    }

    DocumentModel version =
        documentManager.getDocumentWithVersion(
            currentDocument.getRef(), versionModelSelection.getData());
    if (version == null) {
      throw new ClientException(
          "Cannot process version select row since selected version document is null.");
    }

    if (Boolean.TRUE.equals(versionModelSelection.isSelected())) {
      documentsListsManager.addToWorkingList(
          DocumentsListsManager.CURRENT_VERSION_SELECTION, version);
    } else {
      documentsListsManager.removeFromWorkingList(
          DocumentsListsManager.CURRENT_VERSION_SELECTION, version);
    }
  }
  public int unlock(String userName) throws ClientException {

    LockInfo lockInfo = getLockInfo();
    boolean canUnLock = false;
    int returnCode = NOT_LOCKED;

    if (lockInfo != null) {
      // document is already locked
      String lockingUser = lockInfo.getUserName();

      if (lockingUser == null) {
        canUnLock = true;
        returnCode = NOT_LOCKED;
      } else {
        if (lockingUser.equals(userName)) {
          canUnLock = true;
          returnCode = ALREADY_LOCKED_BY_YOU;
        } else {
          if (isLockExpired()) {
            canUnLock = true;
            returnCode = LOCK_EXPIRED;
          } else {
            canUnLock = false;
            return CAN_NOT_UNLOCK;
          }
        }
      }
    }

    if (canUnLock) {
      getSessionFromDoc(targetDoc).unlock(targetDoc.getRef());
    }

    return returnCode;
  }
  /**
   * A&ntilde;ade un hijo al arbol.
   *
   * @param result XML resultado
   * @param parent Elemento padre del xml
   * @param root Documento ra&iacute;z.
   * @param depth profundidad
   * @return devuelve un elemento XML
   */
  private Element addChildren(DOMDocument result, Element parent, DocumentModel root, int depth) {
    try {

      List<DocumentModel> hijos = documentManager.getChildren(root.getRef());

      for (DocumentModel documento : hijos) {
        String estado = documento.getCurrentLifeCycleState();
        if (!"deleted".equals(estado) && documento.isFolder()) {
          Element hijo = result.createElement("document");

          hijo.setAttribute("title", documento.getTitle());
          hijo.setAttribute("type", documento.getType());
          hijo.setAttribute("id", documento.getId());

          parent.appendChild(hijo);

          if (depth > ArchivoConstantes.NUMERO_UNO) {
            addChildren(result, hijo, documento, depth - ArchivoConstantes.NUMERO_UNO);
          }
        }
      }
    } catch (ClientException e) {
      LOG.error("No se pudo a&ntilde;adir elementos al xml ", e);
    }
    return parent;
  }
  public int lock(String userName) throws ClientException {

    LockInfo lockInfo = getLockInfo();
    boolean canLock = false;
    int returnCode = LOCKED_OK;

    if (lockInfo != null) {
      // document is already locked
      String lockingUser = lockInfo.getUserName();

      if (lockingUser == null) {
        canLock = true;
      } else {
        if (lockingUser.equals(userName)) {
          canLock = true;
          return ALREADY_LOCKED_BY_YOU;
        } else {
          if (isLockExpired()) {
            canLock = true;
            returnCode = LOCK_BORROWED;
          } else {
            canLock = false;
            return CAN_NOT_BORROW_LOCK;
          }
        }
      }
    }

    CoreSession session = getSessionFromDoc(targetDoc);
    session.setLock(targetDoc.getRef(), getLockToken(userName));
    session.save();

    return returnCode;
  }
 public void processSelectRow(
     String docRef, String contentViewName, String listName, Boolean selection)
     throws ClientException {
   List<DocumentModel> documents = getCurrentPageDocuments(contentViewName);
   DocumentModel doc = null;
   if (documents != null) {
     for (DocumentModel pagedDoc : documents) {
       if (pagedDoc.getRef().toString().equals(docRef)) {
         doc = pagedDoc;
         break;
       }
     }
   }
   if (doc == null) {
     log.error(
         String.format(
             "could not find doc '%s' in the current page of " + "content view page provider '%s'",
             docRef, contentViewName));
     return;
   }
   String lName = (listName == null) ? DocumentsListsManager.CURRENT_DOCUMENT_SELECTION : listName;
   if (Boolean.TRUE.equals(selection)) {
     documentsListsManager.addToWorkingList(lName, doc);
   } else {
     documentsListsManager.removeFromWorkingList(lName, doc);
   }
 }
  @Test
  public void testAllowVersionWrite() {
    DocumentModel doc = session.createDocumentModel("/", "doc", "File");
    doc.setPropertyValue("icon", "icon1");
    doc = session.createDocument(doc);
    DocumentRef verRef = session.checkIn(doc.getRef(), null, null);

    // regular version cannot be written
    DocumentModel ver = session.getDocument(verRef);
    ver.setPropertyValue("icon", "icon2");
    try {
      session.saveDocument(ver);
      fail("Should not allow version write");
    } catch (PropertyException e) {
      assertTrue(e.getMessage(), e.getMessage().contains("Cannot set property on a version"));
    }

    // with proper option, it's allowed
    ver.setPropertyValue("icon", "icon3");
    ver.putContextData(CoreSession.ALLOW_VERSION_WRITE, Boolean.TRUE);
    session.saveDocument(ver);
    // refetch to check
    ver = session.getDocument(verRef);
    assertEquals("icon3", ver.getPropertyValue("icon"));
  }
Example #21
0
  @Test
  public void testCreateNoteTwiceFromSameBlob() throws Exception {
    // create doc
    File file = getTestFile("test-data/hello.html");
    Blob input = Blobs.createBlob(file, "text/html");

    DocumentModel doc =
        service.createDocumentFromBlob(
            coreSession, input, workspace.getPathAsString(), true, "test-data/hello.html");
    DocumentRef docRef = doc.getRef();

    assertNotNull(doc);
    assertEquals("hello.html", doc.getProperty("dublincore", "title"));
    String expectedNoteTest = NOTE_HTML_CONTENT;
    String noteText = ((String) doc.getProperty("note", "note"));
    if (SystemUtils.IS_OS_WINDOWS) {
      expectedNoteTest = expectedNoteTest.trim();
      expectedNoteTest = expectedNoteTest.replace("\n", "");
      expectedNoteTest = expectedNoteTest.replace("\r", "");
      noteText = expectedNoteTest.trim();
      noteText = expectedNoteTest.replace("\n", "");
      noteText = expectedNoteTest.replace("\r", "");
    }
    assertEquals(expectedNoteTest, noteText);

    List<DocumentModel> versions = coreSession.getVersions(docRef);
    assertEquals(0, versions.size());

    // create again with same file
    doc =
        service.createDocumentFromBlob(
            coreSession, input, workspace.getPathAsString(), true, "test-data/hello.html");
    assertNotNull(doc);
    DocumentRef newDocRef = doc.getRef();
    assertEquals(docRef, newDocRef);
    assertEquals("hello.html", doc.getProperty("dublincore", "title"));
    noteText = ((String) doc.getProperty("note", "note"));
    if (SystemUtils.IS_OS_WINDOWS) {
      noteText = expectedNoteTest.trim();
      noteText = expectedNoteTest.replace("\n", "");
      noteText = expectedNoteTest.replace("\r", "");
    }
    assertEquals(expectedNoteTest, noteText);

    versions = coreSession.getVersions(docRef);
    assertEquals(1, versions.size());
  }
Example #22
0
 public void unpublish(DocumentModel doc, PublicationNode targetNode) {
   List<PublishedDocument> publishedDocs = getPublishedDocumentInNode(targetNode);
   for (PublishedDocument pubDoc : publishedDocs) {
     if (pubDoc.getSourceDocumentRef().equals(doc.getRef())) {
       unpublish(pubDoc);
     }
   }
 }
Example #23
0
  public DocumentModel getDocumentFor(String mainTabId, DocumentModel defaultDocument) {
    DocumentModel doc = documentsByMainTabs.get(mainTabId);
    if (doc == null
        || !documentManager.exists(doc.getRef())
        || !documentManager.hasPermission(doc.getRef(), SecurityConstants.READ)) {
      documentsByMainTabs.put(mainTabId, defaultDocument);
      doc = null;
    }

    if (doc != null && !documentManager.exists(new PathRef(doc.getPathAsString()))) {
      // path has changed, refresh the document to have a correct URL
      doc = documentManager.getDocument(doc.getRef());
      documentsByMainTabs.put(mainTabId, doc);
    }

    return doc != null ? doc : defaultDocument;
  }
Example #24
0
  @Test
  public void shouldFilterLogEntriesOnEventCategories() throws Exception {
    DocumentModel doc = RestServerInit.getFile(1, session);

    List<LogEntry> logEntries = new ArrayList<>();
    LogEntry logEntry = auditLogger.newLogEntry();
    logEntry.setDocUUID(doc.getRef());
    logEntry.setCategory("One");
    logEntry.setEventId("firstEvent");
    logEntries.add(logEntry);
    logEntry = auditLogger.newLogEntry();
    logEntry.setDocUUID(doc.getRef());
    logEntry.setCategory("One");
    logEntry.setEventId("secondEvent");
    logEntries.add(logEntry);
    logEntry = auditLogger.newLogEntry();
    logEntry.setDocUUID(doc.getRef());
    logEntry.setCategory("Two");
    logEntry.setEventId("firstEvent");
    logEntries.add(logEntry);
    auditLogger.addLogEntries(logEntries);

    TransactionHelper.commitOrRollbackTransaction();
    TransactionHelper.startTransaction();

    MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
    queryParams.add("category", "One");
    queryParams.add("category", "Two");
    ClientResponse response =
        getResponse(
            BaseTest.RequestType.GET, "id/" + doc.getId() + "/@" + AuditAdapter.NAME, queryParams);
    assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
    JsonNode node = mapper.readTree(response.getEntityInputStream());
    List<JsonNode> nodes = getLogEntries(node);
    assertEquals(3, nodes.size());

    queryParams = new MultivaluedMapImpl();
    queryParams.add("category", "Two");
    response =
        getResponse(
            BaseTest.RequestType.GET, "id/" + doc.getId() + "/@" + AuditAdapter.NAME, queryParams);
    assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
    node = mapper.readTree(response.getEntityInputStream());
    nodes = getLogEntries(node);
    assertEquals(1, nodes.size());
  }
Example #25
0
  @Test
  public void testUpdateFileDocWithPlainTextFile() throws Exception {

    // create a File whose title is "hello.html" and content is "hello.rtf"
    File file = getTestFile("test-data/hello.rtf");
    Blob input = Blobs.createBlob(file, "text/rtf", null, "hello.html");

    DocumentModel doc =
        coreSession.createDocumentModel(workspace.getPathAsString(), "hello.html", "File");
    doc.setPropertyValue("dc:title", "hello.html");
    doc.setPropertyValue("file:content", (Serializable) input);
    doc.setPropertyValue("file:filename", "hello.html");

    // create doc
    doc = coreSession.createDocument(doc);
    coreSession.save();
    DocumentRef docRef = doc.getRef();

    assertNotNull(doc);
    assertEquals("hello.html", doc.getProperty("dublincore", "title"));
    assertEquals("hello.html", doc.getProperty("file", "filename"));
    assertNotNull(doc.getProperty("file", "content"));
    assertTrue(extractText(doc).contains("RTF"));
    assertEquals("text/rtf", getMimeType(doc));

    List<DocumentModel> versions = coreSession.getVersions(docRef);
    assertEquals(0, versions.size());

    // update the with a file that matches the same importer
    file = getTestFile("test-data/hello.html");
    input = Blobs.createBlob(file, "text/html");
    doc =
        service.createDocumentFromBlob(
            coreSession, input, workspace.getPathAsString(), true, "test-data/hello.html");
    assertNotNull(doc);

    DocumentRef newDocRef = doc.getRef();
    assertEquals(docRef, newDocRef);
    assertEquals("hello.html", doc.getProperty("file", "filename"));
    assertNotNull(doc.getProperty("file", "content"));
    assertTrue(extractText(doc).contains("HTML"));
    assertEquals("text/html", getMimeType(doc));

    versions = coreSession.getVersions(docRef);
    assertEquals(1, versions.size());
  }
 protected void assertLatestVersion(String expected, DocumentModel doc) throws Exception {
   DocumentModel ver = doc.getCoreSession().getLastDocumentVersion(doc.getRef());
   if (ver == null) {
     assertNull(expected);
   } else {
     assertVersion(expected, ver);
   }
 }
  @Test
  public void testRestoreToVersion() throws Exception {
    String name2 = "file#456";
    DocumentModel doc = new DocumentModelImpl("/", name2, "File");
    doc = session.createDocument(doc);
    DocumentRef docRef = doc.getRef();

    session.save();
    DocumentRef v1Ref = session.checkIn(docRef, null, null);
    assertFalse(session.isCheckedOut(docRef));
    session.checkOut(docRef);
    assertTrue(session.isCheckedOut(docRef));

    doc.setProperty("file", "filename", "second name");
    doc.setProperty("dc", "title", "f1");
    doc.setProperty("dc", "description", "desc 1");
    session.saveDocument(doc);
    session.save();

    maybeSleepToNextSecond();
    DocumentRef v2Ref = session.checkIn(docRef, null, null);
    session.checkOut(docRef);

    DocumentModel newDoc = session.getDocument(docRef);
    assertNotNull(newDoc);
    assertNotNull(newDoc.getRef());
    assertEquals("second name", newDoc.getProperty("file", "filename"));

    waitForFulltextIndexing();
    maybeSleepToNextSecond();
    DocumentModel restoredDoc = session.restoreToVersion(docRef, v1Ref);

    assertNotNull(restoredDoc);
    assertNotNull(restoredDoc.getRef());
    assertNull(restoredDoc.getProperty("file", "filename"));

    waitForFulltextIndexing();
    maybeSleepToNextSecond();
    restoredDoc = session.restoreToVersion(docRef, v2Ref);

    assertNotNull(restoredDoc);
    assertNotNull(restoredDoc.getRef());
    String pr = (String) restoredDoc.getProperty("file", "filename");
    assertEquals("second name", pr);
  }
  protected DocumentModel subPublish(
      CoreSession session, DocumentModel parentProxy, Snapshot tree, boolean skipParent)
      throws ClientException {

    DocumentModel newFolderishProxy = null;
    if (skipParent) {
      newFolderishProxy = parentProxy;
    } else {
      DocumentModel version = tree.getDocument();
      newFolderishProxy = session.createProxy(version.getRef(), parentProxy.getRef());
    }

    for (Snapshot snap : tree.getChildrenSnapshots()) {
      subPublish(session, newFolderishProxy, snap, false);
    }

    return newFolderishProxy;
  }
Example #29
0
 public void unpublish(PublishedDocument publishedDocument) {
   if (!accept(publishedDocument)) {
     return;
   }
   DocumentModel proxy = ((SimpleCorePublishedDocument) publishedDocument).getProxy();
   PublicationRelationHelper.removePublicationRelation(proxy);
   getCoreSession().removeDocument(proxy.getRef());
   getCoreSession().save();
 }
  // Creates 3 versions and removes the last.
  @Test
  public void testRemoveLastDocVersion() throws Exception {
    DocumentModel folder = new DocumentModelImpl("/", "folder#1", "Folder");
    folder = session.createDocument(folder);

    DocumentModel file = new DocumentModelImpl(folder.getPathAsString(), "file#1", "File");
    file = session.createDocument(file);

    createTrioVersions(file);

    final int VERSION_INDEX = 2;
    DocumentModel lastversion = session.getVersions(file.getRef()).get(VERSION_INDEX);
    assertNotNull(lastversion);

    assertTrue(lastversion.isVersion());
    session.removeDocument(lastversion.getRef());

    checkVersions(file, "0.1", "0.2");
  }