2

I have written a Java program (using main method only) for sending HTML content using JSON. i have some data in my object in separate program which i want to import programmatically in my page of Atlassian Confluence. Still i am able to insert only static html content and data inside body of my page. i never worked before with json. how to insert data in row of table.

public class Example {

 private static final String BASE_URL = "www.example.com";
 private static final String USERNAME = "xxxxx";
 private static final String PASSWORD = "xxxxx";
 private static final String ENCODING = "utf-8";
 
 static HttpResponse putPageResponse;
 static HttpEntity putPageEntity = null;

 private static String getContentRestUrl(final Long contentId,
   final String[] expansions) throws UnsupportedEncodingException {
   final String expand = URLEncoder.encode(StringUtils.join(expansions, ","), ENCODING);
  return String
    .format("%s/rest/api/content/%s?expand=%s&os_authType=basic&os_username=%s&os_password=%s",
      BASE_URL, contentId, expand,
      URLEncoder.encode(USERNAME, ENCODING),
      URLEncoder.encode(PASSWORD, ENCODING));
 }

 public static void main(final String[] args) throws Exception {
  final long pageId = 00000000;
  HttpClient client = new DefaultHttpClient();
  // Get current page version
  String pageObj = null;
  HttpEntity pageEntity = null;
  try {
   HttpGet getPageRequest = new HttpGet(getContentRestUrl(pageId,
     new String[] { "body.storage", "version", "ancestors" }));
   
   HttpResponse getPageResponse = client.execute(getPageRequest);
   
   pageEntity = getPageResponse.getEntity();
   pageObj = IOUtils.toString(pageEntity.getContent());
  
  } finally {
   if (pageEntity != null) {
    EntityUtils.consume(pageEntity);
   }
  }

  // Parse response into JSON
  JSONObject page = new JSONObject(pageObj);
  try {
 
  **page.getJSONObject("body").getJSONObject("storage")
  .put("value","<b>html content here<b>");**
  <!--if i try to write any thing in second param of put() than it replace the all content of body with that.-->
  
  int currentVersion = page.getJSONObject("version").getInt("number");
  page.getJSONObject("version").put("number", currentVersion + 1);

  // Send update request
  
   HttpPut putPageRequest = new HttpPut(getContentRestUrl(pageId,
     new String[] {}));

   StringEntity entity = new StringEntity(page.toString(),
     ContentType.APPLICATION_JSON);
   putPageRequest.setEntity(entity);

   putPageResponse = client.execute(putPageRequest);
   System.out.println("");
   EntityUtils.consume(putPageEntity);
  
   
  } catch(Exception e) {
   System.out.println("exception is: "+e);
  }
 }
}

** if any one share any sample code for that that will very helpfull for me. i have following table in my page.

__________________________________________|

| S.no | book title | book author | publish date | price
|_____|________|___________|__________|______|

Mayank
  • 51
  • 2
  • 7

1 Answers1

2

You can do this using the Confluence REST API. From Confluence 5.5 onwards, there is a new REST API. It's fully documented here.

In particular, consider this endpoint:

POST: /rest/api/content

Content-Type: application/json

BODY:

{
   "type":"page",
   "title":"My test page",
   "space":{
      "key":"DEMO"
   },
   "body":{
      "storage":{
         "value":"<p>This is a new page</p>",
         "representation":"storage"
      }
   }
}

Here's an example that will add a page at the top level of a space:

curl -v -u admin:admin -X POST -H 'Content-Type: application/json' -H 'Accept: application/json' -d'{"type":"page","title":"new page","space":{"key":"DEMO"},"body":{"storage":{"value":"<p>This is a new page</p>","representation":"storage"}}}' "http://localhost:1990/confluence/rest/api/content/?os_authType=basic"

If you want to add a page as a child of a specific page, you'll need to know the pageId of the parent:

curl -v -u admin:admin -X POST -H 'Content-Type: application/json' -H 'Accept: application/json' -d'{"type":"page","ancestors":[{"type":"page","id":1048582}],"title":"third new child page","space":{"key":"DEMO"},"body":{"storage":{"value":"<p>This is a new page</p>","representation":"storage"}}}' "http://localhost:1990/confluence/rest/api/content/?os_authType=basic"

See Scott Dudley's answer to a previous question for more details.

Also, I recommend using the Confluence REST API Browser for testing your calls.


Update:

Here's some sample code - adapted from here:

  1. Get the Apache HttpClient, this would enable you to make the required request
  2. Create an HttpPost request with it and add the header "application/x-www-form-urlencoded"
  3. Create a StringEntity that you will pass JSON to it
  4. Execute the call

The code roughly looks like (you will still need to debug it and make it work)

HttpClient httpClient = new DefaultHttpClient();

try {
    HttpPost request = new HttpPost("http://localhost:1990/confluence/rest/api/content/?os_username=admin&os_password=admin");
    StringEntity params = new StringEntity("{"type":"page","ancestors":[{"type":"page","id":1048582}],"title":"third new child page","space":{"key":"DEMO"},"body":{"storage":{"value":"<p>This is a new page</p>","representation":"storage"}}}"); // properly escape this
    request.addHeader("content-type", "application/json");
    request.setEntity(params);
    HttpResponse response = httpClient.execute(request);

    // handle response here...
}catch (Exception ex) {
    // handle exception here
} finally {
    httpClient.getConnectionManager().shutdown();
}

If you've created a POJO with the correct structure for the JSON, you could use the GSON libray (or something else) to convert your Java object into the JSON rather than manually building the JSON from a string.

Community
  • 1
  • 1
dvdsmpsn
  • 2,839
  • 1
  • 26
  • 40
  • Thank for comment.i go through your link and now i have some more clearity on this.but in programming point of view this is not helpfull for me.now i am able to insert data in page but unable to insert data into table.my problem is now little bit change.so i am updating my question.so please see it and give some answer like above answer. – Mayank Nov 07 '14 at 07:44
  • Sorry to be commenting on an old question, but I found this page when searching for how to update a table in Confluence. The trick is to either enable the html macro, and insert your table that way (insecure) *or* to change the representation format to 'wiki' (from 'storage' in the answer above) and markup your table using one of the supported wiki markups for tables listed here: https://confluence.atlassian.com/doc/confluence-wiki-markup-251003035.html – unickshaxor Nov 12 '15 at 17:30