Skip to content

Commit

Permalink
Cleanup after #2903 (#2933)
Browse files Browse the repository at this point in the history
Improved dump output of ServletHandler and ContextHandler
Fixed duplicate listeners
Removed unused fields

Signed-off-by: Greg Wilkins <[email protected]>
  • Loading branch information
gregw authored Oct 3, 2018
1 parent baca9ca commit 9d37feb
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,23 @@

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ListenerHolder;
import org.eclipse.jetty.servlet.ServletContextHandler;

import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.ServletResponse;
import java.io.IOException;
import java.util.EnumSet;

public class OneServletContext
{
public static void main( String[] args ) throws Exception
Expand All @@ -35,11 +50,72 @@ public static void main( String[] args ) throws Exception
server.setHandler(context);

// Add dump servlet
context.addServlet(DumpServlet.class, "/dump/*");
// Add default servlet
context.addServlet(
context.addServlet(DumpServlet.class, "/dump/*"),
"*.dump");
context.addServlet(HelloServlet.class, "/hello/*");
context.addServlet(DefaultServlet.class, "/");

context.addFilter(TestFilter.class,"/*", EnumSet.of(DispatcherType.REQUEST));
context.addFilter(TestFilter.class,"/test", EnumSet.of(DispatcherType.REQUEST,DispatcherType.ASYNC));
context.addFilter(TestFilter.class,"*.test", EnumSet.of(DispatcherType.REQUEST,DispatcherType.INCLUDE,DispatcherType.FORWARD));

context.getServletHandler().addListener(new ListenerHolder(InitListener.class));
context.getServletHandler().addListener(new ListenerHolder(RequestListener.class));

server.start();
server.dumpStdErr();
server.join();
}


public static class TestFilter implements Filter
{
@Override
public void init(FilterConfig filterConfig) throws ServletException
{

}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
chain.doFilter(request, response);
}

@Override
public void destroy()
{

}
}

public static class InitListener implements ServletContextListener
{
@Override
public void contextInitialized(ServletContextEvent sce)
{
}

@Override
public void contextDestroyed(ServletContextEvent sce)
{
}
}


public static class RequestListener implements ServletRequestListener
{
@Override
public void requestDestroyed(ServletRequestEvent sre)
{

}

@Override
public void requestInitialized(ServletRequestEvent sre)
{

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ public static void setServerInfo(String serverInfo)
private final List<ServletRequestAttributeListener> _servletRequestAttributeListeners = new CopyOnWriteArrayList<>();
private final List<ContextScopeListener> _contextListeners = new CopyOnWriteArrayList<>();
private final List<EventListener> _durableListeners = new CopyOnWriteArrayList<>();
private Map<String, Object> _managedAttributes;
private String[] _protectedTargets;
private final CopyOnWriteArrayList<AliasCheck> _aliasChecks = new CopyOnWriteArrayList<ContextHandler.AliasCheck>();

Expand Down Expand Up @@ -258,9 +257,10 @@ else if (parent instanceof HandlerCollection)
public void dump(Appendable out, String indent) throws IOException
{
dumpBeans(out,indent,Collections.singletonList(new ClassLoaderDump(getClassLoader())),
Collections.singletonList(new DumpableCollection("Handler attributes " + this,((AttributesMap)getAttributes()).getAttributeEntrySet())),
Collections.singletonList(new DumpableCollection("Context attributes " + this,((Context)getServletContext()).getAttributeEntrySet())),
Collections.singletonList(new DumpableCollection("Initparams " + this,getInitParams().entrySet())));
Collections.singletonList(new DumpableCollection("eventListeners "+this,_eventListeners)),
Collections.singletonList(new DumpableCollection("handler attributes " + this,((AttributesMap)getAttributes()).getAttributeEntrySet())),
Collections.singletonList(new DumpableCollection("context attributes " + this,((Context)getServletContext()).getAttributeEntrySet())),
Collections.singletonList(new DumpableCollection("initparams " + this,getInitParams().entrySet())));
}

/* ------------------------------------------------------------ */
Expand Down Expand Up @@ -1553,10 +1553,9 @@ public void clearAttributes()
}

