Search in sources :

Example 6 with ContentWriter

use of org.alfresco.service.cmr.repository.ContentWriter in project alfresco-remote-api by Alfresco.

the class TestCMIS method testCreationDate.

/**
 * MNT-12680
 * The creation date of version should be the same as creation date of the original node
 * @throws Exception
 */
@Test
public void testCreationDate() throws Exception {
    // create a site
    final TestNetwork network1 = getTestFixture().getRandomNetwork();
    String username = "user" + System.currentTimeMillis();
    PersonInfo personInfo = new PersonInfo(username, username, username, "password", null, null, null, null, null, null, null);
    TestPerson person1 = network1.createUser(personInfo);
    String person1Id = person1.getId();
    final String siteName = "site" + System.currentTimeMillis();
    TenantUtil.runAsUserTenant(new TenantRunAsWork<NodeRef>() {

        @Override
        public NodeRef doWork() throws Exception {
            SiteInformation siteInfo = new SiteInformation(siteName, siteName, siteName, SiteVisibility.PUBLIC);
            final TestSite site = network1.createSite(siteInfo);
            final NodeRef resNode = repoService.createDocument(site.getContainerNodeRef("documentLibrary"), "testdoc.txt", "Test Doc1 Title", "Test Doc1 Description", "Test Content");
            return resNode;
        }
    }, person1Id, network1.getId());
    // create a document
    publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1Id));
    CmisSession cmisSession = publicApiClient.createPublicApiCMISSession(Binding.atom, CMIS_VERSION_10, AlfrescoObjectFactoryImpl.class.getName());
    AlfrescoFolder docLibrary = (AlfrescoFolder) cmisSession.getObjectByPath("/Sites/" + siteName + "/documentLibrary");
    Map<String, String> properties = new HashMap<String, String>();
    {
        properties.put(PropertyIds.OBJECT_TYPE_ID, TYPE_CMIS_DOCUMENT);
        properties.put(PropertyIds.NAME, "mydoc-" + GUID.generate() + ".txt");
    }
    ContentStreamImpl fileContent = new ContentStreamImpl();
    {
        ContentWriter writer = new FileContentWriter(TempFileProvider.createTempFile(GUID.generate(), ".txt"));
        writer.putContent("some content");
        ContentReader reader = writer.getReader();
        fileContent.setMimeType(MimetypeMap.MIMETYPE_TEXT_PLAIN);
        fileContent.setStream(reader.getContentInputStream());
    }
    Document autoVersionedDoc = docLibrary.createDocument(properties, fileContent, VersioningState.MAJOR);
    String objectId = autoVersionedDoc.getId();
    String bareObjectId = stripCMISSuffix(objectId);
    // create versions
    for (int i = 0; i < 3; i++) {
        Document doc1 = (Document) cmisSession.getObject(bareObjectId);
        ObjectId pwcId = doc1.checkOut();
        Document pwc = (Document) cmisSession.getObject(pwcId.getId());
        ContentStreamImpl contentStream = new ContentStreamImpl();
        {
            ContentWriter writer = new FileContentWriter(TempFileProvider.createTempFile(GUID.generate(), ".txt"));
            writer.putContent(GUID.generate());
            ContentReader reader = writer.getReader();
            contentStream.setMimeType(MimetypeMap.MIMETYPE_TEXT_PLAIN);
            contentStream.setStream(reader.getContentInputStream());
        }
        pwc.checkIn(true, Collections.EMPTY_MAP, contentStream, "checkin " + i);
    }
    GregorianCalendar cDateFirst = cmisSession.getAllVersions(bareObjectId).get(0).getCreationDate();
    GregorianCalendar cDateSecond = cmisSession.getAllVersions(bareObjectId).get(2).getCreationDate();
    if (cDateFirst.before(cDateSecond) || cDateFirst.after(cDateSecond)) {
        fail("The creation date of version should be the same as creation date of the original node");
    }
}
Also used : ContentStreamImpl(org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl) AlfrescoObjectFactoryImpl(org.alfresco.cmis.client.impl.AlfrescoObjectFactoryImpl) CmisSession(org.alfresco.rest.api.tests.client.PublicApiClient.CmisSession) HashMap(java.util.HashMap) ObjectId(org.apache.chemistry.opencmis.client.api.ObjectId) TestSite(org.alfresco.rest.api.tests.RepoService.TestSite) AlfrescoFolder(org.alfresco.cmis.client.AlfrescoFolder) FileContentWriter(org.alfresco.repo.content.filestore.FileContentWriter) ContentReader(org.alfresco.service.cmr.repository.ContentReader) GregorianCalendar(java.util.GregorianCalendar) AlfrescoDocument(org.alfresco.cmis.client.AlfrescoDocument) Document(org.apache.chemistry.opencmis.client.api.Document) CmisUpdateConflictException(org.apache.chemistry.opencmis.commons.exceptions.CmisUpdateConflictException) CmisConstraintException(org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException) CmisPermissionDeniedException(org.apache.chemistry.opencmis.commons.exceptions.CmisPermissionDeniedException) CmisInvalidArgumentException(org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException) PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException) CmisObjectNotFoundException(org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) FileContentWriter(org.alfresco.repo.content.filestore.FileContentWriter) SiteInformation(org.alfresco.rest.api.tests.RepoService.SiteInformation) TestNetwork(org.alfresco.rest.api.tests.RepoService.TestNetwork) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) TestPerson(org.alfresco.rest.api.tests.RepoService.TestPerson) VersionableAspectTest(org.alfresco.repo.version.VersionableAspectTest) Test(org.junit.Test)

