Search in sources :

Example 1 with ReplayableInputStream

use of org.apache.manifoldcf.connectorcommon.fuzzyml.ReplayableInputStream in project manifoldcf by apache.

the class CmisOutputConnector method addOrReplaceDocumentWithException.

@Override
public int addOrReplaceDocumentWithException(String documentURI, VersionContext pipelineDescription, RepositoryDocument document, String authorityNameString, IOutputAddActivity activities) throws ManifoldCFException, ServiceInterruption, IOException {
    getSession();
    boolean isDropZoneFolder = isDropZoneFolder(cmisQuery);
    long startTime = System.currentTimeMillis();
    String resultDescription = StringUtils.EMPTY;
    Folder leafParent = null;
    String fileName = StringUtils.EMPTY;
    InputStream inputStream = null;
    ReplayableInputStream replayableInputStream = null;
    ContentStream contentStream = null;
    // properties
    // (minimal set: name and object type id)
    Map<String, Object> properties = new HashMap<String, Object>();
    Long binaryLength = null;
    String mimeType = StringUtils.EMPTY;
    try {
        if (isDropZoneFolder) {
            // Creation of the new Repository Node
            fileName = document.getFileName();
            Date creationDate = document.getCreatedDate();
            Date lastModificationDate = document.getModifiedDate();
            String objectId = StringUtils.EMPTY;
            mimeType = document.getMimeType();
            binaryLength = document.getBinaryLength();
            // check if the repository connector includes the content path
            String primaryPath = StringUtils.EMPTY;
            List<String> sourcePath = document.getSourcePath();
            if (sourcePath != null && !sourcePath.isEmpty()) {
                primaryPath = sourcePath.get(0);
            }
            // if the source is CMIS Repository Connector we override the objectId for synchronizing with removeDocument method
            if (isSourceRepoCmisCompliant(document)) {
                String[] cmisObjectIdArray = (String[]) document.getField(PropertyIds.OBJECT_ID);
                if (cmisObjectIdArray != null && cmisObjectIdArray.length > 0) {
                    objectId = cmisObjectIdArray[0];
                }
            // Mapping all the CMIS properties ...
            /*
          Iterator<String> fields = document.getFields();
          while (fields.hasNext()) {
            String field = (String) fields.next();
            if(!StringUtils.equals(field, "cm:lastThumbnailModification")
                || !StringUtils.equals(field, "cmis:secondaryObjectTypeIds")) {
              String[] valuesArray = (String[]) document.getField(field);
              properties.put(field,valuesArray);
            }
          }
          */
            }
            // Agnostic metadata
            properties.put(PropertyIds.OBJECT_TYPE_ID, EnumBaseObjectTypeIds.CMIS_DOCUMENT.value());
            properties.put(PropertyIds.NAME, fileName);
            properties.put(PropertyIds.CREATION_DATE, creationDate);
            properties.put(PropertyIds.LAST_MODIFICATION_DATE, lastModificationDate);
            // check objectId
            if (StringUtils.isNotEmpty(objectId)) {
                ObjectId objId = new ObjectIdImpl(objectId);
                properties.put(PropertyIds.OBJECT_ID, objId);
            }
            // Content Stream
            inputStream = document.getBinaryStream();
            replayableInputStream = new ReplayableInputStream(inputStream);
            contentStream = new ContentStreamImpl(fileName, BigInteger.valueOf(binaryLength), mimeType, replayableInputStream);
            // create a new content
            leafParent = getOrCreateLeafParent(parentDropZoneFolder, creationDate, Boolean.valueOf(createTimestampTree), primaryPath);
            leafParent.createDocument(properties, contentStream, versioningState);
            resultDescription = DOCUMENT_STATUS_ACCEPTED_DESC;
            return DOCUMENT_STATUS_ACCEPTED;
        } else {
            resultDescription = DOCUMENT_STATUS_REJECTED_DESC;
            return DOCUMENT_STATUS_REJECTED;
        }
    } catch (CmisContentAlreadyExistsException | CmisNameConstraintViolationException e) {
        // updating the existing content
        if (leafParent != null) {
            String documentFullPath = leafParent.getPath() + CmisOutputConnectorUtils.SLASH + fileName;
            String newFileName = fileName + System.currentTimeMillis();
            Document currentContent = (Document) session.getObjectByPath(documentFullPath);
            currentContent.updateProperties(properties);
            replayableInputStream.restart(true);
            contentStream = new ContentStreamImpl(newFileName, BigInteger.valueOf(binaryLength), mimeType, replayableInputStream);
            currentContent.setContentStream(contentStream, true);
            Logging.connectors.warn("CMIS: Document already exists - Updating: " + documentFullPath);
        }
        resultDescription = DOCUMENT_STATUS_ACCEPTED_DESC;
        return DOCUMENT_STATUS_ACCEPTED;
    } catch (Exception e) {
        resultDescription = DOCUMENT_STATUS_REJECTED_DESC;
        throw new ManifoldCFException(e.getMessage(), e);
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
        activities.recordActivity(startTime, ACTIVITY_INJECTION, document.getBinaryLength(), documentURI, resultDescription, resultDescription);
    }
}
Also used : ContentStreamImpl(org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl) HashMap(java.util.HashMap) ObjectId(org.apache.chemistry.opencmis.client.api.ObjectId) ReplayableInputStream(org.apache.manifoldcf.connectorcommon.fuzzyml.ReplayableInputStream) InputStream(java.io.InputStream) Folder(org.apache.chemistry.opencmis.client.api.Folder) RepositoryDocument(org.apache.manifoldcf.agents.interfaces.RepositoryDocument) Document(org.apache.chemistry.opencmis.client.api.Document) Date(java.util.Date) CmisContentAlreadyExistsException(org.apache.chemistry.opencmis.commons.exceptions.CmisContentAlreadyExistsException) URISyntaxException(java.net.URISyntaxException) CmisObjectNotFoundException(org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException) NotBoundException(java.rmi.NotBoundException) ManifoldCFException(org.apache.manifoldcf.core.interfaces.ManifoldCFException) RemoteException(java.rmi.RemoteException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CmisPermissionDeniedException(org.apache.chemistry.opencmis.commons.exceptions.CmisPermissionDeniedException) InterruptedIOException(java.io.InterruptedIOException) CmisNameConstraintViolationException(org.apache.chemistry.opencmis.commons.exceptions.CmisNameConstraintViolationException) CmisConnectionException(org.apache.chemistry.opencmis.commons.exceptions.CmisConnectionException) IOException(java.io.IOException) CmisNameConstraintViolationException(org.apache.chemistry.opencmis.commons.exceptions.CmisNameConstraintViolationException) ContentStream(org.apache.chemistry.opencmis.commons.data.ContentStream) ManifoldCFException(org.apache.manifoldcf.core.interfaces.ManifoldCFException) ReplayableInputStream(org.apache.manifoldcf.connectorcommon.fuzzyml.ReplayableInputStream) CmisContentAlreadyExistsException(org.apache.chemistry.opencmis.commons.exceptions.CmisContentAlreadyExistsException) ObjectIdImpl(org.apache.chemistry.opencmis.client.runtime.ObjectIdImpl)

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InterruptedIOException (java.io.InterruptedIOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URISyntaxException (java.net.URISyntaxException)1 NotBoundException (java.rmi.NotBoundException)1 RemoteException (java.rmi.RemoteException)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Document (org.apache.chemistry.opencmis.client.api.Document)1 Folder (org.apache.chemistry.opencmis.client.api.Folder)1 ObjectId (org.apache.chemistry.opencmis.client.api.ObjectId)1 ObjectIdImpl (org.apache.chemistry.opencmis.client.runtime.ObjectIdImpl)1 ContentStream (org.apache.chemistry.opencmis.commons.data.ContentStream)1 CmisConnectionException (org.apache.chemistry.opencmis.commons.exceptions.CmisConnectionException)1 CmisContentAlreadyExistsException (org.apache.chemistry.opencmis.commons.exceptions.CmisContentAlreadyExistsException)1 CmisNameConstraintViolationException (org.apache.chemistry.opencmis.commons.exceptions.CmisNameConstraintViolationException)1 CmisObjectNotFoundException (org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException)1 CmisPermissionDeniedException (org.apache.chemistry.opencmis.commons.exceptions.CmisPermissionDeniedException)1 ContentStreamImpl (org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl)1