Skip to content

Commit

Permalink
Issue #11260 - Allow QuickStartConfiguration to be used in mixed co…
Browse files Browse the repository at this point in the history
…ntexts environment where some do not have a WEB-INF/quickstart-web.xml

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
  • Loading branch information
joakime committed Jan 11, 2024
1 parent dc96d91 commit ecc5853
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class QuickStartConfiguration extends AbstractConfiguration
public static final String ORIGIN_ATTRIBUTE = "org.eclipse.jetty.quickstart.origin";
public static final String QUICKSTART_WEB_XML = "org.eclipse.jetty.quickstart.xml";
public static final String MODE = "org.eclipse.jetty.quickstart.mode";
public static final String QUICKSTART_ENABLED = "org.eclipse.jetty.quickstart.enabled";

static
{
Expand Down Expand Up @@ -77,7 +78,6 @@ public enum Mode
}

private Mode _mode = Mode.AUTO;
private boolean _quickStart;

public QuickStartConfiguration()
{
Expand All @@ -86,6 +86,14 @@ public QuickStartConfiguration()
addDependents(WebXmlConfiguration.class);
}

private boolean isQuickStartEnabled(WebAppContext context)
{
Boolean enabled = (Boolean)context.getAttribute(QUICKSTART_ENABLED);
if (enabled == null)
return true; // default is true
return enabled;
}

