tohokuaikiのチラシの裏

技術的ネタとか。

ConfluenceのREST APIを作ってみる - 実践編1(atlas-create-confluence-plugin-moduleコマンドを実行)

atlassian-plugin.xml

1か所だけになる。
あとは、Classのアノテーションを使っていくことで対応できる。サンプル。

    <rest key="conf2dita-get-topictype" path="/conf2dita" version="1.0">
        <description>get dita topictype assigned to this page</description>
    </rest>

URLのマッピングについて

ConfluenceのSDKの場合は、
[http://localhost:1990/confluence/rest/api-name/api-version/resource-name
となる。

このなかで、それぞれPlugin側で指定しなければいけないのは先ほどのatlassian-plugin.xml内の

api-name rest要素のpath属性値
api-version rest要素のversion属性値
resource-name Restを実装するJavaのクラスにおけるアノテーションで指定

となる。

pom.xmlとatlasian-plugin.xmlへの追加

チュートリアルの通りやってみる。
Developing a REST Service Plugin - Atlassian Developers

  1. 作っているPluginのプロジェクトディレクトリまで移動
  2. atlas-create-confluence-plugin-moduleコマンドを実行
  3. ウィザードにこたえていく

こんな感じになる。。

C:\Users\Takashi\ConfluencePluginDev>cd conf2dita
C:\Users\Takashi\ConfluencePluginDev\conf2dita>atlas-create-confluence-plugin-module
Executing: "C:\Users\Takashi\atlassian-plugin-sdk\apache-maven\bin\mvn.bat" com.atlassian.maven.plugins:maven-confluence-plugin:4.2.10:create-plugin-module -gs
C:\Users\Takashi\atlassian-plugin-sdk\apache-maven/conf/settings.xml
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building conf2dita
[INFO]    task-segment: [com.atlassian.maven.plugins:maven-confluence-plugin:4.2.10:create-plugin-module]
[INFO] ------------------------------------------------------------------------
[INFO] [confluence:create-plugin-module]
Choose Plugin Module:
1:  Blueprint
2:  Component Import
3:  Component
4:  Keyboard Shortcut
5:  Downloadable Plugin Resource
6:  Gadget Plugin Module
7:  Licensing API Support
8:  Module Type
9:  REST Plugin Module
10: Servlet Context Listener
11: Servlet Context Parameter
12: Servlet Filter
13: Servlet
14: Template Context Item
15: Web Item
16: Web Panel
17: Web Panel Renderer
18: Web Resource
19: Web Resource Transformer
20: Web Section
Choose a number (1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20): 9
[INFO] Google Analytics Tracking is enabled to collect AMPS usage statistics.
[INFO] Although no personal information is sent, you may disable tracking by adding <allowGoogleTracking>false</allowGoogleTracking> to the amps plugin configuration in your pom.xml
[INFO] Sending event to Google Analytics: AMPS:confluence - Create Plugin Module
 - confluence:REST Plugin Module
Enter New Classname [MyRestResource]: FooBarTestResource
Enter Package Name [jp.junoe.confluence.plugins.rest]: jp.junoe.confluence.plugins.conf2dita.rest
Enter REST Path [/foobartestresource]: /foofoofoo
Enter Version [1.0]:
Show Advanced Setup? (Y/y/N/n) [N]: y
Module Name [Foo Bar Test Resource]:
Module Key [foo-bar-test-resource]:
Module Description [The Foo Bar Test Resource Plugin]:
i18n Name Key [foo-bar-test-resource.name]:
i18n Description Key [foo-bar-test-resource.description]:
Add Package To Scan? (Y/y/N/n) [N]:
Add Dispatcher? (Y/y/N/n) [N]:
[INFO] Adding the following items to the project:
[INFO]   [class: it.jp.junoe.confluence.plugins.conf2dita.rest.FooBarTestResourceFuncTest]
[INFO]   [class: jp.junoe.confluence.plugins.conf2dita.rest.FooBarTestResourceModel]
[INFO]   [class: jp.junoe.confluence.plugins.conf2dita.rest.FooBarTestResource]
[INFO]   [class: ut.jp.junoe.confluence.plugins.conf2dita.rest.FooBarTestResourceTest]
[INFO]   [dependency: com.atlassian.plugins.rest:atlassian-rest-common]
[INFO]   [dependency: com.atlassian.sal:sal-api]
[INFO]   [dependency: javax.servlet:servlet-api]
[INFO]   [dependency: javax.ws.rs:jsr311-api]
[INFO]   [dependency: javax.xml.bind:jaxb-api]
[INFO]   [dependency: org.apache.wink:wink-client]
[INFO]   [dependency: org.mockito:mockito-all]
[INFO]   [module: rest]
[INFO]   i18n strings: 2
Add Another Plugin Module? (Y/y/N/n) [N]: n
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 minute 34 seconds
[INFO] Finished at: Tue Jan 06 21:45:22 JST 2015
[INFO] Final Memory: 73M/325M
[INFO] ------------------------------------------------------------------------

上記のコマンド結果

pom.xml
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.atlassian.plugins.rest</groupId>
            <artifactId>atlassian-rest-common</artifactId>
            <version>1.0.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.wink</groupId>
            <artifactId>wink-client</artifactId>
            <version>1.1.3-incubating</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.8.5</version>
            <scope>test</scope>
        </dependency>

が追加

atlassian-plugin.xml
	<rest name="Foo Bar Test Resource" i18n-name-key="foo-bar-test-resource.name" key="foo-bar-test-resource" path="/foofoofoo" version="1.0">
		<description key="foo-bar-test-resource.description">The Foo Bar Test Resource Plugin</description>
	</rest>

が追加される。

Javaファイル

2つのファイルが追加された
src/main/java/jp/junoe/confluence/plugins/conf2dita/rest/FooBarTestResource.java

package jp.junoe.confluence.plugins.conf2dita.rest;

import com.atlassian.plugins.rest.common.security.AnonymousAllowed;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

/**
 * A resource of message.
 */
@Path("/message")
public class FooBarTestResource {

    @GET
    @AnonymousAllowed
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public Response getMessage()
    {
       return Response.ok(new FooBarTestResourceModel("Hello World")).build();
    }

src/main/java/jp/junoe/confluence/plugins/conf2dita/rest/FooBarTestResourceModel.java

package jp.junoe.confluence.plugins.conf2dita.rest;

import javax.xml.bind.annotation.*;
@XmlRootElement(name = "message")
@XmlAccessorType(XmlAccessType.FIELD)
public class FooBarTestResourceModel {

    @XmlElement(name = "value")
    private String message;

    public FooBarTestResourceModel() {
    }

    public FooBarTestResourceModel(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
ちょっと後悔・・

atlas-create-confluence-plugin-moduleコマンドを実行すると、pom.xmlとatlassian-plugin.xmlを書き換えちゃうのでインデントとか自分好みじゃなくなってしまった。。。

コンパイルして実行

pom.xmlを変更しつつJavaクラスをウィザードで加えたので、加えたファイルの@AnonymousAllowedアノテーションが使えないといわれてしまった。NetBeansの「プロジェクトを解決」で再ビルドして解決。

その後、
http://localhost:1990/confluence/rest/foofoofoo/1.0/message
にアクセスすると、

<message>
    <value>Hello World</value>
</message>

と表示を確認。


@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
となっているので、
http://localhost:1990/confluence/rest/foofoofoo/1.0/message.xml
だとXMLで返ってきて、
http://localhost:1990/confluence/rest/foofoofoo/1.0/message.json
だとJSONで返ってくる。
デフォルトはXML(なんで?)

パッケージの位置とか

特に固定では無いみたい。
パッケージ名を変えても、コンパイル時に探ってくれるようでお好きな感じでどうぞ。

更にいうと、atlassian-plugin.xmlを2つ書くとどちらもpathが有効になってしまう。そのが対象とするClassを指定してないから当然だよねっていう・・・。かなりアバウトな感じ。