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);
}
}
Aggregations