You are looking at documentation for an older release. Not what you want? See the current release documentation.
If you ship servlets or filters as part of your portal extension, and these servlets/filters need to access specific resources of a portal during the process of the servlets or filters request, make sure that these servlets/filters are associated with the current portal container. The proper way to do that is making your servlet or filter extend the org.exoplatform.container.web.AbstractHttpServlet or org.exoplatform.container.web.AbstractFilter class respectively. Both AbstractHttpServlet and AbstractFilter have the method named getContainer(), which returns the current PortalContainer.
Declaring servlet/filter extension to get the current portal
Add the dependency to pom.xml
of your extension project.
<dependency>
<groupId>org.exoplatform.kernel</groupId>
<artifactId>exo.kernel.container</artifactId>
<scope>provided</scope>
</dependency>
Implement the servlet/filter.
Servlet
package org.sample.servlet;
...
public class SampleServlet extends AbstractHttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("[SampleServlet]: Current portal " + getContainer());
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
Filter
package org.sample.filter;
...
public class SampleFilter extends AbstractFilter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException {
System.out.println("[SampleFilter]: Current portal " + getContainer());
chain.doFilter(request, response);
}
@Override
public void destroy() {
}
}
Register the servlet/filter to web.xml
of your extension.
Servlet
<servlet>
<servlet-name>SampleServlet</servlet-name>
<servlet-class>org.sample.servlet.SampleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SampleServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
The servlet will not only properly initialize the current PortalContainer, but also set the current thread's context ClassLoader to servlets or filters. The ClassLoader looks for resources in associated web applications in the order specified by the dependencies configuration.
Filter
<filter>
<filter-name>SampleFilter</filter-name>
<filter-class>org.sample.filter.SampleFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SampleFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>