use of org.apache.chemistry.opencmis.client.api.Document 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.client.api.Document in project alfresco-remote-api by Alfresco.
the class TestCMIS method testDeleteNonCurrentVersion.
/**
* Test delete version on versions other than latest (most recent) version (MNT-17228)
*/
@Test
public void testDeleteNonCurrentVersion() 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 person = network1.createUser(personInfo);
String personId = person.getId();
publicApiClient.setRequestContext(new RequestContext(network1.getId(), personId));
CmisSession cmisSession = publicApiClient.createPublicApiCMISSession(Binding.browser, CMIS_VERSION_11, AlfrescoObjectFactoryImpl.class.getName());
Folder homeFolder = (Folder) cmisSession.getObjectByPath("/User Homes/" + personId);
assertNotNull(homeFolder.getId());
// Create a document
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 = homeFolder.createDocument(properties, fileContent, VersioningState.MAJOR);
String versionLabel = doc.getVersionLabel();
assertEquals("1.0", versionLabel);
Document docVersionToDelete = null;
Document latestDoc = doc;
int cnt = 4;
for (int i = 1; i <= cnt; i++) {
// Update content to create new versions (1.1, 1.2, 1.3, 1.4)
fileContent = new ContentStreamImpl();
{
ContentWriter writer = new FileContentWriter(TempFileProvider.createTempFile(GUID.generate(), ".txt"));
writer.putContent("Ipsum and so on and so on " + i);
ContentReader reader = writer.getReader();
fileContent.setMimeType(MimetypeMap.MIMETYPE_TEXT_PLAIN);
fileContent.setStream(reader.getContentInputStream());
}
latestDoc.setContentStream(fileContent, true);
latestDoc = latestDoc.getObjectOfLatestVersion(false);
versionLabel = latestDoc.getVersionLabel();
assertEquals("1." + i, versionLabel);
assertEquals(1 + i, cmisSession.getAllVersions(latestDoc.getId()).size());
if (i == 2) {
// ie. 1.2
docVersionToDelete = latestDoc;
}
}
// Test delete with a user without permissions
String username2 = "user" + System.currentTimeMillis();
PersonInfo person2Info = new PersonInfo(username2, username2, username2, TEST_PASSWORD, null, null, null, null, null, null, null);
TestPerson person2 = network1.createUser(person2Info);
String person2Id = person2.getId();
TenantUtil.runAsSystemTenant(new TenantRunAsWork<Void>() {
@Override
public Void doWork() throws Exception {
String nodeId = stripCMISSuffix(doc.getId());
NodeRef nodeRef = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, nodeId);
// Give user person2 READ permissions to access the node
permissionService.setPermission(nodeRef, person2Id, PermissionService.READ, true);
return null;
}
}, network1.getId());
// Connect with person2
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person2Id));
CmisSession cmisSession2 = publicApiClient.createPublicApiCMISSession(Binding.browser, CMIS_VERSION_11, AlfrescoObjectFactoryImpl.class.getName());
CmisObject docVersionToDeleteBy2 = cmisSession2.getObject(docVersionToDelete.getId());
try {
// (-) Delete v 1.2 (without DELETE permission)
docVersionToDeleteBy2.delete(false);
fail("Node version was deleted without permissions.");
} catch (CmisPermissionDeniedException ex) {
// expected
}
// (+) Delete v 1.2 (with permission)
docVersionToDelete.delete(false);
// eg. 1.0, 1.2, 1.3, 1.4 (not 1.1)
assertEquals(cnt, cmisSession.getAllVersions(doc.getId()).size());
}
use of org.apache.chemistry.opencmis.client.api.Document in project iaf by ibissource.
the class CmisSender method sendMessageForActionGet.
private String sendMessageForActionGet(String correlationID, String message, ParameterResolutionContext prc) throws SenderException, TimeOutException {
if (StringUtils.isEmpty(message)) {
throw new SenderException(getLogPrefix() + "input string cannot be empty but must contain a documentId");
}
CmisObject object = null;
try {
object = session.getObject(session.createObjectId(message));
} catch (CmisObjectNotFoundException e) {
if (StringUtils.isNotEmpty(getResultOnNotFound())) {
log.info(getLogPrefix() + "document with id [" + message + "] not found", e);
return getResultOnNotFound();
} else {
throw new SenderException(e);
}
}
Document document = (Document) object;
ContentStream contentStream = document.getContentStream();
try {
InputStream inputStream = contentStream.getStream();
if (isStreamResultToServlet()) {
HttpServletResponse response = (HttpServletResponse) prc.getSession().get(IPipeLineSession.HTTP_RESPONSE_KEY);
String contentType = contentStream.getMimeType();
if (StringUtils.isNotEmpty(contentType)) {
log.debug(getLogPrefix() + "setting response Content-Type header [" + contentType + "]");
response.setHeader("Content-Type", contentType);
}
String contentDisposition = "attachment; filename=\"" + contentStream.getFileName() + "\"";
log.debug(getLogPrefix() + "setting response Content-Disposition header [" + contentDisposition + "]");
response.setHeader("Content-Disposition", contentDisposition);
OutputStream outputStream;
outputStream = response.getOutputStream();
Misc.streamToStream(inputStream, outputStream);
log.debug(getLogPrefix() + "copied document content input stream [" + inputStream + "] to output stream [" + outputStream + "]");
return "";
} else if (isGetProperties()) {
if (StringUtils.isNotEmpty(fileInputStreamSessionKey)) {
prc.getSession().put(getFileInputStreamSessionKey(), inputStream);
} else {
byte[] bytes = Misc.streamToBytes(inputStream);
prc.getSession().put(getFileContentSessionKey(), Base64.encodeBase64String(bytes));
}
XmlBuilder cmisXml = new XmlBuilder("cmis");
XmlBuilder propertiesXml = new XmlBuilder("properties");
for (Iterator it = document.getProperties().iterator(); it.hasNext(); ) {
Property property = (Property) it.next();
propertiesXml.addSubElement(getPropertyXml(property));
}
cmisXml.addSubElement(propertiesXml);
return cmisXml.toXML();
} else {
return Misc.streamToString(inputStream, null, false);
}
} catch (IOException e) {
throw new SenderException(e);
}
}
use of org.apache.chemistry.opencmis.client.api.Document in project alfresco-repository by Alfresco.
the class OpenCmisLocalTest method testCancelCheckoutWhileInCheckedOutState.
/**
* MNT-14687 - Creating a document as checkedout and then cancelling the
* checkout should delete the document.
*
* This test would have fit better within CheckOutCheckInServiceImplTest but
* was added here to make use of existing methods
*/
public void testCancelCheckoutWhileInCheckedOutState() {
ServiceRegistry serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
CheckOutCheckInService cociService = serviceRegistry.getCheckOutCheckInService();
// Authenticate as system
AuthenticationComponent authenticationComponent = (AuthenticationComponent) ctx.getBean(BEAN_NAME_AUTHENTICATION_COMPONENT);
authenticationComponent.setSystemUserAsCurrentUser();
/* Create the document using openCmis services */
Repository repository = getRepository("admin", "admin");
Session session = repository.createSession();
Folder rootFolder = session.getRootFolder();
// Set file properties
String docname = "myDoc-" + GUID.generate() + ".txt";
Map<String, String> props = new HashMap<String, String>();
{
props.put(PropertyIds.OBJECT_TYPE_ID, BaseTypeId.CMIS_DOCUMENT.value());
props.put(PropertyIds.NAME, docname);
}
// Create some content
byte[] byteContent = "Some content".getBytes();
InputStream stream = new ByteArrayInputStream(byteContent);
ContentStream contentStream = new ContentStreamImpl(docname, BigInteger.valueOf(byteContent.length), MIME_PLAIN_TEXT, stream);
// Create the document
Document doc1 = rootFolder.createDocument(props, contentStream, VersioningState.CHECKEDOUT);
NodeRef doc1NodeRef = cmisIdToNodeRef(doc1.getId());
NodeRef doc1WorkingCopy = cociService.getWorkingCopy(doc1NodeRef);
/* Cancel Checkout */
cociService.cancelCheckout(doc1WorkingCopy);
/* Check if both the working copy and the document were deleted */
NodeService nodeService = serviceRegistry.getNodeService();
assertFalse(nodeService.exists(doc1NodeRef));
assertFalse(nodeService.exists(doc1WorkingCopy));
}
use of org.apache.chemistry.opencmis.client.api.Document in project iaf by ibissource.
the class CmisSender method sendMessageForActionCreate.
private Message sendMessageForActionCreate(Session cmisSession, Message message, PipeLineSession session, ParameterValueList pvl) throws SenderException {
String fileName = null;
try {
fileName = session.getMessage(getParameterOverriddenAttributeValue(pvl, "filenameSessionKey", getFilenameSessionKey())).asString();
} catch (IOException e) {
throw new SenderException("Unable to get filename from session key [" + getFilenameSessionKey() + "]", e);
}
String mediaType;
Map<String, Object> props = new HashMap<String, Object>();
Element cmisElement;
try {
if (XmlUtils.isWellFormed(message, "cmis")) {
cmisElement = XmlUtils.buildElement(message);
} else {
cmisElement = XmlUtils.buildElement("<cmis/>");
}
String objectTypeId = XmlUtils.getChildTagAsString(cmisElement, "objectTypeId");
if (StringUtils.isNotEmpty(objectTypeId)) {
props.put(PropertyIds.OBJECT_TYPE_ID, objectTypeId);
} else {
props.put(PropertyIds.OBJECT_TYPE_ID, "cmis:document");
}
String name = XmlUtils.getChildTagAsString(cmisElement, "name");
if (StringUtils.isEmpty(fileName)) {
fileName = XmlUtils.getChildTagAsString(cmisElement, "fileName");
}
mediaType = XmlUtils.getChildTagAsString(cmisElement, "mediaType");
if (StringUtils.isNotEmpty(name)) {
props.put(PropertyIds.NAME, name);
} else if (StringUtils.isNotEmpty(fileName)) {
props.put(PropertyIds.NAME, fileName);
} else {
props.put(PropertyIds.NAME, "[unknown]");
}
Element propertiesElement = XmlUtils.getFirstChildTag(cmisElement, "properties");
if (propertiesElement != null) {
processProperties(propertiesElement, props);
}
} catch (DomBuilderException e) {
throw new SenderException(getLogPrefix() + "exception parsing [" + message + "]", e);
}
if (StringUtils.isEmpty(mediaType)) {
mediaType = getDefaultMediaType();
}
ContentStream contentStream;
try {
Message inputFromSessionKey = session.getMessage(getParameterOverriddenAttributeValue(pvl, "fileSessionKey", getFileSessionKey()));
if (convert2Base64 && inputFromSessionKey.asObject() instanceof String) {
inputFromSessionKey = new Message(Base64.decodeBase64(inputFromSessionKey.asByteArray()));
}
long fileLength = inputFromSessionKey.size();
contentStream = cmisSession.getObjectFactory().createContentStream(fileName, fileLength, mediaType, inputFromSessionKey.asInputStream());
} catch (IOException e) {
throw new SenderException(e);
}
if (isUseRootFolder()) {
Folder folder = cmisSession.getRootFolder();
Document document = folder.createDocument(props, contentStream, VersioningState.NONE);
log.debug(getLogPrefix() + "created new document [ " + document.getId() + "]");
return new Message(document.getId());
}
ObjectId objectId = cmisSession.createDocument(props, null, contentStream, VersioningState.NONE);
log.debug(getLogPrefix() + "created new document [ " + objectId.getId() + "]");
return new Message(objectId.getId());
}
Aggregations