Search in sources :

Example 16 with Document

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);
    }
}
Also used : Message(nl.nn.adapterframework.stream.Message) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Document(org.apache.chemistry.opencmis.client.api.Document) ContentStream(org.apache.chemistry.opencmis.commons.data.ContentStream) CmisObjectNotFoundException(org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException) Iterator(java.util.Iterator) XmlBuilder(nl.nn.adapterframework.util.XmlBuilder) CmisObject(org.apache.chemistry.opencmis.client.api.CmisObject) MessageContext(nl.nn.adapterframework.stream.MessageContext) SenderException(nl.nn.adapterframework.core.SenderException) Property(org.apache.chemistry.opencmis.client.api.Property)

Example 17 with Document

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;
}
Also used : ContentStreamImpl(org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) BigInteger(java.math.BigInteger) CmisObject(org.apache.chemistry.opencmis.client.api.CmisObject) Document(org.apache.chemistry.opencmis.client.api.Document)

Example 18 with Document

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;
}
Also used : DataSourceRepository(org.craftercms.studio.api.v2.dal.DataSourceRepository) MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) HashMap(java.util.HashMap) Folder(org.apache.chemistry.opencmis.client.api.Folder) Document(org.apache.chemistry.opencmis.client.api.Document) ContentStream(org.apache.chemistry.opencmis.commons.data.ContentStream) StringTokenizer(java.util.StringTokenizer) CmisBaseException(org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException) CmisUploadItem(org.craftercms.studio.model.rest.CmisUploadItem) CmisObject(org.apache.chemistry.opencmis.client.api.CmisObject) CmisObject(org.apache.chemistry.opencmis.client.api.CmisObject) CmisUnauthorizedException(org.apache.chemistry.opencmis.commons.exceptions.CmisUnauthorizedException) CmisPathNotFoundException(org.craftercms.studio.api.v1.exception.CmisPathNotFoundException) Session(org.apache.chemistry.opencmis.client.api.Session) HasPermission(org.craftercms.commons.security.permissions.annotations.HasPermission)

Example 19 with Document

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);
}
Also used : ContentStreamImpl(org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl) QueryResult(org.apache.chemistry.opencmis.client.api.QueryResult) ContentStream(org.apache.chemistry.opencmis.commons.data.ContentStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BigInteger(java.math.BigInteger) Document(org.apache.chemistry.opencmis.client.api.Document)

Example 20 with Document

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;
}
Also used : RepositoryDocument(org.apache.manifoldcf.agents.interfaces.RepositoryDocument) Document(org.apache.chemistry.opencmis.client.api.Document) Folder(org.apache.chemistry.opencmis.client.api.Folder)

Aggregations

Document (org.apache.chemistry.opencmis.client.api.Document)101 Folder (org.apache.chemistry.opencmis.client.api.Folder)62 HashMap (java.util.HashMap)43 CmisObject (org.apache.chemistry.opencmis.client.api.CmisObject)41 CmisTestResult (org.apache.chemistry.opencmis.tck.CmisTestResult)40 ContentStream (org.apache.chemistry.opencmis.commons.data.ContentStream)36 ContentStreamImpl (org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl)30 CmisObjectNotFoundException (org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException)28 ByteArrayInputStream (java.io.ByteArrayInputStream)27 ObjectId (org.apache.chemistry.opencmis.client.api.ObjectId)25 DocumentTypeDefinition (org.apache.chemistry.opencmis.commons.definitions.DocumentTypeDefinition)23 CmisConstraintException (org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException)21 CmisInvalidArgumentException (org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException)20 ArrayList (java.util.ArrayList)19 Test (org.junit.Test)19 InputStream (java.io.InputStream)18 NodeRef (org.alfresco.service.cmr.repository.NodeRef)17 CmisPermissionDeniedException (org.apache.chemistry.opencmis.commons.exceptions.CmisPermissionDeniedException)17 AlfrescoDocument (org.alfresco.cmis.client.AlfrescoDocument)16 VersionableAspectTest (org.alfresco.repo.version.VersionableAspectTest)16