Search in sources :

Example 1 with PublicApiAlfrescoCmisServiceFactory

use of org.alfresco.opencmis.PublicApiAlfrescoCmisServiceFactory in project alfresco-remote-api by Alfresco.

the class TestCMIS method testSetMaxContentSize.

/**
 * REPO-2041 / MNT-16236 Upload via cmis binding atom and browser files with different maxContentSize
 */
@Test
public void testSetMaxContentSize() throws Exception {
    final TestNetwork network1 = getTestFixture().getRandomNetwork();
    Iterator<String> personIt = network1.getPersonIds().iterator();
    final String personId = personIt.next();
    assertNotNull(personId);
    // Create a site
    final TestSite site = TenantUtil.runAsUserTenant(new TenantRunAsWork<TestSite>() {

        @Override
        public TestSite doWork() throws Exception {
            String siteName = "site" + System.currentTimeMillis();
            SiteInformation siteInfo = new SiteInformation(siteName, siteName, siteName, SiteVisibility.PRIVATE);
            TestSite site = network1.createSite(siteInfo);
            return site;
        }
    }, personId, network1.getId());
    publicApiClient.setRequestContext(new RequestContext(network1.getId(), personId));
    CmisSession cmisSessionAtom = publicApiClient.createPublicApiCMISSession(Binding.atom, CMIS_VERSION_11, AlfrescoObjectFactoryImpl.class.getName());
    CmisSession cmisSessionBrowser = publicApiClient.createPublicApiCMISSession(Binding.browser, CMIS_VERSION_11, AlfrescoObjectFactoryImpl.class.getName());
    Folder documentLibrary = (Folder) cmisSessionAtom.getObjectByPath("/Sites/" + site.getSiteId() + "/documentLibrary");
    // create file for upload
    String fileName = "test+" + GUID.generate();
    // 6MB
    long fileLength = 6291456L;
    RandomAccessFile f = new RandomAccessFile(fileName, "rw");
    f.setLength(fileLength);
    Map<String, Serializable> properties = new HashMap<String, Serializable>();
    properties.put(PropertyIds.OBJECT_TYPE_ID, TYPE_CMIS_DOCUMENT);
    properties.put(PropertyIds.NAME, fileName);
    // change maxContentSize so that the file will be to big
    // 5MB
    double maxContentSize = 5.8;
    PublicApiAlfrescoCmisServiceFactory publicApiAlfrescoCmisServiceFactory = (PublicApiAlfrescoCmisServiceFactory) ctx.getBean("publicApiCMISServiceFactory");
    publicApiAlfrescoCmisServiceFactory.setMaxContentSize(maxContentSize);
    // for atom
    FileChannel fc = f.getChannel();
    InputStream is = Channels.newInputStream(fc);
    ContentStreamImpl contentStream = new ContentStreamImpl(fileName, BigInteger.valueOf(fileLength), MimetypeMap.MIMETYPE_TEXT_PLAIN, is);
    try {
        cmisSessionAtom.createDocument(documentLibrary.getId(), fileName, properties, contentStream, VersioningState.MAJOR);
        fail("The file should be to big to upload via atom binding");
    } catch (CmisConstraintException e) {
    }
    // for browser
    fc.position(0);
    try {
        cmisSessionBrowser.createDocument(documentLibrary.getId(), fileName, properties, contentStream, VersioningState.MAJOR);
        fail("The file should be to big to upload via browser binding");
    } catch (CmisConstraintException e) {
    }
    // increase maxContensize so that the file is not to big
    // 10MB
    maxContentSize = 10.6;
    publicApiAlfrescoCmisServiceFactory.setMaxContentSize(maxContentSize);
    // for atom
    fc.position(0);
    Document result = cmisSessionAtom.createDocument(documentLibrary.getId(), fileName, properties, contentStream, VersioningState.MAJOR);
    assertNotNull(result);
    // for browser
    fc.position(0);
    result = cmisSessionBrowser.createDocument(documentLibrary.getId(), fileName + "2", properties, contentStream, VersioningState.MAJOR);
    assertNotNull(result);
    // ignore the size check
    maxContentSize = -1;
    publicApiAlfrescoCmisServiceFactory.setMaxContentSize(maxContentSize);
    // for atom
    fc.position(0);
    result = cmisSessionAtom.createDocument(documentLibrary.getId(), fileName + "3", properties, contentStream, VersioningState.MAJOR);
    assertNotNull(result);
    // for browser
    fc.position(0);
    result = cmisSessionBrowser.createDocument(documentLibrary.getId(), fileName + "4", properties, contentStream, VersioningState.MAJOR);
    assertNotNull(result);
}
Also used : ContentStreamImpl(org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl) AlfrescoObjectFactoryImpl(org.alfresco.cmis.client.impl.AlfrescoObjectFactoryImpl) Serializable(java.io.Serializable) CmisSession(org.alfresco.rest.api.tests.client.PublicApiClient.CmisSession) HashMap(java.util.HashMap) TestSite(org.alfresco.rest.api.tests.RepoService.TestSite) FileChannel(java.nio.channels.FileChannel) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CmisConstraintException(org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException) AlfrescoFolder(org.alfresco.cmis.client.AlfrescoFolder) Folder(org.apache.chemistry.opencmis.client.api.Folder) 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) PublicApiAlfrescoCmisServiceFactory(org.alfresco.opencmis.PublicApiAlfrescoCmisServiceFactory) RandomAccessFile(java.io.RandomAccessFile) SiteInformation(org.alfresco.rest.api.tests.RepoService.SiteInformation) TestNetwork(org.alfresco.rest.api.tests.RepoService.TestNetwork) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) VersionableAspectTest(org.alfresco.repo.version.VersionableAspectTest) Test(org.junit.Test)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 RandomAccessFile (java.io.RandomAccessFile)1 Serializable (java.io.Serializable)1 FileChannel (java.nio.channels.FileChannel)1 HashMap (java.util.HashMap)1 AlfrescoDocument (org.alfresco.cmis.client.AlfrescoDocument)1 AlfrescoFolder (org.alfresco.cmis.client.AlfrescoFolder)1 AlfrescoObjectFactoryImpl (org.alfresco.cmis.client.impl.AlfrescoObjectFactoryImpl)1 PublicApiAlfrescoCmisServiceFactory (org.alfresco.opencmis.PublicApiAlfrescoCmisServiceFactory)1 VersionableAspectTest (org.alfresco.repo.version.VersionableAspectTest)1 SiteInformation (org.alfresco.rest.api.tests.RepoService.SiteInformation)1 TestNetwork (org.alfresco.rest.api.tests.RepoService.TestNetwork)1 TestSite (org.alfresco.rest.api.tests.RepoService.TestSite)1 CmisSession (org.alfresco.rest.api.tests.client.PublicApiClient.CmisSession)1 PublicApiException (org.alfresco.rest.api.tests.client.PublicApiException)1 RequestContext (org.alfresco.rest.api.tests.client.RequestContext)1 Document (org.apache.chemistry.opencmis.client.api.Document)1 Folder (org.apache.chemistry.opencmis.client.api.Folder)1 CmisConstraintException (org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException)1