我正在使用Java获取SBT Toolkit。工作得很好,但文件夹有些困难:
我需要在社区中创建一个文件夹并将一些文件放入其中。不幸的是,班级CommunityService
没有这样的方法。
我可以使用FileService.createFolder(name, description, shareWith)
方法与社区分享,但实际上我只想要社区中的文件,因为否则它们会在文件应用程序中显示(公共,警告消息:& #34;与公共社区共享'社区名称将公开此文件夹。")
我怎样才能做到这一点?
我查看了社区/文件窗口小部件中的按钮,发现它正在对社区Feed进行POST:
目标网址:https://connections.host.ch/files/form/api/communitycollection/{community-uuid}/feed
POST内容:
<entry xmlns="http://www.w3.org/2005/Atom">
<category term="collection" label="collection" scheme="tag:ibm.com,2006:td/type"></category>
<label xmlns="urn:ibm.com/td" makeUnique="true">TEST Folder</label>
<title>TEST Folder</title>
<summary type="text">teset set e</summary>
</entry>
那么,我可以使用communityService.createData
方法来调用此REST服务吗?如果是,那么语法是什么?我还没有找到任何文档或示例。
另外,我需要在创建文件夹后获取该文件夹的ID,但是我可以从响应中解析..
将文件添加到新创建的文件夹应该很容易(SBT提供相应的类和方法),但我还没有使用它: - )
答案 0 :(得分:2)
我找到了解决方案.. 虽然我宁愿使用JSON,也许Xerces XML Document的句柄并不是很优雅,因为我没有经验...但是它有效..
public String createFolderInCommunity() {
String folderId = "";
try {
// this is the atom entry.. would be nices if it was JSON..
String entry = "<entry xmlns=\"http://www.w3.org/2005/Atom\" xmlns:app=\"http://www.w3.org/2007/app\" xmlns:snx=\"http://www.ibm.com/xmlns/prod/sn\">"
+ "<category term=\"collection\" label=\"collection\" scheme=\"tag:ibm.com,2006:td/type\"></category>"
+ "<label xmlns=\"urn:ibm.com/td\" makeUnique=\"true\">TESTssss4444</label>"
+ "<title>Test: "
+ (new Date()).toString()
+ "</title>"
+ "<summary type=\"text\">teset set e</summary>"
+ "</entry>";
// Request URI with the Community ID.. of course the community id
// will be given as a parameter
String requestUri = "/files/form/api/communitycollection/1802d0e8-f6b8-4d51-8db0-75997ed83489/feed";
String payload = entry;
// here would be the point of using APPLICATION_JSON, but did not
// find any documentation about the JSON Object format :-(
ClientService.ContentString cc = new ClientService.ContentString(
payload, CommonConstants.APPLICATION_ATOM_XML);
// create the service uppon the IBM Connections endpoint (in this
// case SSO)
Response response = getEndPoint().getClientService().post(
requestUri, cc);
// Getting the object as a apache xerces DeferredDocumentImpl, with
// which i have absolutely no experience..
DeferredDocumentImpl obj = (DeferredDocumentImpl) response
.getData();
NodeList lstNodes = obj.getFirstChild().getChildNodes();
// so getting the value this way might be clumsy, but it works...
for (int x = 0; x < lstNodes.getLength(); x++) {
String name = lstNodes.item(x).getNodeName();
if (name.equals("td:uuid")) {
folderId = lstNodes.item(x).getFirstChild()
.getTextContent();
break;
}
}
} catch (Exception e) {
Util.logError(e);
}
return folderId;
}
答案 1 :(得分:0)
将文件添加到社区文件夹非常简单,我正在研究这个问题,我想我会与您分享我的解决方案。
我正在使用最新版本的核心文件:com.ibm.sbt.core-1.1.0.20140717-1200.jar
班级com.ibm.sbt.services.client.connections.files.FileService
已经有了将文件添加到MYUSERLIBRARY
或USERLIBRARY
的方法。您需要修改的唯一内容是requestUri
。
public void addFileToCommunityFolder(String communityId, String fileId, List<String> folderIds, Map<String, String> parameters) throws ClientServicesException {
String accessType = AccessType.AUTHENTICATED.getText();
// COMMUNITY_FILE_FEED : {files}/{authType}/{accessType}/communitylibrary/{communityId}/document/{fileId}/feed
String requestUri = FileUrls.COMMUNITY_FILE_FEED.format(this, FileUrlParts.accessType.get(accessType), FileUrlParts.communityId.get(communityId), FileUrlParts.fileId.get(fileId));
Map<String, String> headers = new HashMap<String, String>();
headers.put(Headers.ContentType, Headers.ATOM);
headers.put(Headers.ContentLanguage, Headers.UTF);
parameters = (null == parameters) ? new HashMap<String, String>() : parameters;
String payload = new EntityIdSerializer(folderIds,FileConstants.CATEGORY_COLLECTION).fileIdListPayload();
Response response = createData(requestUri, parameters, headers, payload);
checkResponseCode(response, HTTPCode.NO_CONTENT);
}
请注意,无法将文件添加到多个社区文件夹。这可能会在未来得到支持
编辑:
如果想要在社区中创建文件夹,可以通过修改public File createFolder(File folder) throws ClientServicesException
com.ibm.sbt.services.client.connections.files.FileService
方法轻松地在社区中创建文件夹
public File createCommunityFolder(String communityId, File folder) throws ClientServicesException {
String accessType = AccessType.AUTHENTICATED.getText();
// COMMUNITY_COLLECTIONS_FEED : {files}/{authType}/{accessType}/communitycollection/{communityId}/feed
String requestUri = FileUrls.COMMUNITY_COLLECTIONS_FEED.format(this, FileUrlParts.accessType.get(accessType), FileUrlParts.communityId.get(communityId));
String payload = new FileSerializer(folder).generateFileUpdatePayload();
Response response = createData(requestUri, null, new ClientService.ContentString(payload, CommonConstants.APPLICATION_ATOM_XML));
checkResponseCode(response, HTTPCode.CREATED);
File r = getFileFeedHandler().createEntity(response);
folder.clearFieldsMap();
folder.setDataHandler(r.getDataHandler());
return folder;
}
确保在File
对象中设置类别(=集合),否则会返回One or more of the following required paramters are missing: itemId.
错误。