Package net.shibboleth.shared.spring.servlet.impl

# Class DelegatingServletProxy

java.lang.Object
jakarta.servlet.GenericServlet
net.shibboleth.shared.spring.servlet.impl.DelegatingServletProxy

All Implemented Interfaces:
  `Servlet`, `ServletConfig`, `Serializable`

public class DelegatingServletProxy extends GenericServlet
Proxy for a standard Servlet, delegating to a Spring-managed bean that implements the Servlet interface. Supports a "targetBeanName" init-param in `web.xml`, specifying the name of the target bean in the Spring application context.

This class was originally inspired by Spring's `DelegatingFilterProxy` class and has the same general behavior.

See Also:
  - Serialized Form

## Field Summary

Fields
Modifier and Type
Field
Description
`private String`
`contextAttribute`
The name of the ServletContext attribute which should be used to retrieve the `WebApplicationContext` from which to load the delegate `Servlet` bean.
`private Servlet`
`delegate`
The `HttpServlet` instance that this proxy will delegate to and manage the lifecycle for.
`private Object`
`delegateMonitor`
Object used to syncrhonize access where needed.
`private static final long`
`serialVersionUID`
For serialization.
`private String`
`targetBeanName`
The name of the target bean in the Spring application context.
`private boolean`
`targetFilterLifecycle`
Whether to invoke the Servlet lifecycle methods on the target bean.
`private WebApplicationContext`
`webApplicationContext`
The application context from which the target filter will be retrieved.

## Constructor Summary

Constructors
Constructor
Description
`DelegatingServletProxy()`
Constructor for traditional use in `web.xml`.
`DelegatingServletProxy(Servlet del)`
Constructor using the given `Servlet` delegate.
`DelegatingServletProxy(String targetBeanName)`
Constructor that will retrieve the named target bean from the Spring `WebApplicationContext` found in the `ServletContext` (either the 'root' application context or the context named by `setContextAttribute(java.lang.String)`).
`DelegatingServletProxy(String targetBeanName, WebApplicationContext wac)`
Constructor that will retrieve the named target bean from the given Spring `WebApplicationContext`.

## Method Summary

Modifier and Type
Method
Description
`void`
`destroy()`
`protected void`
`destroyDelegate(Servlet del)`
Destroy the Servlet delegate.
`protected WebApplicationContext`
`findWebApplicationContext()`
Return the `WebApplicationContext` passed in at construction time, if available.
`String`
`getContextAttribute()`
Return the name of the ServletContext attribute which should be used to retrieve the `WebApplicationContext` from which to load the delegate `Servlet` bean.
`protected String`
`getTargetBeanName()`
Return the name of the target bean in the Spring application context.
`void`
`init()`
`protected Servlet`
`initDelegate(WebApplicationContext wac)`
Initialize the Servlet delegate, defined as a bean in the given Spring application context.
`protected void`
`invokeDelegate(Servlet del, ServletRequest request, ServletResponse response)`
Actually invoke the delegate Servlet with the given request and response.
`protected boolean`
`isTargetFilterLifecycle()`
Return whether to invoke the Servlet lifecycle methods on the target bean.
`void`
`service(ServletRequest request, ServletResponse response)`
`void`
`setContextAttribute(String name)`
Set the name of the ServletContext attribute which should be used to retrieve the `WebApplicationContext` from which to load the delegate `Servlet` bean.
`void`
`setTargetBeanName(String name)`
Set the name of the target bean in the Spring application context.
`void`
`setTargetFilterLifecycle(boolean flag)`
Set whether to invoke the `Servlet.init(jakarta.servlet.ServletConfig)` and `Servlet.destroy()` lifecycle methods on the target bean.

### Methods inherited from class jakarta.servlet.GenericServlet

