How do I use Confluence's servlet-filter module?

Brian Alston July 31, 2015

I have recently become aware of Confluence's servlet-filter module. However, after trying to get it to work, I have run into a dead end. I am getting my information here:

https://developer.atlassian.com/confdev/confluence-plugin-guide/confluence-plugin-module-types/servlet-filter-module

Here is what I tried. I annotated my atlassian-plugin.xml file like so:

<servlet-filter name="My Test Filter" key="OURAPP" class="com.test.filters.HelloWorldFilter" location="after-encoding" weight="100">
    <description>Forwards you to a test "Hello, world!" page.</description>
    <url-pattern>/helloworld</url-pattern>
    <dispatcher>REQUEST</dispatcher
</servlet-filter>

 Then, I created a standard Java servlet (extends HttpServlet, etc.) at com/test/filters called HelloWorldFilter.java. In the "doGet()" method, I simply have a System.out.println() line that reads "IN THE FILTER". However, when I go to the following URLs, I always get the "Page Not Found" error page:

    http://127.0.0.1:8090/helloworld

    http://127.0.0.1:8090/OURAPP/helloworld

    http://127.0.0.1:8090/plugins/OURAPP/helloworld

    http://127.0.0.1:8090/rest/helloworld

    http://127.0.0.1:8090/rest/OURAPP/helloworld

    http://127.0.0.1:8090/OURAPP/rest/helloworld

 

We have a custom Confluence plugin, lets call it OURAPP, that is serving up data not only through the browser GUI, but also through Confluence's REST capabilities. I am trying to create a filter that manages valid and invalid requests alike. We can access the REST at:

    http://127.0.0.1:8090/rest/reststuff/v1/some_function_here

Like I said in the beginning, I have run into a dead end here, and cannot figure out how to get this to work. Can anybody offer up any suggestions or example code of how to get this working?

Thanks, and have a great day. smile

1 answer

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Midori
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 31, 2015

Brian, I think you are mixing servlets and servlet filters!

In your example, you created a filter that maps to the "/helloworld" pattern. Fine, but there is no servlet that would respond to that URL, that's why you get a 404 (page not found). You should either create both a servlet-filter and a servlet (both declared in the plugin descriptor) and filter the same URL with the servlet-filter and handle the same URL by the servlet.

Ultimately, this is not what you want.

If you know the REST URL of your app, why don't you use that URL in the servlet-filter's configuration? (Tip: you can use wildcards.)

Brian Alston July 31, 2015

@Ferenc Kiss [Midori] What we have are a bunch of REST endpoints (e.g., /getalleuropeancars, /getalljapanesecars, /getallamericancars). We want to send an error message should anybody hit the service with PUT, DELETE, etc. We don't want every class to have the same error checking. Is a servlet filter what we are looking for? It seems we are talking apples and oranges (servlet filters and REST APIs).

TAGS
AUG Leaders

Atlassian Community Events