/* ------------------------------------------------------------ */
@Deprecated
public void setManagedAttribute(String name, Object value)
{
Object old = _managedAttributes.put(name,value);
updateBean(old,value);
}

/* ------------------------------------------------------------ */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
package org.eclipse.jetty.servlet;

import java.io.IOException;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.stream.Collectors;

import javax.servlet.DispatcherType;

Expand Down Expand Up @@ -86,11 +88,31 @@ public static int dispatch(DispatcherType type)
throw new IllegalArgumentException(type.toString());
}

/* ------------------------------------------------------------ */
/** Dispatch type from name
* @param type the dispatcher type
* @return the type constant ({@link #REQUEST}, {@link #ASYNC}, {@link #FORWARD}, {@link #INCLUDE}, or {@link #ERROR})
*/
public static DispatcherType dispatch(int type)
{
switch(type)
{
case REQUEST:
return DispatcherType.REQUEST;
case ASYNC:
return DispatcherType.ASYNC;
case FORWARD:
return DispatcherType.FORWARD;
case INCLUDE:
return DispatcherType.INCLUDE;
case ERROR:
return DispatcherType.ERROR;
}
throw new IllegalArgumentException(Integer.toString(type));
}

/* ------------------------------------------------------------ */
/* ------------------------------------------------------------ */


private int _dispatches=DEFAULT;
private String _filterName;
private transient FilterHolder _holder;
Expand Down Expand Up @@ -122,7 +144,7 @@ boolean appliesTo(String path, int type)
/* ------------------------------------------------------------ */
/** Check if this filter applies to a particular dispatch type.
* @param type The type of request:
* {@link Handler#REQUEST}, {@link Handler#FORWARD}, {@link Handler#INCLUDE} or {@link Handler#ERROR}.
* {@link #REQUEST}, {@link #FORWARD}, {@link #INCLUDE} or {@link #ERROR}.
* @return <code>true</code> if this filter applies
*/
boolean appliesTo(int type)
Expand Down Expand Up @@ -295,9 +317,9 @@ public void setServletName(String servletName)
public String toString()
{
return
TypeUtil.asList(_pathSpecs)+"/"+
TypeUtil.asList(_servletNames)+"=="+
_dispatches+"=>"+
TypeUtil.asList(_pathSpecs)+"/"+
TypeUtil.asList(_servletNames)+"/"+
Arrays.stream(DispatcherType.values()).filter(this::appliesTo).collect(Collectors.toSet())+"=>"+
_filterName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ public ListenerHolder(Source source)
{
super(source);
}


public ListenerHolder(Class<? extends EventListener> listenerClass)
{
super(Source.EMBEDDED);
setHeldClass(listenerClass);
}

public EventListener getListener()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public ServletContextHandler(HandlerContainer parent, String contextPath, Sessio
/* ------------------------------------------------------------ */
public ServletContextHandler(HandlerContainer parent, String contextPath, SessionHandler sessionHandler, SecurityHandler securityHandler, ServletHandler servletHandler, ErrorHandler errorHandler,int options)
{
super((ContextHandler.Context)null);
super(parent, contextPath);
_options=options;
_scontext = new Context();
_sessionHandler = sessionHandler;
Expand All @@ -163,15 +163,6 @@ public ServletContextHandler(HandlerContainer parent, String contextPath, Sessio
_objFactory = new DecoratedObjectFactory();
_objFactory.addDecorator(new DeprecationWarning());

if (contextPath!=null)
setContextPath(contextPath);

if (parent instanceof HandlerWrapper)
((HandlerWrapper)parent).setHandler(this);
else if (parent instanceof HandlerCollection)
((HandlerCollection)parent).addHandler(this);


// Link the handlers
relinkHandlers();

Expand Down
Loading

0 comments on commit 9d37feb

Please sign in to comment.