use of org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException in project alfresco-repository by Alfresco.
the class CMISConnector method getContentStream.
/**
* Gets the content from the repository.
*/
public ContentStream getContentStream(CMISNodeInfo info, String streamId, BigInteger offset, BigInteger length) {
// get the type and check if the object can have content
TypeDefinitionWrapper type = info.getType();
checkDocumentTypeForContent(type);
// looks like a document, now get the content
ContentStreamImpl result = new ContentStreamImpl();
result.setFileName(info.getName());
// if streamId is set, fetch other content
NodeRef streamNodeRef = info.getNodeRef();
if ((streamId != null) && (streamId.length() > 0)) {
CMISNodeInfo streamInfo = createNodeInfo(streamId);
if (!streamInfo.isVariant(CMISObjectVariant.CURRENT_VERSION)) {
throw new CmisInvalidArgumentException("Stream id is invalid: " + streamId + ", expected variant " + CMISObjectVariant.CURRENT_VERSION + ", got variant " + streamInfo.getObjectVariant());
}
streamNodeRef = streamInfo.getNodeRef();
type = streamInfo.getType();
checkDocumentTypeForContent(type);
}
// get the stream now
try {
ContentReader contentReader = contentService.getReader(streamNodeRef, ContentModel.PROP_CONTENT);
if (contentReader == null) {
throw new CmisConstraintException("Document has no content!");
}
result.setMimeType(contentReader.getMimetype());
long contentSize = contentReader.getSize();
if ((offset == null) && (length == null)) {
result.setStream(contentReader.getContentInputStream());
result.setLength(BigInteger.valueOf(contentSize));
publishReadEvent(streamNodeRef, info.getName(), result.getMimeType(), contentSize, contentReader.getEncoding(), null);
} else {
long off = (offset == null ? 0 : offset.longValue());
long len = (length == null ? contentSize : length.longValue());
if (off + len > contentSize) {
len = contentReader.getSize() - off;
}
result.setStream(new RangeInputStream(contentReader.getContentInputStream(), off, len));
result.setLength(BigInteger.valueOf(len));
publishReadEvent(streamNodeRef, info.getName(), result.getMimeType(), contentSize, contentReader.getEncoding(), off + " - " + len);
}
} catch (Exception e) {
if (e instanceof CmisBaseException) {
throw (CmisBaseException) e;
} else {
StringBuilder msg = new StringBuilder("Failed to retrieve content: " + e.getMessage());
Throwable cause = e.getCause();
if (cause != null) {
// add the cause to the CMIS exception
msg.append(", ");
msg.append(cause.getMessage());
}
throw new CmisRuntimeException(msg.toString(), e);
}
}
return result;
}
use of org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException in project alfresco-repository by Alfresco.
the class CMISConnector method getRootNodeRef.
/**
* Returns the root folder node ref.
*/
public NodeRef getRootNodeRef() {
NodeRef rootNodeRef = (NodeRef) singletonCache.get(KEY_CMIS_ROOT_NODEREF);
if (rootNodeRef == null) {
rootNodeRef = AuthenticationUtil.runAs(new RunAsWork<NodeRef>() {
public NodeRef doWork() throws Exception {
return transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<NodeRef>() {
public NodeRef execute() throws Exception {
NodeRef root = nodeService.getRootNode(storeRef);
List<NodeRef> rootNodes = searchService.selectNodes(root, rootPath, null, namespaceService, false);
if (rootNodes.size() != 1) {
throw new CmisRuntimeException("Unable to locate CMIS root path " + rootPath);
}
return rootNodes.get(0);
}
}, true);
}
}, AuthenticationUtil.getSystemUserName());
if (rootNodeRef == null) {
throw new CmisObjectNotFoundException("Root folder path '" + rootPath + "' not found!");
}
singletonCache.put(KEY_CMIS_ROOT_NODEREF, rootNodeRef);
}
return rootNodeRef;
}
use of org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException in project alfresco-repository by Alfresco.
the class AlfrescoCmisServiceImpl method getObjectInfoIntern.
/**
* Collects the {@link ObjectInfo} about an object.
*
* (Provided by OpenCMIS, but optimized for Alfresco.)
*/
@SuppressWarnings("unchecked")
@Override
protected ObjectInfo getObjectInfoIntern(String repositoryId, ObjectData object) {
// if the object has no properties, stop here
if (object.getProperties() == null || object.getProperties().getProperties() == null) {
throw new CmisRuntimeException("No properties!");
}
String objectId = object.getId();
CMISNodeInfo ni = getOrCreateNodeInfo(objectId);
ObjectInfoImpl info = new ObjectInfoImpl();
// general properties
info.setObject(object);
info.setId(objectId);
info.setName(getStringProperty(object, PropertyIds.NAME));
info.setCreatedBy(getStringProperty(object, PropertyIds.CREATED_BY));
info.setCreationDate(getDateTimeProperty(object, PropertyIds.CREATION_DATE));
info.setLastModificationDate(getDateTimeProperty(object, PropertyIds.LAST_MODIFICATION_DATE));
info.setTypeId(getIdProperty(object, PropertyIds.OBJECT_TYPE_ID));
info.setBaseType(object.getBaseTypeId());
if (ni.isRelationship()) {
// versioning
info.setWorkingCopyId(null);
info.setWorkingCopyOriginalId(null);
info.setVersionSeriesId(null);
info.setIsCurrentVersion(true);
info.setWorkingCopyId(null);
info.setWorkingCopyOriginalId(null);
// content
info.setHasContent(false);
info.setContentType(null);
info.setFileName(null);
// parent
info.setHasParent(false);
// policies and relationships
info.setSupportsRelationships(false);
info.setSupportsPolicies(false);
// renditions
info.setRenditionInfos(null);
// relationships
info.setRelationshipSourceIds(null);
info.setRelationshipTargetIds(null);
// global settings
info.setHasAcl(false);
info.setSupportsDescendants(false);
info.setSupportsFolderTree(false);
} else if (ni.isFolder()) {
// versioning
info.setWorkingCopyId(null);
info.setWorkingCopyOriginalId(null);
info.setVersionSeriesId(null);
info.setIsCurrentVersion(true);
info.setWorkingCopyId(null);
info.setWorkingCopyOriginalId(null);
// content
info.setHasContent(false);
info.setContentType(null);
info.setFileName(null);
// parent
info.setHasParent(!ni.isRootFolder());
// policies and relationships
info.setSupportsRelationships(true);
info.setSupportsPolicies(true);
// renditions
info.setRenditionInfos(null);
// relationships
setRelaionshipsToObjectInfo(object, info);
// global settings
info.setHasAcl(true);
info.setSupportsDescendants(true);
info.setSupportsFolderTree(true);
} else if (ni.isDocument()) {
// versioning
info.setWorkingCopyId(null);
info.setWorkingCopyOriginalId(null);
info.setVersionSeriesId(ni.getCurrentNodeId());
if (ni.isPWC()) {
info.setIsCurrentVersion(false);
info.setWorkingCopyId(ni.getObjectId());
info.setWorkingCopyOriginalId(ni.getCurrentObjectId());
} else {
info.setIsCurrentVersion(ni.isCurrentVersion());
if (ni.hasPWC()) {
info.setWorkingCopyId(ni.getCurrentNodeId() + CMISConnector.ID_SEPERATOR + CMISConnector.PWC_VERSION_LABEL);
info.setWorkingCopyOriginalId(ni.getCurrentObjectId());
} else {
info.setWorkingCopyId(null);
info.setWorkingCopyOriginalId(null);
}
}
// content
String fileName = getStringProperty(object, PropertyIds.CONTENT_STREAM_FILE_NAME);
String mimeType = getStringProperty(object, PropertyIds.CONTENT_STREAM_MIME_TYPE);
String streamId = getIdProperty(object, PropertyIds.CONTENT_STREAM_ID);
BigInteger length = getIntegerProperty(object, PropertyIds.CONTENT_STREAM_LENGTH);
boolean hasContent = fileName != null || mimeType != null || streamId != null || length != null;
if (hasContent) {
info.setHasContent(hasContent);
info.setContentType(mimeType);
info.setFileName(fileName);
} else {
info.setHasContent(false);
info.setContentType(null);
info.setFileName(null);
}
// parent
info.setHasParent(ni.isCurrentVersion() || ni.isPWC());
// policies and relationships
info.setSupportsRelationships(true);
info.setSupportsPolicies(true);
// renditions
String renditionFilter = getRequestParameterRenditionFilter();
List<RenditionInfo> renditionInfos = new ArrayList<>();
CMISNodeInfo nodeInfo = getOrCreateNodeInfo(objectId);
NodeRef nodeRef = nodeInfo.getNodeRef();
if (nodeRef != null) {
List<RenditionData> renditions = connector.getRenditions(nodeRef, renditionFilter, null, null);
for (RenditionData rendition : renditions) {
RenditionInfoImpl renditionInfo = new RenditionInfoImpl();
renditionInfo.setId(rendition.getStreamId());
renditionInfo.setKind(rendition.getKind());
renditionInfo.setContentType(rendition.getMimeType());
renditionInfo.setTitle(rendition.getTitle());
renditionInfo.setLength(rendition.getBigLength());
renditionInfos.add(renditionInfo);
}
}
info.setRenditionInfos(renditionInfos);
// relationships
setRelaionshipsToObjectInfo(object, info);
// global settings
info.setHasAcl(true);
info.setSupportsDescendants(true);
info.setSupportsFolderTree(true);
} else if (ni.isItem()) {
info.setHasAcl(true);
info.setHasContent(false);
}
return info;
}
use of org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException in project alfresco-repository by Alfresco.
the class AlfrescoCmisServiceImpl method create.
// --- object service ---
@Override
public String create(String repositoryId, Properties properties, String folderId, ContentStream contentStream, VersioningState versioningState, List<String> policies, ExtensionsData extension) {
checkRepositoryId(repositoryId);
// check properties
if (properties == null || properties.getProperties() == null) {
throw new CmisInvalidArgumentException("Properties must be set!");
}
// get the type
String objectTypeId = connector.getObjectTypeIdProperty(properties);
// find the type
TypeDefinitionWrapper type = connector.getOpenCMISDictionaryService().findType(objectTypeId);
if (type == null) {
throw new CmisInvalidArgumentException("Type '" + objectTypeId + "' is unknown!");
}
// create object
String newId = null;
switch(type.getBaseTypeId()) {
case CMIS_DOCUMENT:
versioningState = getDocumentDefaultVersioningState(versioningState, type);
newId = createDocument(repositoryId, properties, folderId, contentStream, versioningState, policies, null, null, extension);
break;
case CMIS_FOLDER:
newId = createFolder(repositoryId, properties, folderId, policies, null, null, extension);
break;
case CMIS_POLICY:
newId = createPolicy(repositoryId, properties, folderId, policies, null, null, extension);
break;
case CMIS_ITEM:
newId = createItem(repositoryId, properties, folderId, policies, null, null, extension);
break;
default:
break;
}
// check new object id
if (newId == null) {
throw new CmisRuntimeException("Creation failed!");
}
boolean isObjectInfoRequired = getContext().isObjectInfoRequired();
if (isObjectInfoRequired) {
try {
getObjectInfo(repositoryId, newId, "*", IncludeRelationships.NONE);
} catch (InvalidNodeRefException e) {
throw new CmisRuntimeException("Creation failed! New object not found!");
}
}
// return the new object id
return newId;
}
use of org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException in project alfresco-repository by Alfresco.
the class AlfrescoCmisServiceImpl method getFolderParent.
@Override
public ObjectData getFolderParent(String repositoryId, String folderId, String filter, ExtensionsData extension) {
checkRepositoryId(repositoryId);
// get the node ref
CMISNodeInfo info = getOrCreateFolderInfo(folderId, "Folder");
// the root folder has no parent
if (info.isRootFolder()) {
throw new CmisInvalidArgumentException("Root folder has no parent!");
}
// get the parent
List<CMISNodeInfo> parentInfos = info.getParents();
if (parentInfos.isEmpty()) {
throw new CmisRuntimeException("Folder has no parent and is not the root folder?!");
}
CMISNodeInfo parentInfo = addNodeInfo(parentInfos.get(0));
ObjectData result = connector.createCMISObject(parentInfo, filter, false, IncludeRelationships.NONE, CMISConnector.RENDITION_NONE, false, false);
boolean isObjectInfoRequired = getContext().isObjectInfoRequired();
if (isObjectInfoRequired) {
getObjectInfo(repositoryId, parentInfo.getObjectId(), IncludeRelationships.NONE);
}
return result;
}
Aggregations