LoginSignup
8
9

More than 5 years have passed since last update.

AlfrescoのREST APIを活用する

Last updated at Posted at 2015-07-07

AlfrescoのREST API

AlfrescoにはRESTのAPIが用意されていて、外部アプリケーション連携・管理作業の自動化が可能です。
例えば、ドキュメントの属性変更、ユーザーの登録・更新、タグやカテゴリーの登録がREST APIで実現できます。
http://docs.alfresco.com/community/pra/1/topics/pra-welcome-aara.html
http://docs.alfresco.com/community/pra/1/concepts/pra-resources.html

対象環境

Alfresco Community Edition 5.0d

REST API利用例

非常にシンプルな例として「タグの一括登録」をREST API経由で自動化するケースを題材に、外部アプリケーションの代わりにRubyスクリプトからAPIを利用してみます。

リファレンスを読むと説明がありますが、認証が必要なAPIを利用する場合、最初にログイン処理でAPIキーを取得し、それを引き回す必要があります。
http://docs.alfresco.com/5.0/references/RESTful-RepositoryLoginPost.html

create_tags.rb
require 'json'
require 'rest-client'

TARGET_HOST = 'http://xx.xx.xx.xx'
LOGIN_URL = TARGET_HOST + '/alfresco/s/api/login'
TAGS_URL = TARGET_HOST + '/alfresco/service/api/tags/workspace/SpacesStore?'

# 実行ユーザー
USER = 'admin'
PASSWORD = 'xxxxxx'

def login(user, password)
  begin
    response = RestClient.post LOGIN_URL, {'username' => user, 'password' => password}.to_json, :content_type => :json, :accept => :json
    @alf_ticket = 'alf_ticket=' + JSON.parse(response)["data"]["ticket"]
    puts @alf_ticket
  rescue => e
    puts e.response
  end
end

def create_tags(tags)
  begin
    tags.each {|t|
      RestClient.post TAGS_URL+ @alf_ticket, {'name' => t}.to_json, :content_type => :json, :accept => :json
    }
  rescue => e
    puts e.response
  end
end

# 登録するタグのリスト
# Alfrescoの仕様で、ASCII部分はdowncaseされてしまう
tags = [
  "NeverWinter",
  "Pathfinder",
  "マウスガード"
]


login(USER, PASSWORD)
create_tags(tags)

(TARGET_HOST, USER, PASSWORDは各自の環境に合わせてください)

実行すると、列挙したタグが登録されます。
(Alforescoの管理画面のタグから確認可能です)

こんな感じで、外部アプリケーションとの連携や管理作業の自動化が可能です。

8
9
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
9