use of org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl in project alfresco-remote-api by Alfresco.
the class TestCMIS method testContentDisposition_MNT_17477.
@Test
public void testContentDisposition_MNT_17477() throws Exception {
final TestNetwork network1 = getTestFixture().getRandomNetwork();
String username = "user" + System.currentTimeMillis();
PersonInfo personInfo = new PersonInfo(username, username, username, TEST_PASSWORD, null, null, null, null, null, null, null);
TestPerson person1 = network1.createUser(personInfo);
String person1Id = person1.getId();
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1Id));
CmisSession cmisSession = publicApiClient.createPublicApiCMISSession(Binding.browser, CMIS_VERSION_11, AlfrescoObjectFactoryImpl.class.getName());
Folder folder = (Folder) cmisSession.getObjectByPath("/Shared");
//
// Upload test JPG document
//
String name = GUID.generate() + ".jpg";
Map<String, Object> properties = new HashMap<>();
{
properties.put(PropertyIds.OBJECT_TYPE_ID, TYPE_CMIS_DOCUMENT);
properties.put(PropertyIds.NAME, name);
}
ContentStreamImpl fileContent = new ContentStreamImpl();
{
fileContent.setMimeType(MimetypeMap.MIMETYPE_IMAGE_JPEG);
fileContent.setStream(this.getClass().getResourceAsStream("/test.jpg"));
}
Document doc = folder.createDocument(properties, fileContent, VersioningState.MAJOR);
String docId = doc.getId();
// note: Content-Disposition can be "inline or "attachment" for content types that are white-listed (eg. specific image types & pdf)
HttpResponse response = publicApiClient.get(network1.getId() + "/public/cmis/versions/1.1/browser/root/Shared/" + name, null);
assertEquals(200, response.getStatusCode());
assertTrue(response.getHeaders().get("Content-Disposition").startsWith("inline"));
response = publicApiClient.get(network1.getId() + "/public/cmis/versions/1.1/browser/root/Shared/" + name + "?download=inline", null);
assertEquals(200, response.getStatusCode());
assertTrue(response.getHeaders().get("Content-Disposition").startsWith("inline"));
response = publicApiClient.get(network1.getId() + "/public/cmis/versions/1.1/browser/root/Shared/" + name + "?download=attachment", null);
assertEquals(200, response.getStatusCode());
assertTrue(response.getHeaders().get("Content-Disposition").startsWith("attachment"));
// note: AtomPub binding (via OpenCMIS) does not support "download" query parameter
response = publicApiClient.get(network1.getId() + "/public/cmis/versions/1.1/atom/content?id=" + docId, null);
assertEquals(200, response.getStatusCode());
assertTrue(response.getHeaders().get("Content-Disposition").startsWith("attachment"));
//
// Create test HTML document
//
name = GUID.generate() + ".html";
properties = new HashMap<>();
{
properties.put(PropertyIds.OBJECT_TYPE_ID, TYPE_CMIS_DOCUMENT);
properties.put(PropertyIds.NAME, name);
}
fileContent = new ContentStreamImpl();
{
ContentWriter writer = new FileContentWriter(TempFileProvider.createTempFile(GUID.generate(), ".html"));
writer.putContent("<html><script>alert(123);</script><body>Hello <b>world</b></body</html>");
ContentReader reader = writer.getReader();
fileContent.setMimeType(MimetypeMap.MIMETYPE_HTML);
fileContent.setStream(reader.getContentInputStream());
}
doc = folder.createDocument(properties, fileContent, VersioningState.MAJOR);
docId = doc.getId();
// note: Content-Disposition will always be "attachment" for content types that are not white-listed
response = publicApiClient.get(network1.getId() + "/public/cmis/versions/1.1/browser/root/Shared/" + name, null);
assertEquals(200, response.getStatusCode());
assertTrue(response.getHeaders().get("Content-Disposition").startsWith("attachment;"));
response = publicApiClient.get(network1.getId() + "/public/cmis/versions/1.1/browser/root/Shared/" + name + "?download=inline", null);
assertEquals(200, response.getStatusCode());
assertTrue(response.getHeaders().get("Content-Disposition").startsWith("attachment;"));
// note: AtomPub binding (via OpenCMIS) does not support "download" query parameter
response = publicApiClient.get(network1.getId() + "/public/cmis/versions/1.1/atom/content?id=" + docId, null);
assertEquals(200, response.getStatusCode());
assertTrue(response.getHeaders().get("Content-Disposition").startsWith("attachment;"));
}
use of org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl in project alfresco-remote-api by Alfresco.
the class TestCMIS method testMnt11631.
@Test
public void testMnt11631() throws Exception {
final TestNetwork network = getTestFixture().getRandomNetwork();
String username = String.format(TEST_USER_NAME_PATTERN, System.currentTimeMillis());
PersonInfo personInfo = new PersonInfo(username, username, username, TEST_PASSWORD, null, null, null, null, null, null, null);
TestPerson person = network.createUser(personInfo);
String personId = person.getId();
final String siteName = String.format(TEST_SITE_NAME_PATTERN, System.currentTimeMillis());
TenantUtil.runAsUserTenant(new TenantRunAsWork<NodeRef>() {
@Override
public NodeRef doWork() throws Exception {
SiteInformation siteInfo = new SiteInformation(siteName, siteName, siteName, SiteVisibility.PRIVATE);
TestSite site = repoService.createSite(null, siteInfo);
String name = GUID.generate();
NodeRef folderNodeRef = repoService.createFolder(site.getContainerNodeRef(DOCUMENT_LIBRARY_CONTAINER_NAME), name);
return folderNodeRef;
}
}, personId, network.getId());
publicApiClient.setRequestContext(new RequestContext(network.getId(), personId));
CmisSession cmisSession = publicApiClient.createPublicApiCMISSession(Binding.atom, CMIS_VERSION_11);
Folder docLibrary = (Folder) cmisSession.getObjectByPath(String.format(DOCUMENT_LIBRARY_PATH_PATTERN, siteName));
String name = String.format(TEST_DOCUMENT_NAME_PATTERN, GUID.generate());
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(PropertyIds.OBJECT_TYPE_ID, TYPE_CMIS_DOCUMENT);
properties.put(PropertyIds.NAME, name);
ContentStreamImpl fileContent = new ContentStreamImpl();
ByteArrayInputStream stream = new ByteArrayInputStream(GUID.generate().getBytes());
fileContent.setMimeType(MimetypeMap.MIMETYPE_TEXT_PLAIN);
fileContent.setStream(stream);
Document doc = docLibrary.createDocument(properties, fileContent, VersioningState.MAJOR);
ObjectId pwcId = doc.checkOut();
Document pwc = (Document) cmisSession.getObject(pwcId.getId());
assertIsPwcProperty(pwc, false);
cmisSession = publicApiClient.createPublicApiCMISSession(Binding.atom, CMIS_VERSION_10);
CmisObject pwc10 = cmisSession.getObject(pwc.getId());
assertIsPwcProperty(pwc10, true);
}
use of org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl in project alfresco-remote-api by Alfresco.
the class TestCMIS method testAppendContentStream.
@Test
public void testAppendContentStream() throws Exception {
final TestNetwork network1 = getTestFixture().getRandomNetwork();
String username = "user" + System.currentTimeMillis();
PersonInfo personInfo = new PersonInfo(username, username, username, TEST_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.PRIVATE);
TestSite site = repoService.createSite(null, siteInfo);
String name = GUID.generate();
NodeRef folderNodeRef = repoService.createFolder(site.getContainerNodeRef(DOCUMENT_LIBRARY_CONTAINER_NAME), name);
return folderNodeRef;
}
}, person1Id, network1.getId());
// Create a document...
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1Id));
CmisSession cmisSession = publicApiClient.createPublicApiCMISSession(Binding.atom, CMIS_VERSION_11);
Folder docLibrary = (Folder) cmisSession.getObjectByPath("/Sites/" + siteName + "/documentLibrary");
String name = "mydoc-" + GUID.generate() + ".txt";
Map<String, String> properties = new HashMap<String, String>();
{
// create a document with 2 aspects
properties.put(PropertyIds.OBJECT_TYPE_ID, TYPE_CMIS_DOCUMENT);
properties.put(PropertyIds.NAME, name);
}
ContentStreamImpl fileContent = new ContentStreamImpl();
{
ContentWriter writer = new FileContentWriter(TempFileProvider.createTempFile(GUID.generate(), ".txt"));
writer.putContent("Ipsum and so on");
ContentReader reader = writer.getReader();
fileContent.setMimeType(MimetypeMap.MIMETYPE_TEXT_PLAIN);
fileContent.setStream(reader.getContentInputStream());
}
Document doc = docLibrary.createDocument(properties, fileContent, VersioningState.MAJOR);
// append a few times
for (int i = 0; i < 5; i++) {
fileContent = new ContentStreamImpl();
{
ContentWriter writer = new FileContentWriter(TempFileProvider.createTempFile(GUID.generate(), ".txt"));
writer.putContent("Ipsum and so on");
ContentReader reader = writer.getReader();
fileContent.setMimeType(MimetypeMap.MIMETYPE_TEXT_PLAIN);
fileContent.setStream(reader.getContentInputStream());
}
doc.appendContentStream(fileContent, false);
}
fileContent = new ContentStreamImpl();
{
ContentWriter writer = new FileContentWriter(TempFileProvider.createTempFile(GUID.generate(), ".txt"));
writer.putContent("Ipsum and so on");
ContentReader reader = writer.getReader();
fileContent.setMimeType(MimetypeMap.MIMETYPE_TEXT_PLAIN);
fileContent.setStream(reader.getContentInputStream());
}
doc.appendContentStream(fileContent, true);
// check the appends
String path = "/Sites/" + siteName + "/documentLibrary/" + name;
Document doc1 = (Document) cmisSession.getObjectByPath(path);
ContentStream contentStream = doc1.getContentStream();
InputStream in = contentStream.getStream();
StringWriter writer = new StringWriter();
IOUtils.copy(in, writer, "UTF-8");
String content = writer.toString();
assertEquals("Ipsum and so onIpsum and so onIpsum and so onIpsum and so onIpsum and so onIpsum and so onIpsum and so on", content);
}
use of org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl in project alfresco-remote-api by Alfresco.
the class TestCMIS method testAppendContentVersioning.
/* MNT-10175 test */
@Test
public void testAppendContentVersioning() throws Exception {
final TestNetwork network1 = getTestFixture().getRandomNetwork();
String username = "user" + System.currentTimeMillis();
PersonInfo personInfo = new PersonInfo(username, username, username, TEST_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.PRIVATE);
TestSite site = repoService.createSite(null, siteInfo);
String name = GUID.generate();
NodeRef folderNodeRef = repoService.createFolder(site.getContainerNodeRef(DOCUMENT_LIBRARY_CONTAINER_NAME), name);
return folderNodeRef;
}
}, person1Id, network1.getId());
// Create a document
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1Id));
// Use CMIS 1.1 to test content appending
CmisSession cmisSession = publicApiClient.createPublicApiCMISSession(Binding.atom, CMIS_VERSION_11);
Folder docLibrary = (Folder) cmisSession.getObjectByPath("/Sites/" + siteName + "/documentLibrary");
String name = GUID.generate() + ".txt";
Map<String, String> properties = new HashMap<String, String>();
{
properties.put(PropertyIds.OBJECT_TYPE_ID, TYPE_CMIS_DOCUMENT);
properties.put(PropertyIds.NAME, name);
}
// Create content to append
ContentStreamImpl fileContent = new ContentStreamImpl();
{
ContentWriter writer = new FileContentWriter(TempFileProvider.createTempFile(GUID.generate(), ".txt"));
writer.putContent("Ipsum and so on");
ContentReader reader = writer.getReader();
fileContent.setMimeType(MimetypeMap.MIMETYPE_TEXT_PLAIN);
fileContent.setStream(reader.getContentInputStream());
}
Document doc = docLibrary.createDocument(properties, fileContent, VersioningState.MAJOR);
String versionLabel1 = doc.getObjectOfLatestVersion(false).getVersionLabel();
// append a few times
for (int i = 0; i < 5; i++) {
doc.appendContentStream(fileContent, false);
}
String versionLabel2 = doc.getObjectOfLatestVersion(false).getVersionLabel();
// Version label should not be incremented by appending with isLustChunk = false
assertEquals(versionLabel1, versionLabel2);
doc.appendContentStream(fileContent, true);
String versionLabel3 = doc.getObjectOfLatestVersion(false).getVersionLabel();
Integer majorVer1 = Integer.valueOf(versionLabel2.substring(0, 1));
Integer majorVer2 = Integer.valueOf(versionLabel3.substring(0, 1));
Integer minorVer1 = Integer.valueOf(versionLabel2.substring(2, 3));
Integer minorVer2 = Integer.valueOf(versionLabel3.substring(2, 3));
// Only one MINOR version should be created
assertEquals(majorVer1, majorVer2);
assertEquals(Integer.valueOf(minorVer1 + 1), minorVer2);
}
use of org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl 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);
}
Aggregations