Package com.helger.servlet.mock
Class MockServletContext
- java.lang.Object
-
- com.helger.servlet.mock.MockServletContext
-
- All Implemented Interfaces:
IServletContext300To310Migration,IServletContext310To400Migration,jakarta.servlet.ServletContext
@ThreadSafe public class MockServletContext extends Object implements IServletContext310To400Migration
Mock implementation of theServletContextinterface.- Author:
- Philip Helger
-
-
Field Summary
Fields Modifier and Type Field Description static StringDEFAULT_SERVLET_CONTEXT_NAMEstatic StringDEFAULT_SERVLET_CONTEXT_PATHstatic intSERVLET_SPEC_MAJOR_VERSIONstatic intSERVLET_SPEC_MINOR_VERSION
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description jakarta.servlet.FilterRegistration.DynamicaddFilter(String filterName, jakarta.servlet.Filter filter)jakarta.servlet.FilterRegistration.DynamicaddFilter(String filterName, Class<? extends jakarta.servlet.Filter> filterClass)jakarta.servlet.FilterRegistration.DynamicaddFilter(String filterName, String className)voidaddInitParameter(String sName, String sValue)voidaddListener(Class<? extends EventListener> listenerClass)voidaddListener(String className)<T extends EventListener>
voidaddListener(T t)jakarta.servlet.ServletRegistration.DynamicaddServlet(String servletName, jakarta.servlet.Servlet servlet)jakarta.servlet.ServletRegistration.DynamicaddServlet(String servletName, Class<? extends jakarta.servlet.Servlet> servletClass)jakarta.servlet.ServletRegistration.DynamicaddServlet(String servletName, String className)static MockServletContextcreate()Create a newMockServletContext, using no base path and no context path.static MockServletContextcreate(String sContextPath)Create a newMockServletContextusing no base path but the provided context path.static MockServletContextcreate(String sContextPath, String sResourceBasePath)Create a newMockServletContextusing the provided context path and resource base oath.static MockServletContextcreate(String sContextPath, String sResourceBasePath, com.helger.commons.io.resourceprovider.IReadableResourceProvider aResourceLoader, Map<String,String> aInitParams)Create a newMockServletContextwith all possible parameters.static MockServletContextcreate(String sContextPath, Map<String,String> aInitParams)Create a newMockServletContextusing the provided context path and init parameters.static MockServletContextcreate(Map<String,String> aInitParams)Create a newMockServletContext, using no base path and no context path using the provided initialization parameters.<T extends jakarta.servlet.Filter>
TcreateFilter(Class<T> clazz)<T extends EventListener>
TcreateListener(Class<T> clazz)<T extends jakarta.servlet.Servlet>
TcreateServlet(Class<T> clazz)MockServletConfigcreateServletConfig(String sServletName)Create a newMockServletConfigobject without servlet init parameters.MockServletConfigcreateServletConfig(String sServletName, Map<String,String> aServletInitParams)Create a newMockServletConfigobject.voiddeclareRoles(String... roleNames)ObjectgetAttribute(String sName)Enumeration<String>getAttributeNames()ClassLoadergetClassLoader()jakarta.servlet.ServletContextgetContext(String sContextPath)StringgetContextPath()com.helger.commons.collection.impl.ICommonsSet<jakarta.servlet.SessionTrackingMode>getDefaultSessionTrackingModes()intgetEffectiveMajorVersion()intgetEffectiveMinorVersion()com.helger.commons.collection.impl.ICommonsSet<jakarta.servlet.SessionTrackingMode>getEffectiveSessionTrackingModes()jakarta.servlet.FilterRegistrationgetFilterRegistration(String filterName)com.helger.commons.collection.impl.ICommonsMap<String,? extends jakarta.servlet.FilterRegistration>getFilterRegistrations()StringgetInitParameter(String sName)Enumeration<String>getInitParameterNames()jakarta.servlet.descriptor.JspConfigDescriptorgetJspConfigDescriptor()intgetMajorVersion()StringgetMimeType(String sFilename)intgetMinorVersion()jakarta.servlet.RequestDispatchergetNamedDispatcher(String sPath)Deprecated.StringgetRealPath(String sPath)jakarta.servlet.RequestDispatchergetRequestDispatcher(String sPath)URLgetResource(String sPath)InputStreamgetResourceAsStream(String sPath)protected StringgetResourceLocation(String sPath)Build a full resource location for the given path, prepending the resource base path of this MockServletContext.com.helger.commons.collection.impl.ICommonsSet<String>getResourcePaths(String sPath)Deprecated.StringgetServerInfo()jakarta.servlet.ServletgetServlet(String sName)Deprecated.StringgetServletContextName()Enumeration<String>getServletNames()Deprecated.MockServletPoolgetServletPool()jakarta.servlet.ServletRegistrationgetServletRegistration(String servletName)com.helger.commons.collection.impl.ICommonsMap<String,? extends jakarta.servlet.ServletRegistration>getServletRegistrations()Enumeration<jakarta.servlet.Servlet>getServlets()Deprecated.jakarta.servlet.SessionCookieConfiggetSessionCookieConfig()StringgetVirtualServerName()protected voidinitListeners()voidinvalidate()MockHttpServletResponseinvoke(jakarta.servlet.http.HttpServletRequest aHttpRequest)static booleanisReThrowListenerException()voidlog(Exception ex, String sMessage)Deprecated.voidlog(String sMessage)voidlog(String sMessage, Throwable ex)voidregisterContext(String sContextPath, jakarta.servlet.ServletContext aContext)voidremoveAttribute(String sName)voidsetAttribute(String sName, Object aValue)voidsetContextPath(String sContextPath)booleansetInitParameter(String sName, String sValue)static voidsetReThrowListenerException(boolean bReThrowListenerException)voidsetServletContextName(String sServletContextName)voidsetSessionTrackingModes(Set<jakarta.servlet.SessionTrackingMode> sessionTrackingModes)-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.helger.servlet.spec.IServletContext310To400Migration
addJspFile, getRequestCharacterEncoding, getResponseCharacterEncoding, getSessionTimeout, setRequestCharacterEncoding, setResponseCharacterEncoding, setSessionTimeout
-
-
-
-
Field Detail
-
SERVLET_SPEC_MAJOR_VERSION
public static final int SERVLET_SPEC_MAJOR_VERSION
- See Also:
- Constant Field Values
-
SERVLET_SPEC_MINOR_VERSION
public static final int SERVLET_SPEC_MINOR_VERSION
- See Also:
- Constant Field Values
-
DEFAULT_SERVLET_CONTEXT_NAME
public static final String DEFAULT_SERVLET_CONTEXT_NAME
- See Also:
- Constant Field Values
-
DEFAULT_SERVLET_CONTEXT_PATH
public static final String DEFAULT_SERVLET_CONTEXT_PATH
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
MockServletContext
protected MockServletContext(@Nullable String sContextPath, @Nullable String sResourceBasePath, @Nullable com.helger.commons.io.resourceprovider.IReadableResourceProvider aResourceLoader, @Nullable Map<String,String> aInitParams)
Create a new MockServletContext.- Parameters:
sContextPath- The context path to usesResourceBasePath- the WAR root directory (should not end with a slash)aResourceLoader- the IReadableResourceProvider to use (or null for the default)aInitParams- Optional map with initialization parameters
-
-
Method Detail
-
isReThrowListenerException
public static boolean isReThrowListenerException()
- Returns:
trueif runtime exceptions from context listeners should be propagated to the outside or if they should be logged and processing should continue. Default isfalse.
-
setReThrowListenerException
public static void setReThrowListenerException(boolean bReThrowListenerException)
- Parameters:
bReThrowListenerException-trueto re-throw listener exceptions (on context inited and destroyed)
-
initListeners
@OverrideOnDemand protected void initListeners()
-
getResourceLocation
@Nonnull protected String getResourceLocation(@Nonnull String sPath)
Build a full resource location for the given path, prepending the resource base path of this MockServletContext.- Parameters:
sPath- the path as specified- Returns:
- the full resource path
-
getContextPath
@Nonnull public String getContextPath()
- Specified by:
getContextPathin interfacejakarta.servlet.ServletContext
-
registerContext
public void registerContext(@Nonnull String sContextPath, @Nonnull jakarta.servlet.ServletContext aContext)
-
getContext
@Nullable public jakarta.servlet.ServletContext getContext(@Nullable String sContextPath)
- Specified by:
getContextin interfacejakarta.servlet.ServletContext
-
getMajorVersion
@Nonnegative public int getMajorVersion()
- Specified by:
getMajorVersionin interfacejakarta.servlet.ServletContext
-
getMinorVersion
@Nonnegative public int getMinorVersion()
- Specified by:
getMinorVersionin interfacejakarta.servlet.ServletContext
-
getMimeType
@Nullable public String getMimeType(@Nonnull String sFilename)
- Specified by:
getMimeTypein interfacejakarta.servlet.ServletContext
-
getResourcePaths
@UnsupportedOperation @Deprecated(forRemoval=false) public com.helger.commons.collection.impl.ICommonsSet<String> getResourcePaths(String sPath)
Deprecated.- Specified by:
getResourcePathsin interfacejakarta.servlet.ServletContext
-
getResource
@Nullable public URL getResource(@Nonnull String sPath) throws MalformedURLException
- Specified by:
getResourcein interfacejakarta.servlet.ServletContext- Throws:
MalformedURLException
-
getResourceAsStream
@Nullable public InputStream getResourceAsStream(@Nonnull String sPath)
- Specified by:
getResourceAsStreamin interfacejakarta.servlet.ServletContext
-
getRequestDispatcher
@Nonnull public jakarta.servlet.RequestDispatcher getRequestDispatcher(@Nonnull String sPath)
- Specified by:
getRequestDispatcherin interfacejakarta.servlet.ServletContext
-
getNamedDispatcher
@Nullable @Deprecated(forRemoval=false) public jakarta.servlet.RequestDispatcher getNamedDispatcher(@Nullable String sPath)
Deprecated.- Specified by:
getNamedDispatcherin interfacejakarta.servlet.ServletContext
-
getServlet
@Deprecated(forRemoval=false) public jakarta.servlet.Servlet getServlet(@Nullable String sName)
Deprecated.- Specified by:
getServletin interfacejakarta.servlet.ServletContext
-
getServlets
@Deprecated(forRemoval=false) @Nonnull public Enumeration<jakarta.servlet.Servlet> getServlets()
Deprecated.- Specified by:
getServletsin interfacejakarta.servlet.ServletContext
-
getServletNames
@Deprecated(forRemoval=false) @Nonnull public Enumeration<String> getServletNames()
Deprecated.- Specified by:
getServletNamesin interfacejakarta.servlet.ServletContext
-
log
public void log(@Nullable String sMessage)
- Specified by:
login interfacejakarta.servlet.ServletContext
-
log
@Deprecated(forRemoval=false) public void log(@Nullable Exception ex, @Nullable String sMessage)
Deprecated.- Specified by:
login interfacejakarta.servlet.ServletContext
-
log
public void log(@Nullable String sMessage, @Nullable Throwable ex)
- Specified by:
login interfacejakarta.servlet.ServletContext
-
getRealPath
@Nonnull public String getRealPath(@Nonnull String sPath)
- Specified by:
getRealPathin interfacejakarta.servlet.ServletContext
-
getServerInfo
@Nonnull @Nonempty public String getServerInfo()
- Specified by:
getServerInfoin interfacejakarta.servlet.ServletContext
-
getInitParameter
@Nullable public String getInitParameter(@Nonnull String sName)
- Specified by:
getInitParameterin interfacejakarta.servlet.ServletContext
-
getInitParameterNames
@Nonnull public Enumeration<String> getInitParameterNames()
- Specified by:
getInitParameterNamesin interfacejakarta.servlet.ServletContext
-
getAttribute
@Nullable public Object getAttribute(@Nonnull String sName)
- Specified by:
getAttributein interfacejakarta.servlet.ServletContext
-
getAttributeNames
@Nonnull public Enumeration<String> getAttributeNames()
- Specified by:
getAttributeNamesin interfacejakarta.servlet.ServletContext
-
setAttribute
public final void setAttribute(@Nonnull String sName, @Nullable Object aValue)
- Specified by:
setAttributein interfacejakarta.servlet.ServletContext
-
removeAttribute
public void removeAttribute(@Nonnull String sName)
- Specified by:
removeAttributein interfacejakarta.servlet.ServletContext
-
getServletContextName
@Nullable public String getServletContextName()
- Specified by:
getServletContextNamein interfacejakarta.servlet.ServletContext
-
createServletConfig
@Nonnull public MockServletConfig createServletConfig(@Nonnull @Nonempty String sServletName)
Create a newMockServletConfigobject without servlet init parameters.- Parameters:
sServletName- Name of the servlet. May neither benullnor empty.- Returns:
- A new
MockServletConfigobject for this servlet context.
-
createServletConfig
@Nonnull public MockServletConfig createServletConfig(@Nonnull @Nonempty String sServletName, @Nullable Map<String,String> aServletInitParams)
Create a newMockServletConfigobject.- Parameters:
sServletName- Name of the servlet. May neither benullnor empty.aServletInitParams- The map with all servlet init parameters. May benullor empty.- Returns:
- A new
MockServletConfigobject for this servlet context.
-
getServletPool
@Nonnull public MockServletPool getServletPool()
- Returns:
- The servlet pool for registering mock servlets.
-
invoke
@Nullable public MockHttpServletResponse invoke(@Nonnull jakarta.servlet.http.HttpServletRequest aHttpRequest)
-
invalidate
public void invalidate()
-
getEffectiveMajorVersion
public int getEffectiveMajorVersion()
- Specified by:
getEffectiveMajorVersionin interfacejakarta.servlet.ServletContext
-
getEffectiveMinorVersion
public int getEffectiveMinorVersion()
- Specified by:
getEffectiveMinorVersionin interfacejakarta.servlet.ServletContext
-
setInitParameter
public boolean setInitParameter(String sName, String sValue)
- Specified by:
setInitParameterin interfacejakarta.servlet.ServletContext
-
addServlet
@UnsupportedOperation public jakarta.servlet.ServletRegistration.Dynamic addServlet(String servletName, String className)
- Specified by:
addServletin interfacejakarta.servlet.ServletContext
-
addServlet
@UnsupportedOperation public jakarta.servlet.ServletRegistration.Dynamic addServlet(String servletName, jakarta.servlet.Servlet servlet)
- Specified by:
addServletin interfacejakarta.servlet.ServletContext
-
addServlet
@UnsupportedOperation public jakarta.servlet.ServletRegistration.Dynamic addServlet(String servletName, Class<? extends jakarta.servlet.Servlet> servletClass)
- Specified by:
addServletin interfacejakarta.servlet.ServletContext
-
createServlet
@UnsupportedOperation public <T extends jakarta.servlet.Servlet> T createServlet(Class<T> clazz) throws jakarta.servlet.ServletException
- Specified by:
createServletin interfacejakarta.servlet.ServletContext- Throws:
jakarta.servlet.ServletException
-
getServletRegistration
@UnsupportedOperation public jakarta.servlet.ServletRegistration getServletRegistration(String servletName)
- Specified by:
getServletRegistrationin interfacejakarta.servlet.ServletContext
-
getServletRegistrations
@UnsupportedOperation public com.helger.commons.collection.impl.ICommonsMap<String,? extends jakarta.servlet.ServletRegistration> getServletRegistrations()
- Specified by:
getServletRegistrationsin interfacejakarta.servlet.ServletContext
-
addFilter
@UnsupportedOperation public jakarta.servlet.FilterRegistration.Dynamic addFilter(String filterName, String className)
- Specified by:
addFilterin interfacejakarta.servlet.ServletContext
-
addFilter
@UnsupportedOperation public jakarta.servlet.FilterRegistration.Dynamic addFilter(String filterName, jakarta.servlet.Filter filter)
- Specified by:
addFilterin interfacejakarta.servlet.ServletContext
-
addFilter
@UnsupportedOperation public jakarta.servlet.FilterRegistration.Dynamic addFilter(String filterName, Class<? extends jakarta.servlet.Filter> filterClass)
- Specified by:
addFilterin interfacejakarta.servlet.ServletContext
-
createFilter
@UnsupportedOperation public <T extends jakarta.servlet.Filter> T createFilter(Class<T> clazz) throws jakarta.servlet.ServletException
- Specified by:
createFilterin interfacejakarta.servlet.ServletContext- Throws:
jakarta.servlet.ServletException
-
getFilterRegistration
@UnsupportedOperation public jakarta.servlet.FilterRegistration getFilterRegistration(String filterName)
- Specified by:
getFilterRegistrationin interfacejakarta.servlet.ServletContext
-
getFilterRegistrations
@UnsupportedOperation public com.helger.commons.collection.impl.ICommonsMap<String,? extends jakarta.servlet.FilterRegistration> getFilterRegistrations()
- Specified by:
getFilterRegistrationsin interfacejakarta.servlet.ServletContext
-
getSessionCookieConfig
@UnsupportedOperation public jakarta.servlet.SessionCookieConfig getSessionCookieConfig()
- Specified by:
getSessionCookieConfigin interfacejakarta.servlet.ServletContext
-
setSessionTrackingModes
@UnsupportedOperation public void setSessionTrackingModes(Set<jakarta.servlet.SessionTrackingMode> sessionTrackingModes)
- Specified by:
setSessionTrackingModesin interfacejakarta.servlet.ServletContext
-
getDefaultSessionTrackingModes
@UnsupportedOperation public com.helger.commons.collection.impl.ICommonsSet<jakarta.servlet.SessionTrackingMode> getDefaultSessionTrackingModes()
- Specified by:
getDefaultSessionTrackingModesin interfacejakarta.servlet.ServletContext
-
getEffectiveSessionTrackingModes
@UnsupportedOperation public com.helger.commons.collection.impl.ICommonsSet<jakarta.servlet.SessionTrackingMode> getEffectiveSessionTrackingModes()
- Specified by:
getEffectiveSessionTrackingModesin interfacejakarta.servlet.ServletContext
-
addListener
@UnsupportedOperation public void addListener(String className)
- Specified by:
addListenerin interfacejakarta.servlet.ServletContext
-
addListener
@UnsupportedOperation public <T extends EventListener> void addListener(T t)
- Specified by:
addListenerin interfacejakarta.servlet.ServletContext
-
addListener
@UnsupportedOperation public void addListener(Class<? extends EventListener> listenerClass)
- Specified by:
addListenerin interfacejakarta.servlet.ServletContext
-
createListener
@UnsupportedOperation public <T extends EventListener> T createListener(Class<T> clazz) throws jakarta.servlet.ServletException
- Specified by:
createListenerin interfacejakarta.servlet.ServletContext- Throws:
jakarta.servlet.ServletException
-
getJspConfigDescriptor
@UnsupportedOperation public jakarta.servlet.descriptor.JspConfigDescriptor getJspConfigDescriptor()
- Specified by:
getJspConfigDescriptorin interfacejakarta.servlet.ServletContext
-
getClassLoader
public ClassLoader getClassLoader()
- Specified by:
getClassLoaderin interfacejakarta.servlet.ServletContext
-
declareRoles
@UnsupportedOperation public void declareRoles(String... roleNames)
- Specified by:
declareRolesin interfacejakarta.servlet.ServletContext
-
getVirtualServerName
public String getVirtualServerName()
- Specified by:
getVirtualServerNamein interfaceIServletContext300To310Migration- Specified by:
getVirtualServerNamein interfacejakarta.servlet.ServletContext
-
create
@Nonnull public static MockServletContext create()
Create a newMockServletContext, using no base path and no context path. The initialization listeners are triggered automatically.- Returns:
- The created
MockServletContext
-
create
@Nonnull public static MockServletContext create(@Nullable Map<String,String> aInitParams)
Create a newMockServletContext, using no base path and no context path using the provided initialization parameters. The initialization listeners are triggered automatically.- Parameters:
aInitParams- The init parameter. May benull.- Returns:
- The created
MockServletContext
-
create
@Nonnull public static MockServletContext create(@Nullable String sContextPath)
Create a newMockServletContextusing no base path but the provided context path. The initialization listeners are triggered automatically.- Parameters:
sContextPath- The context path to use. May benull.- Returns:
- The created
MockServletContext
-
create
@Nonnull public static MockServletContext create(@Nullable String sContextPath, @Nullable Map<String,String> aInitParams)
Create a newMockServletContextusing the provided context path and init parameters. The initialization listeners are triggered automatically.- Parameters:
sContextPath- Context path to use. May benull.aInitParams- The init parameter. May benull.- Returns:
- The created
MockServletContext
-
create
@Nonnull public static MockServletContext create(@Nullable String sContextPath, @Nullable String sResourceBasePath)
Create a newMockServletContextusing the provided context path and resource base oath. The initialization listeners are triggered automatically.- Parameters:
sContextPath- The context path to use. May benull.sResourceBasePath- the WAR root directory (should not end with a slash). May benull.- Returns:
- The created
MockServletContext
-
create
@Nonnull public static MockServletContext create(@Nullable String sContextPath, @Nullable String sResourceBasePath, @Nullable com.helger.commons.io.resourceprovider.IReadableResourceProvider aResourceLoader, @Nullable Map<String,String> aInitParams)
Create a newMockServletContextwith all possible parameters.- Parameters:
sContextPath- The context path to use. May benull.sResourceBasePath- the WAR root directory (should not end with a slash). May benull.aResourceLoader- the IReadableResourceProvider to use. May benull.aInitParams- Optional map with initialization parameters. May benull.- Returns:
- The created
MockServletContext
-
-