如何从 Java Team Server 检索所有项目区域?
我安装了本地 Java Team Server,版本 3.0.1。我正在尝试使用 REST Web 服务来检索所有项目区域。为此,我首先验证了自己的身份:
public HttpContext login() throws ClientProtocolException, IOException {
client = new DefaultHttpClient();
CookieStore cookieStore = new BasicCookieStore();
HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpGet httpGetID = new HttpGet("https://localhost:9443/ccm/authenticated/identity");
client.execute(httpGetID, localContext);
httpGetID.abort();
List<Cookie> cookies1 = cookieStore.getCookies();
for (Cookie cookie : cookies1) {
System.out.println("\t"+cookie.getName()+" : "+cookie.getValue());
}
List<NameValuePair> authFormParams = new ArrayList<NameValuePair>();
authFormParams.add(new BasicNameValuePair("j_username", "ADMIN"));
authFormParams.add(new BasicNameValuePair("j_password", "ADMIN"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(authFormParams, "UTF-8");
HttpPost httpPostAuth = new HttpPost("https://localhost:9443/ccm/authenticated/j_security_check");
httpPostAuth.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
httpPostAuth.setEntity(entity);
client.execute(httpPostAuth, localContext);
//httpPostAuth.abort();
List<Cookie> cookies2 = cookieStore.getCookies();
for (Cookie cookie : cookies2) {
System.out.println("\t"+cookie.getName()+" : "+cookie.getValue());
}
return localContext;
}
然后我尝试使用以下代码获取项目区域:
HttpGet getProjectsRequest = new HttpGet("https://localhost:9443/ccm/oslc-scm/catalog");
getProjectsRequest.addHeader("Content-Type", "application/xml;charset=UTF-8");
getProjectsRequest.addHeader("Accept-Charset", "UTF-8");
getProjectsRequest.addHeader("Accept", "application/x-oslc-cm-change-request+xml");
ResponseHandler<String> handler = new BasicResponseHandler();
String projectResponse = client.execute(getProjectsRequest, handler, localContext);
System.out.println(projectResponse);
不幸的是,答案总是相同的:
{
"userId": "ADMIN",
"roles": [
"JazzUsers",
"JazzProjectAdmins",
"JazzAdmins"]
}
这对我来说就像一个 JSON 对象。相反,我应该获取一个列出所有项目的 XML 文档。我已经使用 Firefox 的 REST 插件尝试了相同的 REST 服务。在那里我得到了预期的 XML 文档。但是,我看不出我的代码和我在插件中所做的事情之间有任何差异。
I installed a local Java Team Server, version 3.0.1. I'm trying to use the REST web services to retrieve all the project areas. For that I first authenticated myself:
public HttpContext login() throws ClientProtocolException, IOException {
client = new DefaultHttpClient();
CookieStore cookieStore = new BasicCookieStore();
HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpGet httpGetID = new HttpGet("https://localhost:9443/ccm/authenticated/identity");
client.execute(httpGetID, localContext);
httpGetID.abort();
List<Cookie> cookies1 = cookieStore.getCookies();
for (Cookie cookie : cookies1) {
System.out.println("\t"+cookie.getName()+" : "+cookie.getValue());
}
List<NameValuePair> authFormParams = new ArrayList<NameValuePair>();
authFormParams.add(new BasicNameValuePair("j_username", "ADMIN"));
authFormParams.add(new BasicNameValuePair("j_password", "ADMIN"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(authFormParams, "UTF-8");
HttpPost httpPostAuth = new HttpPost("https://localhost:9443/ccm/authenticated/j_security_check");
httpPostAuth.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
httpPostAuth.setEntity(entity);
client.execute(httpPostAuth, localContext);
//httpPostAuth.abort();
List<Cookie> cookies2 = cookieStore.getCookies();
for (Cookie cookie : cookies2) {
System.out.println("\t"+cookie.getName()+" : "+cookie.getValue());
}
return localContext;
}
Then I try to get the project areas with the following code:
HttpGet getProjectsRequest = new HttpGet("https://localhost:9443/ccm/oslc-scm/catalog");
getProjectsRequest.addHeader("Content-Type", "application/xml;charset=UTF-8");
getProjectsRequest.addHeader("Accept-Charset", "UTF-8");
getProjectsRequest.addHeader("Accept", "application/x-oslc-cm-change-request+xml");
ResponseHandler<String> handler = new BasicResponseHandler();
String projectResponse = client.execute(getProjectsRequest, handler, localContext);
System.out.println(projectResponse);
Unfortunately, the answer is always the same:
{
"userId": "ADMIN",
"roles": [
"JazzUsers",
"JazzProjectAdmins",
"JazzAdmins"]
}
This looks for me like a JSON object. Instead I should get a XML document, that lists all projects. I already tried the same REST service with a REST plugin for Firefox. There I get the XML document as expected. However, I can't see any differences between my code and the things I do in the plugin.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在此处找到答案。
我的理解是这样的:基本上,您需要访问另一个页面来接收 cookie,然后运行您的查询。只需运行相同的执行两次,您就会明白我的意思。
You can find the answer here.
Here is what I understood: basically, you need to visit another page to receive a cookie and then run your query. Just run the same execute twice and you see what I mean.
您可以使用 DraftTeamProcessRestAPIs 获取特定用户的所有项目区域
,特别是请参阅此部分
此 API 以 XML 形式响应
,并且还有 ReportableRestAPIs 可以在代码中使用此 URL 来满足您的需求:
You can get All Project Areas of a particular user using DraftTeamProcessRestAPIs
and in particular see this section
This API responses in XML
and also there are ReportableRestAPIs which can fulfill what you want using this URL in the code: