IBM SBT: Create a folder in a community
我正在使用带有 Java 的 SBT 工具包。效果很好,但文件夹有些困难:
我需要在社区中创建一个文件夹并将一些文件放入其中。不幸的是,类
我可以使用
我怎样才能做到这一点?
我检查了社区/文件小部件中的按钮,发现它正在对社区提要进行 POST:
目标网址:
POST 内容:
1 2 3 4 5 6 | <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> TEST Folder <summary type="text">teset set e</summary> </entry> |
那么,我可以使用
另外,我需要在创建文件夹后获取它的 ID,但是我可以从响应中解析出来..
将文件添加到新创建的文件夹应该很容易(SBT 提供了相应的类和方法),但我还没有使用这个 :-)
我找到了解决方案..
虽然我更愿意使用 JSON,也许 Xerces XML 文档的句柄不是很优雅,因为我没有使用它的经验.. 但它可以工作..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | 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>" +"Test:" + (new Date()).toString() +"" +"<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; } |
将文件添加到社区文件夹非常简单,我只是在做这个,我想我会与你分享我的解决方案。
我正在使用最新版本的核心文件:com.ibm.sbt.core-1.1.0.20140717-1200.jar
类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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); } |
请注意,无法将文件添加到多个社区文件夹。将来可能会支持此功能
编辑:
看过在社区中创建文件夹,你可以通过修改
中的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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; } |
确保在您的