Interface PortletContext
PortletContext interface defines a portlet view of the portlet container. The
PortletContext also makes resources available to the portlet. Using the context, a portlet can access
the portlet log, and obtain URL references to resources.
There is one context per "portlet application" per Java Virtual Machine. (A "portlet application" is a collection
of portlets, servlets, and content installed under a specific subset of the server URL namespace, such as
/catalog. They are possibly installed via a .war file.) As a web application, a portlet
application also has a servlet context. The portlet context leverages most of its functionality from the servlet
context of the portlet application.
Attibutes stored in the context are global for all users and all components in the portlet
application.
In the case of a web application marked "distributed" in its deployment descriptor, there will be one context
instance for each virtual machine. In this situation, the context cannot be used as a location to share global
information (because the information is not truly global). Use an external resource, such as a database to achieve
sharing on a global scope.
- Version:
- $Revision: 5441 $
- Author:
- Julien Viet
-
Method Summary
Modifier and TypeMethodDescriptiongetAttribute(String name) Returns the portlet container attribute with the given name, or null if there is no attribute by that name.Returns anEnumerationcontaining the attribute names available within this portlet context, or an emtpyEnumerationif no attributes are available.getInitParameter(String name) Returns a String containing the value of the named context-wide initialization parameter, ornullif the parameter does not exist.Returns the names of the context initialization parameters as anEnumerationof String objects, or an empty Enumeration if the context has no initialization parameters.intReturns the major version of the Portlet API that this portlet container supports.getMimeType(String file) Returns the MIME type of the specified file, ornullif the MIME type is not known.intReturns the minor version of the Portlet API that this portlet container supports.getNamedDispatcher(String name) Returns aPortletRequestDispatcherobject that acts as a wrapper for the named servlet.Returns the name of this portlet application correponding to this PortletContext as specified in theweb.xmldeployment descriptor for this web application by thedisplay-nameelement.getRealPath(String path) Returns aStringcontaining the real path for a given virtual path.getRequestDispatcher(String path) Returns aPortletRequestDispatcherobject that acts as a wrapper for the resource located at the given path.getResource(String path) Returns a URL to the resource that is mapped to a specified path.getResourceAsStream(String path) Returns the resource located at the given path as an InputStream object.getResourcePaths(String path) Returns a directory-like listing of all the paths to resources within the web application longest sub-path of which matches the supplied path argument.Returns the name and version of the portlet container in which the portlet is running.voidWrites the specified message to a portlet log file, usually an event log.voidWrites an explanatory message and a stack trace for a given Throwable exception to the portlet log file.voidremoveAttribute(String name) Removes the attribute with the given name from the portlet context.voidsetAttribute(String name, Object object) Binds an object to a given attribute name in this portlet context.
-
Method Details
-
getServerInfo
String getServerInfo()Returns the name and version of the portlet container in which the portlet is running. The form of the returned string iscontainername/versionnumber.- Returns:
- the string containing at least name and version number
-
getRequestDispatcher
Returns aPortletRequestDispatcherobject that acts as a wrapper for the resource located at the given path. APortletRequestDispatcherobject can be used include the resource in a response. The resource can be dynamic or static.The pathname must begin with a slash (
/) and is interpreted as relative to the current context root.This method returns
nullif thePortletContextcannot return aPortletRequestDispatcherfor any reason.- Parameters:
path- aStringspecifying the pathname to the resource- Returns:
- a
PortletRequestDispatcherobject that acts as a wrapper for the resource at the specified path. - See Also:
-
getNamedDispatcher
Returns aPortletRequestDispatcherobject that acts as a wrapper for the named servlet.Servlets (and also JSP pages) may be given names via server administration or via a web application deployment descriptor.
This method returns
nullif thePortletContextcannot return aPortletRequestDispatcherfor any reason.- Parameters:
name- aStringspecifying the name of a servlet to be wrapped- Returns:
- a
PortletRequestDispatcherobject that acts as a wrapper for the named servlet - See Also:
-
getResourceAsStream
Returns the resource located at the given path as an InputStream object. The data in the InputStream can be of any type or length. The method returns null if no resource exists at the given path. In order to access protected resources the path has to be prefixed with/WEB-INF/(for example/WEB-INF/myportlet/myportlet.jsp). Otherwise, the direct path is used (for example/myportlet/myportlet.jsp).- Parameters:
path- the path to the resource- Returns:
- the input stream
-
getMajorVersion
int getMajorVersion()Returns the major version of the Portlet API that this portlet container supports.- Returns:
- the major version
- See Also:
-
getMinorVersion
int getMinorVersion()Returns the minor version of the Portlet API that this portlet container supports.- Returns:
- the minor version
- See Also:
-
getMimeType
Returns the MIME type of the specified file, ornullif the MIME type is not known. The MIME type is determined by the configuration of the portlet container and may be specified in a web application deployment descriptor. Common MIME types aretext/htmlandimage/gif.- Parameters:
file- aStringspecifying the name of a file- Returns:
- a
Stringspecifying the MIME type of the file
-
getRealPath
Returns aStringcontaining the real path for a given virtual path. For example, the path/index.htmlreturns the absolute file path of the portlet container file system.The real path returned will be in a form appropriate to the computer and operating system on which the portlet container is running, including the proper path separators. This method returns
nullif the portlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made available from a.wararchive).- Parameters:
path- aStringspecifying a virtual path- Returns:
- a
Stringspecifying the real path, or null if the transformation cannot be performed.
-
getResourcePaths
Returns a directory-like listing of all the paths to resources within the web application longest sub-path of which matches the supplied path argument. Paths indicating subdirectory paths end with a slash (/). The returned paths are all relative to the root of the web application and have a leading slash. For example, for a web application containing
/welcome.html
/catalog/index.html
/catalog/products.html
/catalog/offers/books.html
/catalog/offers/music.html
/customer/login.jsp
/WEB-INF/web.xml
/WEB-INF/classes/com.acme.OrderPortlet.class,
getResourcePaths("/")returns{"/welcome.html", "/catalog/", "/customer/", "/WEB-INF/"}
getResourcePaths("/catalog/")returns{"/catalog/index.html", "/catalog/products.html", "/catalog/offers/"}.- Parameters:
path- the partial path used to match the resources, which must start with a slash- Returns:
- a Set containing the directory listing, or
nullif there are no resources in the web application of which the path begins with the supplied path.
-
getResource
Returns a URL to the resource that is mapped to a specified path. The path must begin with a slash (/) and is interpreted as relative to the current context root.This method allows the portlet container to make a resource available to portlets from any source. Resources can be located on a local or remote file system, in a database, or in a
.warfile.The portlet container must implement the URL handlers and
URLConnectionobjects that are necessary to access the resource.This method returns
nullif no resource is mapped to the pathname.Some containers may allow writing to the URL returned by this method using the methods of the URL class.
The resource content is returned directly, so be aware that requesting a
.jsppage returns the JSP source code. Use aRequestDispatcherinstead to include results of an execution.This method has a different purpose than
java.lang.Class.getResource, which looks up resources based on a class loader. This method does not use class loaders.- Parameters:
path- aStringspecifying the path to the resource- Returns:
- the resource located at the named path, or
nullif there is no resource at that path - Throws:
MalformedURLException- if the pathname is not given in the correct form
-
getAttribute
Returns the portlet container attribute with the given name, or null if there is no attribute by that name. An attribute allows a portlet container to give the portlet additional information not already provided by this interface. A list of supported attributes can be retrieved usinggetAttributeNames.The attribute is returned as a
java.lang.Objector some subclass. Attribute names should follow the same convention as package names. The Java Portlet API specification reserves names matchingjava.*,javax.*, andsun.*.- Parameters:
name- aStringspecifying the name of the attribute- Returns:
- an
Objectcontaining the value of the attribute, ornullif no attribute exists matching the given name - Throws:
IllegalArgumentException- if name isnull.- See Also:
-
getAttributeNames
Enumeration getAttributeNames()Returns anEnumerationcontaining the attribute names available within this portlet context, or an emtpyEnumerationif no attributes are available. Use thegetAttribute(java.lang.String)method with an attribute name to get the value of an attribute.- Returns:
- an
Enumerationof attribute names - See Also:
-
getInitParameter
Returns a String containing the value of the named context-wide initialization parameter, ornullif the parameter does not exist. This method provides configuration information which may be useful for an entire "portlet application".- Parameters:
name- aStringcontaining the name of the requested parameter- Returns:
- a
Stringcontaining the value of the initialization parameter, ornullif the parameter does not exist. - Throws:
IllegalArgumentException- if name isnull.- See Also:
-
getInitParameterNames
Enumeration getInitParameterNames()Returns the names of the context initialization parameters as anEnumerationof String objects, or an empty Enumeration if the context has no initialization parameters.- Returns:
- an
EnumerationofStringobjects containing the names of the context initialization parameters - See Also:
-
log
Writes the specified message to a portlet log file, usually an event log. The name and type of the portlet log file is specific to the portlet container. This method mapps to theServletContext.logmethod. The portlet container may in addition log this message in a portlet container specific log file.- Parameters:
msg- aStringspecifying the message to be written to the log file
-
log
Writes an explanatory message and a stack trace for a given Throwable exception to the portlet log file. The name and type of the portlet log file is specific to the portlet container, usually an event log. This method is mapped to theServletContext.logmethod. The portlet container may in addition log this message in a portlet container specific log file.- Parameters:
msg- aStringthat describes the error or exceptionthrowable- theThrowableerror or exception
-
removeAttribute
Removes the attribute with the given name from the portlet context. After removal, subsequent calls togetAttribute(java.lang.String)to retrieve the attribute's value will returnnull.- Parameters:
name- aStringspecifying the name of the attribute to be removed- Throws:
IllegalArgumentException- if name isnull.
-
setAttribute
Binds an object to a given attribute name in this portlet context. If the name specified is already used for an attribute, this method removes the old attribute and binds the name to the new attribute. If a null value is passed, the effect is the same as callingremoveAttribute().Attribute names should follow the same convention as package names. The Java Portlet API specification reserves names matching
java.*,javax.*, andsun.*.- Parameters:
name- aStringspecifying the name of the attributeobject- anObjectrepresenting the attribute to be bound- Throws:
IllegalArgumentException- if name isnull.
-
getPortletContextName
String getPortletContextName()Returns the name of this portlet application correponding to this PortletContext as specified in theweb.xmldeployment descriptor for this web application by thedisplay-nameelement.- Returns:
- The name of the web application or null if no name has been declared in the deployment descriptor.
-