2014-09-24 33 views
2

在IBM Connections用戶界面中,可以將文件直接附加到wiki頁面。
我想通過編程的方式將文件附加到wiki頁面,但找不到有文檔說明的方式。IBM Connections API - 如何將文件附加到wiki頁面?

我一直在尋找的連接4.5 API文檔在這裏:
http://www-10.lotus.com/ldd/appdevwiki.nsf/xpDocViewer.xsp?lookupName=IBM+Connections+4.5+API+Documentation#action=openDocument&content=catcontent&ct=prodDoc

在API的文件和文件維基專門找,似乎有什麼關於維基頁的附件。儘管我主要是想上傳附件,但我甚至無法看到一個記錄在案的API來檢索附件,儘管用戶界面有一個鏈接到一個提要(在每個wiki頁面上)。

是否有任何(可能未公開)API將文件附加到wiki頁面?

+0

我正在查看API。當我完成後,我會發布回覆 – 2014-09-25 13:57:37

回答

2

下面是一些示例代碼來顯示如何做到這一點在一個支撐辦法。

首先,您需要兩個URL。

private static String apiUrlWikiNonce = "https://sdkdev.swg.usma.ibm.com:444/wikis/basic/api/nonce"; 
private static String apiUrlWikiAttachment = "https://sdkdev.swg.usma.ibm.com:444/wikis/form/api/wiki/{WIKI_LABEL_OR_WIKI_UUID}/page/{PAGE_LABEL_OR_PAGE_UUID}/feed"; 

獲取隨機值

