To add a new story to a user's Activity Stream, send a POST request containing the necessary data to the story server URL.
POST http://${STORY_SERVER_URL}/api/story/v1/activities/${USER_ID_TYPE}/${USER_ID_PREFIX}/${USER_ID_SUFFIX} <soc:activity xmlns:soc="http://social.bea.com/activity"> <body><![CDATA[${EVENT}]]></body> </soc:activity>
Variable | Description |
---|---|
STORY_SERVER_URL | The URL to the story server, typically, %HOSTNAME%:21030/activityservice. |
USER_ID_TYPE | The type of user identifier that follows; acceptable values
are: username (login name), UUID, & ID (portal object integer
ID). Note: These strings must be lowercase.
|
USER_ID_PREFIX | The identifier of the user that the story is about; in the case of domain qualified names, this is the domain name. (For example, bea\jking will be represented as .../username/bea/jking in the post URL.) |
USER_ID_SUFFIX | (Optional.) In the case of domain qualified names, this is the username; omitted in all other cases. |
EVENT | The story itself (the CDATA envelope is optional if the story does not include any markup). |
<soc:activity xmlns:soc="http://social.bea.com/activity"> <body>post content</body> <userId>4258</userId> <senderFullName>Joe King</senderName> <senderIP>10.60.28.195</senderIP> <userUUID>E523D1A7-475E-0B4D-76CA-6F9001480000</userUUID> <userFullName>Joe King</userFullName> <version>v1</version> </soc:activity>The post can be implemented in many ways, as shown in the examples that follow.
HTML/JavaScript Example
<script type="text/javascript"> // Get the gatewayed URL - This will give us authentication and prevent cross domain POST errors. var activitystreamBaseURL = "<pt:common.url xmlns:pt='http://www.plumtree.com/xmlschemas/ptui/' pt:href='http://bfraser03.bea.com:21030/activityservice/api/story/v1/activities/'/>"; // Get the sender's full name. var senderName = "<pt:common.userinfo pt:info='FullName' xmlns:pt='http://www.plumtree.com/xmlschemas/ptui/'/>"; var portalURL = "http://bfraser03.bea.com:8080/portal/server.pt"; function sendStory() { var to = document.getElementById('to').value; var message = linkedName() + ' wrote: ' + document.getElementById('message').value; doRestCall( 'POST', activitystreamBaseURL + 'username/' + to, '<?xml version="1.0" encoding="UTF-8"?>' + '<soc:activity xmlns:soc="http://social.bea.com/activity">' + '<body><![CDATA[' + message + ']]></body>' + '</soc:activity>'); } function doRestCall(requestType, postURL, xml) { var xmlhttp; // Get the XMLHttpRequest object -- needs to work for IE and non-IE browsers if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else if (window.ActiveXObject) { xmlhttp = new ActiveXObject("Microsoft.XMLHttp"); } // Set the callback as an anonymous funcion xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4) { var str = '<div>'; // We should get 201 CREATED on successful POST if (xmlhttp.status == 201) { str = str + 'Message sent!'; } else { str = str + 'ERROR: ' + xmlhttp.status + '</div><div>' + xmlhttp.responseText + '</div>'; } document.getElementById('output').innerHTML = str; } } xmlhttp.open(requestType, postURL, true); xmlhttp.setRequestHeader("Content-Type", "text/xml"); xmlhttp.send(xml); } function linkedName() { // This function returns viewing userÕs name hyperlinked to their homepage. var nameurl = senderName.replace(" ", "_"); nameurl = nameurl.toLowerCase(); var link = '<a href="' + portalURL + '/user/' + nameurl + '">' + senderName + '</a>'; return link; } </script> <div>Send a message to: <input id="to" type="text" size="30"/></div> <div>Message: <input id="message" type="text" size="40" /></div> <div><a href="#" onclick="sendStory();"> send it! </a></div> <div id="output"></div>
Java Example
The Java example below sends the message “Check out the <a href=\"http://www.bea.com\">BEA</a> home page!”.
package bea.sample.activitystream; import java.io.UnsupportedEncodingException; import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.auth.AuthScope; import org.apache.commons.httpclient.methods.*; public class ActivityStreamUtil { public static HttpClient CreateAuthenticatedClient(String username, String password) { HttpClient client = new HttpClient(); // This is promiscuous be careful client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); // Send credentials with request without waiting for challenge client.getParams().setAuthenticationPreemptive(true); return client; } public static PostMethod CreateActivityStreamPost(String url, String body) { PostMethod postMethod = new PostMethod(url); RequestEntity requestEntity = null; try { requestEntity = new StringRequestEntity("<activity><body><![CDATA[" + body + "]]></body></activity>", "text/xml", "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } postMethod.setRequestEntity(requestEntity); return postMethod; } }ActivityStreamPost.java
package bea.sample.activitystream; import java.io.IOException; import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.methods.PostMethod; public class ActivityStreamPost { public static final String ACT_USERNAME = "username"; public static final String ACT_ID = "id"; public static final String ACT_UUID = "uuid"; public static void main(String[] args) { // Post to the guest user by name from the administrator account. String host = "localhost:21030"; String userIDType = ACT_USERNAME; String userID = "guest"; String username = "administrator"; String password = "admin"; String url = "http://" + host + "/activityservice/api/story/v1/activities/" + userIDType + "/" + userID; String message = "Check out the <a href=\"http://www.bea.com\">BEA</a> home page!"; HttpClient client = ActivityStreamUtil.CreateAuthenticatedClient( username, password); PostMethod post = ActivityStreamUtil.CreateActivityStreamPost(url, message); try { int status = client.executeMethod(post); if (status == HttpStatus.SC_CREATED) { System.out.println("Post successful"); System.out.println(post.getResponseBodyAsString()); } else { System.err.println("Method failed: " + post.getStatusLine()); } } catch (HttpException e) { System.err.println("Fatal protocol violation: " + e.getMessage()); e.printStackTrace(); } catch (IOException e) { System.err.println("Fatal transport error: " + e.getMessage()); e.printStackTrace(); } finally { post.releaseConnection(); } } }
.NET Example
using System; using System.Collections.Generic; using System.Text; using System.Net; using System.IO; namespace ActivityServiceTest { class Program { static void Main(string[] args) { string url = "http://localhost:8090/activityservice/api/story/v1/activities/username/bea/nsuravar"; string username = "test"; string password = "plumtree"; string data = "<soc:activity xmlns:soc=\"http://social.bea.com/activity\"><body>Greetings from .NET!</body></soc:activity>"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.Credentials = new NetworkCredential(username, password); request.ContentLength = data.Length; request.KeepAlive = false; System.Net.ServicePointManager.Expect100Continue = false; Uri uri = new Uri(url); NetworkCredential Credentials = request.Credentials.GetCredential(uri, "Basic"); request.ContentType = "text/xml;charset=UTF-8"; if (Credentials != null) { byte[] credentialBuffer = new UTF8Encoding().GetBytes( Credentials.UserName + ":" + Credentials.Password); request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(credentialBuffer); } StreamWriter writer = new StreamWriter(request.GetRequestStream()); writer.Write(data); writer.Close(); try { WebResponse response = request.GetResponse(); byte[] ByteArray = response.Headers.ToByteArray(); using (StreamReader reader = new StreamReader(response.GetResponseStream())) { while (reader.Peek() != -1) { Console.WriteLine(reader.ReadLine()); } } } catch (Exception ex) { Console.Write(ex.ToString()); } } } }