// 下階層用テンプレート
#topicpath

----
//ここにコンテンツを記述します。
#contents

RedmineにはデータにアクセスするためのREST APIが備わっています。
[[Rest api - Redmine>http://www.redmine.org/projects/redmine/wiki/Rest_api]] これです。
RedmineのWikiを解析したくって、これを使ってみました。

ちなみに上記のサイトによると、WikiへアクセスするREST APIはRedmine Ver.2.2からだそうですね。

**やってみる [#db66f4fa]
REST APIへアクセスするライブラリとして [[Jersey>https://jersey.java.net/]] をつかうことにします。
JerseyはJavaでRESTfullなAPIにアクセスするためのライブラリです。


**プロジェクトの作成 [#b479fe39]
いろいろ準備するのは大変なのでMavenでプロジェクトをつくることにします。pom.xmlはこんな感じ。
 <project xmlns="http://maven.apache.org/POM/4.0.0" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>nu.mine.kino</groupId>
   <artifactId>JerseyExamples</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <dependencies>
   	<dependency>
   		<groupId>com.sun.jersey</groupId>
   		<artifactId>jersey-core</artifactId>
   		<version>1.17.1</version>
   	</dependency>
   	<dependency>
   		<groupId>com.sun.jersey</groupId>
   		<artifactId>jersey-client</artifactId>
   		<version>1.17.1</version>
   	</dependency>
   	<dependency>
   		<groupId>junit</groupId>
   		<artifactId>junit</artifactId>
   		<version>4.11</version>
   	</dependency>
   </dependencies>
  </project>

**まずはIndexをとる [#w666968f]
Wikiページの一覧を取得するREST APIは http://www.redmine.org/projects/redmine/wiki/Rest_WikiPages
によると
 GET /projects/foo/wiki/index.xml

 GET /projects/foo/wiki/index.json
だそうです。Jerseyでアクセスしてみます。

    @Test
    public void wikiIndex() { 
        String url = redmineHost + "projects/" + projectKey
                + "/wiki/index.json?key=" + apiAccessKey;
 
        Client client = Client.create();
        WebResource resource = client.resource(url);
        ClientResponse getResponse = resource.get(ClientResponse.class);
        String responseStr = getResponse.getEntity(String.class);
        System.out.println(responseStr);
 
    }

結果は以下の通り。
 { "wiki_pages": [
        {
            "created_on": "2013-06-15T01:37:35Z", 
            "title": "Wiki", 
            "updated_on": "2013-06-15T10:37:35+09:00", 
            "version": 1
        }
    ]}
「Wiki」というタイトルのページが存在することが分かりました。RedmineのWikiのキーはタイトルなので、つぎはタイトルをつかって本体を取得します。


**タイトルを指定してWikiページを取得する [#oac658de]
Wikiページの一覧を取得するREST APIは http://www.redmine.org/projects/redmine/wiki/Rest_WikiPages
によると
 GET /projects/foo/wiki/[title].xml

 GET /projects/foo/wiki/[title].json
 
だそうです。Jerseyでアクセスしてみます。
    @Test
    public void wikiDetail() {
        String title = "Wiki";
        String url = redmineHost + "projects/" + projectKey + "/wiki/" + title
                + ".json?key=" + apiAccessKey;
 
        Client client = Client.create();
        WebResource resource = client.resource(url);
        ClientResponse getResponse = resource.get(ClientResponse.class);
        String responseStr = getResponse.getEntity(String.class);
        System.out.println(responseStr);
    }


結果は以下の通り。
 {"wiki_page": {
         "author": {
             "id": 3, 
             "name": "hogehoge"
         }, 
         "comments": "", 
         "created_on": "2013-06-15T01:37:35Z", 
         "text": "h1. Wiki", 
         "title": "Wiki", 
         "updated_on": "2013-06-15T01:37:35Z", 
         "version": 1
 }}

"text" のところがWikiの本文です。属性情報も含めて簡単にコンテンツを取得することが出来ました。





**関連リンク [#xd929836]








----
この記事は
#vote(おもしろかった,そうでもない)

#comment
#topicpath


SIZE(10){現在のアクセス:&counter;}


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS