Container Module

Last modified by Vincent Massol on 2024/07/05 18:10

cogProvides an abstraction of a Container (request, response, session)
TypeJAR
Category
Developed by

XWiki Development Team

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1
Bundled With

XWiki Standard

Description

The notion of Container complements the notion of Environment by adding the notions of Request, Response and Session.

Similarly to the Environment, the idea is to allow using XWiki libraries that require a Container to be executed transparently in various environments such as a Servlet environment, a Portlet environment and more.

To get access to the Container implementation you'd use the following in your code:

@Inject
private Container container;

This will allow you to access the following API:

public interface Container
{
   /**
     * @deprecated starting with 3.5M1, use the notion of Environment instead
     */

   @Deprecated
    ApplicationContext getApplicationContext();

   /**
     * @deprecated starting with 3.5M1, use the notion of Environment instead
     */

   @Deprecated
   void setApplicationContext(ApplicationContext context);
   
    Request getRequest();
   void setRequest(Request request);
   void removeRequest();
   void pushRequest(Request request);
   void popRequest();

    Response getResponse();
   void setResponse(Response response);
   void removeResponse();
   void pushResponse(Response response);
   void popResponse();

    Session getSession();
   void setSession(Session session);
   void removeSession();
   void pushSession(Session session);
   void popSession();
}

Request and Response decorators

Request and Response decorators are interfaces that allows either a Request or a Response implementation to declare a new capability.

Redirect Response

Since 10.0, the RedirectResponse decorator, when applied to a Response allows it be marked as "redirectable", which means that the response can send redirects to the client.

A proper example is given in the implementation of Response for Servlets:

public class ServletResponse implements Response, RedirectResponse
{
   private HttpServletResponse httpServletResponse;

   [... other attributes and method implementations ...]

   @Override
   public void sendRedirect(String location) throws IOException
   {
       this.httpServletResponse.sendRedirect(location);
   }
}

When dealing with a Response object, it is then quite easy to check if it supports redirection :

Response myResponse = container.getResponse();

if (myResponse instanceof RedirectResponse) {
   ((RedirectResponse) myResponse).sendRedirect("http://xwiki.org");
}

Container Initialization

It's up to the environment in which the XWiki code runs to initialize the Container component. For the Servlet Container we provide, this is done in XWikiServletContextListener which is a Servlet Listener that needs to be registered in your web.xml as follows:

<listener>
 <listener-class>org.xwiki.container.servlet.XWikiServletContextListener</listener-class>
</listener>

Get Connected