Java源码示例:org.apache.chemistry.opencmis.commons.data.ContentStream
示例1
/**
* Create dispatch for AtomPub
*
* @param context call context
* @param properties the properties
* @param folderId identifier of the parent folder
* @param contentStream stream of the document to create
* @param versioningState state of the version
* @param objectInfos informations
*
* @return the newly created object
*/
public ObjectData create(CallContext context, Properties properties, String folderId, ContentStream contentStream,
VersioningState versioningState, ObjectInfoHandler objectInfos) {
debug("create " + folderId);
validatePermission(folderId, context, Permission.WRITE);
String typeId = getTypeId(properties);
TypeDefinition type = types.getType(typeId);
if (type == null)
throw new CmisObjectNotFoundException("Type '" + typeId + "' is unknown!");
String objectId = null;
if (type.getBaseTypeId() == BaseTypeId.CMIS_DOCUMENT) {
objectId = createDocument(context, properties, folderId, contentStream, versioningState);
return compileObjectType(context, getDocument(objectId), null, false, false, objectInfos);
} else if (type.getBaseTypeId() == BaseTypeId.CMIS_FOLDER) {
objectId = createFolder(context, properties, folderId);
return compileObjectType(context, getFolder(objectId), null, false, false, objectInfos);
} else {
throw new CmisObjectNotFoundException("Cannot create object of type '" + typeId + "'!");
}
}
示例2
/**
* Create test document with content
*
* @param target
* @param newDocName
*/
private static void createDocument(Folder target, String newDocName) {
Map<String, String> props = new HashMap<String, String>();
props.put(PropertyIds.OBJECT_TYPE_ID, "cmis:document");
props.put(PropertyIds.NAME, newDocName);
props.put(TypeManager.PROP_TAGS, "tag1,tag2,tag3");
System.out.println("This is a test document: " + newDocName);
String content = "aegif Mind Share Leader Generating New Paradigms by aegif corporation.";
byte[] buf = null;
try {
buf = content.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
ByteArrayInputStream input = new ByteArrayInputStream(buf);
ContentStream contentStream = session.getObjectFactory().createContentStream(newDocName, buf.length,
"text/plain; fileNameCharset=UTF-8", input);
target.createDocument(props, contentStream, VersioningState.NONE);
}
示例3
private String parseMimeType(ContentStream contentStream)
{
String mimeType = null;
String tmp = contentStream.getMimeType();
if(tmp != null)
{
int idx = tmp.indexOf(";");
if(idx != -1)
{
mimeType = tmp.substring(0, idx).trim();
}
else
{
mimeType = tmp;
}
}
return mimeType;
}
示例4
@Override
public ContentStream getContentStream(
String repositoryId, String objectId, String streamId, BigInteger offset,
BigInteger length, ExtensionsData extension)
{
checkRepositoryId(repositoryId);
// what kind of object is it?
CMISNodeInfo info = getOrCreateNodeInfo(objectId, "Object");
// relationships cannot have content
if (info.isVariant(CMISObjectVariant.ASSOC))
{
throw new CmisInvalidArgumentException("Object is a relationship and cannot have content!");
}
// now get it
return connector.getContentStream(info, streamId, offset, length);
}
示例5
private static Document createDocument(Folder target, String newDocName, Session session)
{
Map<String, String> props = new HashMap<String, String>();
props.put(PropertyIds.OBJECT_TYPE_ID, "cmis:document");
props.put(PropertyIds.NAME, newDocName);
String content = "aegif Mind Share Leader Generating New Paradigms by aegif corporation.";
byte[] buf = null;
try
{
buf = content.getBytes("ISO-8859-1"); // set the encoding here for the content stream
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
ByteArrayInputStream input = new ByteArrayInputStream(buf);
ContentStream contentStream = session.getObjectFactory().createContentStream(newDocName, buf.length,
"text/plain; charset=UTF-8", input); // additionally set the charset here
// NOTE that we intentionally specified the wrong charset here (as UTF-8)
// because Alfresco does automatic charset detection, so we will ignore this explicit request
return target.createDocument(props, contentStream, VersioningState.MAJOR);
}
示例6
@Override
public String create(String repositoryId, Properties properties, String folderId,
ContentStream contentStream, VersioningState versioningState,
List<String> policies, ExtensionsData extension)
{
FileFilterMode.setClient(Client.cmis);
try
{
return super.create(
repositoryId,
properties,
folderId,
contentStream,
versioningState,
policies,
extension);
}
finally
{
FileFilterMode.clearClient();
}
}
示例7
/**
* Overridden to capture content upload for publishing to analytics service.
*/
@Override
public String createDocument(String repositoryId, Properties properties, String folderId,
ContentStream contentStream, VersioningState versioningState,
List<String> policies, Acl addAces, Acl removeAces, ExtensionsData extension)
{
String newId = super.createDocument(
repositoryId,
properties,
folderId,
contentStream,
versioningState,
policies,
addAces,
removeAces,
extension);
return newId;
}
示例8
/**
* Overridden to capture content upload for publishing to analytics service.
*/
@Override
public void setContentStream(String repositoryId, Holder<String> objectId,
Boolean overwriteFlag, Holder<String> changeToken, ContentStream contentStream,
ExtensionsData extension)
{
FileFilterMode.setClient(Client.cmis);
try
{
super.setContentStream(repositoryId, objectId, overwriteFlag, changeToken, contentStream, extension);
}
finally
{
FileFilterMode.clearClient();
}
}
示例9
/**
* This is a custom implementation of the doGet method of the
* {@link HttpServlet}.
* <p>
* Here, we expect an "objectId" to be passed as input parameter. This
* objectId will be fetched to the {@link Session} to fetch the existing
* document(if present).
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
final String objectId = request.getParameter("objectId");
try {
if (openCmisSession != null) {
Document doc = (Document) openCmisSession.getObject(objectId);
ContentStream content = doc.getContentStream();
String type = content.getMimeType();
String name = content.getFileName();
int length = (int) content.getLength();
InputStream stream = content.getStream();
response.setContentType(type);
response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + name);
response.setContentLength(length);
ioCopy(stream, response.getOutputStream());
} else {
response.setStatus(501);
}
} catch (Exception exception) {
LOGGER.error(exception.getMessage());
}
}
示例10
@Transactional
public void setContentStream(CmisRepositoryConfiguration config,
Holder<String> objectId,
Boolean overwriteFlag,
Holder<String> changeToken,
ContentStream contentStream,
ExtensionsData extension) {
Object object = getObjectInternal(config, objectId.getValue(), Collections.EMPTY_SET, false, IncludeRelationships.NONE,
"", false, false, extension);
if (object != null) {
config.cmisDocumentStorage().setContent(object, contentStream.getStream());
// re-fetch to ensure we have the latest
object = getObjectInternal(config, objectId.getValue(), Collections.EMPTY_SET, false, IncludeRelationships.NONE,
"", false, false, extension);
if (BeanUtils.hasFieldWithAnnotation(object, MimeType.class)) {
BeanUtils.setFieldWithAnnotation(object, MimeType.class, contentStream.getMimeType());
}
config.cmisDocumentRepository().save(object);
}
}
示例11
public void checkIn(String repositoryId,
Holder<String> objectId,
Boolean major,
Properties properties,
ContentStream contentStream,
String checkinComment,
List<String> policies,
Acl addAces, Acl
removeAces,
ExtensionsData extension) {
bridge.checkIn(config,
objectId.getValue(),
major,
properties,
contentStream,
checkinComment,
policies,
addAces,
removeAces,
extension);
}
示例12
public String createDocument(String repositoryId,
Properties properties,
String folderId,
ContentStream contentStream,
VersioningState versioningState,
List<String> policies,
Acl addAces,
Acl removeAces,
ExtensionsData extension) {
return bridge.createDocument(config,
properties,
folderId,
contentStream,
versioningState,
policies,
addAces,
removeAces,
extension);
}
示例13
public void checkIn(Holder<String> objectId, Boolean major, ContentStream contentStream, Properties properties,
String checkinComment) {
//debug("checkin " + objectId);
log.debug("checkin {}", objectId);
validatePermission(objectId.getValue(), null, Permission.WRITE);
try {
PersistentObject object = getObject(objectId.getValue());
if (object == null)
throw new CmisObjectNotFoundException(String.format("Object %s not found!", objectId.getValue()));
if (!(object instanceof Document))
throw new CmisObjectNotFoundException(
String.format("Object %s is not a Document!", objectId.getValue()));
Document doc = (Document) object;
if (doc.getStatus() == Document.DOC_CHECKED_OUT
&& ((getSessionUser().getId() != doc.getLockUserId()) && (!getSessionUser().isMemberOf("admin")))) {
throw new CmisPermissionDeniedException(
String.format("You can't do a checkin on object %s!", objectId.getValue()));
}
DocumentHistory transaction = new DocumentHistory();
transaction.setSessionId(sid);
transaction.setEvent(DocumentEvent.CHECKEDIN.toString());
transaction.setUser(getSessionUser());
transaction.setComment(checkinComment);
if (properties != null) {
updateDocumentMetadata(doc, properties, false);
}
documentManager.checkin(doc.getId(), contentStream.getStream(), doc.getFileName(), major, null,
transaction);
} catch (Throwable t) {
catchError(t);
}
}
示例14
@Override
public String create(String repositoryId, Properties properties, String folderId, ContentStream contentStream,
VersioningState versioningState, List<String> policies, ExtensionsData extension) {
validateSession();
ObjectData object = getRepository().create(getCallContext(), properties, folderId, contentStream,
versioningState, this);
return object.getId();
}
示例15
@Override
public String createDocument(String repositoryId, Properties properties, String folderId,
ContentStream contentStream, VersioningState versioningState, List<String> policies, Acl addAces,
Acl removeAces, ExtensionsData extension) {
validateSession();
return getRepository().createDocument(getCallContext(), properties, folderId, contentStream, versioningState);
}
示例16
@Override
public void checkIn(String repositoryId, Holder<String> objectId, Boolean major, Properties properties,
ContentStream contentStream, String checkinComment, List<String> policies, Acl addAces, Acl removeAces,
ExtensionsData extension) {
validateSession();
getRepository().checkIn(objectId, major, contentStream, properties, checkinComment);
}
示例17
@Override
public void setContentStream(String repositoryId, Holder<String> objectId, Boolean overwriteFlag,
Holder<String> changeToken, ContentStream contentStream, ExtensionsData extension) {
validateSession();
log.debug("setContentStream {}", objectId);
checkOut(repositoryId, objectId, extension, new Holder<Boolean>(false));
checkIn(repositoryId, objectId, false, null, contentStream, "", null, null, null, extension);
}
示例18
public ReusableContentStream(ContentStream contentStream) throws Exception
{
setLength(contentStream.getBigLength());
setMimeType(contentStream.getMimeType());
setFileName(contentStream.getFileName());
file = TempFileProvider.createTempFile(contentStream.getStream(), "cmis", "contentStream");
}
示例19
@Override
public String createDocument(String repositoryId, Properties properties, String folderId,
ContentStream contentStream, VersioningState versioningState, List<String> policies, Acl addAces,
Acl removeAces, ExtensionsData extension) {
return service.createDocument(repositoryId, properties, folderId, contentStream, versioningState, policies,
addAces, removeAces, extension);
}
示例20
@Override
public void checkIn(String repositoryId, Holder<String> objectId, Boolean major, Properties properties,
ContentStream contentStream, String checkinComment, List<String> policies, Acl addAces, Acl removeAces,
ExtensionsData extension) {
service.checkIn(repositoryId, objectId, major, properties, contentStream, checkinComment, policies, addAces,
removeAces, extension);
}
示例21
@Override
public String create(String repositoryId, Properties properties, String folderId, ContentStream contentStream,
VersioningState versioningState, List<String> policies, ExtensionsData extension) {
checkRepositoryId(repositoryId);
checkProperties(properties);
checkProperty(properties, PropertyIds.OBJECT_TYPE_ID, String.class);
try {
return getWrappedService().create(repositoryId, properties, folderId, contentStream, versioningState,
policies, extension);
} catch (Exception e) {
throw createCmisException(e);
}
}
示例22
@Override
public String createDocument(String repositoryId, Properties properties, String folderId,
ContentStream contentStream, VersioningState versioningState, List<String> policies, Acl addAces,
Acl removeAces, ExtensionsData extension) {
checkRepositoryId(repositoryId);
checkProperties(properties);
checkProperty(properties, PropertyIds.OBJECT_TYPE_ID, String.class);
try {
return getWrappedService().createDocument(repositoryId, properties, folderId, contentStream,
versioningState, policies, addAces, removeAces, extension);
} catch (Exception e) {
throw createCmisException(e);
}
}
示例23
@Override
public ContentStream getContentStream(String repositoryId, String objectId, String streamId, BigInteger offset,
BigInteger length, ExtensionsData extension) {
checkRepositoryId(repositoryId);
checkId("Object Id", objectId);
checkNullOrPositive("Offset", offset);
checkNullOrPositive("Length", length);
try {
return getWrappedService().getContentStream(repositoryId, objectId, streamId, offset, length, extension);
} catch (Exception e) {
throw createCmisException(e);
}
}
示例24
@Override
public void setContentStream(String repositoryId, Holder<String> objectId, Boolean overwriteFlag,
Holder<String> changeToken, ContentStream contentStream, ExtensionsData extension) {
checkRepositoryId(repositoryId);
checkHolderId("Object Id", objectId);
overwriteFlag = getDefaultTrue(overwriteFlag);
checkContentStream(contentStream);
try {
getWrappedService().setContentStream(repositoryId, objectId, overwriteFlag, changeToken, contentStream,
extension);
} catch (Exception e) {
throw createCmisException(e);
}
}
示例25
@Override
public void appendContentStream(String repositoryId, Holder<String> objectId, Holder<String> changeToken,
ContentStream contentStream, boolean isLastChunk, ExtensionsData extension) {
checkRepositoryId(repositoryId);
checkHolderId("Object Id", objectId);
checkContentStream(contentStream);
try {
getWrappedService().appendContentStream(repositoryId, objectId, changeToken, contentStream, isLastChunk,
extension);
} catch (Exception e) {
throw createCmisException(e);
}
}
示例26
@Override
public void checkIn(String repositoryId, Holder<String> objectId, Boolean major, Properties properties,
ContentStream contentStream, String checkinComment, List<String> policies, Acl addAces, Acl removeAces,
ExtensionsData extension) {
checkRepositoryId(repositoryId);
checkHolderId("Object Id", objectId);
major = getDefaultTrue(major);
try {
getWrappedService().checkIn(repositoryId, objectId, major, properties, contentStream, checkinComment,
policies, addAces, removeAces, extension);
} catch (Exception e) {
throw createCmisException(e);
}
}
示例27
public void testDownloadEvent() throws InterruptedException
{
Repository repository = getRepository("admin", "admin");
Session session = repository.createSession();
Folder rootFolder = session.getRootFolder();
String docname = "mydoc-" + GUID.generate() + ".txt";
Map<String, String> props = new HashMap<String, String>();
{
props.put(PropertyIds.OBJECT_TYPE_ID, "D:cmiscustom:document");
props.put(PropertyIds.NAME, docname);
}
// content
byte[] byteContent = "Hello from Download testing class".getBytes();
InputStream stream = new ByteArrayInputStream(byteContent);
ContentStream contentStream = new ContentStreamImpl(docname, BigInteger.valueOf(byteContent.length), "text/plain", stream);
Document doc1 = rootFolder.createDocument(props, contentStream, VersioningState.MAJOR);
NodeRef doc1NodeRef = cmisIdToNodeRef(doc1.getId());
ContentStream content = doc1.getContentStream();
assertNotNull(content);
//range request
content = doc1.getContentStream(BigInteger.valueOf(2),BigInteger.valueOf(4));
assertNotNull(content);
}
示例28
public void testEncodingForCreateContentStream()
{
ServiceRegistry serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
FileFolderService ffs = serviceRegistry.getFileFolderService();
// Authenticate as system
AuthenticationComponent authenticationComponent = (AuthenticationComponent) ctx
.getBean(BEAN_NAME_AUTHENTICATION_COMPONENT);
authenticationComponent.setSystemUserAsCurrentUser();
try
{
/* Create the document using openCmis services */
Repository repository = getRepository("admin", "admin");
Session session = repository.createSession();
Folder rootFolder = session.getRootFolder();
Document document = createDocument(rootFolder, "test_file_" + GUID.generate() + ".txt", session);
ContentStream content = document.getContentStream();
assertNotNull(content);
content = document.getContentStream(BigInteger.valueOf(2), BigInteger.valueOf(4));
assertNotNull(content);
NodeRef doc1NodeRef = cmisIdToNodeRef(document.getId());
FileInfo fileInfo = ffs.getFileInfo(doc1NodeRef);
Map<QName, Serializable> properties = fileInfo.getProperties();
ContentDataWithId contentData = (ContentDataWithId) properties
.get(QName.createQName("{http://www.alfresco.org/model/content/1.0}content"));
String encoding = contentData.getEncoding();
assertEquals("ISO-8859-1", encoding);
}
finally
{
authenticationComponent.clearCurrentSecurityContext();
}
}
示例29
public ContentData getContent(String objectId) throws IOException
{
CmisObject o = getObject(objectId);
if(o instanceof Document)
{
Document d = (Document)o;
ContentStream res = d.getContentStream();
ContentData c = new ContentData(res);
return c;
}
else
{
throw new IllegalArgumentException("Object does not exist or is not a document");
}
}
示例30
public void putContent(String objectId, String filename, BigInteger length, String mimetype, InputStream content, boolean overwrite)
{
CmisObject o = getObject(objectId);
if(o instanceof Document)
{
Document d = (Document)o;
ContentStream contentStream = new ContentStreamImpl(filename, length, mimetype, content);
try
{
d.setContentStream(contentStream, overwrite);
}
finally
{
try
{
contentStream.getStream().close();
}
catch (Exception e)
{
}
}
}
else
{
throw new IllegalArgumentException("Object does not exist or is not a document");
}
}