Atomパブリケーションプロトコルを認識し、第2部:Atomパブリケーションプロトコルを適用する


開始の準備
まず、Apache Abderaの現在のバージョンがインストールされていることを確認します.ソースコードはApache Subversionリポジトリからhttp://svn.apache.org/repos/asf/incubator/abdera/java/trunkダウンロードします.ソースコードを取得するには、subversionクライアントをインストールし、次のコマンドを使用します.
> svn co http://svn.apache.org/repos/asf/incubator/abdera/java/trunk

ソースコードのミラーをダウンロードすると、Ant version 1.6.5以降でAbderaを構築できます.
> cd trunk
> ant -f build/build.xml dist

構築が完了すると、コンパイルされたjarとdependencyが新しいdistディレクトリに配置されます.これらの例を実行するには、クラスパスに次のjarを追加する必要があります.
表1.実行例に必要なjar
Abdera (dist)
Dependency (dist/lib)
abdera.client.0.2.0-incubating-SNAPSHOT.jar
abdera.core.0.2.0-incubating-SNAPSHOT.jar
abdera.parser.0.2.0-incubating-SNAPSHOT.jar
abdera.protocol.0.2.0-incubating-SNAPSHOT.jar
axiom-api-1.0.jar
axiom-impl-1.0.jar
commons-codec-1.3.jar
commons-httpclient-3.0.1.jar
commons-logging-1.0.4.jar
geronimo-activation_1.0.2_spec-1.1.jar
geronimo-javamail_1.3.1_spec-1.1.jar
log4j-1.2.12.jar
stax-1.1.2-dev.jar
stax-api-1.0.jar
Weblog
IETF Atom Publishing Format and Protocolワークグループの定款によると、Atomパブリケーションプロトコル(Atom Publishing Protocol)の設計目標は主にweblogレコードのパブリッシュと管理に使用される.不思議なことに、Google、SixApart、Rollerなどの多くのbloggingソフトウェアプロバイダがプロトコルを初歩的にサポートし始めました.
GoogleのBlogger Beta
2006年8月初め、Googleは提供するネット日記サービスを待望のアップグレードすると発表した.サービスが新たに追加された特性の1つは、Atomパブリケーションプロトコルを使用して公告の作成と編集をサポートすることです.
公告の作成は簡単です.まず、新しいレコードが送信するAtomコレクションのURLを知る必要があります.Bloggerの場合、Atom抽出はチェーンblogのコンテンツに使用され、Atomパブリッシュプロトコルセットを構成して抽出リーダーと集約器で使用されます.したがって,集合のURIを決定するには,weblogホームヘッダの代替リンクを表示すればよい.
リスト1.Bloggerホームページのヘッダ

  
    testing
    
    
    
    
    
    
    ...

Atomが要請した住所を知ってからブログに記録を発表することができます.インベントリ2には、送信する投稿の例が表示されます.
リスト2.Blogger投稿
POST /feeds/7352231422284704069/posts/full HTTP/1.1
Host: beta.blogger.com
Content-Type: application/atom+xml
Content-Length: 349
Authorization: GoogleLogin auth={auth token}



  urn:uuid:1332534422684714363
  Posting to Blogger
  James
  2006-09-02T12:12:12Z
  
    

This is an example post to the new blogger beta


