IBM SBT: Create a folder in a community

旧街凉风 提交于 2019-12-11 01:37:31

问题


I'm getting my hands on the SBT Toolkit with Java. Works great but having some difficulties with folders:

I need to create a folder in a community and put some files into it. Unfortunately, the class CommunityService has no such method.

I could use the FileService.createFolder(name, description, shareWith) method and share it with the community, but actually i only want the files in the community, because otherwise they're visible in the files application (public, warning message: "Sharing with the public community 'COMMUNITY NAME' will make this folder public.")

How can I achieve this?

I checked out the button in the community / files widget and found out that it is doing a POST to the communities feed:

Target URL: https://connections.host.ch/files/form/api/communitycollection/{community-uuid}/feed

POST content:

<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>

So, could I use the communityService.createData method to invoke this REST service? If yes, whats the syntax? I haven't found any documentation or examples for it.

Also, I need to get the ID of the folder after it is created, but I this i can parse from the response..

Adding files to the newly created folder should be easy (SBT provides corresponding classes and methods), but i didn't work with this yet :-)


回答1:


I've found a solution .. altough i would rather use JSON, and maybe the handle of the Xerces XML Document is not really elegant since i have not experience with it.. but it works..

    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;
}



回答2:


Adding files to a community folder is quite simple, I was just working on this and I thought I'd share my solution with you.

I'm using the latest version of the core file: com.ibm.sbt.core-1.1.0.20140717-1200.jar

The class com.ibm.sbt.services.client.connections.files.FileService already has a method to add a file to either MYUSERLIBRARY or USERLIBRARY. The only thing you need to modify is the 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);
}

Please note that it's not possible to add a file to multiple community folders. This might be supported in the future

Edit:

Had a look at creating a folder in a community, you can easily create a folder in a community by modifying the public File createFolder(File folder) throws ClientServicesException method in 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;
}

Make sure to set the category (=collection) in your File object, or it will return a One or more of the following required paramters are missing: itemId. error.



来源:https://stackoverflow.com/questions/26912475/ibm-sbt-create-a-folder-in-a-community

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!