如何从Java Team Server检索所有项目区域?

时间:2011-10-11 13:42:26

标签: java authentication rest jazz

我安装了本地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文档。但是,我看不出我的代码与我在插件中所做的事情之间存在任何差异。

2 个答案:

答案 0 :(得分:0)

您可以找到答案here

以下是我的理解:基本上,您需要访问另一个页面以接收Cookie然后运行您的查询。只需执行两次相同的执行即可看到我的意思。

答案 1 :(得分:0)

您可以使用DraftTeamProcessRestAPIs

获取特定用户的所有项目区域

,特别是看到这个section

此API以XML格式回复

还有ReportableRestAPIs可以在代码中使用此URL实现您想要的内容:

 https://<server>:<port>/ccm/rpt/repository/foundation?fields=foundation/projectArea/(name|teamMembers/userId)