リスト2のリクエストにはいくつか注意が必要です.まず最も重要なのはAuthorization頭部です.GoogleでAtom Publishing Protocolをサポートするサービスでは、プライベートな認証方法を使用する必要があります.幸いなことに、実現するのは面倒ではありません.次に,Googleの実装要求要求にはContent-Longthヘッダが含まれる.重要ではないように見えますが、Content-Longthの使用が要求されることは、サーバにレコードを送信するクライアントが送信する前に要求のサイズを計算しなければならないため、要求の効率を低下させるという明らかな副作用をもたらします.最後に、発行されたレコードには、Bloggerが完全に無視するID、author、updated要素が含まれています.
ステップ1認証
Bloggerに投稿を送信する最初のステップはGoogleLogin認証方法で認証することです.GoogleLoginツールクラスを作成する必要があります.このツールには、GoogleアカウントにアクセスするユーザーIDとパスワード、およびアクセスするサービス名を入力する必要があります.この例のサービス名は「blogger」です.
リスト3.GoogleLogin認証メソッドは、認証サーバに簡単なHTTP POST要求を送信する必要がある
public final class GoogleLogin {
  ...
  public static String getAuth(
    Client client, 
    String service, 
    String id, 
    String pwd) {
    try {
      StringRequestEntity stringreq =
        new StringRequestEntity(getRequest(id,pwd,service));
      RequestOptions options = client.getDefaultRequestOptions();
      options.setContentType("application/x-www-form-urlencoded");
      ClientResponse response = client.post(URI, stringreq, options);
      String auth = read(response.getInputStream());
      response.release();
      return auth.split("
")[2].replaceAll("Auth=", "auth="); } catch (Exception e) {} return null; } ... }

適切な証明書でGoogleLoginを呼び出します.getAuth(...) メソッドは、Bloggerレコードの公開または編集の要求を検証するために使用できる認証タグを取得します.
第2歩レコードの作成
Bloggerへの投稿の第2のステップは、Apache AbderaのFeed Object Model APIで送信されたAtomレコードを作成することです.
リスト4.Bloggerレコードの作成
Abdera abdera = new Abdera();
Factory factory = abdera.getFactory();
Entry entry = factory.newEntry();
entry.setId(FOMHelper.generateUuid());
entry.setUpdated(new java.util.Date());
entry.addAuthor("James");
entry.setTitle("Posting to Blogger");
entry.setContentAsXhtml(
  "

This is an example post to the new blogger beta

");

ステップ3記録を発表する.
最後のステップは、Bloggerサーバにレコードを送信することです.
リスト5.レコードをBloggerに送信
Client client = new CommonsClient(abdera);
String auth = GoogleLogin.getAuth(
  client, "blogger", 
  "[email protected]", 
  "your.password");
    
RequestOptions options = client.getDefaultRequestOptions();
options.setAuthorization("GoogleLogin " + auth);
    
BaseRequestEntity bre = new BaseRequestEntity(entry, false);
    
Response response = client.post(
  "http://beta.blogger.com/feeds/7352231422284704069/posts/full", 
  bre, options);

if (response.getStatus() == 201) 
  System.out.println("Success!");
else 
  System.out.println("Failed!");

リスト5には、上記の各ステップが結合されていることがわかる.まず、認証タグが取得され、リクエストオプションとして設定されます.次に、AbderaのBaseRequestEntityツールクラスを記録のパッケージとして使用して、必要なContent-Longthヘッダの正確な計算を保証します.最後にBloggerのホームページヘッダに与えられたAtom抽出URLにレコードを送信する.発行に成功すると、サーバは201 HTTPステータスコードを返す.
図1.Bloggerで成功した投稿
认识 Atom 发布协议,第 2 部分: 应用 Atom 发布协议_第1张图片
レコードの編集と削除
Atom公開プロトコルによって、既存のレコードを編集および削除することもできます.
リスト6.既存のレコードを更新
String location = // get the URI of the entry to edit
    
Document entry_doc = client.get(location).getDocument();
entry = (Entry) entry_doc.getRoot().clone();
entry.setTitle("This is the changed title");
    
response = client.put(
  location, new BaseRequestEntity(entry,false), options);

リスト7.レコードの削除
String location = // get the URI of the entry to delete

response = client.delete(location, options);

Roller Weblogger
しかし、GoogleはAtomリリースプロトコルをサポートする唯一のブログプロバイダではありません.人気のオープンソースRoller Webloggerパッケージは、Sunのような多くの大手企業のブログネットワークのバックエンドです.http://blogs.sun.comWebサイトとIBMのイントラネットブログサービスは、アプリをサポートするためにアップグレードする準備をしています.1、2箇所の違いを除いて、Rollerに貼るのは基本的にBloggerと同じです.
リスト8.Roller投稿の例
POST /app/myblog/entries HTTP/1.1
Host: example.org
Content-Type: application/atom+xml
Authorization: Basic {user:password}


  urn:uuid:4BA4E6A3334F88813011571535258641
  Posting to Roller
  2006-09-01T23:32:05.880Z
  James
  
    

This is an example post to Roller


Roller AtomインタフェースとBloggerの主な違いは、RollerがAPP Service Documentを提供して利用可能なセット(blogホームページで使用される代替リンクではなく)を発見することです.また、Rollerは標準的なBasic認証スキームを使用しています.
リスト9.Rollerへの投稿
String start = "http://roller.example.org/app";
    
Abdera abdera = new Abdera();
Factory factory = abdera.getFactory();
Entry entry = factory.newEntry();
entry.setId(FOMHelper.generateUuid());
entry.setUpdated(new java.util.Date());
entry.addAuthor("James");
entry.setTitle("Posting to Roller");
entry.setContentAsHtml("

This is an example post to Roller

"); Client client = new CommonsClient(abdera); client.addCredentials( start, null, null, new UsernamePasswordCredentials( "username", "password")); // Get the collection URI from the service document Document service_doc = client.get(start).getDocument(); Service service = service_doc.getRoot(); Collection collection = service.getWorkspaces().get(0) .getCollections().get(0); String uri = collection.getHref().toString(); Response response = client.post(uri, entry); if (response.getStatus() == 201) System.out.println("Success!"); else System.out.println("Failed!");

メディアリソースの公開
Roller Atom Publishingが実装するもう一つの重要な特性は、任意のメディアリソースのアップロードをサポートすることです.たとえば、サーバ上の伝播者は、上記の例のレコードをアップロードするMP 3を表すリクエストエンティティに置き換えるだけです.
リスト10.Roller Weblogへのオーディオリソースの公開
FileInputStream fis = new FileInputStream(
  "mypodcast.mp3");
InputStreamRequestEntity re = 
  new InputStreamRequestEntity(fis, "audio/mp3");
Client client = // init the client    
String uri = // get the collection uri
      
RequestOptions options = client.getDefaultRequestOptions();
options.setHeader("Title", "mypodcast.mp3");
    
Response response = client.post(uri, re, options);
    
if (response.getStatus() == 201)
  System.out.println("Success!");
else
  System.out.println("Failed!");

また、Atomパブリッシュプロトコル操作を使用して、サーバにアップロードされたすべてのリソースを更新、削除、リストすることもできます.
カレンダ管理
Atom Publishingワークグループの重要な目標は、主にWeblog、Wiki、および類似のアプリケーションにコンテンツを公開することですが、他の多くのアプリケーションにも使用できるプロトコルを設計することです.現在、Atom Publishingのサポートはブログに属していない多くのアプリケーションにまで拡張されています.一例はGoogleのCalendarアプリです.
Google Calendar
Google Calendarは、ブラウザベースのインタフェースまたはAtomパブリッシュプロトコルを使用して、共通または個人のスケジュールを管理できるように管理されているカレンダー管理サービスです.
リスト11.Google CalendarのAtom投稿
POST /calendar/feeds/default/private/full HTTP/1.1
Host: www.google.com
Authorization: GoogleLogin auth={auth}
Content-Length: 834
Content-Type: application/atom+xml


  
  urn:uuid:421D94DE83D298A91211571550147641
  Tennis with Beth
  Meet for a quick lesson
  2006-09-01T23:56:54.772Z
  James
  
  
  
  

Google Calendarは、新しいBlogger Betaと同様に、同じバックエンドAtomに基づいてプロトコルインフラストラクチャを公開していることに注意してください.前のBloggerの例と同様に、CalendarもプライベートなGoogleLogin認証方法を使用しており、Content-Longthヘッダも要求されています.さらに、Atomレコードには拡張要素が必要です.
明細書12.Calendarレコードの作成
Abdera abdera = new Abdera();
Factory factory = abdera.getFactory();
Entry entry = factory.newEntry();
entry.setId(FOMHelper.generateUuid());
entry.setUpdated(new java.util.Date());
entry.addAuthor("James");
entry.setTitle("New Calendar Event");
entry.setContentAsXhtml("

A new calendar event

"); entry.addExtension(TRANSPARENCY).setAttributeValue( "value", "http://schemas.google.com/g/2005#event.opaque"); entry.addExtension(EVENTSTATUS).setAttributeValue( "value", "http://schemas.google.com/g/2005#event.confirmed"); entry.addExtension(WHERE).setAttributeValue( "valueString", "Rolling Lawn Courts"); Element el = entry.addExtension(WHEN); el.setAttributeValue("startTime", AtomDate.valueOf(new Date()).toString()); el.setAttributeValue("endTime", AtomDate.valueOf(new Date()).toString());

レコードを作成した後、残りはBloggerへの投稿とほぼ同じです.
リスト13.Google Calendarに公開
Client client = new CommonsClient(abdera);
String auth = GoogleLogin.getAuth(client, "cl", "username", "password"); 
RequestOptions options = client.getDefaultRequestOptions();
options.setAuthorization("GoogleLogin " + auth);
    
BaseRequestEntity bre = new BaseRequestEntity(entry, false);   
String uri = "http://www.google.com/calendar/feeds/default/private/full";    
Response response = client.post(uri, bre, options);
    
// calendar may return a 302 with a new URI to post to
if (response.getStatus() == 302) {
  uri = response.getLocation().toString();
  response = client.post(uri, bre, options);
}
    
if (response.getStatus() == 201) 
  System.out.println("Success!");
else
  System.out.println("Failed!");

別の方法
Googleカレンダー管理で採用されているAtomメソッドが直面している課題は、レコードにメーカー専用の拡張が必要であることです.IBM® Lotus® 研究者は、Atomが任意のメディアタイプをサポートするという特徴を利用して、iCalやxCalなどの既存の標準カレンダーフォーマットを使用する代替方法を提案した.
リスト14.AtomコレクションへのxCalendarイベントの送信
POST /calendar HTTP/1.1
Host: example.org
Content-Type: application/calendar+xml
Content-Length: nnnn



 
  [email protected]
  20060901T130000Z
  20060901T163000Z
  20060901T164000Z
  Tennis with Beth
  PUBLIC
 

コレクションに送信されると、サーバは、イベントを表すテーブルを作成します.
リスト15.Atomサーバが作成したカレンダーアイテム
HTTP/1.1 201 Created
Date: Fri, 7 Oct 2005 17:17:11 GMT
Content-Length: nnn
Content-Type: application/atom+xml; charset="utf-8"
Content-Location: http://example.org/calendar/1.atom
Location: http://example.org/calendar/1.atom



 Tennis with Beth
 urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a
 2006-09-01T18:30:02Z
 James
 Tennis with Beth
 
 
 
 

この方法では、Atomフォーマットを拡張する必要はなく、既存のカレンダーフォーマットを引き続き使用することができます.
ストレージデータ
また,Atomパブリケーションプロトコルを汎用的なフロントエンドAPIとして用いて各種データストレージサービスを実現しようとする試みもある.ドキュメントおよびコンテンツ管理サービス、ソフトウェアリポジトリ、データベース・サーバ、ワークフローおよびシナリオ・アプリケーションなどが含まれます.
例えば、Googleは最近、新しいGoogle Baseサービスbetaバージョンが様々なアプリケーションサービスに任意のデータを格納すると発表しました.
リスト16.Google Baseに公開
POST /base/feeds/items HTTP/1.1
Host: www.google.com
Authorization: GoogleLogin auth={auth}
X-Google-Key: key={key}
Content-Type: application/atom+xml
Content-Length: 632



  urn:uuid:1215d395-cfb1-4faa-b1cd-12da123e3a7a
  
  Acme 2000 series laptop
  
    
The fastest Acme Laptop yet...
products

柔軟な設計と任意のメディアリソースのサポートにより、Atomパブリケーションプロトコルは汎用Webデータ管理APIになることが期待されている.
終わりの言葉
この論文では、導入され、何千人ものユーザーによって使用されている実際のAtomパブリケーション実装について説明します.本明細書では、本明細書の例をサポートするために、オープンソースのApache Abderaプロジェクトを使用して、様々なブログ、カレンダー、およびデータ管理サービスとのインタラクションを例示する.このシリーズの次の記事では、Feed Object Model APIとXPath、XSLT変換、コンテンツフィルタリング、XMLデジタル署名のサポートなど、さまざまな特性のApache Abderaプロジェクトについて詳しく説明します.