@Override
public void preConfigure(WebAppContext context) throws Exception
{
Expand All @@ -102,9 +110,10 @@ public void preConfigure(WebAppContext context) throws Exception
Mode mode = (Mode)context.getAttribute(MODE);
if (mode != null)
_mode = mode;

_quickStart = false;


// disable quickstart for this context (it is only enabled if AUTO or QUICKSTART mode succeeds)
context.setAttribute(QUICKSTART_ENABLED, false);

switch (_mode)
{
case GENERATE:
Expand Down Expand Up @@ -139,7 +148,9 @@ public void preConfigure(WebAppContext context) throws Exception
if (quickStartWebXml.exists())
quickStart(context);
else
throw new IllegalStateException("No " + quickStartWebXml);
{
LOG.warn("Skipping {} for {} as it does not contain {}", this.getClass().getName(), context, quickStartWebXml);
}
break;

default:
Expand All @@ -160,37 +171,42 @@ protected void configure(QuickStartGeneratorConfiguration generator, WebAppConte
@Override
public void configure(WebAppContext context) throws Exception
{
if (!_quickStart)
if (!isQuickStartEnabled(context))
{
super.configure(context);
return;
}
else
{
//add the processor to handle normal web.xml content
context.getMetaData().addDescriptorProcessor(new StandardDescriptorProcessor());

//add a processor to handle extended web.xml format
context.getMetaData().addDescriptorProcessor(new QuickStartDescriptorProcessor());
//add the processor to handle normal web.xml content
context.getMetaData().addDescriptorProcessor(new StandardDescriptorProcessor());

//add a decorator that will find introspectable annotations
context.getObjectFactory().addDecorator(new AnnotationDecorator(context)); //this must be the last Decorator because they are run in reverse order!
//add a processor to handle extended web.xml format
context.getMetaData().addDescriptorProcessor(new QuickStartDescriptorProcessor());

//add a context bean that will run ServletContainerInitializers as the context starts
ServletContainerInitializersStarter starter = (ServletContainerInitializersStarter)context.getAttribute(AnnotationConfiguration.CONTAINER_INITIALIZER_STARTER);
if (starter != null)
throw new IllegalStateException("ServletContainerInitializersStarter already exists");
starter = new ServletContainerInitializersStarter(context);
context.setAttribute(AnnotationConfiguration.CONTAINER_INITIALIZER_STARTER, starter);
context.addBean(starter, true);
//add a decorator that will find introspectable annotations
context.getObjectFactory().addDecorator(new AnnotationDecorator(context)); //this must be the last Decorator because they are run in reverse order!

LOG.debug("configured {}", this);
}
//add a context bean that will run ServletContainerInitializers as the context starts
ServletContainerInitializersStarter starter = (ServletContainerInitializersStarter)context.getAttribute(AnnotationConfiguration.CONTAINER_INITIALIZER_STARTER);
if (starter != null)
throw new IllegalStateException("ServletContainerInitializersStarter already exists");
starter = new ServletContainerInitializersStarter(context);
context.setAttribute(AnnotationConfiguration.CONTAINER_INITIALIZER_STARTER, starter);
context.addBean(starter, true);

LOG.debug("configured {}", this);
}

@Override
public void postConfigure(WebAppContext context) throws Exception
{
super.postConfigure(context);
if (!isQuickStartEnabled(context))
{
super.postConfigure(context);
return;
}

context.removeAttribute(QUICKSTART_ENABLED);

ServletContainerInitializersStarter starter = (ServletContainerInitializersStarter)context.getAttribute(AnnotationConfiguration.CONTAINER_INITIALIZER_STARTER);
if (starter != null)
{
Expand All @@ -203,7 +219,7 @@ protected void quickStart(WebAppContext context)
throws Exception
{
LOG.info("Quickstarting {}", context);
_quickStart = true;
context.setAttribute(QUICKSTART_ENABLED, true);
context.setConfigurations(context.getConfigurations().stream()
.filter(c -> !__replacedConfigurations.contains(c.replaces()) && !__replacedConfigurations.contains(c.getClass()))
.collect(Collectors.toList()).toArray(new Configuration[]{}));
Expand All @@ -229,7 +245,10 @@ public Resource getQuickStartWebXml(WebAppContext context) throws Exception
if (webInf == null || !webInf.exists())
{
File tmp = new File(context.getBaseResource().getFile(), "WEB-INF");
tmp.mkdirs();
if (!tmp.mkdirs())
{
throw new IllegalStateException("Unable to create directory " + tmp);
}
webInf = context.getWebInf();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.eclipse.jetty.plus.webapp.PlusConfiguration;
import org.eclipse.jetty.server.NetworkConnector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.toolchain.test.FS;
import org.eclipse.jetty.toolchain.test.MavenPaths;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.resource.PathResource;
Expand All @@ -41,6 +43,41 @@

public class QuickStartTest
{
/**
* Test of an exploded webapp directory, no WEB-INF/quickstart-web.xml,
* with QuickStartConfiguration enabled.
*/
@Test
public void testExplodedWebAppDirNoWebXml() throws Exception
{
Path jettyHome = MavenPaths.targetDir();
Path webappDir = MavenPaths.targetTestDir("no-web-xml");
Path src = MavenPaths.projectBase().resolve("src/test/webapps/no-web-xml");
FS.ensureEmpty(webappDir);
org.eclipse.jetty.toolchain.test.IO.copyDir(src, webappDir);

System.setProperty("jetty.home", jettyHome.toString());

Server server = new Server(0);

WebAppContext webapp = new WebAppContext();
webapp.addConfiguration(new QuickStartConfiguration(),
new EnvConfiguration(),
new PlusConfiguration(),
new AnnotationConfiguration());
webapp.setAttribute(QuickStartConfiguration.MODE, QuickStartConfiguration.Mode.QUICKSTART);
webapp.setWarResource(new PathResource(webappDir));
webapp.setContextPath("/");
server.setHandler(webapp);
server.start();

URL url = new URL("http://127.0.0.1:" + server.getBean(NetworkConnector.class).getLocalPort() + "/index.html");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
assertEquals(200, connection.getResponseCode());
assertThat(IO.toString((InputStream)connection.getContent()), Matchers.containsString("<p>Contents of no-web-xml</p>"));

server.stop();
}

@Test
public void testStandardTestWar() throws Exception
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>Contents of no-web-xml</p>

0 comments on commit ecc5853

Please sign in to comment.