Example 7 with ContentWriter

use of org.alfresco.service.cmr.repository.ContentWriter in project alfresco-remote-api by Alfresco.

the class TestPeople method createAvatarDirect.

private NodeRef createAvatarDirect(NodeRef personRef, File avatarFile) {
    // create new avatar node
    nodeService.addAspect(personRef, ContentModel.ASPECT_PREFERENCES, null);
    ChildAssociationRef assoc = nodeService.createNode(personRef, ContentModel.ASSOC_PREFERENCE_IMAGE, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "origAvatar"), ContentModel.TYPE_CONTENT);
    final NodeRef avatarRef = assoc.getChildRef();
    // JSF client compatibility?
    nodeService.createAssociation(personRef, avatarRef, ContentModel.ASSOC_AVATAR);
    // upload the avatar content
    ContentService contentService = applicationContext.getBean("ContentService", ContentService.class);
    ContentWriter writer = contentService.getWriter(avatarRef, ContentModel.PROP_CONTENT, true);
    writer.guessMimetype(avatarFile.getName());
    writer.putContent(avatarFile);
    Rendition avatarR = new Rendition();
    avatarR.setId("avatar");
    Renditions renditions = applicationContext.getBean("Renditions", Renditions.class);
    renditions.createRendition(avatarRef, avatarR, false, null);
    return avatarRef;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) Rendition(org.alfresco.rest.api.model.Rendition) Renditions(org.alfresco.rest.api.Renditions) ContentService(org.alfresco.service.cmr.repository.ContentService) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef)

Example 8 with ContentWriter

use of org.alfresco.service.cmr.repository.ContentWriter in project alfresco-remote-api by Alfresco.

the class RepoService method createDocument.

public NodeRef createDocument(final NodeRef parentNodeRef, final String name, final String title, final String description, final String content) {
    NodeRef nodeRef = fileFolderService.create(parentNodeRef, name, ContentModel.TYPE_CONTENT).getNodeRef();
    ContentWriter writer = contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true);
    writer.putContent(content);
    nodeService.setProperty(nodeRef, ContentModel.PROP_TITLE, title);
    nodeService.setProperty(nodeRef, ContentModel.PROP_DESCRIPTION, description);
    return nodeRef;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter)

Example 9 with ContentWriter

use of org.alfresco.service.cmr.repository.ContentWriter in project alfresco-remote-api by Alfresco.

the class RepoStore method createDocument.

/* (non-Javadoc)
     * @see org.alfresco.web.scripts.Store#createDocument(java.lang.String, java.lang.String)
     */
public void createDocument(String documentPath, String content) throws IOException {
    String[] pathElements = documentPath.split("/");
    String[] folderElements = new String[pathElements.length - 1];
    System.arraycopy(pathElements, 0, folderElements, 0, pathElements.length - 1);
    List<String> folderElementsList = Arrays.asList(folderElements);
    // create folder
    FileInfo pathInfo;
    if (folderElementsList.size() == 0) {
        pathInfo = fileService.getFileInfo(getBaseNodeRef());
    } else {
        pathInfo = FileFolderServiceImpl.makeFolders(fileService, getBaseNodeRef(), folderElementsList, ContentModel.TYPE_FOLDER);
    }
    // create file
    String fileName = pathElements[pathElements.length - 1];
    if (fileService.searchSimple(pathInfo.getNodeRef(), fileName) != null) {
        throw new IOException("Document " + documentPath + " already exists");
    }
    FileInfo fileInfo = fileService.create(pathInfo.getNodeRef(), fileName, ContentModel.TYPE_CONTENT);
    ContentWriter writer = fileService.getWriter(fileInfo.getNodeRef());
    writer.putContent(content);
}
Also used : ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) FileInfo(org.alfresco.service.cmr.model.FileInfo) IOException(java.io.IOException)

