Search code examples
javaliferayservlet-filtersliferay-hook

Can a liferay hook xml contain 2 servlet-filter configurations?


I have created a hook in liferay with given xml

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

<hook> 
     <servlet-filter>
        <servlet-filter-name>Servlet Request Filter</servlet-filter-name>
        <servlet-filter-impl>package.SecurityFilters</servlet-filter-impl> 
    </servlet-filter>
    
    <servlet-filter-mapping>
        <servlet-filter-name>Servlet Request Filter</servlet-filter-name>       
        <url-pattern>/*</url-pattern>       
        <dispatcher>REQUEST</dispatcher> 
    </servlet-filter-mapping>
 
    <servlet-filter>
        <servlet-filter-name>Servlet Request DocumentDownloadFilter</servlet-filter-name>
        <servlet-filter-impl>package.DocumentDownloadFilter</servlet-filter-impl>
    </servlet-filter>

    <servlet-filter-mapping>
        <servlet-filter-name>Servlet Request DocumentDownloadFilter</servlet-filter-name>
        <url-pattern>/documents/*</url-pattern>
        <dispatcher>REQUEST</dispatcher> 
    </servlet-filter-mapping>
</hook>

After deploying this hook I am seeing this exception:

The content of element type "hook" must match "(portal-properties?,language-properties*,custom-jsp-dir?,custom-jsp-global?,indexer-post-processor*,service*,servlet-filter*,servlet-filter-mapping*,struts-action*)".

If I comment out one or the other servlet config the hook is deployed correctly and I am able to see in the logs that the filters are working. From the DTD I understand the I should be able to put many servlet-filter and servlet-filter-mapping, so I am not sure where did I made the mistake. DTD documentation

If an element name in DTD is followed by the star [*], this element can occur zero, once or several times.

Is it possible to create 2 servlet filters in one liferay hook?


Solution

  • Finaly I have managed to put 2 filters. The only problem was really my xml. I I was not aware that the order of elements in XML matteres. I was puting element in order:

    • servlet-filter
    • servlet-filter-mapping
    • servlet-filter
    • servlet-filter-mapping

    which was causing xml validation exception.

    Puting elements in order:

    • servlet-filter
    • servlet-filter
    • servlet-filter-mapping
    • servlet-filter-mapping

    solved the issue. Both filters are working. What helped me during the process was the online validator: https://www.truugo.com/xml_validator/ where I was able to track the problem easier than redeploying the app. Final xml looks like:

    <?xml version="1.0"?>
    <!DOCTYPE hook PUBLIC "-//Liferay//DTD Hook 7.2.0//EN" "http://www.liferay.com/dtd/liferay-hook_7_2_0.dtd">
    
    <hook> 
         <servlet-filter>
            <servlet-filter-name>Servlet Request Filter</servlet-filter-name>
            <servlet-filter-impl>package.SecurityFilters</servlet-filter-impl> 
        </servlet-filter>
        
        <servlet-filter>
            <servlet-filter-name>Servlet Request DocumentDownloadFilter</servlet-filter-name>
            <servlet-filter-impl>package.DocumentDownloadFilter</servlet-filter-impl>
        </servlet-filter>
        
        <servlet-filter-mapping>
            <servlet-filter-name>Servlet Request Filter</servlet-filter-name>       
            <url-pattern>/*</url-pattern>       
            <dispatcher>REQUEST</dispatcher> 
        </servlet-filter-mapping>
       
        <servlet-filter-mapping>
            <servlet-filter-name>Servlet Request DocumentDownloadFilter</servlet-filter-name>
            <url-pattern>/documents/*</url-pattern>
            <dispatcher>REQUEST</dispatcher> 
        </servlet-filter-mapping>
    </hook>