`getInitParameter, getInitParameterNames, 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 Details

### serialVersionUID

private static final long serialVersionUID
For serialization.

See Also:
  - Constant Field Values

### contextAttribute

@Nullable @NotEmpty private String contextAttribute
The name of the ServletContext attribute which should be used to retrieve the `WebApplicationContext` from which to load the delegate `Servlet` bean.

### webApplicationContext

@Nullable private WebApplicationContext webApplicationContext
The application context from which the target filter will be retrieved.

### targetBeanName

@Nullable @NotEmpty private String targetBeanName
The name of the target bean in the Spring application context.

### targetFilterLifecycle

private boolean targetFilterLifecycle
Whether to invoke the Servlet lifecycle methods on the target bean.

### delegate

@Nullable private volatile Servlet delegate
The `HttpServlet` instance that this proxy will delegate to and manage the lifecycle for.

### delegateMonitor

@Nonnull private Object delegateMonitor
Object used to syncrhonize access where needed.

## Constructor Details

### DelegatingServletProxy

public DelegatingServletProxy()
Constructor for traditional use in `web.xml`.

### DelegatingServletProxy

public DelegatingServletProxy(@Nonnull Servlet del)
Constructor using the given `Servlet` delegate.

Bypasses entirely the need for interacting with a Spring application context, specifying the target bean name, etc.

For use with instance-based registration of filters.

Parameters:
  `del` - the `HttpServlet` instance that this proxy will delegate to and manage the lifecycle for

### DelegatingServletProxy

public DelegatingServletProxy(@Nonnull @NotEmpty String targetBeanName)
Constructor that will retrieve the named target bean from the Spring `WebApplicationContext` found in the `ServletContext` (either the 'root' application context or the context named by `setContextAttribute(java.lang.String)`).

For use with instance-based registration of filters.

The target bean must implement the standard `Servlet` interface.

Parameters:
  `targetBeanName` - name of the target servlet bean to look up in the Spring application context

### DelegatingServletProxy

public DelegatingServletProxy(@Nonnull @NotEmpty String targetBeanName, @Nullable WebApplicationContext wac)
Constructor that will retrieve the named target bean from the given Spring `WebApplicationContext`.

For use with instance-based registration of filters.

The target bean must implement the standard `Servlet` interface.

The given `WebApplicationContext` may or may not be refreshed when passed in. If it has not, and if the context implements `ConfigurableApplicationContext`, a `refresh()` will be attempted before retrieving the named target bean.

Parameters:
  `targetBeanName` - name of the target filter bean in the Spring application context
  `wac` - the application context from which the target filter will be retrieved; if `null`, an application context will be looked up from `ServletContext` as a fallback

## Method Details

### setContextAttribute

public void setContextAttribute(@Nullable @NotEmpty String name)
Set the name of the ServletContext attribute which should be used to retrieve the `WebApplicationContext` from which to load the delegate `Servlet` bean.

Parameters:
  `name` - of attribute

### getContextAttribute

@Nullable @NotEmpty public String getContextAttribute()
Return the name of the ServletContext attribute which should be used to retrieve the `WebApplicationContext` from which to load the delegate `Servlet` bean.

Returns:
  name of attribute

### setTargetBeanName

public void setTargetBeanName(@Nullable @NotEmpty String name)
Set the name of the target bean in the Spring application context.

The target bean must implement the standard `Servlet` interface.

By default, the `servlet-name` as specified for the DelegatingFilterProxy in `web.xml` will be used.

Parameters:
  `name` - bean name

### getTargetBeanName

@Nullable @NotEmpty protected String getTargetBeanName()
Return the name of the target bean in the Spring application context.

Returns:
  bean name

### setTargetFilterLifecycle

public void setTargetFilterLifecycle(boolean flag)
Set whether to invoke the `Servlet.init(jakarta.servlet.ServletConfig)` and `Servlet.destroy()` lifecycle methods on the target bean.

Default is "false"; target beans usually rely on the Spring application context for managing their lifecycle. Setting this flag to "true" means that the servlet container will control the lifecycle of the target bean, with this proxy delegating the corresponding calls.

Parameters:
  `flag` - flag to set

### isTargetFilterLifecycle

protected boolean isTargetFilterLifecycle()
Return whether to invoke the Servlet lifecycle methods on the target bean.

Returns:
  whether to invoke the Servlet lifecycle methods on the target bean

### init

public void init() throws ServletException

Overrides:
  `init` in class `GenericServlet`
Throws:
  `ServletException`

### service

public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException

Specified by:
  `service` in interface `Servlet`
Specified by:
  `service` in class `GenericServlet`
Throws:
  `ServletException`
  `IOException`

### destroy

public void destroy()

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

### findWebApplicationContext

@Nullable protected WebApplicationContext findWebApplicationContext()
Return the `WebApplicationContext` passed in at construction time, if available.

Otherwise, attempt to retrieve a `WebApplicationContext` from the `ServletContext` attribute with the configured name if set. Otherwise look up a `WebApplicationContext` under the well-known "root" application context attribute.

The `WebApplicationContext` must have already been loaded and stored in the `ServletContext` before this filter gets initialized (or invoked).

Subclasses may override this method to provide a different `WebApplicationContext` retrieval strategy.

Returns:
  the `WebApplicationContext` for this proxy, or `null` if not found

### initDelegate

@Nonnull protected Servlet initDelegate(@Nonnull WebApplicationContext wac) throws ServletException
Initialize the Servlet delegate, defined as a bean in the given Spring application context.

The default implementation fetches the bean from the application context and calls the standard `Servlet.init` method on it, passing in the ServletConfig of this Servlet proxy.

Parameters:
  `wac` - the root application context
Returns:
  the initialized delegate Filter
Throws:
  `ServletException` - if thrown by the servlet or if no bean name can be found

### invokeDelegate

protected void invokeDelegate(@Nonnull Servlet del, @Nonnull ServletRequest request, @Nonnull ServletResponse response) throws ServletException, IOException
Actually invoke the delegate Servlet with the given request and response.

Parameters:
  `del` - the delegate Servlet
  `request` - the current request
  `response` - the current response
Throws:
  `ServletException` - if thrown by the Servlet
  `IOException` - if thrown by the Servlet

### destroyDelegate

protected void destroyDelegate(@Nonnull Servlet del)
Destroy the Servlet delegate.

Parameters:
  `del` - the Servlet delegate