com.caucho.portal.generic
Class PortletServlet

java.lang.Object
  extended by javax.servlet.GenericServlet
      extended by javax.servlet.http.HttpServlet
          extended by com.caucho.portal.generic.PortletServlet
All Implemented Interfaces:
Window, java.io.Serializable, PortletConfig, Servlet, ServletConfig

public class PortletServlet
extends HttpServlet
implements Window, PortletConfig

A servlet that uses a class implementing javax.portlet.Portlet. This servlet supports the following configuration items. Items marked with a * can be set as init-param.

portal
an instance of Portal, default is an instance of GenericPortal.
portal-class*
a class name, an alternative to portal
portal-ref*
an attribute name to use for a lookup for a Portal as an application attribute. This is an alternative to using portal and is useful when more than one portlet servlet share a Portal.
portlet
an instance of Portlet, required
portlet-class*
a class name, an alternative to portlet
portlet-name*
a name for the value of PortletConfig.getPortletName(), the default is ServletConfig.getServletName()
portlet-preferences
Set the default preferences for the portlet (see "Portlet Preferences")
expiration-cache*
an integer, a cache time in seconds for the portlet, 0 (the default) disables and -1 means forever
private*
"true" or "false", if true the response is marked as private for the client, no cache will share the response with other users
supported-locales* A comma-separated list of locales of the form "en-us" or "en_us', the default is to support all locales.
resource-bundle*
the name of a resource bundle, the portlet may use the resource bundle with PortletConfig.getResourceBundle(Locale)
renderer
an instance of Renderer, used to add decorations like headers footers and controls to the portlet, the default is no renderer. See AbstractRenderer.
renderer-class*
a class name, an alternative to renderer setRenderer(Renderer).

Portlet Preferences

 
 <servlet servlet-name="portal" 
          servlet-class="com.caucho.portal.generic.PortletServlet"> 

   <init>

     ... 

     <portlet-preferences>
       <preference name="colour" value="green" read-only="true"/> 
         or
       <preference>
         <name>colour</name>
         <value>green</value>
         <read-only>true</read-only>
       </preference>
     </portlet-preferences>

     ... 
   </init>
  </servlet> 
 

See Also:
Serialized Form

Field Summary
protected static java.util.logging.Logger log
           
 
Constructor Summary
PortletServlet()
           
 
Method Summary
 void addInitParam(NameValuePair nameValuePair)
           
 void addInitParam(java.lang.String name, java.lang.String value)
          Add an init-param for the portlet.
 void destroy()
          Called when the servlet (and the application) shuts down.
protected  void doGet(HttpServletRequest req, HttpServletResponse res)
          Process a GET or HEAD request
protected  void doPost(HttpServletRequest req, HttpServletResponse res)
          Process a POST request
