Class SlingServletContext
- java.lang.Object
-
- org.apache.sling.engine.impl.helper.SlingServletContext
-
- All Implemented Interfaces:
javax.servlet.ServletContext
public class SlingServletContext extends Object implements javax.servlet.ServletContext
TheSlingServletContext
class is theServletContext
which is registered as a service usable by servlets and helpers inside Sling. Most methods just call into the servlet context in which theSlingMainServlet
is running.- MIME Type Mapping
- Just forwards to the servlet context of the
SlingMainServlet
for MIME type mapping. - Resources
- This class provides access to the resources in the web application by
means of the respective resource accessor methods. These are not the same
resources as available through the
ResourceResolver
. - Request Dispatcher
- The
getRequestDispatcher(String)
method returns aSlingRequestDispatcher
which may dispatch a request inside sling without going through the servlet container. ThegetNamedDispatcher(String)
method returns a servlet container request dispatcher which always goes through the servlet container. - Parameters and Attributes
- Initialization parameters and context attributes are shared with the
servlet context in which the
SlingMainServlet
is running. - Logging
- Logging is diverted to a logger whose name is the fully qualified name of this class.
This class implements the Servlet API 3.0
ServletContext
interface.
-
-
Constructor Summary
Constructors Constructor Description SlingServletContext(org.osgi.framework.BundleContext bundleContext, org.apache.sling.engine.impl.SlingMainServlet slingMainServlet)
Creates an instance of this class delegating some methods to the givenSlingMainServlet
.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description javax.servlet.FilterRegistration.Dynamic
addFilter(String filterName, Class<? extends javax.servlet.Filter> filterClass)
javax.servlet.FilterRegistration.Dynamic
addFilter(String filterName, String className)
javax.servlet.FilterRegistration.Dynamic
addFilter(String filterName, javax.servlet.Filter filter)
void
addListener(Class<? extends EventListener> listenerClass)
void
addListener(String className)
<T extends EventListener>
voidaddListener(T t)
javax.servlet.ServletRegistration.Dynamic
addServlet(String servletName, Class<? extends javax.servlet.Servlet> servletClass)
javax.servlet.ServletRegistration.Dynamic
addServlet(String servletName, String className)
javax.servlet.ServletRegistration.Dynamic
addServlet(String servletName, javax.servlet.Servlet servlet)
<T extends javax.servlet.Filter>
TcreateFilter(Class<T> clazz)
<T extends EventListener>
TcreateListener(Class<T> clazz)
<T extends javax.servlet.Servlet>
TcreateServlet(Class<T> clazz)
void
declareRoles(String... roleNames)
void
dispose()
Unregisters this servlet context as a service (if registered at all)Object
getAttribute(String name)
Returns the named servlet context attribute.Enumeration<String>
getAttributeNames()
Returns the names of all servlet context attributes.ClassLoader
getClassLoader()
javax.servlet.ServletContext
getContext(String uripath)
Returns the servlet context from the servlet container in which sling is running.String
getContextPath()
Returns the context path of the web application.Set<javax.servlet.SessionTrackingMode>
getDefaultSessionTrackingModes()
int
getEffectiveMajorVersion()
int
getEffectiveMinorVersion()
Set<javax.servlet.SessionTrackingMode>
getEffectiveSessionTrackingModes()
javax.servlet.FilterRegistration
getFilterRegistration(String filterName)
Map<String,? extends javax.servlet.FilterRegistration>
getFilterRegistrations()
String
getInitParameter(String name)
Returns the init-param of the servlet context in which Sling is configured.Enumeration<String>
getInitParameterNames()
Returns the names of the init-params of the servlet context in which Sling is configured.javax.servlet.descriptor.JspConfigDescriptor
getJspConfigDescriptor()
int
getMajorVersion()
Returns the major version number of the Servlet API supported by the servlet container in which Sling is running.String
getMimeType(String file)
Returns a MIME type for the extension of the given file name.int
getMinorVersion()
Returns the minor version number of the Servlet API supported by the servlet container in which Sling is running.javax.servlet.RequestDispatcher
getNamedDispatcher(String name)
Returns a servlet container request dispatcher for the named servlet.String
getRealPath(String path)
Returns the real file inside the web application to which the given path maps ornull
if no such file exists.javax.servlet.RequestDispatcher
getRequestDispatcher(String path)
Returns aSlingRequestDispatcher
for the given path if notnull
.URL
getResource(String path)
Returns the URI for the given path.InputStream
getResourceAsStream(String path)
Returns an input stream to the given path.Set<String>
getResourcePaths(String parentPath)
Returns a set of names for path entries considered children of the given path.String
getServerInfo()
Returns the Sling server info string.javax.servlet.Servlet
getServlet(String name)
Deprecated.protected javax.servlet.ServletContext
getServletContext()
Returns the real servlet context of the servlet container in which the Sling Servlet is running.String
getServletContextName()
Returns the name of the servlet context in which Sling is configured.Enumeration<String>
getServletNames()
Deprecated.javax.servlet.ServletRegistration
getServletRegistration(String servletName)
Map<String,? extends javax.servlet.ServletRegistration>
getServletRegistrations()
Enumeration<javax.servlet.Servlet>
getServlets()
Deprecated.javax.servlet.SessionCookieConfig
getSessionCookieConfig()
String
getVirtualServerName()
void
log(Exception exception, String message)
Deprecated.void
log(String message)
Logs the message at info level to the loggervoid
log(String message, Throwable throwable)
Logs the message and optional throwable at error level to the loggervoid
register(org.osgi.framework.BundleContext bundleContext)
void
removeAttribute(String name)
Removes the named servlet context attribute.void
setAttribute(String name, Object object)
Sets the name servlet context attribute to the requested value.boolean
setInitParameter(String name, String value)
void
setSessionTrackingModes(Set<javax.servlet.SessionTrackingMode> sessionTrackingModes)
protected javax.servlet.ServletContext
wrapServletContext(javax.servlet.ServletContext context)
-
-
-
Constructor Detail
-
SlingServletContext
public SlingServletContext(org.osgi.framework.BundleContext bundleContext, org.apache.sling.engine.impl.SlingMainServlet slingMainServlet)
Creates an instance of this class delegating some methods to the givenSlingMainServlet
. In addition the new instance is registered as aServletContext
.This method must only be called after the sling main servlet has been fully initialized. Otherwise the
getServletContext()
method may cause aNullPointerException
!- Parameters:
bundleContext
- the OSGi Bundle ContextslingMainServlet
- the main servlet- See Also:
dispose()
-
-
Method Detail
-
register
public void register(org.osgi.framework.BundleContext bundleContext)
-
dispose
public void dispose()
Unregisters this servlet context as a service (if registered at all)This method must be called before the sling main servlet is destroyed. Otherwise the
getServletContext()
method may cause aNullPointerException
!
-
getServletContextName
public String getServletContextName()
Returns the name of the servlet context in which Sling is configured. This method calls on theServletContext
in which theSlingMainServlet
is running.- Specified by:
getServletContextName
in interfacejavax.servlet.ServletContext
-
getContextPath
public String getContextPath()
Returns the context path of the web application. (Servlet API 2.5)- Specified by:
getContextPath
in interfacejavax.servlet.ServletContext
-
getInitParameter
public String getInitParameter(String name)
Returns the init-param of the servlet context in which Sling is configured. This method calls on theServletContext
in which theSlingMainServlet
is running.- Specified by:
getInitParameter
in interfacejavax.servlet.ServletContext
-
getInitParameterNames
public Enumeration<String> getInitParameterNames()
Returns the names of the init-params of the servlet context in which Sling is configured. This method calls on theServletContext
in which theSlingMainServlet
is running.- Specified by:
getInitParameterNames
in interfacejavax.servlet.ServletContext
-
getAttribute
public Object getAttribute(String name)
Returns the named servlet context attribute. This method calls on theServletContext
in which theSlingMainServlet
is running.- Specified by:
getAttribute
in interfacejavax.servlet.ServletContext
-
getAttributeNames
public Enumeration<String> getAttributeNames()
Returns the names of all servlet context attributes. This method calls on theServletContext
in which theSlingMainServlet
is running.- Specified by:
getAttributeNames
in interfacejavax.servlet.ServletContext
-
removeAttribute
public void removeAttribute(String name)
Removes the named servlet context attribute. This method calls on theServletContext
in which theSlingMainServlet
is running.- Specified by:
removeAttribute
in interfacejavax.servlet.ServletContext
-
setAttribute
public void setAttribute(String name, Object object)
Sets the name servlet context attribute to the requested value. This method calls on theServletContext
in which theSlingMainServlet
is running.- Specified by:
setAttribute
in interfacejavax.servlet.ServletContext
-
getServerInfo
public String getServerInfo()
Returns the Sling server info string. This is not the same server info string as returned by the servlet context in which Sling is configured.- Specified by:
getServerInfo
in interfacejavax.servlet.ServletContext
-
getMajorVersion
public int getMajorVersion()
Returns the major version number of the Servlet API supported by the servlet container in which Sling is running. This method calls on theServletContext
in which theSlingMainServlet
is running.- Specified by:
getMajorVersion
in interfacejavax.servlet.ServletContext
-
getMinorVersion
public int getMinorVersion()
Returns the minor version number of the Servlet API supported by the servlet container in which Sling is running. This method calls on theServletContext
in which theSlingMainServlet
is running.- Specified by:
getMinorVersion
in interfacejavax.servlet.ServletContext
-
getMimeType
public String getMimeType(String file)
Returns a MIME type for the extension of the given file name. This method calls on theServletContext
in which theSlingMainServlet
is running.- Specified by:
getMimeType
in interfacejavax.servlet.ServletContext
-
getRequestDispatcher
public javax.servlet.RequestDispatcher getRequestDispatcher(String path)
- Specified by:
getRequestDispatcher
in interfacejavax.servlet.ServletContext
-
getNamedDispatcher
public javax.servlet.RequestDispatcher getNamedDispatcher(String name)
Returns a servlet container request dispatcher for the named servlet. This method calls on theServletContext
in which theSlingMainServlet
is running.- Specified by:
getNamedDispatcher
in interfacejavax.servlet.ServletContext
-
getResource
public URL getResource(String path) throws MalformedURLException
Returns the URI for the given path. This method calls on theServletContext
in which theSlingMainServlet
is running.- Specified by:
getResource
in interfacejavax.servlet.ServletContext
- Throws:
MalformedURLException
-
getResourceAsStream
public InputStream getResourceAsStream(String path)
Returns an input stream to the given path. This method calls on theServletContext
in which theSlingMainServlet
is running.- Specified by:
getResourceAsStream
in interfacejavax.servlet.ServletContext
-
getResourcePaths
public Set<String> getResourcePaths(String parentPath)
Returns a set of names for path entries considered children of the given path. This method calls on theServletContext
in which theSlingMainServlet
is running.- Specified by:
getResourcePaths
in interfacejavax.servlet.ServletContext
-
getRealPath
public String getRealPath(String path)
Returns the real file inside the web application to which the given path maps ornull
if no such file exists. This method calls on theServletContext
in which theSlingMainServlet
is running.- Specified by:
getRealPath
in interfacejavax.servlet.ServletContext
-
log
public void log(String message, Throwable throwable)
Logs the message and optional throwable at error level to the logger- Specified by:
log
in interfacejavax.servlet.ServletContext
-
log
public void log(String message)
Logs the message at info level to the logger- Specified by:
log
in interfacejavax.servlet.ServletContext
-
log
@Deprecated public void log(Exception exception, String message)
Deprecated.Logs the message and optional exception at error level to the logger- Specified by:
log
in interfacejavax.servlet.ServletContext
-
getContext
public javax.servlet.ServletContext getContext(String uripath)
Returns the servlet context from the servlet container in which sling is running. This method calls on theServletContext
in which theSlingMainServlet
is running.- Specified by:
getContext
in interfacejavax.servlet.ServletContext
-
getServlet
@Deprecated public javax.servlet.Servlet getServlet(String name)
Deprecated.Returnsnull
as defined in Servlet API 2.4- Specified by:
getServlet
in interfacejavax.servlet.ServletContext
-
getServletNames
@Deprecated public Enumeration<String> getServletNames()
Deprecated.Returns an empty enumeration as defined in Servlet API 2.4- Specified by:
getServletNames
in interfacejavax.servlet.ServletContext
-
getServlets
@Deprecated public Enumeration<javax.servlet.Servlet> getServlets()
Deprecated.Returns an empty enumeration as defined in Servlet API 2.4- Specified by:
getServlets
in interfacejavax.servlet.ServletContext
-
getEffectiveMajorVersion
public int getEffectiveMajorVersion()
- Specified by:
getEffectiveMajorVersion
in interfacejavax.servlet.ServletContext
-
getEffectiveMinorVersion
public int getEffectiveMinorVersion()
- Specified by:
getEffectiveMinorVersion
in interfacejavax.servlet.ServletContext
-
setInitParameter
public boolean setInitParameter(String name, String value)
- Specified by:
setInitParameter
in interfacejavax.servlet.ServletContext
-
getSessionCookieConfig
public javax.servlet.SessionCookieConfig getSessionCookieConfig()
- Specified by:
getSessionCookieConfig
in interfacejavax.servlet.ServletContext
-
setSessionTrackingModes
public void setSessionTrackingModes(Set<javax.servlet.SessionTrackingMode> sessionTrackingModes)
- Specified by:
setSessionTrackingModes
in interfacejavax.servlet.ServletContext
-
getDefaultSessionTrackingModes
public Set<javax.servlet.SessionTrackingMode> getDefaultSessionTrackingModes()
- Specified by:
getDefaultSessionTrackingModes
in interfacejavax.servlet.ServletContext
-
getEffectiveSessionTrackingModes
public Set<javax.servlet.SessionTrackingMode> getEffectiveSessionTrackingModes()
- Specified by:
getEffectiveSessionTrackingModes
in interfacejavax.servlet.ServletContext
-
getJspConfigDescriptor
public javax.servlet.descriptor.JspConfigDescriptor getJspConfigDescriptor()
- Specified by:
getJspConfigDescriptor
in interfacejavax.servlet.ServletContext
-
getClassLoader
public ClassLoader getClassLoader()
- Specified by:
getClassLoader
in interfacejavax.servlet.ServletContext
-
declareRoles
public void declareRoles(String... roleNames)
- Specified by:
declareRoles
in interfacejavax.servlet.ServletContext
-
addServlet
public javax.servlet.ServletRegistration.Dynamic addServlet(String servletName, String className)
- Specified by:
addServlet
in interfacejavax.servlet.ServletContext
-
addServlet
public javax.servlet.ServletRegistration.Dynamic addServlet(String servletName, javax.servlet.Servlet servlet)
- Specified by:
addServlet
in interfacejavax.servlet.ServletContext
-
addServlet
public javax.servlet.ServletRegistration.Dynamic addServlet(String servletName, Class<? extends javax.servlet.Servlet> servletClass)
- Specified by:
addServlet
in interfacejavax.servlet.ServletContext
-
createServlet
public <T extends javax.servlet.Servlet> T createServlet(Class<T> clazz)
- Specified by:
createServlet
in interfacejavax.servlet.ServletContext
-
getServletRegistration
public javax.servlet.ServletRegistration getServletRegistration(String servletName)
- Specified by:
getServletRegistration
in interfacejavax.servlet.ServletContext
-
getServletRegistrations
public Map<String,? extends javax.servlet.ServletRegistration> getServletRegistrations()
- Specified by:
getServletRegistrations
in interfacejavax.servlet.ServletContext
-
addFilter
public javax.servlet.FilterRegistration.Dynamic addFilter(String filterName, String className)
- Specified by:
addFilter
in interfacejavax.servlet.ServletContext
-
addFilter
public javax.servlet.FilterRegistration.Dynamic addFilter(String filterName, javax.servlet.Filter filter)
- Specified by:
addFilter
in interfacejavax.servlet.ServletContext
-
addFilter
public javax.servlet.FilterRegistration.Dynamic addFilter(String filterName, Class<? extends javax.servlet.Filter> filterClass)
- Specified by:
addFilter
in interfacejavax.servlet.ServletContext
-
createFilter
public <T extends javax.servlet.Filter> T createFilter(Class<T> clazz)
- Specified by:
createFilter
in interfacejavax.servlet.ServletContext
-
getFilterRegistration
public javax.servlet.FilterRegistration getFilterRegistration(String filterName)
- Specified by:
getFilterRegistration
in interfacejavax.servlet.ServletContext
-
getFilterRegistrations
public Map<String,? extends javax.servlet.FilterRegistration> getFilterRegistrations()
- Specified by:
getFilterRegistrations
in interfacejavax.servlet.ServletContext
-
addListener
public void addListener(String className)
- Specified by:
addListener
in interfacejavax.servlet.ServletContext
-
addListener
public <T extends EventListener> void addListener(T t)
- Specified by:
addListener
in interfacejavax.servlet.ServletContext
-
addListener
public void addListener(Class<? extends EventListener> listenerClass)
- Specified by:
addListener
in interfacejavax.servlet.ServletContext
-
createListener
public <T extends EventListener> T createListener(Class<T> clazz)
- Specified by:
createListener
in interfacejavax.servlet.ServletContext
-
getServletContext
protected javax.servlet.ServletContext getServletContext()
Returns the real servlet context of the servlet container in which the Sling Servlet is running.- Returns:
- the servlet context
-
wrapServletContext
protected javax.servlet.ServletContext wrapServletContext(javax.servlet.ServletContext context)
-
getVirtualServerName
public String getVirtualServerName()
- Specified by:
getVirtualServerName
in interfacejavax.servlet.ServletContext
-
-