IBM Connections Activity Streams Integration

30
IBM Connections 5.0 Workshop Author: Paul Godby IBM Ecosystem Development Last Updated: July 3, 2014 Duration: 75 minutes IBM Connections Workshop Lab Manual Activity Streams Integration NOTE: In this lab you will be writing code. If you are not a developer (or if you don't feel like typing everything) you can choose to copy/paste the code from the Lab_files solutions located at: /labs/solutions/ecod.activitystreams.java COPYRIGHT IBM CORPORATION 2014. ALL RIGHTS RESERVED. IBM Ecosystem Development 1

description

This lab will walk you through the process of using the Social Business Toolkit SDK to post a new event to the IBM Connections Activity Stream. It will also show how to create and deploy a new Embedded Experience (OpenSocial Gadget). Finally, you will explore some of the OpenSocial APIs available to your gadgets.

Transcript of IBM Connections Activity Streams Integration

Page 1: IBM Connections Activity Streams Integration

IBM Connections 5.0 Workshop

Author: Paul GodbyIBM Ecosystem DevelopmentLast Updated: July 3, 2014Duration: 75 minutes

IBM Connections Workshop

Lab Manual

Activity Streams Integration

NOTE: In this lab you will be writing code. If you are not a developer (or if you don't feel like typing everything)you can choose to copy/paste the code from the Lab_files solutions located at:

/labs/solutions/ecod.activitystreams.java

COPYRIGHT IBM CORPORATION 2014. ALL RIGHTS RESERVED.IBM Ecosystem Development

1

Page 2: IBM Connections Activity Streams Integration

IBM Connections 5.0 Workshop

Introduction:IBM Connections includes Activity Streams in the Home Page, Profiles, and Communities applications. Similar to a news feed on a social networking site but applied to enterprise data and data sources, an activity stream is updated with new content and events as data is retrieved and published to the stream. Data that is published to the activity stream comes from any service, including IBM services, enterprise services, and internet services.

In the activity stream, the user can click any of the events to get more information about that event. In IBM Connections, clicking on the event will open an embedded experience.

An embedded experience is a way for applications to embed content into other applications and let users interact dynamically with that content directly in context without having to switch to another application.

Embedded experiences eliminate the hassle of constantly switching windows, opening applications, and authenticating with multiple sites and services. This constant switching causes users to lose focus on theirtasks, which decreases their levels of productivity. If users can access, filter, and respond to most of their work processes from a stream of activities in a single interface, they can manage their work more effectively.

From a developer's point of view, embedded experiences require a gadget, an OpenSocial container, and a data model. A gadget is a mini application that can be embedded in web pages, applications, and other OpenSocial-enabled sites. Gadgets can be written in just about any language and are described by an XML document that conforms to the OpenSocial 2.0 specification. Gadget XML files can be hosted anywhere on a public location on the web. The data model provides the OpenSocial container all the information it needs to render the embedded experience as a gadget. The data model is written in JavaScript Object Notation (JSON) format.

Description:In this lab, you will be using the Eclipse IDE to perform API operations. The lab will use the IBM Social Business Toolkit (SBT) SDK to interact with the Activity Stream. By using the SBT SDK, you will be able to focus entirely on the task of Activity Streams integration development (and not the development of HTTP, authentication, JSON libraries, XML libraries, SSL components, etc.).

Objective:This lab will explain the following tasks:

How to configure user roles for Activity Stream integration

How to configure OpenSocial gadgets on the server

How to post a new event to a user's Activity Stream

How to post a new event to a user's Activity Stream that references an OpenSocial gadget

COPYRIGHT IBM CORPORATION 2014. ALL RIGHTS RESERVED.IBM Ecosystem Development

2

Page 3: IBM Connections Activity Streams Integration

IBM Connections 5.0 Workshop

Procedure:

SETUP

Step 1 Review the workshop setup document for instructions on how to ensure all required servers have been started on your SoftLayer device.

Step 2 You may choose to complete this lab on your local machine. However, the instructions will assume you are working locally on the SoftLayer device using a VNC client.

Step 3 Important! Any time you open a new terminal window (Applications → System Tools → Terminal), you should first enter the following commands to switch to the root user. All of the steps documented in this lab will assume you are running with root privileges!

su – root

(Enter <<SSH Password>>)

PREPARE THE SBT SDK SAMPLE APPLICATIONS

The SBT SDK includes some sample applications. These applications are invaluable for learning how to buildnew social integrations. They include all libraries you will need for an application and some sample configurationfiles. In this lab, you will reuse pieces of the sample applications.

Note: Depending on which labs you have already finished, you may have already completed some of the stepsin this section.

Step 4 In a terminal window, enter the following commands to prepare the Social Business Toolkit sample applications. (Replace <version#> with the actual version number of the file included with the VMWare image.)

cd /labs

unzip -o sbtsdk-<version#>.zip

cd sbtsdk/sources

unzip SocialSDK.zip

Step 5 Close the terminal window.

CONFIGURE A HOME PAGE ADMINISTRATOR

To make the Open Social gadgets available for use in the Activity Stream, you need to add the gadgets from theAdministration tab in the Home page user interface and then enable them for use.

Note: Depending on which labs you have already finished, you may have already completed the steps in thissection. If so, you may proceed to the next section.

Step 6 In a web browser, navigate to URL: https://<<Host Name>>:9043/ibm/console

COPYRIGHT IBM CORPORATION 2014. ALL RIGHTS RESERVED.IBM Ecosystem Development

3

Page 4: IBM Connections Activity Streams Integration

IBM Connections 5.0 Workshop

Step 7 Log in with the following credentials

Field Value

User name wasadmin

Password iBmC0nn3ti0ns

Step 8 On the left, select Applications → Application Types → WebSphere enterprise applications

Step 9 On the right, click the link for Homepage

Step 10 Under the Detail Properties section, click the Security role to user/group mapping link.

Step 11 On the next page, place a check beside “admin” and click Map Users...

COPYRIGHT IBM CORPORATION 2014. ALL RIGHTS RESERVED.IBM Ecosystem Development

4

Page 5: IBM Connections Activity Streams Integration

IBM Connections 5.0 Workshop

Step 12 In the Search and Select Users section, search for “dmisawa” and click Search.

Step 13 From the Available list, highlight “DMisawa” and click the right arrow to move his name to the Selected list. When finished, the Selected list should look as follows:

Step 14 Click OK. You should see “DMisawa” under the mapped users for the admin role.

Step 15 Click OK again. When you return to the Homepage Configuration page, click the Save link at the top.

Step 16 Continue to the next section.

COPYRIGHT IBM CORPORATION 2014. ALL RIGHTS RESERVED.IBM Ecosystem Development

5

Page 6: IBM Connections Activity Streams Integration

IBM Connections 5.0 Workshop

CONFIGURE THE TRUSTEDEXTERNALAPPLICATION ROLE FOR THE WIDGETCONTAINER

By default IBM Connections only lets users post to their own activity stream. However, when a 3rd partyapplication is posting events to the activity stream in Connections, the 3rd party application will generally want theability to post to any user's activity stream. To do this we need to assign a designated user thetrustedExternalApplication role.

Note: The user assigned here corresponds to the user configured in the sbt.properties file.

Note: Depending on which labs you have already finished, you may have already completed the steps in thissection. If so, you may proceed to the next section.

Step 17 If you are not already in the Integrated Solutions Console, navigate to the following URL and log in with the credentials provided: https://<< Host Name >>:9043/ibm/console

Field Value

User name wasadmin

Password iBmC0nn3ti0ns

Step 18 On the left, select Applications → Application Types → WebSphere enterprise applications

Step 19 On the right, click the link for WidgetContainer (it may be on page 2)

Step 20 Under the Detail Properties section, click the Security role to user/group mapping link.

Step 21 On the next page, place a check beside “trustedExternalApplication” and click Map Users...

COPYRIGHT IBM CORPORATION 2014. ALL RIGHTS RESERVED.IBM Ecosystem Development

6

Page 7: IBM Connections Activity Streams Integration

IBM Connections 5.0 Workshop

Step 22 In the Search and Select Users section, search for “fadams” and click Search.

Note: In a production scenario, you would use a functional ID that exists in your Profiles database for yourapplication. In this lab environment, you will just use an existing user in IBM Connections.

Step 23 From the Available list, highlight “fadams” and click the right arrow to move the name to the Selected list. When finished, the Selected list should look as follows:

Step 24 Click OK. You should see “fadams” under the mapped users for the “trustedExternalApplication” role.

Step 25 Click OK again. When you return to the Configuration page, click the Save link at the top.

Step 26 Click Logout to exit the Integrated Solutions Console.

COPYRIGHT IBM CORPORATION 2014. ALL RIGHTS RESERVED.IBM Ecosystem Development

7

Page 8: IBM Connections Activity Streams Integration

IBM Connections 5.0 Workshop

CONFIGURE NEW OPENSOCIAL GADGETS FOR IBM CONNECTIONS

In order to use an external OpenSocial gadget in IBM Connections, it will have to be registered with the system.This prevents unauthorized applications from posting events into various Activity Streams. This helps preventpotential SPAM from unwanted applications as well as introduction of possibly malicious code. For this reason,an administrator will need to register and authorize individual gadgets in the system.

In this example, a user has already been defined as a “Home Page administrator”.

Step 27 In a web browser, navigate to URL: http://<<Host Name>>/homepage

Step 28 (If prompted, accept the certificate) Log in with the following credentials

Field Value

User name dmisawa

Password passw0rd

Step 29 On the left, select the Administration link.

Step 30 On the right, under the “Disabled widgets” section, click the Add another widget button.

COPYRIGHT IBM CORPORATION 2014. ALL RIGHTS RESERVED.IBM Ecosystem Development

8

Page 9: IBM Connections Activity Streams Integration

IBM Connections 5.0 Workshop

Step 31 In later steps, you will be creating a new OpenSocial gadget. Enter the following information for the gadget so that it is registered in IBM Connections. Failure to register a gadget will cause those events to display inside the default gadget.

Field Value

Widget Type Open Social Gadget

Security Trusted

Use SSO <checked>

Show for Activity stream events <checked>

Server access via Proxy All servers

Widget Title ICS EcoD Gadget

URL Address http://<<Host Name>>/gadgets/ecodGadget.xml

Step 32 Scroll to the bottom and click Save.

Step 33 In the “Disabled widgets” section, highlight the ICS EcoD Gadget entry.

Step 34 Underneath the “Disabled widgets” section, click Enable.

Step 35 Leave the browser open on the current page and proceed to the next section.

COPYRIGHT IBM CORPORATION 2014. ALL RIGHTS RESERVED.IBM Ecosystem Development

9

Page 10: IBM Connections Activity Streams Integration

IBM Connections 5.0 Workshop

PREPARE A PROJECT USING THE ECLIPSE IDE

The Eclipse IDE for Java EE Developers is free and contains tools for creating Web applications, includingeditors for JavaScript, HTML, CSS, and XML. Best of all, this IDE is free to use. Visit www.eclipse.org/downloads for more information.

Step 36 In a terminal window, enter the following commands to start Eclipse:

/labs/eclipse/eclipse

Step 37 In the Workspace Launcher window, you will select a location for your development workspace.A workspace is a development profile that contains your projects, folders, files, and settings. Enter the following information and click OK.

Field Value

Workspace /labs/workspaces/connections_java

Step 38 If you see the Welcome to Eclipse page, click the “Workbench” button.

Step 39 From the Eclipse file menu, choose File → New → Project

Step 40 In the New Project wizard, choose Java → Java Project and click Next.

COPYRIGHT IBM CORPORATION 2014. ALL RIGHTS RESERVED.IBM Ecosystem Development

10

Page 11: IBM Connections Activity Streams Integration

IBM Connections 5.0 Workshop

Step 41 In the New Java Project wizard, enter the following information and click Finish.

Field Value

Project name ecod.activitystreams.java

Step 42 If asked about switching to a Java Perspective, click Yes.

Step 43 From the Eclipse top menu bar, click Window → Preferences

Step 44 Expand General → Editors → Text Editors

Step 45 On the right, place a check next to the option “Show line numbers” and click OK.

CREATE NEW JAVA PACKAGES

A Java package is a mechanism for organizing Java classes into namespaces. Programmers also typically usepackages to organize classes belonging to the same category or providing similar functionality.

Step 46 In the Eclipse IDE, expand the project, right-click the “src” folder and choose New → Package.

Step 47 In the New Java Package window, enter the following information and click Finish.

Field Value

Name com.ibm.ecod.streams

COPYRIGHT IBM CORPORATION 2014. ALL RIGHTS RESERVED.IBM Ecosystem Development

11

Page 12: IBM Connections Activity Streams Integration

IBM Connections 5.0 Workshop

PREPARE THE SOCIAL BUSINESS TOOLKIT SDK

In this exercise, you will be using the Social Business Toolkit SDK to write your Java application that integratesinto the Activity Stream.

In a prerequisite lab, you extracted the SDK to a location on disk. In this section you will add the required JARSfrom the SDK to your Java project.

In an earlier step, you configured a user in the TrustedExternalApplication role for the widget container. As you'llrecall, this gave that particular user the ability to post to any user's Activity Stream.

Step 48 Return to the Eclipse IDE. Right-click the “src” folder and select Build Path → Configure Build Path.

Step 49 Choose the Libraries tab.

Step 50 Click Add External JARS...

Step 51 Navigate to the following location and select the following files (hold down the CTRL key to select multiple files). These libraries contain the Social Business Toolkit SDK APIs. When finished, click OK.

Field Value

Location /labs/sbtsdk/redist/jar

Files com.ibm.commons.runtime-<version#>.jarcom.ibm.commons.xml-<version#>.jarcom.ibm.commons-<version#>.jarcom.ibm.sbt.core-<version#>.jar

Step 52 Click Add External JARS...

COPYRIGHT IBM CORPORATION 2014. ALL RIGHTS RESERVED.IBM Ecosystem Development

12

Page 13: IBM Connections Activity Streams Integration

IBM Connections 5.0 Workshop

Step 53 Navigate to the following location and select the following files (hold down the CTRL key to select multiple files). The SDK APIs have required dependencies on these open source libraries. For example, the HTTP Client libraries are used to send/receive HTTP requests. When finished, click OK.

Note: Depending on the SBT SDK APIs used in your specific application, you may not need to include all ofthese libraries in your project. For purposes of this lab, you will add them all.

Field Value

Location /labs/sbtsdk/sources/libraries/com.ibm.sbt.libs.java/lib

Files apache-mime4j-<version#>.jarcom.ibm.sbt.javamail-<version#>.jarcommons-codec-<version#>.jarcommons-fileupload-<version#>.jarcommons-io-<version#>.jarcommons-logging-<version#>.jarhttpclient-<version#>.jarhttpcore-<version#>.jarhttpmime-<version#>.jar

Step 54 Click OK to close the Java Build Path window. In the Package Explorer on the left, expand the Referenced Libraries section. Verify that you have the following libraries listed. If you are missing any, repeat the previous steps until they are all listed. (Note: Your version numbers may vary from the screenshot depending on which version of the SDK you are using.)

COPYRIGHT IBM CORPORATION 2014. ALL RIGHTS RESERVED.IBM Ecosystem Development

13

Page 14: IBM Connections Activity Streams Integration

IBM Connections 5.0 Workshop

Step 55 Now that you have the required libraries referenced, you will need to set up a managed-beans.xml file that contains information about the various endpoints used by the SBT SDK. Youcould create this file manually. However, there is a sample application included in the SBT SDK that provides an XML file for this purpose. In this lab, you will leverage this existing file instead of creating a new one from scratch.

Step 56 In the Package Explorer, right-click the “src” folder and choose New → Folder. Name the new folder “META-INF” and click Finish.

Step 57 Right-click the META-INF folder and choose Import...

Step 58 In the Import window, choose General → File System and click Next.

Step 59 Import the following file and click Finish.

Field Value

From directory /labs/sbtsdk/sources/samples/java/sbt.sample.app/src/main/resources/META-INF

Files managed-beans.xml

Step 60 In the Package Explorer, expand the META-INF folder to reveal the imported XML file. Double-click this file to open it and review it's contents. Scroll down to the “Connections” section and review the following properties: url, user, and password.

Step 61 Notice that the value for each of these properties is not entered explicitly. Instead, there are some substitution keys listed. The SBT SDK application will subsitute values from the sbt.properties file for these properties.

Step 62 When finished reviewing the XML, close the file.

COPYRIGHT IBM CORPORATION 2014. ALL RIGHTS RESERVED.IBM Ecosystem Development

14

Page 15: IBM Connections Activity Streams Integration

IBM Connections 5.0 Workshop

Step 63 To complete the configuration of your application, you must now create a sbt.properties file. This properties file will contain endpoint (server) and user authentication information for the application. Right-click the project name, ecod.activitystreams.java, and choose New → File.

Step 64 In the New File window, name the file “sbt.properties” and click Finish. When finished, your project should look as follows: (Make sure the properties file is in the right location!)

Step 65 Double-click “sbt.properties” to open it with a text editor (if it is not already open).

Step 66 Add the following lines to the properties file. In this example, you will be configuring the endpoint your Java application will use (the IBM Connections server) and the user that will be posting the event to the Activity Stream. Notice that the keys you are using correspond to the keys entered in the managed-beans.xml file. Also, you will be using the name of the user that was added to the TrustedExternalApplication role of the widget container so that the applicationcan post to any user's Activity Stream.

connections.url=https://<<Host Name>>connections.user=fadamsconnections.password=passw0rd

Step 67 When finished, Save and close the file.

Step 68 The Social Business Toolkit SDK is now configured for your application.

COPYRIGHT IBM CORPORATION 2014. ALL RIGHTS RESERVED.IBM Ecosystem Development

15

Page 16: IBM Connections Activity Streams Integration

IBM Connections 5.0 Workshop

POST A NEW EVENT TO THE ACTIVITY STREAM

In order to post an event to the users Activity Stream, you will need to make an authenticated HTTP POST to aspecific URL with the event included in JSON format.

Depending on the endpoint used, you can make the HTTP POST using either Basic authentication or OAuth.

(Note: OAuth authentication requires some additional server configuration before it can be used.)

In this exercise, you will see how the Social Business Toolkit SDK hides much of this underlying setup andconfiguration and allows you to focus on the integration code itself.

Step 69 Return to the web browser and click the Updates link to go to the current user's (Dan Misawa) Activity Stream. Make sure the I'm Following tab is selected.

Step 70 View the current Activity Stream for Dan Misawa's Home Page. (Depending on what labs you have already completed before this one, you may see various entries in the stream.)

Step 71 Leave the browser open and return to the Eclipse IDE.

Step 72 In the Package Explorer, right click the “com.ibm.ecod.streams” package and select New → Class.

Step 73 In the New Java Class window, leave all of the default values and add the following information. When finished, click Finish.

Field Value

Name EcodAsPost

Step 74 The new class should automatically open in a Java editor.

Step 75 At line 2, add two new blank lines so that lines 2,3,4 are all blank.

COPYRIGHT IBM CORPORATION 2014. ALL RIGHTS RESERVED.IBM Ecosystem Development

16

Page 17: IBM Connections Activity Streams Integration

IBM Connections 5.0 Workshop

Step 76 In the following code sample, you will need various import statements in order for the code to compile. Rather than add them after the fact, go ahead and include all of the import statements now. This will also enable auto-complete and some of the other functions of the Eclipse IDE to make it easier to write the actual code. At line 3, insert the following import statements:

import java.util.Date;

import com.ibm.commons.runtime.Application;import com.ibm.commons.runtime.Context;import com.ibm.commons.runtime.RuntimeFactory;import com.ibm.commons.runtime.impl.app.RuntimeFactoryStandalone;import com.ibm.commons.util.io.json.JsonJavaArray;import com.ibm.commons.util.io.json.JsonJavaObject;import com.ibm.sbt.services.client.connections.activitystreams.ASVerb;import com.ibm.sbt.services.client.connections.activitystreams.ActivityStreamService;import com.ibm.sbt.services.endpoints.BasicEndpoint;import com.ibm.sbt.services.endpoints.EndpointFactory;

Step 77 Click Save.

Step 78 At line 17, add two additional blank lines so that lines 17,18,19 are all blank.

Step 79 You will create a main function that will build a JSON event and POST it to the IBM Connections server. Paste the following Java code at line 18. A description of all of the following code can be found in the next steps.

public static void main(String[] args) {

}

Step 80 At line 19, add 2 blank lines so that lines 19,20,21 are all blank.

COPYRIGHT IBM CORPORATION 2014. ALL RIGHTS RESERVED.IBM Ecosystem Development

17

Page 18: IBM Connections Activity Streams Integration

IBM Connections 5.0 Workshop

Step 81 In this lab exercise, you will use the Social Business Toolkit SDK Java APIs to programmatically build and send the following JSON event. Take special notice of the “to” section. This section will indicate the target user(s) for the event.

Note: Instead of a top-level “to” object, you can also add target users to a “deliverTo” object inside the“openSocial” object. In the sample below, this would be on the same nested level as the “embed” section.

Note: If you want to post to multiple users streams at once, you can add them as additional array entries in the“to” JSON object.

Note: You should use the Profiles ID for users in the “to” JSON object. In this example, the id is for “dmisawa”.

{"content":"<b>Event<\/b> Content","title":"Event Title","verb":"CREATE","updated":1368824127839,"generator": {

"id":"EcoDApp","displayName":"EcoD Demo Gadget","url":"http:\/\/www.ibm.com"

},"actor": {

"id":"@me" },"to": [

{“objectType”:”person”, “id”:”e35499c0-10de-1033-96e5-c9aae6db5f69”}],"object": {

"summary":"Event posted using the SBT SDK Java API","objectType":"SomeTypeFromYourApp","id":"RandomIdFromYourApp","displayName":"Object Display Name","url":"http:\/\/www.ibm.com"

},"openSocial": {

"embed": {"gadget":"http:\/\/connections.demos.ibm.com\/gadgets\/ecodGadget.xml","context": {

"var1":"data1","var2":"data2"

}}

}}

COPYRIGHT IBM CORPORATION 2014. ALL RIGHTS RESERVED.IBM Ecosystem Development

18

Page 19: IBM Connections Activity Streams Integration

IBM Connections 5.0 Workshop

Step 82 You will need to implement the code to programmatically build the JSON event listed above. The SDK includes a JsonJavaObject class for this purpose. This class allows you to build a JSON object and perform the appropriate nesting to replicate the required JSON event for posting to the Activity Stream.

Note: You can use any method applicable to your application to build the JSON event. For example, you mightimport the event as a string from a text file or some other system. Whichever method you choose, you'll need tothe load the final JSON event into a JsonJavaObject.

Step 83 At line 20, insert the following code to programmically build the JSON object:

Note: In the code, line 30 is the IBM Connections unqiue id for user Dan Misawa.

JsonJavaObject generator = new JsonJavaObject();generator.put("id", "EcoDApp");generator.put("displayName", "EcoD Demo Gadget");generator.put("url", "http://www.ibm.com");

JsonJavaObject actor = new JsonJavaObject();actor.put("id", "@me");

JsonJavaObject user1 = new JsonJavaObject();user1.put("objectType", "person");user1.put("id", "e35499c0-10de-1033-96e5-c9aae6db5f69");JsonJavaArray to = new JsonJavaArray();to.put(0, user1);

JsonJavaObject object = new JsonJavaObject();object.put("id", "RandomIdFromYourApp");object.put("summary", "Event posted using the SBT SDK Java API");object.put("displayName", "Object Display Name");object.put("objectType", "SomeTypeFromYourApp");object.put("url", "http://www.ibm.com");

JsonJavaObject json = new JsonJavaObject();json.put("content", "<b>Event</b> Content");json.put("title", "Event Title");json.put("verb", ASVerb.CREATE.getVerbType());json.put("updated", new Date().getTime());json.put("generator", generator);json.put("actor", actor);json.put("to", to);json.put("object", object);

System.out.println("Event: " + json.toString());

COPYRIGHT IBM CORPORATION 2014. ALL RIGHTS RESERVED.IBM Ecosystem Development

19

Page 20: IBM Connections Activity Streams Integration

IBM Connections 5.0 Workshop

Step 84 Next, you will need to implement the code required to invoke a stand-alone Social Business Toolkit SDK Java application. At line 52, add 2 blank lines so that lines 52,53,54 are all blank. At line 53, add the following code to both initialize and destroy the application.

RuntimeFactory rf = new RuntimeFactoryStandalone();Application app = rf.initApplication(null);Context ctx = Context.init(app, null, null);

Context.destroy(ctx);Application.destroy(app);

Step 85 Finally, it's time to use the Social Business Toolkit SDK Activity Streams API to send the JSON event to the server. At line 56 (between the initialization and destroy code), add 2 blank lines so that lines 56,57,58 are all blank. At line 57, insert the following code fragment to post your event:

Note: You configured the server, username, and password to use in the sbt.properties file. There's no need towrite special code to insert these values (or even hard code them). The SDK handles it all for you!

try {BasicEndpoint ep = (BasicEndpoint)

EndpointFactory.getEndpoint(ActivityStreamService.DEFAULT_ENDPOINT_NAME);

ActivityStreamService svc = new ActivityStreamService();svc.setEndpoint(ep);String result = svc.postEntry(json);System.out.println("Result: " + result);

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

Step 86 Click Save.

COPYRIGHT IBM CORPORATION 2014. ALL RIGHTS RESERVED.IBM Ecosystem Development

20

Page 21: IBM Connections Activity Streams Integration

IBM Connections 5.0 Workshop

Step 87 Refer to the following table for a description of the code:

Lines Description

20 – 23 Create the “generator” section of the JSON event

25 – 26 Create the “actor” section of the JSON event (who is posting)

28 – 32 Create the “to” section of the JSON event (an array of users)

34 – 39 Create the “object” section of the JSON event

41 – 49 Build the JSON event by adding all data and nested sections

51 Output statement to help verify the JSON created

53 – 55 Set up the stand-alone SDK application

58 Create an endpoint that will pull the IBM Connections server information from the properties file. Adds HTTP Basic authentication to the request with user information from the properties file.

60 – 62 Post the JSON event to the Activity Stream

63 Output statement to verify request accepted by the server

67 – 68 Destroy the SDK application

Step 88 From the Eclipse toolbar, click the Run drop-down button and choose Run Configurations...

Step 89 In the Run Configurations window, on the left click Java Application to highlight the entry. Afterselecting “Java Application”, click the New Launch Configuration button (the white page with a yellow plus in the upper right corner).

COPYRIGHT IBM CORPORATION 2014. ALL RIGHTS RESERVED.IBM Ecosystem Development

21

Page 22: IBM Connections Activity Streams Integration

IBM Connections 5.0 Workshop

Step 90 On the right, enter the following information.

Field Value

Name EcoD Activity Streams

Project ecod.activitystreams.java

Main class com.ibm.ecod.streams.EcodAsPost

Step 91 Click Apply. Click Run.

Step 92 Once the code begins to execute, you can review the Console tab at the bottom of the Eclipse IDE to review the output messages from the code sample. As you'll recall, the code above will output the JSON event that is being sent to the server. It will also show the the result returned. As you can see, the result contains the IBM Connections internal ID for the new event.

Step 93 Return to the web browser and refresh the entire page (alternatively, just click the Updates linkon the left). You should see your new (programmatically created) Event in the Activity Stream! In line 26 of your Java code, you set the actor to “@me”. Since we configured user “fadams” inthe sbt.properties file (the same user as defined in the WidgetContainer), the event will be posted by user “Frank Adams”.

COPYRIGHT IBM CORPORATION 2014. ALL RIGHTS RESERVED.IBM Ecosystem Development

22

Page 23: IBM Connections Activity Streams Integration

IBM Connections 5.0 Workshop

Step 94 Click the event in the Activity Stream and select the arrows on the right to open the event in an OpenSocial gadget (Embedded Experience). Since no OpenSocial gadget was defined with the event, it will open with the default gadget provided by IBM Connections. As you can see, it contains basic information about the event.

Step 95 Return to the Eclipse IDE. In an earlier step, you added user “fadams” to a role in the WidgetContainer that allows this user to post to any Activity Stream....and on behalf of any other user! At line 26, the current actor is currently set to @me. Modify this code as follows to post on behalf of another user. In this example, you will enter the Profile ID for Samantha Daryn.

actor.put("id", "e485c6c0-10de-1033-9721-c9aae6db5f69");

Step 96 Finally, you will want to post a new Event, so you will need to change the ID in the JSON that you send to the server. If you use the same ID as a previous event, that old event will be updated and will roll-up to the top of the Activity Stream. At line 35, change the value from “RandomIdFromYourApp” to “RandomIdFromYourApp2”. When finished, the code should look as follows:

object.put("id", "RandomIdFromYourApp2");

Step 97 Click Save.

Step 98 From the Eclipse toolbar, click the Run drop-down button and choose EcoD Activity Streams

COPYRIGHT IBM CORPORATION 2014. ALL RIGHTS RESERVED.IBM Ecosystem Development

23

Page 24: IBM Connections Activity Streams Integration

IBM Connections 5.0 Workshop

Step 99 Return to the browser and refresh the stream. You should see a new event posted by user Frank Adams, on behalf of user Samantha Daryn, to user Dan Misawa!

Step 100 Leave the browser open and proceed to the next section.

CREATE AN OPENSOCIAL GADGET

IBM Connections Embedded Experiences are implemented via OpenSocial gadgets. From a user's perspective,an embedded experience is a way of interacting with events, notifications, and business processes dynamically incontext directly from an inbox, social home page, and other containers without having to log in to and switch toanother application or service.

In the background, an embedded experience occurs when a content host tells a system the preferred way torender the content In response, the system makes the content available to a rendering component, namely anOpenSocial gadget.

Step 101 Return to the Eclipse IDE. In the Package Explorer, right click the “com.ibm.ecod.streams” package and select New → File.

Step 102 For the File name, enter “ecodGadget.xml” and click Finish.

Step 103 After the file opens, click the Source tab at the bottom of the editor.

COPYRIGHT IBM CORPORATION 2014. ALL RIGHTS RESERVED.IBM Ecosystem Development

24

Page 25: IBM Connections Activity Streams Integration

IBM Connections 5.0 Workshop

Step 104 Paste the following XML code into the file. A description of the code can be found in the following steps.

<?xml version="1.0" encoding="UTF-8" ?><Module><ModulePrefs title="ICS EcoD Gadget" >

<Require feature="embedded-experiences"/><Require feature="dynamic-height"/><Require feature="dynamic-width"/><Require feature="views"/>

</ModulePrefs><Content type="html" view="embedded,default">

<![CDATA[<b>ICS EcoD Demo Gadget!</b><p/><div id="data"></div><script type="text/javascript">

var elem = document.getElementById("data");

function output(str) {var div = document.createElement("div");div.innerHTML = str;elem.appendChild(div);gadgets.window.adjustHeight();gadgets.window.adjustWidth();

}

function init() {opensocial.data.getDataContext().registerListener("org.opensocial.ee.context",

function(key) {var ctx = opensocial.data.getDataContext().getDataSet(key);for (var x in ctx) {

output(x + " = " + ctx[x]); }});

}

gadgets.util.registerOnLoadHandler(init);</script>]]>

</Content></Module>

Step 105 Click Save.

Note: You can also just make a reference to your content that resides in another HTML page or on an externalserver. For example, instead of embedding the UI and application logic in the “Content” tags like above, you canmake a reference to an external page as follows:

<Content type="html" view="embedded,default" href="ee.html" />

COPYRIGHT IBM CORPORATION 2014. ALL RIGHTS RESERVED.IBM Ecosystem Development

25

Page 26: IBM Connections Activity Streams Integration

IBM Connections 5.0 Workshop

Step 106 Refer to the following table for a description of the code:

Lines Description

3 – 8 Serves as a container element for all Metadata, Features, and processing rules.

4 Required for an Activity Streams gadget

5 – 6 Required to enable you to resize the gadget to your content

9 Container for the UI and/or application logic for your gadget

13 A DIV element to write sample data to

17 – 23 A helper function to write data to the UI for demo purposes

21 – 22 Whenever the DOM is modified these functions should be called to resize the gadget to the content. May pass a maximum pixel height/width as an argument.

25 A function that gets called when the gadget loads

26 Listen for data in the context JSON element

27 Get a handle to the data in the context JSON element

28 – 30 For each variable in the context JSON element of the event, append the key-value pair to the DOM for demo purposes

34 Register a function to be called when the gadget loads

Step 107 You now have everything in place for a Hello World Gadget! The next step is to place the gadget files on the HTTP server that was configured for in earlier steps. In this lab, you will place your exercises on the IBM HTTP server used by IBM Connections. In a terminal window,create a “gadgets” directory with the following commands:

cd /opt/IBM/HTTPServer/htdocs

mkdir gadgets

cd gadgets

Step 108 Copy the entire contents of the “WebContent” folder in your Eclipse workspace to this new directory on the IBM HTTP Server with the following commands (Note: The line ends in a “space” followed by a “period”!):

'cp' -rf /labs/workspaces/connections_java/ecod.activitystreams.java/src/com/ibm/ecod/streams/* .

Step 109 Leave this terminal window open. You can reuse it in future steps to recopy files and leverage the Linux history feature (press the UP/DOWN key(s) to cycle through the command history) and not have to retype the command each time.

Step 110 Return to the Eclipse IDE and open EcodAsPost.java

COPYRIGHT IBM CORPORATION 2014. ALL RIGHTS RESERVED.IBM Ecosystem Development

26

Page 27: IBM Connections Activity Streams Integration

IBM Connections 5.0 Workshop

Step 111 Add a new JSON object below the previous “object” object and before the “json” object using the code below. This new JSON object will contain the reference to the OpenSocial gadget. It will also utilize the context feature to embed some custom metadata from your third-party application. The OpenSocial gadget you created in previous steps will display all of this custom metadata. At line 40, add two blank lines so that lines 40,41,42 are all blank. At line 41, insert the following code: (Make sure to update your host name!)

JsonJavaObject context = new JsonJavaObject();context.put("var1", "val1");context.put("var2", "val2");JsonJavaObject embed = new JsonJavaObject();embed.put("gadget", "http://<<Host Name>>/gadgets/ecodGadget.xml");embed.put("context", context);JsonJavaObject social = new JsonJavaObject();social.put("embed", embed);

Step 112 Locate your JSON object named “json”. This is the top level node for the JSON event that is being created. At the end of this block, add your new JSON Java object from the previous steps. At line 59, add one blank line so that lines 59,60 are blank. At line 59, insert the following code:

json.put("openSocial", social);

Step 113 Finally, you will want to post a new Event, so you will need to change the ID in the JSON that you send to the server. At line 35, change the value from “RandomIdFromYourApp2” to “RandomIdFromYourApp3”. When finished, the code should look as follows:

object.put("id", "RandomIdFromYourApp3");

Step 114 Click Save.

Step 115 From the Eclipse toolbar, click the Run drop-down button and choose EcoD Activity Streams

COPYRIGHT IBM CORPORATION 2014. ALL RIGHTS RESERVED.IBM Ecosystem Development

27

Page 28: IBM Connections Activity Streams Integration

IBM Connections 5.0 Workshop

Step 116 Return to the web browser and refresh the stream. Click the right arrow on the new event to see your Hello World Embedded Experience! As you can see, the gadget loops through all the embedded metadata from the posted event and displays it to the user.

Note: As an alternative to looping through the top-level gadget keys, you can use the following code in yourgadget to display the entire JSON object returned via the context API.

output(gadgets.json.stringify(ctx));

Step 117 Return to the Eclipse IDE. As you saw in the previous step, when you examine the context data there is an “authUser” section containing informtion about the currently authenticated user.You can retrieve this information very easily with some basic JavaScript. Open ecodGadget.xml and delete lines 28 – 30. When finished, your init function should look like thefollowing:

Step 118 At line 28, insert the following code fragment to print the data contained in the “authUser” object.

output("id: " + ctx.authUser.id);output("name: " + ctx.authUser.name);

Step 119 Click Save.

Step 120 Return to the terminal window where you entered the copy command and leverage the Linux command history feature to recopy the files to the IBM HTTP Server.

COPYRIGHT IBM CORPORATION 2014. ALL RIGHTS RESERVED.IBM Ecosystem Development

28

Page 29: IBM Connections Activity Streams Integration

IBM Connections 5.0 Workshop

Step 121 Return to the web browser and refresh the stream. Open the most recent Embedded Experience that contains a reference to your gadget to view the results.

Note: You may need to clear your browser cache to see the updated gadget.

Step 122 Return to the Eclipse IDE. In ecodGadget.xml, delete all of the code inside the “init” function while leaving the rest of the gadget intact. When finished, the code should look as follows:

Step 123 You may want to make AJAX requests to external applications to retrieve data. In this example, we do not have a back-end service available. So, we will just retrieve some static JSON and print the result. Add the following code to the init function. When finished, click Save. (Make sure to update your host name!!!)

var params = {};params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON;var url = "http://<<Host Name>>/gadgets/response.json";gadgets.io.makeRequest(url, function(response) {

output(gadgets.json.stringify(response));}, params);

Step 124 To simulate the data returned from a service, you will need to create the JSON file referenced in the previous step. In the Package Explorer, right click the “com.ibm.ecod.streams” package and select New → File.

Step 125 For the File name, enter “response.json” and click Finish.

COPYRIGHT IBM CORPORATION 2014. ALL RIGHTS RESERVED.IBM Ecosystem Development

29

Page 30: IBM Connections Activity Streams Integration

IBM Connections 5.0 Workshop

Step 126 Enter the following JSON data.

{ "service": "ICS EcoD Service", "data": "some data" }

Step 127 Click Save.

Step 128 Return to the terminal window where you entered the copy command and leverage the Linux command history feature to recopy the files to the IBM HTTP Server.

Step 129 Return to the web browser and refresh the page. Open the most recent Embedded Experiencethat contains a reference to your gadget to view the results. You may need to scroll to see all of the returned data.

Note: You may need to clear your browser cache to see the updated gadget.

Step 130 That's it (for now)!

Summary:

This lab introduced you to Activity Streams and OpenSocial gadgets. You learned how to use the IBM SocialBusiness Toolkit SDK Java APIs to post new events to a user's stream. In addition, you learned how to write abasic OpenSocial gadget and how to associate it with a given event.

Access FREE education on the IBM Collaboration Solutions portfolio of products today!

1. Visit the IBM Greenhouse and create a free account.

Link → http://greenhouse.lotus.com/

2. Visit the IBM Collaboration Solutions Ecosystem Development Community

Link → https://greenhouse.lotus.com/communities/community/icsecod

3. Learn new skills and share these links with your friends and colleagues!

COPYRIGHT IBM CORPORATION 2014. ALL RIGHTS RESERVED.IBM Ecosystem Development

30