use of org.apache.chemistry.opencmis.client.api.Document in project iaf by ibissource.
the class CmisSender method sendMessageForActionGet.
private Message sendMessageForActionGet(Session cmisSession, Message message, PipeLineSession session, ParameterValueList pvl) throws SenderException, TimeoutException {
if (Message.isEmpty(message)) {
throw new SenderException(getLogPrefix() + "input string cannot be empty but must contain a documentId");
}
CmisObject object = null;
try {
object = getCmisObject(cmisSession, message);
} catch (CmisObjectNotFoundException e) {
if (StringUtils.isNotEmpty(getResultOnNotFound())) {
log.info(getLogPrefix() + "document with id [" + message + "] not found", e);
return new Message(getResultOnNotFound());
}
throw new SenderException(e);
}
Document document = (Document) object;
try {
boolean getProperties = isGetProperties();
boolean getDocumentContent = isGetDocumentContent();
if (pvl != null) {
if (pvl.contains("getProperties"))
getProperties = pvl.get("getProperties").asBooleanValue(isGetProperties());
if (pvl.contains("getDocumentContent"))
getDocumentContent = pvl.get("getDocumentContent").asBooleanValue(isGetDocumentContent());
}
if (isStreamResultToServlet()) {
HttpServletResponse response = (HttpServletResponse) session.get(PipeLineSession.HTTP_RESPONSE_KEY);
ContentStream contentStream = document.getContentStream();
InputStream inputStream = contentStream.getStream();
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 Message.nullMessage();
} else if (getProperties) {
if (getDocumentContent) {
ContentStream contentStream = document.getContentStream();
InputStream inputStream = contentStream.getStream();
if (convert2Base64) {
byte[] bytes = Misc.streamToBytes(inputStream);
session.put(getFileSessionKey(), Base64.encodeBase64String(bytes));
} else {
session.put(getFileSessionKey(), inputStream);
}
}
XmlBuilder cmisXml = new XmlBuilder("cmis");
XmlBuilder propertiesXml = new XmlBuilder("properties");
for (Iterator<Property<?>> it = document.getProperties().iterator(); it.hasNext(); ) {
Property<?> property = it.next();
propertiesXml.addSubElement(CmisUtils.getPropertyXml(property));
}
cmisXml.addSubElement(propertiesXml);
return new Message(cmisXml.toXML());
} else {
ContentStream contentStream = document.getContentStream();
InputStream inputStream = contentStream.getStream();
if (StringUtils.isNotEmpty(getFileSessionKey())) {
if (convert2Base64) {
byte[] bytes = Misc.streamToBytes(inputStream);
session.put(getFileSessionKey(), Base64.encodeBase64String(bytes));
} else {
session.put(getFileSessionKey(), inputStream);
}
return Message.nullMessage();
}
session.put("contentStream:MimeType", contentStream.getMimeType());
session.put("contentStream:Filename", contentStream.getFileName());
MessageContext context = new MessageContext();
context.withName(contentStream.getFileName()).withMimeType(contentStream.getMimeType());
return new Message(inputStream, context);
}
} catch (IOException e) {
throw new SenderException(e);
}
}
use of org.apache.chemistry.opencmis.client.api.Document in project SearchServices by Alfresco.
the class CMISDataCreatorTest method createUniqueDocument.
private Document createUniqueDocument(Folder newFolder) throws UnsupportedEncodingException {
String uniqueName = getUniqueName();
Map<String, Object> uProperties = new HashMap<String, Object>();
uProperties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:document");
uProperties.put(PropertyIds.NAME, uniqueName);
ContentStreamImpl contentStream = new ContentStreamImpl();
contentStream.setFileName("bob");
String shortString = "short";
contentStream.setStream(new ByteArrayInputStream(shortString.getBytes("UTF-8")));
contentStream.setLength(new BigInteger("5"));
contentStream.setMimeType("text/plain");
Document uniqueDocument = newFolder.createDocument(uProperties, contentStream, VersioningState.MAJOR);
return uniqueDocument;
}
use of org.apache.chemistry.opencmis.client.api.Document in project studio by craftercms.
the class CmisServiceImpl method uploadContent.
@Override
@HasPermission(type = DefaultPermission.class, action = "upload_content_cmis")
public CmisUploadItem uploadContent(@ProtectedResourceId(SITE_ID_RESOURCE_ID) String siteId, String cmisRepoId, String cmisPath, String filename, InputStream content) throws CmisUnavailableException, CmisTimeoutException, CmisRepositoryNotFoundException, CmisPathNotFoundException, ConfigurationException {
DataSourceRepository repositoryConfig = getConfiguration(siteId, cmisRepoId);
CmisUploadItem cmisUploadItem = new CmisUploadItem();
logger.debug("Create new CMIS session");
Session session = createCMISSession(repositoryConfig);
if (session != null) {
String contentPath = Paths.get(repositoryConfig.getBasePath(), cmisPath).toString();
logger.debug("Find object for CMIS path: " + contentPath);
CmisObject cmisObject = session.getObjectByPath(contentPath);
if (cmisObject != null) {
if (BaseTypeId.CMIS_FOLDER.equals(cmisObject.getBaseTypeId())) {
CmisObject docObject = null;
try {
docObject = session.getObjectByPath(Paths.get(contentPath, filename).toString());
} catch (CmisBaseException e) {
// Content does not exist - no error
logger.debug("File " + filename + " does not exist at " + contentPath);
}
MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
String mimeType = mimeTypesMap.getContentType(filename);
ContentStream contentStream = session.getObjectFactory().createContentStream(filename, -1, mimeType, content);
Folder folder = (Folder) cmisObject;
cmisUploadItem.setName(filename);
cmisUploadItem.setFolder(false);
cmisUploadItem.setFileExtension(FilenameUtils.getExtension(filename));
if (docObject != null) {
Document doc = (Document) docObject;
doc.setContentStream(contentStream, true);
String contentId = doc.getId();
StringTokenizer st = new StringTokenizer(contentId, ";");
if (st.hasMoreTokens()) {
cmisUploadItem.setUrl(repositoryConfig.getDownloadUrlRegex().replace(ITEM_ID, st.nextToken()));
}
session.removeObjectFromCache(doc.getId());
} else {
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(OBJECT_TYPE_ID, CMIS_DOCUMENT.value());
properties.put(NAME, filename);
Document newDoc = folder.createDocument(properties, contentStream, null);
session.removeObjectFromCache(newDoc.getId());
String contentId = newDoc.getId();
StringTokenizer st = new StringTokenizer(contentId, ";");
if (st.hasMoreTokens()) {
cmisUploadItem.setUrl(repositoryConfig.getDownloadUrlRegex().replace(ITEM_ID, st.nextToken()));
}
}
session.clear();
} else if (CMIS_DOCUMENT.equals(cmisObject.getBaseTypeId())) {
throw new CmisPathNotFoundException();
}
} else {
throw new CmisPathNotFoundException();
}
} else {
throw new CmisUnauthorizedException();
}
return cmisUploadItem;
}
use of org.apache.chemistry.opencmis.client.api.Document in project manifoldcf by apache.
the class APISanityHSQLDBIT method changeDocument.
/**
* change the document content with the new one provided as an argument
* @param session
* @param name
* @param newContent
*/
public void changeDocument(Session session, String name, String newContent) {
String cmisQuery = StringUtils.replace(BaseITSanityTestUtils.CMIS_TEST_QUERY_CHANGE_DOC, BaseITSanityTestUtils.REPLACER, name);
ItemIterable<QueryResult> results = session.query(cmisQuery, false);
String objectId = StringUtils.EMPTY;
for (QueryResult result : results) {
objectId = result.getPropertyById(PropertyIds.OBJECT_ID).getFirstValue().toString();
}
byte[] newContentByteArray = newContent.getBytes(StandardCharsets.UTF_8);
InputStream stream = new ByteArrayInputStream(newContentByteArray);
ContentStream contentStream = new ContentStreamImpl(name, new BigInteger(newContentByteArray), "text/plain", stream);
Document documentToUpdate = (Document) session.getObject(objectId);
documentToUpdate.setContentStream(contentStream, true);
}
use of org.apache.chemistry.opencmis.client.api.Document in project manifoldcf by apache.
the class CmisRepositoryConnector method getDocumentURI.
private String getDocumentURI(CmisObject cmisObject) throws ManifoldCFException {
String documentURI = StringUtils.EMPTY;
String currentBaseTypeId = cmisObject.getBaseTypeId().value();
if (StringUtils.equals(currentBaseTypeId, BaseTypeId.CMIS_DOCUMENT.value())) {
Document currentDocument = (Document) cmisObject;
if (currentDocument.getParents() != null && !currentDocument.getParents().isEmpty()) {
String path = currentDocument.getParents().get(0).getPath();
String name = currentDocument.getName();
String fullContentPath = path + CmisRepositoryConnectorUtils.SLASH + name;
documentURI = fullContentPath;
// Append the new parameters in the query string
String documentDownloadURL = CmisRepositoryConnectorUtils.getDocumentURL(currentDocument, session);
if (StringUtils.contains(documentDownloadURL, '?')) {
documentURI = documentDownloadURL + "&" + CONTENT_PATH_PARAM + "=" + fullContentPath;
} else {
documentURI = documentDownloadURL + "?" + CONTENT_PATH_PARAM + "=" + fullContentPath;
}
}
} else if (StringUtils.equals(currentBaseTypeId, BaseTypeId.CMIS_FOLDER.value())) {
Folder currentFolder = (Folder) cmisObject;
String path = currentFolder.getPath();
String name = currentFolder.getName();
String fullContentPath = path + CmisRepositoryConnectorUtils.SLASH + name;
documentURI = fullContentPath;
}
return documentURI;
}
Aggregations