2015-11-09 51 views
0

我使用下面的代碼在合流4.3創建一個頁面:如何更新合流現有頁面?

public void publish() throws IOException { 

    DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); 
    Date today = Calendar.getInstance().getTime(); 
    XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(CONFLUENCE_URI); 
    try { 
     rpc.login(USER_NAME, PASSWORD); 
     Page page = new Page();      
     page.setSpace(owrConf.getString(ConfigKeys.CONFLUENCE_SPACE)); 
     page.setTitle(owrConf.getString(ConfigKeys.CONFLUENCE_PAGE_TITLE) + "_" + df.format(today));  
     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()); 
     page.setParentId(owrConf.getString(ConfigKeys.CONFLUENCE_PAGE_ID));    
     rpc.storePage(page); 
     } catch (XmlRpcException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 



} 

這工作得很好,但我只是想知道如果我可以更新現有的頁面,而不是總是創建一個新的。我無法找到Confluence 4.3的API信息來執行此操作。

回答

1

看看這篇文章,用Python和XMLRPC更新頁面。

How can I create a new page to confluence with Python

您也可以看看這個頁面 https://answers.atlassian.com/questions/316106/how-to-update-a-page-in-confluence-by-java

但實際上在2015年,你應該使用匯流REST API做這種東西。做一個簡單的帖子這個網址與一些JSON數據和你的網頁被更新

https://docs.atlassian.com/atlassian-confluence/REST/latest/#d3e941

更新:

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

+0

REST是不是很遺憾的選項我們使用的是Confluence 4.3,儘管更新它的計劃正在進行中,但可能至少需要6個月。 – eeijlar

+1

哦,這很糟糕。好的,爲你更新我的答案。我沒有嘗試過這個,但發現它與良好的谷歌;) –

+0

謝謝 - 這真的很有幫助。 – eeijlar