Confluence API 文档
https://developer.atlassian.com/display/CONFDEV/Confluence+Developer+Documentation
Confluence Remote API 有三种方式调用
1 XML-RPC && SOAP
https://developer.atlassian.com/display/CONFDEV/Confluence+XML-RPC+and+SOAP+APIs
http://host:port/rpc/soap-axis/confluenceservice-v2?wsdl
SOAP is generally more useful from a strongly typed language (like Java or C#) but these require more setup. XML-RPC is easier to use from a scripting language (like Perl, Python, or AppleScript) and is often quicker to use.
SOAP方式访问:
有点:中文不会乱码,缺点:设置比较多
2 JSON-RPC
https://developer.atlassian.com/display/CONFDEV/Confluence+JSON-RPC+APIs
最新源码:
https://bitbucket.org/cmiller_atlassian/confluence-json-rpc-plugin/src
https://bitbucket.org/cmiller_atlassian/confluence-json-rpc-plugin/wiki/Home
插件下载(1.0.5):
https://marketplace.atlassian.com/plugins/com.atlassian.confluence.rpc.confluence-json-rpc-plugin
要求:4.1版本后的CF可以直接调用API,3.5及之后的需要安装插件plugin
URL: http://host:port/rpc/json-rpc/confluenceservice-v2
仅支持POST访问
验证方法:在post header中写入加密后的用户名密码
优点:传入JSON,返回JSON,容易解析
缺点:response后的内容没有编码,中文直接乱码,查看源代码后是response的ContentType没用执行编码,也没有使用HttpPost中指定的编码格式,此为CF BUG。
中文乱码服务端解决办法:
1在web.xml加入如下代码然后重启服务
<filter-mapping> <filter-name>encoding</filter-name> <url-pattern>/rpc/*</url-pattern> </filter-mapping>
http://blog.bsdn.org/2012/07/19/resolve-confluence-json-rpc-charset-encoding/
2 替换服务器上的atlassian-voorhees.jar 包,使用1.0.7版本
注意:JSON-RPC 格式不支持token的访问,须采用用户名密码方式的验证,当只传入一个对象时,可以传入JSON对象,当传入多个参数或者一个基本类型参数时,必须传入JSON数组,且参数位置必须按照接口指定位置传入
package com.xxx.cf.util; import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang.StringUtils; import org.apache.http.Header; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicHeader; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; import com.xxxx.commons.json.JSONUtils; import com.xxx.exception.XxxRuntimeException; public class ConfluenceUtils { private static final Logger LOG = LoggerFactory.getLogger(ConfluenceUtils.class); public static final String JSON_RPC_ENDPOINT = "/rpc/json-rpc/confluenceservice-v2"; public static final String UTF8 = "UTF-8"; /************************** Remote Confluence Methods ************************/ public static final String ADD_SPACE = "/addSpace"; public static final String GET_SPACE = "/getSpace"; public static final String ADD_PAGE = "/storePage"; public static final String GET_PAGE = "/getPage"; public static final String GET_PAGES = "/getPages"; public static final String GET_PAGE_HISTORY = "/getPageHistory"; public static final String GET_ATTACHMENTS = "/getAttachments"; public static final String GET_CHILDREN = "/getChildren"; public static final String GET_DESCENDENTS = "/getDescendents"; public static final String SET_PAGE_PERMISSIONS = "/setContentPermissions"; public static final String ADD_SPAGE_PERMISSIONS = "/addPermissionsToSpace"; /************************** End ************************/ /*********host,username,password 采用spring读取properties配置读入*******/ private static String host; private static String username; private static String password; /** * JSON-RPC 支持两种形式的参数编码:1 按照位置,参数作为一个数组里面的值进行传递 * 2按照名称,参数作为一个包含键值对的对象进行传递,由于Java不支持命名参数,只传递一个对象参数时将被转换为按照位置的调用 */ public static String getParamJson(Object obj) { String jsonString = JSONUtils.objectToJson(obj); if (obj instanceof String || obj instanceof Integer || obj instanceof Double || obj instanceof Long|| obj instanceof Byte || obj instanceof Short || obj instanceof Character || obj instanceof Float) { jsonString = "[" + jsonString + "]"; } return jsonString; } /** * @return HttpPost * @Description 返回httpPost */ public static HttpPost getHttpPost(String method) { HttpPost post = new HttpPost(host + JSON_RPC_ENDPOINT + method + "?os_authType=basic"); post.addHeader(ConfluenceUtils.getBasicAuthorizationHeader(username, password)); post.addHeader("Accept", "application/json;charset=UTF-8"); post.addHeader("Content-Type", "application/json;charset=UTF-8"); LOG.debug(post.toString()); return post; } public static StringEntity getStringEntity(String json) throws Exception { return new StringEntity(json, ConfluenceUtils.UTF8);// 解决POST过去的中文乱码 } /** * @param username * @param password * @return Header * @Description 返回包含基本认证信息的HTTP header */ public static Header getBasicAuthorizationHeader(String username, String password) { return new BasicHeader("Authorization", String.format("Basic %s", Base64.encodeBase64String( (username + ":" + password).getBytes()).trim())); } public static String postRequest(String paramJson, String method) { String resultJson = null; if (StringUtils.isBlank(paramJson) || StringUtils.isBlank(method)) { LOG.error("请求CF数据时方法、参数JSON 部分为空 ,{},{} }", method, paramJson); return null; } HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = ConfluenceUtils.getHttpPost(method); try { httpPost.setEntity(ConfluenceUtils.getStringEntity(paramJson)); HttpResponse response = httpClient.execute(httpPost); if (HttpStatus.OK.value() == response.getStatusLine().getStatusCode()) { resultJson = EntityUtils.toString(response.getEntity(), ConfluenceUtils.UTF8); LOG.debug("response json :{}", resultJson); EntityUtils.consume(response.getEntity()); } } catch (Exception e) { LOG.error("请求CF{}方法,参数{}时出现异常 ,{}", new Object[] { method, paramJson, e }); throw new PmpRuntimeException(e); } finally { httpPost.abort(); httpClient.getConnectionManager().shutdown(); } return resultJson; } public String getHost() { return host; } public void setHost(String host) { ConfluenceUtils.host = host; } public String getUsername() { return username; } public void setUsername(String username) { ConfluenceUtils.username = username; } public String getPassword() { return password; } public void setPassword(String password) { ConfluenceUtils.password = password; } }
调用方法:
public List<PageSummary> getPages(String spaceKey) { if (StringUtils.isBlank(spaceKey)) { LOG.warn("getPages 传入参数为空!"); return null; } String paramJson = ConfluenceUtils.getParamJson(spaceKey); String resultJson = ConfluenceUtils.postRequest(paramJson, ConfluenceUtils.GET_PAGES); return JSONUtils.jsonToList(resultJson, PageSummary.class); }
3 RESTFUL风格
https://developer.atlassian.com/display/CONFDEV/Confluence+REST+API
要求:CF版本必须在5.5以上,
URL:http://host:port/rest/api/space?spaceKey=12345
Confluence's REST APIs provide access to resources (data entities) via URI paths.
response:JSON 格式
优点:rest方式友好,无需http://192.168.224.50:8090/rest/api/space?spaceKey=JDTD过多配置,
缺点:rest接口并不全面,从5.5之后的每个版本迭代会增加一点rest接口,最新版本的5.6.1对一些老的插件也不支持,而且5.6.1 的编辑器在IE下面不能正常运行
附:
CF安装包下载链接https://www.atlassian.com/software/confluence/download
https://www.atlassian.com/software/confluence/download-archives
CF版本变更说明:https://confluence.atlassian.com/display/DOC/Confluence+Release+Notes
https://developer.atlassian.com/display/CONFDEV/Development+Resources
Questions: https://answers.atlassian.com/questions/topics/753687/confluence
Methods: https://developer.atlassian.com/display/CONFDEV/Remote+Confluence+Methods
Objects: https://developer.atlassian.com/display/CONFDEV/Remote+Confluence+Data+Objects