新しいストーリーをユーザのアクティビティ ストリームに追加するには、ストーリー サーバ URL に必要なデータを指定して POST リクエストを送信します。
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>
変数 | 説明 |
---|---|
STORY_SERVER_URL | ストーリー サーバの URL。通常は、%HOSTNAME%:21030/activityservice。 |
USER_ID_TYPE | 指定可能なユーザ識別子。使用可能な値 : username (ログイン名)、UUID、ID (ポータル オブジェクト ID (整数))。 注意 : これらの文字列は小文字でなければならない。
|
USER_ID_PREFIX | ストーリーに関連付けられているユーザの識別子。ドメイン修飾名の場合は、ドメイン名 (たとえば、bea\jking の場合、ポスト URL では .../username/bea/jking として表現される)。 |
USER_ID_SUFFIX | (省略可能) ドメイン修飾名の場合は、ユーザ名。それ以外の場合は省略。 |
EVENT | ストーリー自体 (ストーリーにマークアップが含まれていない場合は CDATA エンベロープを省略可能)。 |
<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>ポストは多くの方法で実装できます (以下のサンプル コードを参照)。
HTML/JavaScript のサンプル コード
<script type="text/javascript"> // ゲートウェイ処理された URL を取得 - これにより、認証が行われ、クロスドメイン POST エラーが回避される 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/'/>"; // 送信者のフル ネームを取得 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; // XMLHttpRequest オブジェクトを取得 -- IE および IE 以外のブラウザで動作する必要がある if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else if (window.ActiveXObject) { xmlhttp = new ActiveXObject("Microsoft.XMLHttp"); } // コールバックを匿名関数として設定 xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4) { var str = '<div>'; // POST に成功し、201 (作成された) を取得する必要がある 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() { // この関数によって、ホームページへのハイパーリンクが設定されたユーザ名の表示が返される 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 のサンプル コード
以下に示す Java のサンプル コードでは、メッセージ「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(); // これは、どんな場合でも動作するため、注意が必要 client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); // チャレンジを待つことなくリクエストで認証情報を送信 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) { // 管理者アカウントから名前でゲスト ユーザにポスト 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 のサンプル コード
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()); } } } }