1

The Connections Java API docs and examples make assumptions the user is using a Web server to interact with Connections. In my case, I want to use the Java API using jRuby and there is no web server.

I want to upload a file to a Connections community, or more specifically connect to the Connections server directly, authenticate and do the upload. I tried the following code and hit a wall with the error:

"SBT context is not initialized for the request"

This error message seems to be complaining about a servlet which of course I don't have.

Here is the code I tried...

    ep = new BasicEndpoint();
    ep.setUrl(connections_url);
    ep.login(userid, pw);
    service = new CommunityService();
    service.setEndpoint(ep);
    istream = new FileInputStream(temp_file);
    uploaded_file = service.uploadFile(istream, community_id, filename, filesize)

The API calls used above are just a guess on my part however the login to the Endpoint seems to work as the method returns true and that gave me some hope I was on the right track. But the upload failed with the context error so now I'm stuck.

Any ideas to make this work?

2 Answers2

0

IBM SBT can work on standalone applications too. There is an example in the Github repository.

Basically, you should initialize an application and a context for that.

RuntimeFactory runtimeFactory=new RuntimeFactoryStandalone();
Context context=null;
Application application=null;

try {
    application=runtimeFactory.initApplication(null);
    context = Context.init(application, null, null);

    // Whatever you want to do...


} catch(Throwable t) {
    // Error handling
    t.printStackTrace();
} finally {
    if (context != null)
        Context.destroy(context);
    if (application != null)
         Application.destroy(application);
}
Serdar Basegmez
  • 3,355
  • 16
  • 20
  • I looked at the code examples and Context code was not used in any of the them. Does this mean this code is only needed for File Uploads? BTW, I discovered after my post, the file did successfully upload to the community. The Context error happened after the upload. – user3912390 Aug 06 '14 at 14:27
  • To my knowledge, Context and application is used within SBT classes. For instance, if you want to utilize EndPointFactory, it uses the application and context objects but calls them internally. Did you try Paul's answer? According to his example, they seem like to be not so critical. – Serdar Basegmez Aug 06 '14 at 14:32
  • The Context code is included in the File download example, didn;t see that, thanks – user3912390 Aug 06 '14 at 14:48
  • I tried the Context code and get Java::JavaLang::NoClassDefFoundError (javax/servlet/http/HttpServletResponse) when doing Context.init(). Is there another dependency Jar I need (I am coding this in jRuby)? – user3912390 Aug 06 '14 at 16:32
  • Interesting. This error should not be thrown. Which version of SDK are you using? Make sure you use RuntimeFactoryStandalone... – Serdar Basegmez Aug 06 '14 at 16:48
  • com.ibm.sbt.core-1.0.2.20140527-1807.jar which I downloaded only a few days ago. I added servlet-api.jar from Tomcat to my project and that resolved the error. – user3912390 Aug 06 '14 at 18:55
  • Adding servlet-api.jar will resolve the error but still, it's an unexpected problem. I use this pattern a lot. I suggest opening an issue on the Github repo anyway. – Serdar Basegmez Aug 07 '14 at 06:55
0

I originally wrote an blog on this...

Basically you create an Endpoint, I start off with BasicEndpoint.

/**
* creates a new Basic Endpoint to connect to Connections
* @param url
* @param user
* @param password
* @return
*/
private BasicEndpoint createEndpoint(String url, String user, String password) {
BasicEndpoint endpoint = new ConnectionsBasicEndpoint();
endpoint.setUrl(url);
endpoint.setUser(user);
endpoint.setPassword(password);
endpoint.setForceTrustSSLCertificate(true);
return endpoint;
}

Now, I have the basis for making a call to the backend ForumService by passing in the Endpoint I construct.

ForumService svc = new ForumService(demo.endpoint);
try {
ForumList forumList = svc.getAllForums();
for(BaseForumEntity forumEntity : forumList){
Forum forum = (Forum) forumEntity;
System.out.println(“+F: ” + forum.getTitle());
TopicList topics = forum.getTopics();

for(BaseForumEntity topicEntity : topics){
ForumTopic topic = (ForumTopic) topicEntity;
System.out.println(“– ” + topic.getTitle());
}

}
} catch (ForumServiceException e) {
e.printStackTrace();
}

You can repeat this method for making almost any call.

The blog entry is at http://bastide.org/2014/01/28/how-to-develop-a-simple-java-integration-with-the-ibm-social-business-toolkit-sdk/

Paul Bastide
  • 1,505
  • 4
  • 17
  • 22