如何通过 XWiki XmlRpcClient 调用 confluence 2?

How to make confluence2 calls via XWikiXmlRpcClient?

使用此代码的一个版本。 (代码被修改为使用与我的 confluence 设置相关的页面,除此之外它与此相同):

import java.net.MalformedURLException;
import org.apache.xmlrpc.XmlRpcException;
import org.codehaus.swizzle.confluence.Page;
import org.xwiki.xmlrpc.XWikiXmlRpcClient;


public class UpdatePage {

   public static void main(String[] args) throws MalformedURLException, XmlRpcException {

   //URL of the xwiki instance
   String url = "http://localhost:8080/xwiki/xmlrpc/confluence";

   //Replace user & pass with desired xwiki username & password
   String user = "Admin";
    String pass = "admin";


    XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
   try {

       //Perform Login & Authentication
       rpc.login(user, pass);

       //Create a Page object to hold our Document information
       Page page = new Page();
       //Fetch the required page. In our example, the page is in Space "demo code"
       //and the Page is "Update Page"
       page=rpc.getPage("demo code.Update Page");
       //Fetch the content of the page & store it in a string for temporary storage
       //This is the present content of the Page
       String presentContent=page.getContent();
       //Create a string that will hold the new content that is to be added to the Page
       String newContent="\\Some new content added";
       //Set the content of the page as: present content + new content
       //However, this page is not yet stored to XWiki. It only resides in your application
       page.setContent(presentContent+newContent);
       //Finally, store the "updated" Page to XWiki
       rpc.storePage(page);

       //Just to make sure everything saved all right, fetch the content again for the Page
       System.out.println(page.getContent());

   } catch (XmlRpcException e) {
        System.out.println("invalid username/password was specified or communication problem or ");
        System.out.println(e);
   } finally {
        rpc.logout();
   }
   }
}

取自http://extensions.xwiki.org/xwiki/bin/view/Extension/XML-RPC+Integration+Java+Examples

当它尝试从 Confluence 检索该页面时:

page=rpc.getPage("demo code.Update Page");

我在使用上面的代码时遇到这个错误:

   org.apache.xmlrpc.XmlRpcException: java.lang.Exception:   com.atlassian.confluence.rpc.RemoteException: You must supply a valid number as the page ID.

然后,如果我从页面获取页面 ID,并使用它:

page = rpc.getPage("39201714");

我得到这个异常:

org.apache.xmlrpc.XmlRpcException: java.lang.Exception:   com.atlassian.confluence.rpc.RemoteException: Unsupported operation: Wiki formatted content can no longer be retrieved from this API. Please use the version 2 API. The version 2 WSDL is available at: http://confluence:8080/rpc/soap-axis/confluenceservice-v2?wsdl. XML-RPC requests should prefixed with "confluence2.".

我是否将 confluence URL 更改为访问 confluence2 api?不确定如何更改 XWikiXmlRpcClient 使用的内容..

页面的原始版本似乎是 wiki 格式。可以删除为空吗?

我认为错误是因为您试图获取格式不受支持的页面。

还要确保您拥有适用于 confluence2 的正确 wsdl(因为您之前使用的是版本 1)

如果你查看 XWikiXmlRpcClient 的源代码,构造函数显示它正在使用 confluence1:

    /**
 * Constructor.
 * 
 * @param endpoint The endpoint for the XMLRPC servlet.
 * @throws MalformedURLException
 */
public XWikiXmlRpcClient(String endpoint) throws MalformedURLException
{
    this(endpoint, "confluence1");
}

这使用 'confluence1' 作为 RPC 处理程序调用内部构造函数。

虽然 class 有两个构造函数,因此应该可以直接从 class 外部调用相同的东西:

 XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(CONFLUENCE_URI, "confluence2");

稍后当它调用 rpc 时,confluence2 (this.rpcHandler) 被添加到字符串中:

    private synchronized Object invokeRpc(String methodName, Object... args) throws XmlRpcException
{
    return this.xmlRpcClient.execute(String.format("%s.%s", this.rpcHandler, methodName), args);
}

您需要同时使用对象的 confluence1 和 confluence2 版本来更新页面:

 XWikiXmlRpcClient rpcConfluence1 = new    XWikiXmlRpcClient(CONFLUENCE_URI);
    XWikiXmlRpcClient rpcConfluence2 = new XWikiXmlRpcClient(CONFLUENCE_URI, "confluence2");
    try {
        rpcConfluence1.login(USER_NAME, PASSWORD);
        rpcConfluence2.login(USER_NAME, PASSWORD);
        Page page = new Page();     
        page = rpcConfluence2.getPage(PAGEID);

        List<String> lines = Files.readAllLines(Paths.get("summary.markup"), Charset.defaultCharset());
        StringBuilder b = new StringBuilder();
        for(int i=0; i < lines.size(); i++) {
                b.append(String.format("%s%s", lines.get(i), "\r\n"));
        }              
        page.setContent(b.toString());

        rpcConfluence1.storePage(page);
        } catch (XmlRpcException e) {
            e.printStackTrace();
        }



}