CHH
CHH

Reputation: 41

How to create / update a connections blog post using the IBM SBT SDK java API

At the moment, I am trying to create and update blog posts using the java sbt sdk api.

I could read blogs and blog posts and was able to retrieve a single blog post, using the .getBlogPost(bkogHandle, postId) method of the SDK.

But if I try to update or create a blog post, either I run into a NullPointerException or nothing happens.

For testing purposes I created a demo blog and tried to update the blog post inside it:

String blogHandle = "citb";
String postId = "7973d29e-26b6-4d23-ab04-ebfb734bf512";
BlogPost post = sbcs.getBlogService().getBlogPost(blogHandle, postId);

if (null != post){
    System.out.println("Found post "+post.getTitle()+" ("+post.getAlternateUrl()+")");
    Map<String, Object> fieldMap = post.getFieldsMap();
    System.out.println("Found "+fieldMap.keySet().size()+" Entries in the field Map");

    try{    
    //Save the post
        System.out.println("Update blog post");
        post.setContent("<p dir='ltr'>blabla "+new Date()+"</p>");
        System.out.println(post.getContent());
        post.setTitle(post.getTitle()+" +");
        sbcs.getBlogService().updateBlogPost(post, blogHandle);
    }catch(Exception e){
        e.printStackTrace();
    }
}

This example runs without throwing an exception but doesn't update the blog. Do I miss something?

If I use post.save(blogHandle); I get the this error messages:

com.ibm.sbt.services.client.connections.blogs.BlogServiceException: error creating blog post
at com.ibm.sbt.services.client.connections.blogs.BlogService.createBlogPost(BlogService.java:627)
at com.ibm.sbt.services.client.connections.blogs.BlogPost.save(BlogPost.java:128)
Caused by: java.lang.NullPointerException
at com.ibm.sbt.services.client.connections.blogs.feedhandler.BlogsFeedHandler.createEntity(BlogsFeedHandler.java:42)
at com.ibm.sbt.services.client.connections.blogs.BlogService.createBlogPost(BlogService.java:624)
... 2 more

I get the same errors if I use createBlogPost(post, blogHandle)to create a new post.

Any ideas what the problem is or any suggestions what I could do to save blog posts?

EDIT:

In the BlogService class the problem seems to be in the following method

public BlogPost createBlogPost(BlogPost post, String blogHandle) throws BlogServiceException {
        if (null == post){
            throw new BlogServiceException(null,"null post");
        }
        Response result = null;
        try {
            BaseBlogTransformer transformer = new BaseBlogTransformer(post);
            Object  payload = transformer.transform(post.getFieldsMap());

            Map<String, String> headers = new HashMap<String, String>();
            headers.put("Content-Type", "application/atom+xml");
            String createPostUrl = resolveUrl(blogHandle, FilterType.CREATE_BLOG_POST, null);
            **result = createData(createPostUrl, null, headers, payload);**
            **post = (BlogPost) new BlogPostsFeedHandler(this).createEntity(result);**

        } catch (Exception e) {
            throw new BlogServiceException(e, "error creating blog post");
        }
        return post;
    }

Upvotes: 1

Views: 570

Answers (2)

Manish Kataria
Manish Kataria

Reputation: 78

You can fetch the exact cause for failure from service specific exception class, in this case BlogServiceException. BlogServiceException encapsulates all exceptions which might have been caused while communicating with Connections including Authentication and Authorization ones.

Another alternative for debugging and seeing all network calls would be redirect all traffic to debugging utility like fiddler, details for setting up SBT with fiddler can be fetched from this link SBT Wiki

Upvotes: 0

Swati Singh
Swati Singh

Reputation: 1

I couldn't reproduce this issue but I fixed a bug on updateBlogPost just few days back , I don't think it is available in the mentioned build. So we'll have to check when the next build is due and we can confirm if the issue still comes.

for the save() in BlogPost -I'll make changes in it so as to return the updated BlogPost

Upvotes: 0

Related Questions