Skip to content

Commit

Permalink
Issue #5137 - Adding test case to demonstrate usage.
Browse files Browse the repository at this point in the history
+ Partially reverting change in commit 31175e9

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
  • Loading branch information
joakime committed Sep 16, 2020
1 parent 31175e9 commit 61ffdc2
Show file tree
Hide file tree
Showing 5 changed files with 355 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.eclipse.jetty.servlet;

import java.io.IOException;
import java.util.function.BiFunction;
import javax.servlet.ServletContext;
import javax.servlet.UnavailableException;

Expand Down Expand Up @@ -185,25 +186,30 @@ public synchronized boolean isInstance()
return _instance != null;
}

protected T wrap(T component, Class<? extends BaseWrapFunction<T>> wrapperType)
protected <C, W> C wrap(final C component, final Class<W> wrapperFunctionType, final BiFunction<W, C, C> function)
{
C ret = component;
ServletContextHandler contextHandler = getServletHandler().getServletContextHandler();
if (contextHandler != null)
{
for (BaseWrapFunction<T> wrapperFunction : contextHandler.getBeans(wrapperType))
component = wrapperFunction.wrap(component);
for (W wrapperFunction : contextHandler.getBeans(wrapperFunctionType))
{
ret = function.apply(wrapperFunction, ret);
}
}
return component;
return ret;
}

protected T unwrap(T component)
protected <C> C unwrap(final C component)
{
while (component instanceof Wrapped)
C ret = component;

while (ret instanceof Wrapped)
{
// noinspection unchecked,rawtypes
component = (T)((Wrapped<T>)component).getWrapped();
ret = (C)((Wrapped)ret).getWrapped();
}
return component;
return ret;
}

@Override
Expand All @@ -218,13 +224,8 @@ public String dump()
return Dumpable.dump(this);
}

public interface BaseWrapFunction<T>
{
T wrap(T component);
}

interface Wrapped<T>
interface Wrapped<C>
{
T getWrapped();
C getWrapped();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public void initialize() throws Exception
throw ex;
}
}
_filter = wrap(_filter, WrapFunction.class);
_filter = wrap(_filter, WrapFunction.class, WrapFunction::wrapFilter);
_config = new Config();
if (LOG.isDebugEnabled())
LOG.debug("Filter.init {}", _filter);
Expand Down Expand Up @@ -297,8 +297,16 @@ public String getFilterName()
* (before their {@link Filter#init(FilterConfig)} method is called)
* </p>
*/
public interface WrapFunction extends BaseWrapFunction<Filter>
{}
public interface WrapFunction
{
/**
* Optionally wrap the Filter.
*
* @param filter the Filter being passed in.
* @return the Filter (extend from {@link FilterHolder.Wrapper} if you do wrap the Filter)
*/
Filter wrapFilter(Filter filter);
}

public static class Wrapper implements Filter, Wrapped<Filter>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void doStart() throws Exception
throw ex;
}
}
_listener = wrap(_listener, WrapperFunction.class);
_listener = wrap(_listener, WrapFunction.class, WrapFunction::wrapEventListener);
contextHandler.addEventListener(_listener);
}
}
Expand Down Expand Up @@ -141,8 +141,17 @@ public String toString()
* they are used for the first time.
* </p>
*/
public interface WrapperFunction extends BaseWrapFunction<EventListener>
{}
public interface WrapFunction
{
/**
* Optionally wrap the Servlet EventListener.
*
* @param listener the Servlet EventListener being passed in.
* @return the Servlet EventListener (extend from {@link ListenerHolder.Wrapper}
* if you do wrap the Servlet EventListener)
*/
EventListener wrapEventListener(EventListener listener);
}

public static class Wrapper implements EventListener, Wrapped<EventListener>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ else if (_forcedPath != null)
detectJspContainer();

initMultiPart();
_servlet = wrap(_servlet, WrapperFunction.class);
_servlet = wrap(_servlet, WrapFunction.class, WrapFunction::wrapServlet);

if (LOG.isDebugEnabled())
LOG.debug("Servlet.init {} for {}", _servlet, getName());
Expand Down Expand Up @@ -1281,8 +1281,16 @@ public UnavailableException getUnavailableException()
* (before their {@link Servlet#init(ServletConfig)} method is called)
* </p>
*/
public interface WrapperFunction extends BaseWrapFunction<Servlet>
{}
public interface WrapFunction
{
/**
* Optionally wrap the Servlet.
*
* @param servlet the servlet being passed in.
* @return the servlet (extend from {@link ServletHolder.Wrapper} if you do wrap the Servlet)
*/
Servlet wrapServlet(Servlet servlet);
}

public static class Wrapper implements Servlet, Wrapped<Servlet>
{
Expand Down
Loading

0 comments on commit 61ffdc2

Please sign in to comment.