tohokuaikiのチラシの裏

技術的ネタとか。

Confluenceのプラグインを作ってみるメモ(5) Servletを使えるようにする

目標は、
http://confluence-server-url/plugins/servlet/exampleservlet
みたいなURLを実現することです。

pom.xmldependency を追加

servletをつかうので、それをpom.xmlに明記する。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" ... >
    <dependencies>
        <!-- ...ここにいろいろ.... -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

atlassian-plugin.xmlServletの記述

プラグインservlet要素を追加。

<atlassian-plugin key="${project.groupId}.${project.artifactId}" name="${project.name}" plugins-version="2">
    <servlet name="Hello World Servlet" key="exampleservlet" class="jp.junoe.confluence.plugins.exampleservlet.ExampleServlet">
        <description>Says Hello World, Australia or your name.</description>
        <url-pattern>/exampleservlet</url-pattern>
    </servlet>

これで、このプラグイン
URL: http://confluence-server-url/plugins/servlet/exampleservlet
にアクセスされた際に
jp.junoe.confluence.plugins.exampleservlet.ExampleServlet
を実行する。

ポイントとなるのは、url-patternとservlet:classだけ。あと、keyはこのプラグイン内でユニークな値にすること。

その他の属性とかは
Servlet Plugin Module - Plugin Framework - Atlassian Developer Documentation
を参照。

ExampleServlet.javaの記述

上述のatlassian-plugin.xmlの記述だと
プラグインDIR/src/main/java/jp/junoe/confluence/plugins/ExampleServlet.java
に当該のJavaを記述する。

単純にブラウザに「Hello World」を出すだけの場合

package jp.junoe.confluence.plugins.exampleservlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class ExampleServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("Hello World");
        out.close();
    }
}

とか。

ConfigureのURLを指定する

ServletでConfluence内にURLを作れるようになったので、活用する例。

Adding a Configuration UI for your Plugin - Confluence Development - Atlassian Developer Documentationを参考に、Plugin管理画面でConfigureを出す。

こんな感じに「Configure」ってリンクが出るだけですが。

atlassian-plugin.xmlのplugin-infoに param:name="configure.url"を入れるだけ。

<atlassian-plugin key="${project.groupId}.${project.artifactId}" name="${project.name}" plugins-version="2">
    <plugin-info>
        <param name="configure.url">/plugins/servlet/exampleservlet</param>

URLは先ほどの例でも分かるように/plugins/servlet配下になるのでそこから記述する。