Example 10 with ContentWriter

use of org.alfresco.service.cmr.repository.ContentWriter in project alfresco-remote-api by Alfresco.

the class ThumbnailServiceTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    this.fileFolderService = (FileFolderService) getServer().getApplicationContext().getBean("FileFolderService");
    this.contentService = (ContentService) getServer().getApplicationContext().getBean("ContentService");
    this.repositoryHelper = (Repository) getServer().getApplicationContext().getBean("repositoryHelper");
    this.authenticationService = (MutableAuthenticationService) getServer().getApplicationContext().getBean("AuthenticationService");
    this.personService = (PersonService) getServer().getApplicationContext().getBean("PersonService");
    try {
        this.transactionService = (TransactionServiceImpl) getServer().getApplicationContext().getBean("transactionComponent");
    } catch (ClassCastException e) {
        throw new AlfrescoRuntimeException("The ThumbnailServiceTest needs direct access to the TransactionServiceImpl");
    }
    AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getSystemUserName());
    this.testRoot = this.repositoryHelper.getCompanyHome();
    // Get test content
    InputStream pdfStream = ThumbnailServiceTest.class.getClassLoader().getResourceAsStream("org/alfresco/repo/web/scripts/thumbnail/test_doc.pdf");
    assertNotNull(pdfStream);
    InputStream jpgStream = ThumbnailServiceTest.class.getClassLoader().getResourceAsStream("org/alfresco/repo/web/scripts/thumbnail/test_image.jpg");
    assertNotNull(jpgStream);
    String guid = GUID.generate();
    // Create new nodes and set test content
    FileInfo fileInfoPdf = this.fileFolderService.create(this.testRoot, "test_doc" + guid + ".pdf", ContentModel.TYPE_CONTENT);
    this.pdfNode = fileInfoPdf.getNodeRef();
    ContentWriter contentWriter = this.contentService.getWriter(fileInfoPdf.getNodeRef(), ContentModel.PROP_CONTENT, true);
    contentWriter.setEncoding("UTF-8");
    contentWriter.setMimetype(MimetypeMap.MIMETYPE_PDF);
    contentWriter.putContent(pdfStream);
    FileInfo fileInfoJpg = this.fileFolderService.create(this.testRoot, "test_image" + guid + ".jpg", ContentModel.TYPE_CONTENT);
    this.jpgNode = fileInfoJpg.getNodeRef();
    contentWriter = this.contentService.getWriter(fileInfoJpg.getNodeRef(), ContentModel.PROP_CONTENT, true);
    contentWriter.setEncoding("UTF-8");
    contentWriter.setMimetype(MimetypeMap.MIMETYPE_IMAGE_JPEG);
    contentWriter.putContent(jpgStream);
}
Also used : ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) FileInfo(org.alfresco.service.cmr.model.FileInfo) InputStream(java.io.InputStream) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException)

Aggregations

ContentWriter (org.alfresco.service.cmr.repository.ContentWriter)76 NodeRef (org.alfresco.service.cmr.repository.NodeRef)50 HashMap (java.util.HashMap)31 Serializable (java.io.Serializable)22 QName (org.alfresco.service.namespace.QName)21 ContentReader (org.alfresco.service.cmr.repository.ContentReader)20 Test (org.junit.Test)18 FileContentWriter (org.alfresco.repo.content.filestore.FileContentWriter)14 InputStream (java.io.InputStream)13 AlfrescoDocument (org.alfresco.cmis.client.AlfrescoDocument)13 AlfrescoFolder (org.alfresco.cmis.client.AlfrescoFolder)13 VersionableAspectTest (org.alfresco.repo.version.VersionableAspectTest)13 TestNetwork (org.alfresco.rest.api.tests.RepoService.TestNetwork)13 CmisSession (org.alfresco.rest.api.tests.client.PublicApiClient.CmisSession)13 RequestContext (org.alfresco.rest.api.tests.client.RequestContext)13 ContentStreamImpl (org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl)13 TestPerson (org.alfresco.rest.api.tests.RepoService.TestPerson)12 CmisUpdateConflictException (org.apache.chemistry.opencmis.commons.exceptions.CmisUpdateConflictException)12 PublicApiException (org.alfresco.rest.api.tests.client.PublicApiException)11 CmisConstraintException (org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException)11