2

I'm using this as a reference to create a REST only configuration on Struts2:

https://cwiki.apache.org/confluence/display/WW/REST+Plugin

My current problem is with interceptors. I created a sample interceptor that should be executed before the action gets hit.

Here it is:

public class AuthInterceptor extends AbstractInterceptor implements Interceptor
{
    public String intercept(ActionInvocation invocation) throws Exception {

        System.out.println("intercepting AuthInterceptor...");

        return invocation.invoke();
    }

    public void destroy() {
        System.out.println("Destroying AuthInterceptor...");
    }
    public void init() {
        System.out.println("Initializing AuthInterceptor...");
    }
}

and here's my struts.xml file:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>

    <constant name="struts.enable.DynamicMethodInvocation" value="false"/>
    <constant name="struts.devMode" value="true"/>

    <constant name="struts.mapper.class" value="rest" />

    <constant name="struts.convention.action.suffix" value="Controller"/>
    <constant name="struts.convention.action.mapAllMatches" value="true"/>
    <constant name="struts.convention.default.parent.package" value="rest-default"/>
    <constant name="struts.convention.package.locators" value="controllers"/>

    <package name="default" extends="struts-default">
        <interceptors>
            <interceptor name="myInterceptor" class="com.company.interceptors.AuthInterceptor"/>
            <interceptor-stack name="myStack">
                <interceptor-ref name="myInterceptor"/>
                <interceptor-ref name="defaultStack"/>
            </interceptor-stack>
        </interceptors>
        <default-interceptor-ref name="myStack"/>
    </package>
</struts>

The logs (catalina.out) say that my interceptor got initialized, but never actually intercepted anything.

Initializing AuthInterceptor...

Apparently the default-interceptor-ref doesn't work well with the rest mapper class. Is this the case? or am I doing something wrong here?

Roman C
  • 49,761
  • 33
  • 66
  • 176
alexg
  • 902
  • 11
  • 37

2 Answers2

2

You need to change this

<constant name="struts.convention.default.parent.package" value="default"/>
<package name="default" extends="rest-default">

The convention plugin when creates action configuration inherits interceptors defined in the parent package. To define a parent package you can use @ParentPackage annotation or the constant above which defines a parent package for all actions.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • disregard my last comment. You gave the exact thing I need to change it to. I thought you meant I need to change that part to something else. Thank you :) – alexg Dec 27 '14 at 22:39
  • As soon as you are using convention plugin along with rest plugin, and even if convention plugin provides Zero XML config that you use with the rest plugin, the configuration still could be overridden with annotations. This is what I tried to explain in the last paragraph. – Roman C Dec 28 '14 at 11:03
1

The previous respuest is correct but for implement all REST functionality in the interceptor you must change your inteceptor stack to:

<interceptor-stack name="myStack">
    <interceptor-ref name="myInterceptor"/>
    <interceptor-ref name="restDefaultStack"/>
</interceptor-stack>