Issue #4064 - MinimalServlets test and ServletHolder fix

+ Also made ContextHandler warning message about features
  that are unimplemented (and you should use ServletContextHandler)
  more clear. (this helped with diagnosing where the bug was
  in ServletHolder)

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
This commit is contained in:
Joakim Erdfelt 2019-09-06 09:26:06 -05:00
parent 829cccaef7
commit 7618eae915
4 changed files with 106 additions and 36 deletions

View File

@ -19,7 +19,6 @@
package org.eclipse.jetty.embedded; package org.eclipse.jetty.embedded;
import java.io.IOException; import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@ -29,13 +28,13 @@ import org.eclipse.jetty.servlet.ServletHandler;
public class MinimalServlets public class MinimalServlets
{ {
public static void main(String[] args) throws Exception
public static Server createServer(int port)
{ {
// Create a basic jetty server object that will listen on port 8080.
// Note that if you set this to port 0 then a randomly available port // Note that if you set this to port 0 then a randomly available port
// will be assigned that you can either look in the logs for the port, // will be assigned that you can either look in the logs for the port,
// or programmatically obtain it for use in test cases. // or programmatically obtain it for use in test cases.
Server server = new Server(8080); Server server = new Server(port);
// The ServletHandler is a dead simple way to create a context handler // The ServletHandler is a dead simple way to create a context handler
// that is backed by an instance of a Servlet. // that is backed by an instance of a Servlet.
@ -51,13 +50,19 @@ public class MinimalServlets
// through a web.xml @WebServlet annotation, or anything similar. // through a web.xml @WebServlet annotation, or anything similar.
handler.addServletWithMapping(HelloServlet.class, "/*"); handler.addServletWithMapping(HelloServlet.class, "/*");
return server;
}
public static void main(String[] args) throws Exception
{
// Create a basic jetty server object that will listen on port 8080.
Server server = createServer(8080);
// Start things up! // Start things up!
server.start(); server.start();
// The use of server.join() the will make the current thread join and // The use of server.join() the will make the current thread join and
// wait until the server is done executing. // wait until the server thread is done executing.
// See
// http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join()
server.join(); server.join();
} }
@ -66,11 +71,11 @@ public class MinimalServlets
{ {
@Override @Override
protected void doGet(HttpServletRequest request, protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, HttpServletResponse response) throws IOException
IOException
{ {
response.setContentType("text/html");
response.setStatus(HttpServletResponse.SC_OK); response.setStatus(HttpServletResponse.SC_OK);
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
response.getWriter().println("<h1>Hello from HelloServlet</h1>"); response.getWriter().println("<h1>Hello from HelloServlet</h1>");
} }
} }

View File

@ -0,0 +1,64 @@
//
// ========================================================================
// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.embedded;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;
import org.eclipse.jetty.server.Server;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
public class MinimalServletsTest
{
private Server server;
@BeforeEach
public void startServer() throws Exception
{
server = MinimalServlets.createServer(0);
server.start();
}
@AfterEach
public void stopServer() throws Exception
{
server.stop();
}
@Test
public void testGetHello() throws IOException
{
URI uri = server.getURI().resolve("/hello");
HttpURLConnection http = (HttpURLConnection)uri.toURL().openConnection();
assertThat("HTTP Response Status", http.getResponseCode(), is(HttpURLConnection.HTTP_OK));
// HttpUtil.dumpResponseHeaders(http);
// test response content
String responseBody = HttpUtil.getResponseBody(http);
assertThat("Response Content", responseBody, containsString("Hello"));
}
}

View File

@ -126,7 +126,7 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
public static final int DEFAULT_LISTENER_TYPE_INDEX = 1; public static final int DEFAULT_LISTENER_TYPE_INDEX = 1;
public static final int EXTENDED_LISTENER_TYPE_INDEX = 0; public static final int EXTENDED_LISTENER_TYPE_INDEX = 0;
private static final String __unimplmented = "Unimplemented - use org.eclipse.jetty.servlet.ServletContextHandler"; private static final String UNIMPLEMENTED_USE_SERVLET_CONTEXT_HANDLER = "Unimplemented {} - use org.eclipse.jetty.servlet.ServletContextHandler";
private static final Logger LOG = Log.getLogger(ContextHandler.class); private static final Logger LOG = Log.getLogger(ContextHandler.class);
@ -2479,7 +2479,7 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
@Override @Override
public JspConfigDescriptor getJspConfigDescriptor() public JspConfigDescriptor getJspConfigDescriptor()
{ {
LOG.warn(__unimplmented); LOG.warn(UNIMPLEMENTED_USE_SERVLET_CONTEXT_HANDLER, "getJspConfigDescriptor()");
return null; return null;
} }
@ -2673,130 +2673,130 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
@Override @Override
public Dynamic addFilter(String filterName, Class<? extends Filter> filterClass) public Dynamic addFilter(String filterName, Class<? extends Filter> filterClass)
{ {
LOG.warn(__unimplmented); LOG.warn(UNIMPLEMENTED_USE_SERVLET_CONTEXT_HANDLER, "addFilter(String, Class)");
return null; return null;
} }
@Override @Override
public Dynamic addFilter(String filterName, Filter filter) public Dynamic addFilter(String filterName, Filter filter)
{ {
LOG.warn(__unimplmented); LOG.warn(UNIMPLEMENTED_USE_SERVLET_CONTEXT_HANDLER, "addFilter(String, Filter)");
return null; return null;
} }
@Override @Override
public Dynamic addFilter(String filterName, String className) public Dynamic addFilter(String filterName, String className)
{ {
LOG.warn(__unimplmented); LOG.warn(UNIMPLEMENTED_USE_SERVLET_CONTEXT_HANDLER, "addFilter(String, String)");
return null; return null;
} }
@Override @Override
public javax.servlet.ServletRegistration.Dynamic addServlet(String servletName, Class<? extends Servlet> servletClass) public javax.servlet.ServletRegistration.Dynamic addServlet(String servletName, Class<? extends Servlet> servletClass)
{ {
LOG.warn(__unimplmented); LOG.warn(UNIMPLEMENTED_USE_SERVLET_CONTEXT_HANDLER, "addServlet(String, Class)");
return null; return null;
} }
@Override @Override
public javax.servlet.ServletRegistration.Dynamic addServlet(String servletName, Servlet servlet) public javax.servlet.ServletRegistration.Dynamic addServlet(String servletName, Servlet servlet)
{ {
LOG.warn(__unimplmented); LOG.warn(UNIMPLEMENTED_USE_SERVLET_CONTEXT_HANDLER, "addServlet(String, Servlet)");
return null; return null;
} }
@Override @Override
public javax.servlet.ServletRegistration.Dynamic addServlet(String servletName, String className) public javax.servlet.ServletRegistration.Dynamic addServlet(String servletName, String className)
{ {
LOG.warn(__unimplmented); LOG.warn(UNIMPLEMENTED_USE_SERVLET_CONTEXT_HANDLER, "addServlet(String, String)");
return null; return null;
} }
@Override @Override
public <T extends Filter> T createFilter(Class<T> c) throws ServletException public <T extends Filter> T createFilter(Class<T> c) throws ServletException
{ {
LOG.warn(__unimplmented); LOG.warn(UNIMPLEMENTED_USE_SERVLET_CONTEXT_HANDLER, "createFilter(Class)");
return null; return null;
} }
@Override @Override
public <T extends Servlet> T createServlet(Class<T> c) throws ServletException public <T extends Servlet> T createServlet(Class<T> c) throws ServletException
{ {
LOG.warn(__unimplmented); LOG.warn(UNIMPLEMENTED_USE_SERVLET_CONTEXT_HANDLER, "createServlet(Class)");
return null; return null;
} }
@Override @Override
public Set<SessionTrackingMode> getDefaultSessionTrackingModes() public Set<SessionTrackingMode> getDefaultSessionTrackingModes()
{ {
LOG.warn(__unimplmented); LOG.warn(UNIMPLEMENTED_USE_SERVLET_CONTEXT_HANDLER, "getDefaultSessionTrackingModes()");
return null; return null;
} }
@Override @Override
public Set<SessionTrackingMode> getEffectiveSessionTrackingModes() public Set<SessionTrackingMode> getEffectiveSessionTrackingModes()
{ {
LOG.warn(__unimplmented); LOG.warn(UNIMPLEMENTED_USE_SERVLET_CONTEXT_HANDLER, "getEffectiveSessionTrackingModes()");
return null; return null;
} }
@Override @Override
public FilterRegistration getFilterRegistration(String filterName) public FilterRegistration getFilterRegistration(String filterName)
{ {
LOG.warn(__unimplmented); LOG.warn(UNIMPLEMENTED_USE_SERVLET_CONTEXT_HANDLER, "getFilterRegistration(String)");
return null; return null;
} }
@Override @Override
public Map<String, ? extends FilterRegistration> getFilterRegistrations() public Map<String, ? extends FilterRegistration> getFilterRegistrations()
{ {
LOG.warn(__unimplmented); LOG.warn(UNIMPLEMENTED_USE_SERVLET_CONTEXT_HANDLER, "getFilterRegistrations()");
return null; return null;
} }
@Override @Override
public ServletRegistration getServletRegistration(String servletName) public ServletRegistration getServletRegistration(String servletName)
{ {
LOG.warn(__unimplmented); LOG.warn(UNIMPLEMENTED_USE_SERVLET_CONTEXT_HANDLER, "getServletRegistration(String)");
return null; return null;
} }
@Override @Override
public Map<String, ? extends ServletRegistration> getServletRegistrations() public Map<String, ? extends ServletRegistration> getServletRegistrations()
{ {
LOG.warn(__unimplmented); LOG.warn(UNIMPLEMENTED_USE_SERVLET_CONTEXT_HANDLER, "getServletRegistrations()");
return null; return null;
} }
@Override @Override
public SessionCookieConfig getSessionCookieConfig() public SessionCookieConfig getSessionCookieConfig()
{ {
LOG.warn(__unimplmented); LOG.warn(UNIMPLEMENTED_USE_SERVLET_CONTEXT_HANDLER, "getSessionCookieConfig()");
return null; return null;
} }
@Override @Override
public void setSessionTrackingModes(Set<SessionTrackingMode> sessionTrackingModes) public void setSessionTrackingModes(Set<SessionTrackingMode> sessionTrackingModes)
{ {
LOG.warn(__unimplmented); LOG.warn(UNIMPLEMENTED_USE_SERVLET_CONTEXT_HANDLER, "setSessionTrackingModes(Set<SessionTrackingMode>)");
} }
@Override @Override
public void addListener(String className) public void addListener(String className)
{ {
LOG.warn(__unimplmented); LOG.warn(UNIMPLEMENTED_USE_SERVLET_CONTEXT_HANDLER, "addListener(String)");
} }
@Override @Override
public <T extends EventListener> void addListener(T t) public <T extends EventListener> void addListener(T t)
{ {
LOG.warn(__unimplmented); LOG.warn(UNIMPLEMENTED_USE_SERVLET_CONTEXT_HANDLER, "addListener(T)");
} }
@Override @Override
public void addListener(Class<? extends EventListener> listenerClass) public void addListener(Class<? extends EventListener> listenerClass)
{ {
LOG.warn(__unimplmented); LOG.warn(UNIMPLEMENTED_USE_SERVLET_CONTEXT_HANDLER, "addListener(Class)");
} }
@Override @Override
@ -2843,14 +2843,14 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
@Override @Override
public JspConfigDescriptor getJspConfigDescriptor() public JspConfigDescriptor getJspConfigDescriptor()
{ {
LOG.warn(__unimplmented); LOG.warn(UNIMPLEMENTED_USE_SERVLET_CONTEXT_HANDLER, "getJspConfigDescriptor()");
return null; return null;
} }
@Override @Override
public void declareRoles(String... roleNames) public void declareRoles(String... roleNames)
{ {
LOG.warn(__unimplmented); LOG.warn(UNIMPLEMENTED_USE_SERVLET_CONTEXT_HANDLER, "declareRoles(String...)");
} }
@Override @Override

View File

@ -1265,9 +1265,10 @@ public class ServletHolder extends Holder<Servlet> implements UserIdentity.Scope
try try
{ {
ServletContext ctx = getServletHandler().getServletContext(); ServletContext ctx = getServletHandler().getServletContext();
if (ctx == null) if (ctx instanceof ServletContextHandler.Context)
return getHeldClass().getDeclaredConstructor().newInstance(); return ctx.createServlet(getHeldClass());
return ctx.createServlet(getHeldClass()); return getHeldClass().getDeclaredConstructor().newInstance();
} }
catch (ServletException ex) catch (ServletException ex)
{ {