protected  void doRequest(HttpServletRequest httpRequest, HttpServletResponse httpResponse)
           
 int getBufferSize()
          Return a preferred buffer size, may be overridden by the portlet if it calls setBufferSize().
 java.util.ArrayList<Constraint> getConstraints()
          Return a list of Constraint that are applied before the processAction() and render() of the portlet are called, null if there are no Constraints.
 PortletPreferences getDefaultPreferences()
          Return the default preferences for the portlet, null if there are no default preferences.
 int getExpirationCache()
          Time in seconds, 0 disables caching and -1 means never expire.
 java.lang.String getInitParameter(java.lang.String name)
          Returns an initialization parameter.
 java.util.Enumeration getInitParameterNames()
          Enumerates all the initialization parameter.
 PortletConfig getPortletConfig()
           
 PortletContext getPortletContext()
          
 java.lang.String getPortletName()
          
 java.util.ArrayList<PreferencesValidator> getPreferencesValidators()
          Return the preferences validators for the portlet, null if there are no preference validators.
 Renderer getRenderer()
          
 java.util.ResourceBundle getResourceBundle(java.util.Locale locale)
          
 java.util.Map<java.lang.String,java.lang.String> getRoleRefMap()
          Return a map that map's role names used by the portlet to the role names recognized by the portal, null if the role names used by the portlet are to be used unchanged.
 java.util.Set<java.lang.String> getSupportedContentTypes(PortletMode portletMode)
          Return the content types supported for the mode, null if all content types are permitted.
 java.util.Set<java.util.Locale> getSupportedLocales()
          Return the Locales supported, null if all Locales are permitted.
 void handleConstraintFailure(RenderRequest request, RenderResponse response, ConstraintFailureEvent event)
          Optionally handle a constraint failure by sending some output to the client using the response or by hiding the window.
 void handleException(RenderRequest request, RenderResponse response, ExceptionEvent event)
          Optionally handle an exception by sending some output to the client using the response or by hiding the window.
 PortletMode handlePortletModeFailure(PortletRequest request, PortletMode notAllowed)
          This is called when a request arrives requesting a PortletMode that does not pass the isPortletModeAllowed() tests.
 WindowState handleWindowStateFailure(PortletRequest request, WindowState notAllowed)
          This is called when a request arrives requesting a WindowState that does not pass the isWindowStateAllowed() tests.
 void init(ServletConfig servletConfig)
          Initialize the servlet.
 boolean isPortletModeAllowed(PortletRequest request, PortletMode portletMode)
          Return true if the PortletMode is allowed.
 boolean isPrivate()
          Return true if the contents of the portlet are private.
 boolean isWindowStateAllowed(PortletRequest request, WindowState windowState)
          Return true if the WindowState is allowed.
protected  java.lang.Object newInstance(java.lang.Class targetClass, java.lang.String className)
           
 void setBufferSize(int bufferSize)
          Default is 0
 void setExpirationCache(int expirationCache)
          Enable caching of the response and set the expiration time in seconds.
 void setNamespace(java.lang.String namespace)
          The namespace is used to uniquely identify this usage of the portlet, the default is "" (the empty string).
 void setPortal(Portal portal)
          Default is an instance of GenericPortal.
 void setPortalClass(java.lang.String className)
          An alternative to setPortal(Portal), specify the class name of an object to instantiate
 void setPortalRef(java.lang.String attributeName)
          An alternative to setPortal(Portal), specify the name of an attribute to lookup in the ServletContext.
 void setPortlet(Portlet portlet)
          The portlet, required.
 void setPortletClass(java.lang.String className)
          An alternative to setPortlet(Portlet), specify the class name of an object to instantiate
 void setPortletName(java.lang.String portletName)
          The default is the value of ServletConfig.getServletName()
 void setPortletPreferences(GenericPortletPreferences defaultPreferences)
          Set the default preferences.
 void setPrivate(boolean isPrivate)
          If true then the response is private, indicating that it contains information that should only be provided to the current client, default is false.
 void setRenderer(Renderer renderer)
          A Renderer wraps decorations around the portlet, see AbstractRenderer.
 void setRendererClass(java.lang.String className)
          An alternative to setRenderer(Renderer), specify the class name of an object to instantiate
 void setResourceBundle(java.lang.String name)
          Set a resource bundle name, used to instantiate an instance of ResourceBundleFactory.
 void setResourceBundleFactory(ResourceBundleFactory factory)
           
 
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doHead, doOptions, doPut, doTrace, getLastModified, service, service
 
Methods inherited from class javax.servlet.GenericServlet
getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected static final java.util.logging.Logger log
Constructor Detail

PortletServlet

public PortletServlet()
Method Detail

setPortal

public void setPortal(Portal portal)
Default is an instance of GenericPortal.


setPortalClass

public void setPortalClass(java.lang.String className)
An alternative to setPortal(Portal), specify the class name of an object to instantiate


setPortalRef

public void setPortalRef(java.lang.String attributeName)
An alternative to setPortal(Portal), specify the name of an attribute to lookup in the ServletContext. This is useful for sharing a Portal amongst different servlets.


setNamespace

public void setNamespace(java.lang.String namespace)
The namespace is used to uniquely identify this usage of the portlet, the default is "" (the empty string). The namespace is important when using a portlet preferences store, and also has an effect on the encoding of parameters.


setPortlet

