首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 企业软件 > 行业软件 >

通过Lotus Quickr REST API,开发一个小弟我的空间Portlet

2012-06-29 
通过Lotus Quickr REST API,开发一个我的空间PortletLotus Quickr提供REST API访问其中的数据,这里我通过

通过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 apiHttpClient 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();? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//解析返回的XMLParser 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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.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

?

?

热点排行