1

How to post status update in to ibm connections using abdera?

I am trying to post status in to ibm connections using fallowing code.

public class PostStatusUpdate {

public void postStatus(String username, String password, String feedURL) throws MalformedURLException, URISyntaxException {
    try{
    Abdera abdera = new Abdera();
    AbderaClient client = new AbderaClient(abdera);
    AbderaClient.registerTrustManager();
    RequestOptions options = client.getDefaultRequestOptions();
    URL url = new URL(feedURL);
    String realm = url.getProtocol() + "://" + url.getHost();
    client.usePreemptiveAuthentication(true);
    try {
        client.addCredentials(realm, null, null, new UsernamePasswordCredentials(username, password));
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
    Parser parser = abdera.getParser();
    String urlText = "https://greenhouse.lotus.com/connections/opensocial/basic/rest/ublog/@me/@all";
    String message = "Hai This is Tes Content Fron Nithin!";
    Entry status = abdera.newEntry();
    status.addCategory("http://www.ibm.com/xmlns/prod/sn/type", "entry", null);
    status.addCategory("http://www.ibm.com/xmlns/prod/sn/message-type", "status", null);
    message = StringEscapeUtils.escapeHtml4(message);
    status.setContent(message);
    ClientResponse response = client.put(urlText, status);
    if (response.getType() == ResponseType.SUCCESS) {
        System.out.println("I have posted status");
    } else {
        System.out.println("I failed");
    }
    }catch(Exception e){
        System.out.println("Exception E -->"+e);
    }
}

}

Please help.

  • the microblog is only json based, and not atom based. http://www-10.lotus.com/ldd/appdevwiki.nsf/xpAPIViewer.xsp?lookupName=API+Reference#action=openDocument&res_title=IBM_Connections_Microblogging_API_ic50&content=apicontent – Paul Bastide Feb 13 '15 at 14:48
  • But how can i update message using this API?...@PaulBastide – Manwear Raj Feb 13 '15 at 15:56
  • use the REST API that's linked in the prior comment – Paul Bastide Feb 13 '15 at 16:56
  • Can you provide an example for How to use REST API.And is their any modifications required for above code that i have mentioned – Manwear Raj Feb 16 '15 at 06:52
  • Dude, there's an example right here: [IBM Connections wiki](http://www-10.lotus.com/ldd/lcwiki.nsf/xpDocViewer.xsp?lookupName=IBM+Redbooks%3A+Customizing+IBM+Connections+3.0.1#action=openDocument&res_title=9.2_Using_IBM_Connections_API_in_different_programming_languages&content=pdcontent). PS: The example uses XML, not json. The point is - as Paul Bastide said - you need to use the REST API. – FoggyDay Feb 16 '15 at 18:07

1 Answers1

2

I don't believe Abdera supports JSON.

You can take the example from cURL

 curl --request POST -u "userid:password" -H "Content-Type: application/json" -d "{\"content\": \"A new microblog POST\",}" https://<SERVER>/connections/opensocial/basic/rest/ublog/@me/@all

And use that as a model in your java code.

Or use this as a model

setup the URL String apiUrl = "https://<SERVER>/connections/opensocial/rest/ublog/@me/@all";

Build the HTTP Connection, request, and process

        url = new URL(apiUrl);
        HttpsURLConnection httpCon = (HttpsURLConnection) url
                .openConnection();
        httpCon.setDoOutput(true);


        // Create via POST
        String METHOD = "POST";
        httpCon.setRequestMethod(METHOD);

        String contentType = "application/json";
        String auth = //build Basic auth here

        httpCon.setRequestProperty("User-Agent", "curl/7.37.1");
        httpCon.setRequestProperty("Content-Type", contentType);
        httpCon.setRequestProperty("Authorization", auth);

        String eventJson = "{\"content\":\"test\"}"; //"{\"content\" : \"A new microblog\" }";

        System.out.println("" + eventJson);

        DataOutputStream out = new DataOutputStream(
                httpCon.getOutputStream());
        out.write(eventJson.getBytes());
        out.flush();
        out.close();

        InputStream is = httpCon.getInputStream();

        StringWriter writer = new StringWriter();
        IOUtils.copy(is, writer);
        String response = writer.toString();

        System.out.println(response);

        System.out.println("The Response Code is "
                + httpCon.getResponseCode());

`

Paul Bastide
  • 1,505
  • 4
  • 17
  • 22
  • I tried to execute above code but i am unable to update status.Code is working with out any errors but status is not updating. – Manwear Raj Feb 25 '15 at 07:05