private String copyFileToRepository(String fileName, String folderId) {
LOG.debug("uploading file " + fileName);
FileInputStream is = null;
Map<String, Object> properties = new HashMap<String, Object>();
Folder parentFolder;
String id = null;
if (null == folderId)
parentFolder = session.getRootFolder();
else
parentFolder = (Folder) session.getObject(folderId);
try {
File f = new File(fileName);
Tika tika = new Tika();
String mimeType = tika.detect(f);
LOG.info("Detected MIME type: "+ mimeType);
// extract metadata: first get a parser
MetadataParser parser = CFG.getParser(mimeType);
if (null == parser) {
properties.put(PropertyIds.NAME, f.getName());
properties.put(PropertyIds.OBJECT_TYPE_ID, CFG.getDefaultDocumentType());
} else {
parser.reset();
PropertyMapper mapper = CFG.getPropertyMapper(mimeType);
if (null == mapper)
throw new MapperException("Unknown mime type (no configuration): " + mimeType);
String typeId = mapper.getMappedTypeId();
if (null == typeId)
throw new MapperException("No CMIS type configured for mime type" + mimeType);
TypeDefinition td = session.getTypeDefinition(typeId);
if (null == td)
throw new MapperException("CMIS type " + typeId + " does not exist on server.");
LOG.info("Detected MIME type: "+ mimeType + " is mapped to CMIS type id: " + td.getId());
parser.extractMetadata(f, td);
properties = parser.getCmisProperties();
}
// check if there is an overridden content type configured
int posLastDot = f.getName().indexOf('.');
String ext = posLastDot < 0 ? null : f.getName().substring(posLastDot+1, f.getName().length());
String overridden = null;
if (null != ext && (overridden = CFG.getContentType(ext)) != null)
mimeType = overridden;
long length = f.length();
is = new FileInputStream(fileName);
ContentStream contentStream = session.getObjectFactory().createContentStream(fileName,
length, mimeType, is);
if (!properties.containsKey(PropertyIds.NAME))
properties.put(PropertyIds.NAME, f.getName());
Document doc = parentFolder.createDocument(properties, contentStream, VersioningState.NONE);
id = doc.getId();
LOG.info("New document created with id: " + id + ", name: " + properties.get(PropertyIds.NAME) + " in folder: " + parentFolder.getId());
} catch (Exception e) {
LOG.error("Failed to create CMIS document.", e);
} finally {
if (null != is) {
try {