通过Lotus Quickr REST API,开发一个我的空间Portlet
Lotus Quickr提供REST API访问其中的数据,这里我通过一个例子来说明如何通过REST API创建一个Portlet显示我的空间列表。
这里面我主要使用了两个apache包,httpclient和abdera。
REST API是一个http的请求,当用户访问的时候可以通过 http get的方法获得一个XML。因此这里我通过httpclient获得这个XML,然后采用abdera对这个XML进行解析。
//获取当前空间列表 <% List places = new PlacesHelper().getPlaces(request.getCookies()); %> <table width="100%" border="1" cellspacing="2"> <tr> <th scope="col">我的空间</th> <th scope="col">管理员</th> <th scope="col">最近更新时间</th> </tr> <%for(int i=0;i<places.size();i++){ %> <tr> <td><a href=<%=((Entry)places.get(i)).getAlternateLink().getResolvedHref() %>><%=((Entry)places.get(i)).getTitle() %></a></td> <td><%=((Entry)places.get(i)).getAuthor().getName() %></td> <td><%=DateFormat.getDateTimeInstance().format(((Entry)places.get(i)).getUpdated()) %></td> </tr> <%} %> </table>
Helper类,用来访问REST 内容
public class PlacesHelper { public List<Entry> getPlaces(Cookie[] cookies) { //通过httpclient get方法访问REST api HttpClient httpClient = new HttpClient(); httpClient.getParams().setContentCharset("UTF-8"); HttpMethod httpMethod = new GetMethod("http://ibmquickr.ibm.com/myqcs/rest/places/feed"); //初始化http请求,使httpclient使用当前request的cookie。 HttpState initState = new HttpState(); for(int i=0;i<cookies.length;i++) { org.apache.commons.httpclient.Cookie cookie = new org.apache.commons.httpclient.Cookie(); cookie.setDomain(cookies[i].getDomain()); cookie.setPath(cookies[i].getPath()); cookie.setName(cookies[i].getName()); cookie.setValue(cookies[i].getValue()); initState.addCookie(cookie); } httpClient.setState(initState); try { int statusCode = httpClient.executeMethod(httpMethod); if(HttpStatus.SC_OK == statusCode) { InputStream in = httpMethod.getResponseBodyAsStream(); //解析返回的XML Parser parser = Abdera.getNewParser(); Document<Feed> doc = parser.parse(in); Feed feed = (Feed) doc.getRoot(); httpMethod.releaseConnection(); //返回空间列表 return feed.getEntries(); } } catch (HttpException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } httpMethod.releaseConnection(); return null; } }
Portlet主类
public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException { response.setContentType(request.getResponseContentType()); PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher("/jsps/index.jsp"); rd.include(request,response); }
需要注意的是,从apache下载下来的abdera包包含httpclient以及servlet-api-2.5-6.1.5.jar包,servlet包与Websphere Portal自带的包有重复,因此无需将servlet包,拷贝到lib目录下。
验证平台:
WebSphere Portal 615
Lotus Quickr for Domino 8.2
httpclient 3.1
abdera 0.4.0
相关推荐
futurechallenger 2020-07-10
liuqipao 2020-07-07
ahnjwj 2020-07-28
iflreey 2020-07-04
tuxlcsdn 2020-06-21
84560296 2020-06-09
Burgesszheng 2020-06-07
TimeMagician 2020-05-27
bapinggaitianli 2020-04-07
蜡笔小鑫爱看雪 2020-03-27
Richardxx 2020-03-07
84224552 2020-03-06
时光如瑾雨微凉 2020-03-04
zcl 2020-03-04
tigercn 2020-02-23
明瞳 2020-02-22
tuxlcsdn 2020-02-21
明瞳 2020-02-17