技术开发 频道

IBM Lotus Quickr REST服务简介


获取库的列表

    首先,必须获得库的列表,这样才能在导航树中建立优异节点。按照 Atom 的术语,文档库表示为集合。Atom 定义一个服务文档,作为获取集合的机制。要获得这个服务文档,需要知道服务器名称以及连接服务器所用的用户 ID 和密码。使用的 URL 是一个内省 URL(introspection URL),并且因为文档中发布了 URL 格式,所以对于给定的服务器,您可以自己构建 URL。对于此处的服务器,URL 如下所示:

http://quickr.acme.com/dm/atom/introspection

    对这个 URL 执行 GET 请求来获取库列表。响应包含一个 Atom 服务文档,其中包含一个 <workspace> 元素和每个库的 <collection> 元素(见清单 1)。


清单 1. Atom 服务文档 

<service> <workspace title="Teamspace Documents"> <collection title="Architecture Documents" href="http://quickr.acme.com/library/ 5d06ab0044ed8129bd5ebd4caeec5df1/feed"> <accept>application/*,image/*,*/*</accept> </collection> <collection title="Design Documents" href="http://quickr.acme.com/library/ 3c06ab0044ed8129bd5ebd4cbeec5dc4/feed"> <accept>application/*,image/*,*/*</accept> </collection> </workspace> </service>


    title 属性的值用作树节点的标签。当在树中展开节点时,使用 <collection> 元素的 href 属性中的 URL 获取库的内容。清单 2 给出处理这个响应的部分代码。

清单 2. 处理响应的部分代码

try { httpClient.addCredentials(serverConfig.getUrl(), null, null, new UsernamePasswordCredentials(serverConfig.getUserId(), serverConfig.getPassword())); httpClient.usePreemptiveAuthentication(true); ClientResponse response = httpClient.get(serverConfig.getUrl() + "/dm/atom/introspection"); if (response.getStatus() == 200) { Document serviceDoc = response.getDocument(); Element element = serviceDoc.getRoot().getFirstChild(); while (element != null) { if (element instanceof Workspace) { Workspace ws = (Workspace) element; Iterator collections = ws.getCollections() .iterator(); while (collections.hasNext()) { TreeParent collection = new TreeParent( (Collection) collections.next(), true); invisibleRoot.addChild(collection); } } element = element.getNextSibling(); } } } catch (Exception e) { e.printStackTrace(); showMessage(e.toString()); }


处理了 Atom 服务文档之后,插件就可以与用户进行交互了(见图 1)。

插件显示从服务器获得的库列表 
图 1. 插件显示从服务器获得的库列表
0
相关文章