/** 
    * gets the nonce value only valid return type is text, anything else throws 
    * an error 
    * 
    * @param httpClient 
    * @param user 
    * @param password 
    * @return 
    */ 
    public static String getNonce(CloseableHttpClient httpClient, String user, 
      String password) { 
     String result = ""; 
     HttpGet httpGet = new HttpGet(apiUrlWikiNonce); 

     String combo = user + ":" + password; 
     String hash = org.apache.commons.codec.binary.Base64 
       .encodeBase64String(combo.getBytes()); 
     System.out.println(user + " is logging in with " + hash); 
     String auth = "Basic " + hash; 
     httpGet.setHeader("Authorization", auth.replace("=", "")); 

     HttpClientContext context = HttpClientContext.create(); 

     CloseableHttpResponse response = null; 
     try { 

      response = httpClient.execute(httpGet, context); 

      System.out.println(response.getStatusLine()); 
      HttpEntity entity = response.getEntity(); 

      InputStream is = entity.getContent(); 
      StringWriter writer = new StringWriter(); 

      IOUtils.copy(is, writer); 
      String responseString = writer.toString(); 
      // System.out.println(responseString); 

      result = responseString; 

      EntityUtils.consume(entity); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       response.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

     return result; 
    } 

現在,你有隨機數,如... b1911872-36f1-4767-956d-214759886406

然後上傳與X-更新 - 文件Nonce

/** 
    * uploads a file to a given wiki page. 
    * 
    * @param httpClient 
    * @param user 
    * @param password 
    * @param wikiLabel 
    * @param wikiPage 
    * @param file 
    * @param nonce 
    * @return uuid <empty or uuid of file> 
    */ 
    public static String uploadFileToWiki(CloseableHttpClient httpClient, String user, 
      String password, String wikiLabel, String wikiPage, File file, String nonce){ 
     String uuid = ""; 

     String apiUrl = apiUrlWikiAttachment.replace("{WIKI_LABEL_OR_WIKI_UUID}", wikiLabel); 
     apiUrl = apiUrl.replace("{PAGE_LABEL_OR_PAGE_UUID}", wikiPage); 

     //Mandatory Parameter 
     apiUrl += "?category=attachment"; 


     System.out.println("API Url : " + apiUrl); 

     HttpPost post = new HttpPost(apiUrl); 
     post.addHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:32.0) Gecko/20100101 Firefox/32.0"); 
     post.addHeader("Content-Type", "image/png"); 

     String combo = user + ":" + password; 
     String hash = org.apache.commons.codec.binary.Base64 
       .encodeBase64String(combo.getBytes()); 
     System.out.println(user + " is logging in with " + hash); 
     String auth = "Basic " + hash; 
     post.setHeader("Authorization", auth.replace("=", "")); 

     HttpClientContext context = HttpClientContext.create(); 

     CloseableHttpResponse response = null; 
     try { 
      EntityBuilder builder = EntityBuilder.create(); 
      builder.setFile(file); 
      HttpEntity postentity = builder.build(); 

      post.setHeader("Slug","NominalWorkItem-v1.png"); 
      post.setHeader("title","Test Title"); 
      post.setHeader("label","Test Test"); 

      post.setEntity(postentity); 

      response = httpClient.execute(post, context); 

      System.out.println(response.getStatusLine()); 
      HttpEntity entity = response.getEntity(); 

      InputStream is = entity.getContent(); 
      StringWriter writer = new StringWriter(); 

      IOUtils.copy(is, writer); 
      String responseString = writer.toString(); 
      System.out.println(responseString); 

      uuid = responseString; 

      EntityUtils.consume(entity); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       response.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 


     return uuid; 
    } 

文件已更新,創建時會得到響應代碼201,XML

> <?xml version="1.0" encoding="UTF-8"?><entry 
> xmlns:thr="http://purl.org/syndication/thread/1.0" 
> xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/" 
> xmlns:snx="http://www.ibm.com/xmlns/prod/sn" xmlns:td="urn:ibm.com/td" 
> xmlns="http://www.w3.org/2005/Atom"><id>urn:lsid:ibm.com:td:7780e11c-7240-41b3-8f0c-00a10ea69c93</id><td:uuid>7780e11c-7240-41b3-8f0c-00a10ea69c93</td:uuid><td:label>NominalWorkItem-v1.png</td:label><link 
> href="https://sdkdev.swg.usma.ibm.com:444/wikis/form/api/wiki/744ccaeb-e9af-434c-9fa6-78831fb94846/page/3b017473-2dbe-4920-85a2-bf908a8e1475/attachment/7780e11c-7240-41b3-8f0c-00a10ea69c93/entry" 
> rel="self"></link><link 
> href="https://sdkdev.swg.usma.ibm.com:444/wikis/form/anonymous/api/wiki/744ccaeb-e9af-434c-9fa6-78831fb94846/page/3b017473-2dbe-4920-85a2-bf908a8e1475/attachment/7780e11c-7240-41b3-8f0c-00a10ea69c93/media/NominalWorkItem-v1.png" 
> rel="alternate"></link><link 
> href="https://sdkdev.swg.usma.ibm.com:444/wikis/form/api/wiki/744ccaeb-e9af-434c-9fa6-78831fb94846/page/3b017473-2dbe-4920-85a2-bf908a8e1475/attachment/7780e11c-7240-41b3-8f0c-00a10ea69c93/entry" 
> rel="edit"></link><link 
> href="https://sdkdev.swg.usma.ibm.com:444/wikis/form/api/wiki/744ccaeb-e9af-434c-9fa6-78831fb94846/page/3b017473-2dbe-4920-85a2-bf908a8e1475/attachment/7780e11c-7240-41b3-8f0c-00a10ea69c93/media" 
> rel="edit-media"></link><link 
> href="https://sdkdev.swg.usma.ibm.com:444/wikis/form/anonymous/api/wiki/744ccaeb-e9af-434c-9fa6-78831fb94846/page/3b017473-2dbe-4920-85a2-bf908a8e1475/attachment/7780e11c-7240-41b3-8f0c-00a10ea69c93/media/NominalWorkItem-v1.png" 
> rel="enclosure" type="image/png" title="NominalWorkItem-v1.png" 
> length="107763"></link><category term="attachment" 
> scheme="tag:ibm.com,2006:td/type" 
> label="attachment"></category><summary 
> type="text"></summary><td:documentUuid>3b017473-2dbe-4920-85a2-bf908a8e1475</td:documentUuid><td:libraryId>744ccaeb-e9af-434c-9fa6-78831fb94846</td:libraryId><author><name>Frank 
> Adams</name><snx:userid>6B54D23A-0A70-C7A4-8525-7CA50082A393</snx:userid><email>[email protected]</email><snx:userState>active</snx:userState></author><td:modifier><name>Frank 
> Adams</name><snx:userid>6B54D23A-0A70-C7A4-8525-7CA50082A393</snx:userid><email>[email protected]</email><snx:userState>active</snx:userState></td:modifier><title 
> type="text">NominalWorkItem-v1.png</title><published>2014-09-29T20:29:16.210Z</published><updated>2014-09-29T20:29:16.210Z</updated><td:created>2014-09-29T20:29:16.210Z</td:created><td:modified>2014-09-29T20:29:16.210Z</td:modified><td:lastAccessed></td:lastAccessed><content 
> type="image/png" 
> src="https://sdkdev.swg.usma.ibm.com:444/wikis/form/api/wiki/744ccaeb-e9af-434c-9fa6-78831fb94846/page/3b017473-2dbe-4920-85a2-bf908a8e1475/attachment/7780e11c-7240-41b3-8f0c-00a10ea69c93/media"></content></entry> 
+1

工程,但我不得不做一個小的改變認證。如果你有'httpGet.setHeader(「Authorization」,auth.replace(「=」,「」));',我將它改爲'httpGet.setHeader(「Authorization」,auth);'。我一直得到HTTP 401響應,直到我做出改變。 – 2014-09-30 05:08:51

1

我認爲這是可能的多部分後要做到這一點,但在我自己的代碼,我做的步驟:

  1. POST的XML-Atom條目:

地址:

https://<Server>/<Wikis-Context-Root>/basic/api/wiki/<wikiUuid>/page/<pageUuid>/feed 

內容:

<entry xmlns="http://www.w3.org/2005/Atom" xmlns:td="urn:ibm.com/td"> 
    <category label="attachment" scheme="tag:ibm.com,2006:td/type" term="attachment"/> 
    <td:label>file_name.extension</td:label> 
</entry> 

接頭:

Content-Type: <file mime-type, e.g. IMAGE/JPG> 
Slug: <file_name.extension> 

響應:201;位置:URL到附件條目

  • PUT附件內容
  • 地址:先前響應的位置響應報頭,只交換最後一個「/條目「爲 」/媒體「

    內容:文件的內容流

    頭:

    Content-Type: <Mime-Type> 
    

    響應:200或201

  • 可選:PUT描述爲文件
  • 地址:位置響應標頭來自1的請求。)

    內容:

    <entry xmlns="http://www.w3.org/2005/Atom" xmlns:td="urn:ibm.com/td"> 
        <category label="attachment" scheme="tag:ibm.com,2006:td/type" term="attachment"/> 
        <summary type="text">MY ATTACHMENT DESCRIPTION TEXT</summary> 
        <td:label>file_name.extension</td:label> 
    </entry> 
    

    接頭:

    Content-Type: application/atom+xml 
    

    響應:200或201

    +0

    感謝您的回覆。看起來你的技術應該可以工作,但我沒有嘗試過,因爲Paul的答案中的代碼更容易實現和測試,並且由於它來自IBM而具有支持優勢。 – 2014-09-30 05:20:44

    相關問題