javamavenliferayliferay-7wab

Compiling a OSGi bundle with registered hook for Liferay


I am trying to make configurable logging application for Liferay 7.4.3 with Apache Maven. I have no problem making the app configurable using this approach, when compiling the project as a OSGi bundle (using bnd-maven-plugin).

However I need to register the hook in the webapp/WEB-INF/liferay-hook.xml such as:

<?xml version="1.0"?>
<!DOCTYPE hook PUBLIC "-//Liferay//DTD Hook 7.1.0//EN" "http://www.liferay.com/dtd/liferay-hook_7_1_0.dtd">

<hook>
    <servlet-filter>
        <servlet-filter-name>My Hook</servlet-filter-name>
        <servlet-filter-impl>MyHook</servlet-filter-impl>
    </servlet-filter>
    <servlet-filter-mapping>
        <servlet-filter-name>My Hook</servlet-filter-name>
        <before-filter>SSO Open SSO Filter</before-filter>
        <url-pattern>/*</url-pattern>
    </servlet-filter-mapping>
</hook>

Is there a way to combine both hook and a configurable app into single application? i.e. Compiling a OSGi bundle in such a way that would contain hook registration in webapp/WEB-INF/liferay-hook.xml?

I have a working implementation of application with hook using that approach, however it is compiled as a WAR to include the WEB-INF folder.

I tried compiling the app as a WAB using both bnd-maven-plugin and maven-bundle-plugin to no avail.


Solution

  • There's no need for the 6.x style hooks. You can implement a servlet filter following this example:

    @Component(
            immediate = true,
            property = {
    
    // To figure out valid values for before-filter, look up Liferay's
    // WEB-INF/liferay-web.xml
    
                    "before-filter=Auto Login Filter",
                    "dispatcher=REQUEST",
    //              "dispatcher=FORWARD",
    //              "dispatcher=ASYNC",
    //              "dispatcher=ERROR",
    //              "dispatcher=INCLUDE",
                    "servlet-context-name=",
    
                    // Pick your own unique filter name! Make sure to pick a
                    // different one for every filter you write - see LPS-107575
    
                    "servlet-filter-name=Blade Servlet Filter",
                    "url-pattern=/web/*",
                    "url-pattern=/change/me"
            },
            service = Filter.class
    )
    public class BladeServletFilter extends BaseFilter {
    
        @Override
        protected Log getLog() {
            return _log;
        }
    
        @Override
        protected void processFilter(
                HttpServletRequest httpServletRequest,
                HttpServletResponse httpServletResponse, FilterChain filterChain)
            throws Exception {
    
            // implement your own logic here
            // determine if you shortcut, or continue in the filter chain
    
            httpServletResponse.addHeader(
                "X-Blade-Servlet-Filter", httpServletRequest.getRequestURI());
    
            processFilter(
                BladeServletFilter.class.getName(), httpServletRequest,
                httpServletResponse, filterChain);
        }
    
        private static final Log _log = LogFactoryUtil.getLog(
            BladeServletFilter.class);
    
    }