Home of: [Atelier "FUJIGURUMA"] >> [PageMixer hosted by SourceForge.net]

SEE "For Readers of English Version",
or Japanese version of this page

Create 'ConsumerContext'

This section explains how to customize creating ConsumerContext with PageServlet.

You can skip this section if you have no interest in context customization(many cases are so).


Class diagram

Class diagram in this section is shown below:

Class diagram
Class diagram (click for large figure)

Classes which you must define are colored, and other are already defined.

Class names

In this tutorial, abbreviated class names are used. Complete names are shown below.

Classes of PageMixer framework

NotationFull name
ConsumerContext jp.ne.dti.lares.foozy.pagemixer.mixer.ConsumerContext
ContextFactory jp.ne.dti.lares.foozy.pagemixer.servlet.ContextFactory
DataProvider jp.ne.dti.lares.foozy.pagemixer.mixer.DataProvider
Filter jp.ne.dti.lares.foozy.pagemixer.mixer.Filter
ListDataProvider jp.ne.dti.lares.foozy.pagemixer.mixer.ListDataProvider
PageServlet jp.ne.dti.lares.foozy.pagemixer.servlet.PageServlet
RequestKey jp.ne.dti.lares.foozy.pagemixer.servlet.RequestKey
ServletConsumerContext jp.ne.dti.lares.foozy.pagemixer.servlet.ServletConsumerContext
ServletUtil jp.ne.dti.lares.foozy.pagemixer.servlet.ServletUtil
StrutsConsumerContext jp.ne.dti.lares.foozy.pagemixer.struts.StrutsConsumerContext

Tutorial specific classes

NotationFull name
CustomContextFactory pagemixer.servlet.CustomContextFactory

Purpose of customization

Customization of creating ConsumerContext allows you, per request, to:

Good example of the former is the co-operation of PageMixer and Struts, demostration of it is included in src/bin distribution of PageMixer.

For co-operation with Struts, PageMixer provides "StrutsConsumerContext" derived from "ServletConsumerContext", and the class to customize ConsumerContext creation.

Customization for the later may reduce cost to create complex data by preprocessing.

For example, assumption is that you want (to use the Filter) to process the list of Locales which are created from HTTP request header. It is not efficient to build the list of Locales up on-demand, because how many times processing may be occured depends on graphical design of HTML page.

Customization in the later way seems to satisfy your needs in almost all cases.

Override ContextFactory

PageMixer provides "ContextFactory" for customization of creating ConsumerContext.

In almost all cases, you can satisfy your needs by overriding create method of it, event though it has more overridable methods for complex purpose. Please see API document for detail about them.

public ConsumerContext create(HttpServlet servlet,
                              HttpServletRequest request,
                              HttpServletResponse response)
    throws IOException

Method signature of create()

Sample overriding is shown below.

// use context created by base class
ConsumerContext context = 
super.create(servlet, request, response);

// setup list of locale
if(null == context.getValue(KEY_LOCALE_ENTRY_PROVIDER)){
    ListDataProvider localeProvider = new ListDataProvider();
    Enumeration locales = request.getLocales();
        Object locale = locales.nextElement();

// setup "href" attribute value of "base" tag
if(null == context.getValue(KEY_BASE_HREF)){
    String baseHref =
    ServletUtil.getBaseHref(context, true, true);
    context.setValue(KEY_BASE_HREF, baseHref);

return context;

Customization of context creation

Above sample has two specific customizations.

set "DataProvider" of Locales:
sample code builds DataProvider up and stores it into context by key "KEY_LOCALE_ENTRY_PROVIDER".
set "href" attribute value for "base" tag:
sample code compose "href" attribute value by ServletUtil#getBaseHref() and stores it into context by key "KEY_BASE_HREF".

In this example, KEY_LOCALE_ENTRY_PROVIDER and KEY_BASE_HREF are instances of RequestKey, because scope of both values is limited in request.

NOTE: In above example, Former "true" of ServletUtil#getBaseHref() means "use HTTP HOST header value if specified".

Later "true" maens "use Servlet context path instead of requested URI". Later allows you to specify location of local resource relatively to Servlet context in response HTML page.


Parameters for base class

ContextFactory does not requires any initialization parameters.

Parameter to use custom class

To use your custom ContextFactory, you should specify "servlet.contextFactory" Servlet init-param in deployment descriptor as shown below.


Specify custom ContextFactory

To next section "Determine 'Locale'"