-
Notifications
You must be signed in to change notification settings - Fork 23
開発_ CMIS API(Java)
SUGIMOTO Takuma edited this page Mar 15, 2016
·
1 revision
English/日本語
CMISセッションを作成してCMISサーバに接続し、セッションの持つCMISメソッドを呼び出して操作します
取得したCmisObject(フォルダやドキュメント)に対して同様のメソッドを呼び出すこともできます。
CMISクライアントを開発するための標準的なライブラリがオープンソースで用意されています
Apache ChemistryのOpenCMISライブラリを(mavenなどで)自分のプロジェクトにインポートします。
通常、CMISクライアントを開発するためには、以下のパッケージをインポートする必要があるでしょう:
chemistry-opencmis-commons-api
chemistry-opencmis-commons-impl
chemistry-opencmis-client-api
chemistry-opencmis-client-impl
chemistry-opencmis-client-bindings
#コードサンプル
import org.apache.chemistry.opencmis.client.api.OperationContext;
import org.apache.chemistry.opencmis.client.api.Session;
import org.apache.chemistry.opencmis.client.api.SessionFactory;
import org.apache.chemistry.opencmis.client.runtime.SessionFactoryImpl;
import org.apache.chemistry.opencmis.commons.SessionParameter;
Map<String, String> parameter = new HashMap<String, String>();
// user credentials
parameter.put(SessionParameter.USER, id);
parameter.put(SessionParameter.PASSWORD, password);
// session locale
parameter.put(SessionParameter.LOCALE_ISO3166_COUNTRY, country);
parameter.put(SessionParameter.LOCALE_ISO639_LANGUAGE, language);
// repository url
parameter.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
parameter.put(SessionParameter.ATOMPUB_URL, "http://localhost:8080/core/atom/");
parameter.put(SessionParameter.REPOSITORY_ID, "bedroom");
// create session
SessionFactory f = SessionFactoryImpl.newInstance();
Session session = f.createSession(parameter);
OperationContext operationContext = session.createOperationContext(null,
true, true, false, IncludeRelationships.BOTH, null, false, null, true, 100);
session.setDefaultContext(operationContext);
HashMap<String, Object> param = new HashMap<String, Object>();
param.put(PropertyIds.OBJECT_TYPE_ID, "cmis:folder");
param.put(PropertyIds.NAME, "folder name");
ObjectId objectId = session.createFolder(param, parentId); //Returns object id of the created object
import org.apache.chemistry.opencmis.commons.data.ContentStream
HashMap<String, Object> param = new HashMap<String, Object>();
param.put(PropertyIds.OBJECT_TYPE_ID, "cmis:document");
param.put(PropertyIds.NAME, "document name");
//Get content stream(file content) from somewhere
ContentStream contentStream;
session.getObjectFactory().createContentStream(filename, length, mimetype, fileInputStream);
ObjectId objectId = session.createDocument(param, parentId, contentStream, VersioningState.MAJOR);
HashMap<String, Object> param = new HashMap<String, Object>();
param.put(PropertyIds.NAME, "relationship name");
param.put(PropertyIds.SOURCE_ID, sourceObjectId);
param.put(PropertyIds.TARGET_ID, sourceObjectId);
ObjectId objectId = session.createRelationship(properties);
CmisObject object = session.getObject(id);
CmisObject object = session.getObject(id);
List<Document> versions = ((Document)object).getAllVersions();
CmisObject object = session.getObject(id);
Document document = (Document) object;
ContentStream contentStream = doc.getContentStream();
//Convert content stream to input stream or file
InputStream inputStream = contentStream.getStream();
File file = File.createTempFile(contentStream.getFileName(), "");
CmisObject object = session.getObject(id);
//Build property values (no need for the properties not to be updated)
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(PropertyIds.NAME, "new name");
object.updateProperties(properties);
CmisObject object = session.getObject(id);
Document document = (Document) object;
ContentStream contentStream = ... //Get content stream from somewhere
doc.setContentStream(contentStream, true);
CmisObject object = session.getObject(id);
Document document = (Document) object;
//Check out
if (doc.isVersionSeriesCheckedOut()) {
// You cannot checked out a document twice!
}
//Check out action makes a private working copy as an independent object
ObjectId pwcId = doc.checkOut(); //Returns private working copy id
//Some udpate to the private working copy (pwc)
CmisObject pwc = session.getObject(pwcId.getId());
pwc.setContentStream(contentStream, true);
pwc.updateProperties(properties);
...
//Check in
//the first argument "true" means major version update
ObjectId newDocId = pwc.checkIn(true, properties, contentStream, "Check in comment");
CmisObject object = session.getObject(id);
Document document = (Document) object;
document.cancelCheckOut();
CmisObject object = session.getObject(id);
object.delete();
または
CmisObject object = session.getObject(id);
session.delete(new ObjectIdImpl(id));
CmisObject object = session.getObject(id);
Folder folder = (Folder)object;
folder.deleteTree(true, null, true);
//Build query statement
MessageFormat format = new MessageFormat("cmis:name LIKE ''%{0}%'' OR CONTAINS(''{0}'')");
String statement = "";
if (StringUtils.isNotBlank(term)) {
statement = format.format(new String[] { term });
}
//Execute query
ItemIterable<CmisObject> results = session.queryObjects("cmis:document", statement, false, session.getDefaultContext());
Iterator<CmisObject> itr = docResults.iterator();
List<CmisObject> list = new ArrayList<CmisObject>();
while (itr.hasNext()) {
list.add(itr.next());
}
statementには CMIS Query の記法をつかいます。記法についてはCMIS Query の利用を参照ください。