public void setPortlet(Portlet portlet)
The portlet, required. This method can be called in derived classes, or through the use of depndency injection on containers that support it, or indirectly using the init param `portlet-class'.


setPortletClass

public void setPortletClass(java.lang.String className)
An alternative to setPortlet(Portlet), specify the class name of an object to instantiate


setPortletName

public void setPortletName(java.lang.String portletName)
The default is the value of ServletConfig.getServletName()


addInitParam

public void addInitParam(java.lang.String name,
                         java.lang.String value)
Add an init-param for the portlet. If no init-param are added, the default behaviour is to expose the Servlet's init-param to the portlet. If this method is called at least once, the Servlet's init-param are not exposed to the portlet.


addInitParam

public void addInitParam(NameValuePair nameValuePair)

setPortletPreferences

public void setPortletPreferences(GenericPortletPreferences defaultPreferences)
Set the default preferences.


setExpirationCache

public void setExpirationCache(int expirationCache)
Enable caching of the response and set the expiration time in seconds. 0 (the default) means do not cache, -1 means unlimited cach time, any other number is the number of seconds for which the response can be cached. Can also be specified with init-param `expiration-cache'.


setPrivate

public void setPrivate(boolean isPrivate)
If true then the response is private, indicating that it contains information that should only be provided to the current client, default is false. Setting this to true has an effect on caching, if true then a cached value cannot be shared amongst different users and the effectiveness of caching is greatly reduced. Can also be specified with init-param `private'.


setResourceBundle

public void setResourceBundle(java.lang.String name)
Set a resource bundle name, used to instantiate an instance of ResourceBundleFactory.


setResourceBundleFactory

public void setResourceBundleFactory(ResourceBundleFactory factory)

setRenderer

public void setRenderer(Renderer renderer)
A Renderer wraps decorations around the portlet, see AbstractRenderer.


setRendererClass

public void setRendererClass(java.lang.String className)
An alternative to setRenderer(Renderer), specify the class name of an object to instantiate


setBufferSize

public void setBufferSize(int bufferSize)
Default is 0


init

public void init(ServletConfig servletConfig)
          throws ServletException
Description copied from class: GenericServlet
Initialize the servlet. Most servlets should override the zero parameter init() instead.

Specified by:
init in interface Servlet
Overrides:
init in class GenericServlet
Parameters:
servletConfig - the servlet's configuration
Throws:
ServletException

newInstance

protected java.lang.Object newInstance(java.lang.Class targetClass,
                                       java.lang.String className)
                                throws java.lang.IllegalArgumentException
Throws:
java.lang.IllegalArgumentException

getPortletConfig

public PortletConfig getPortletConfig()
Specified by:
getPortletConfig in interface Window

getInitParameter

public java.lang.String getInitParameter(java.lang.String name)
Description copied from class: GenericServlet
Returns an initialization parameter. Initialization parameters are defined in the servlet configuration (in resin.conf) as follows:

 <servlet servlet-name='myservlet'
          servlet-class='test.MyServlet'>
   <init-param param1='value1'/>
   <init-param param2='value2'/>
 </servlet>
 

Specified by:
getInitParameter in interface PortletConfig
Specified by:
getInitParameter in interface ServletConfig
Overrides:
getInitParameter in class GenericServlet
Parameters:
name - of the parameter
Returns:
the init parameter value

getInitParameterNames

public java.util.Enumeration getInitParameterNames()
Description copied from class: GenericServlet
Enumerates all the initialization parameter.

Specified by:
getInitParameterNames in interface PortletConfig
Specified by:
getInitParameterNames in interface ServletConfig
Overrides:
getInitParameterNames in class GenericServlet

getExpirationCache

public int getExpirationCache()
Time in seconds, 0 disables caching and -1 means never expire.

Specified by:
getExpirationCache in interface Window

isPrivate

public boolean isPrivate()
Return true if the contents of the portlet are private. The portlet specification states that "cached content must not be shared across different user clients displaying the same portlet". If a portal implementation supports caching, then a return value of false will allow the portal to share the cached content across different user clients.

Specified by:
isPrivate in interface Window

isWindowStateAllowed

public boolean isWindowStateAllowed(PortletRequest request,
                                    WindowState windowState)
Return true if the WindowState is allowed. portletRequest.getResponseContentType() can be used if the allowed portlet modes depends on the mime type of the response. This implementation returns true.

Specified by:
isWindowStateAllowed in interface Window

isPortletModeAllowed

public boolean isPortletModeAllowed(PortletRequest request,
                                    PortletMode portletMode)
Return true if the PortletMode is allowed. portletRequest.getResponseContentType() can be used if the allowed portlet modes depends on the mime type of the response. This implementation returns true.

Specified by:
isPortletModeAllowed in interface Window

getSupportedContentTypes

public java.util.Set<java.lang.String> getSupportedContentTypes(PortletMode portletMode)
Return the content types supported for the mode, null if all content types are permitted. This implementation returns null, which means that all content types are supported.

Specified by:
getSupportedContentTypes in interface Window

getSupportedLocales

public java.util.Set<java.util.Locale> getSupportedLocales()
Return the Locales supported, null if all Locales are permitted. This implementation returns null, which means that all locales are supported.

Specified by:
getSupportedLocales in interface Window

getDefaultPreferences

public PortletPreferences getDefaultPreferences()
Return the default preferences for the portlet, null if there are no default preferences. This implementation returns null.

Specified by:
getDefaultPreferences in interface Window

getPreferencesValidators

public java.util.ArrayList<PreferencesValidator> getPreferencesValidators()
Return the preferences validators for the portlet, null if there are no preference validators.

Specified by:
getPreferencesValidators in interface Window

getRoleRefMap

public java.util.Map<java.lang.String,java.lang.String> getRoleRefMap()
Return a map that map's role names used by the portlet to the role names recognized by the portal, null if the role names used by the portlet are to be used unchanged. This implementation returns null.

Specified by:
getRoleRefMap in interface Window

getConstraints

public java.util.ArrayList<Constraint> getConstraints()
Return a list of Constraint that are applied before the processAction() and render() of the portlet are called, null if there are no Constraints. This implementation returns null.

Specified by:
getConstraints in interface Window

getRenderer

public Renderer getRenderer()

Specified by:
getRenderer in interface Window

handlePortletModeFailure

public PortletMode handlePortletModeFailure(PortletRequest request,
                                            PortletMode notAllowed)
This is called when a request arrives requesting a PortletMode that does not pass the isPortletModeAllowed() tests. It gives the Window an opportunity to return a PortletMode that can be used. If the returned mode is also not allowed, a PortletModeException occurs. Implementations can use handleException to handle PortletModeException. This implementation returns PortletMode.VIEW.

Specified by:
handlePortletModeFailure in interface Window
Returns:
a new PortletMode to try, or null to cause an exception.

handleWindowStateFailure

public WindowState handleWindowStateFailure(PortletRequest request,
                                            WindowState notAllowed)
This is called when a request arrives requesting a WindowState that does not pass the isWindowStateAllowed() tests. It gives the Window an opportunity to return a WindowState that can be used. If the returned window state is also not allowed, a WindowStateException occurs. Implementations can use handleException to handle WindowStateException. This implementation returns WindowState.NORMAL.

Specified by:
handleWindowStateFailure in interface Window
Returns:
a new WindowState to try, or null to cause an exception,

handleConstraintFailure

public void handleConstraintFailure(RenderRequest request,
                                    RenderResponse response,
                                    ConstraintFailureEvent event)
Optionally handle a constraint failure by sending some output to the client using the response or by hiding the window. When a Portlet fails a Constriant during processAction(), it is stored until the render stage, and handleConstriantFailure() is called instead of render(). When a Portlet fails a Constraint during render(), handleConstriantFailure() is immediately called. If the implementation of this method does nothing, then the response of this window is reset() and the failure propagates to the parent window. If there is no parent window, PortletConnection#handleConstraintFailure() is called. Implementations of this method can change that behaviour by using the event.setHandled(boolean hideWindow) callback method. See Window.handleException(javax.portlet.RenderRequest, javax.portlet.RenderResponse, com.caucho.portal.generic.ExceptionEvent) for more information. This implementation does nothing.

Specified by:
handleConstraintFailure in interface Window

handleException

public void handleException(RenderRequest request,
                            RenderResponse response,
                            ExceptionEvent event)
Optionally handle an exception by sending some output to the client using the response or by hiding the window. When a Portlet throws an Exception during processAction(), it is stored until the render stage, and handleException() is called instead of render(). When a Portlet throws an exception during render(), the handleException() is immediately called. If the implementation of this method does nothing, then the repsonse of this window is reset() and the exception propagates to the parent window. If there is no parent window, PortletConnection#handleException() is called. Implementations of this method can change that behaviour by using the event.setHandled(boolean hideWindow) callback method.

exceptionEvent.setHandled(false)

If handleException calls exceptionEvent.setHandled(false) then the content that the portlet has written is kept, and the request continues. Implementations use this when they have written some contents to the portlet that should be visible to the user.
 public void handleException(RenderRequest request, RenderResponse response, ExceptionEvent event)
   throws PortletException, IOException 
 {
   if (event.getException() instanceof UnavailableException) {
     PrintWriter out = response.getWriter();
     response.setContentType("text/html"); 
     out.println("This service is currently unavailable."); 
     event.setHandled(false);
   } 
 } 
 
If an implementation is handling an exception that can occur after the Portlet has written some content already, it should call response.reset() before it writes anything to the response.
 public void handleException(RenderRequest request, RenderResponse response, ExceptionEvent event)
   throws PortletException, IOException 
 {
   response.reset(); 
   response.setContentType("text/html"); 
   PrintWriter out = response.getWriter();
   printFancyExceptionMessage(event.getException());
   event.setHandled(false);
 } 
 

exceptionEvent.setHandled(true)

If handleException calls exceptionEvent.setHandled(true) then the output is reset(), including any output from a Renderer, and the request continues. The effect of this call is to make the window invisible, no content for this window is shown but other windows on the page will be shown. Implementations use this when they have dealt with the exception in some way and wish to hide the exception from the user.
 public void handleException(ExceptionEvent event)
   throws PortletException, IOException 
 {
   log.log(Level.WARNING, ex.toString(), ex);
   event.setHandled(true); 
 } 
 
This implementation does nothing.

Specified by:
handleException in interface Window

getPortletName

public java.lang.String getPortletName()

Specified by:
getPortletName in interface PortletConfig

getPortletContext

public PortletContext getPortletContext()

Specified by:
getPortletContext in interface PortletConfig

getResourceBundle

public java.util.ResourceBundle getResourceBundle(java.util.Locale locale)

Specified by:
getResourceBundle in interface PortletConfig

getBufferSize

public int getBufferSize()
Return a preferred buffer size, may be overridden by the portlet if it calls setBufferSize(). A value of 0 disables buffering for the portlet. A value of -1 allows the portal to use a default buffer size.

Specified by:
getBufferSize in interface Window

doGet

protected void doGet(HttpServletRequest req,
                     HttpServletResponse res)
              throws ServletException,
                     java.io.IOException
Description copied from class: HttpServlet
Process a GET or HEAD request

Overrides:
doGet in class HttpServlet
Parameters:
req - the client request
res - response to the client
Throws:
ServletException
java.io.IOException

doPost

protected void doPost(HttpServletRequest req,
                      HttpServletResponse res)
               throws ServletException,
                      java.io.IOException
Description copied from class: HttpServlet
Process a POST request

Overrides:
doPost in class HttpServlet
Parameters:
req - the client request
res - response to the client
Throws:
ServletException
java.io.IOException

doRequest

protected void doRequest(HttpServletRequest httpRequest,
                         HttpServletResponse httpResponse)
                  throws ServletException,
                         java.io.IOException
Throws:
ServletException
java.io.IOException

destroy

public void destroy()
Description copied from class: GenericServlet
Called when the servlet (and the application) shuts down.

Specified by:
destroy in interface Servlet
Overrides:
destroy in class GenericServlet