Skip to content

Commit

Permalink
#22 don't register Vaadin servlet manually but rely on Jetty classpat…
Browse files Browse the repository at this point in the history
…h scanning. Fixes #22
  • Loading branch information
mvysny committed Nov 28, 2023
1 parent 0895844 commit a7adc39
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 49 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,21 @@ public class Main {
}
```

This way is not recommended since any annotations on the servlet will be ignored.

### Overriding Vaadin Servlet

Simply introduce a class into your project which extends `VaadinServlet`, then add any necessary annotations,
for example:

```java
@WebServlet(name = "myservlet", urlPatterns = {"/*"}, initParams = @WebInitParam(name = "foo", value = "bar"))
class MyServlet extends VaadinServlet {}
```

By default, Vaadin's `ServletDeployer` will auto-register `VaadinServlet` but it will skip
this kind of registration if there's already another servlet inheriting from `VaadinServlet`.

## Packaging Your Apps

This part documents hints for buildscripts (`pom.xml`/`build.gradle`) of your app. When in doubt, take a look
Expand Down
6 changes: 2 additions & 4 deletions testapp-kotlin/src/main/kotlin/com/example/Bootstrap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,12 @@ class AppShell : AppShellConfigurator
/**
* Tests a custom servlet with an init parameter. Tests for https://github.com/mvysny/vaadin-boot/issues/22
*/
@WebServlet(name = "myservlet", initParams = [WebInitParam(name = "foo", value = "bar")])
@WebServlet(name = "myservlet", urlPatterns = ["/*"], initParams = [WebInitParam(name = "foo", value = "bar")])
class MyServlet : VaadinServlet()

object Main {
@JvmStatic
fun main(args: Array<String>) {
VaadinBoot()
.withServlet(MyServlet::class.java as Class<Servlet>)
.run()
VaadinBoot().run()
}
}
5 changes: 3 additions & 2 deletions testapp-kotlin/src/test/kotlin/com/example/JettyTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class JettyTest {
@BeforeEach
fun startJetty() {
assertFalse(Bootstrap.initialized)
vaadinBoot = VaadinBoot().setPort(44312).localhostOnly().withServlet(MyServlet::class.java as Class<Servlet>)
vaadinBoot = VaadinBoot().setPort(44312).localhostOnly()
vaadinBoot!!.start()
}

Expand All @@ -43,6 +43,7 @@ class JettyTest {
require(response.statusCode() == 200) {
"$response failed: ${response.body()}"
}
println("Vaadin responded with: $response ${response.body()}")
val body = response.body()
assertTrue(body.contains("This file is auto-generated by Vaadin."), body);
}
}
3 changes: 2 additions & 1 deletion testapp/src/test/java/com/example/JettyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public void testAppIsUp() throws Exception {
if (response.statusCode() != 200) {
throw new IOException(response + " failed: " + response.body());
}
System.out.println("Vaadin responded with: " + response + " " + response.body());
final String body = response.body();
assertTrue(body.contains("This file is auto-generated by Vaadin."), body);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,6 @@ public class VaadinBoot {
@VisibleForTesting
int port = Integer.parseInt(Env.getProperty("SERVER_PORT", "server.port", "" + DEFAULT_PORT));

/**
* The VaadinServlet.
*/
@VisibleForTesting
@NotNull
Class<? extends Servlet> servlet;

/**
* Listen on interface handling given host name. Defaults to <code>null</code> which causes Jetty
* to listen on all interfaces.
Expand Down Expand Up @@ -106,17 +99,6 @@ public class VaadinBoot {
*/
private boolean useVirtualThreadsIfAvailable = true;

/**
* Creates the new instance of the Boot launcher.
*/
public VaadinBoot() {
try {
servlet = Class.forName("com.vaadin.flow.server.VaadinServlet").asSubclass(Servlet.class);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}

/**
* Sets the port to listen on. Listens on {@value #DEFAULT_PORT} by default.
* @param port the new port, 1..65535
Expand Down Expand Up @@ -152,17 +134,6 @@ public VaadinBoot localhostOnly() {
return listenOn("localhost");
}

/**
* Bootstraps custom servlet instead of the default <code>com.vaadin.flow.server.VaadinServlet</code>.
* @param vaadinServlet the custom servlet, not null.
* @return this
*/
@NotNull
public VaadinBoot withServlet(@NotNull Class<? extends Servlet> vaadinServlet) {
this.servlet = Objects.requireNonNull(vaadinServlet);
return this;
}

/**
* Change this to e.g. /foo to host your app on a different context root
* @param contextRoot the new context root, e.g. `/foo`.
Expand Down Expand Up @@ -360,7 +331,11 @@ protected WebAppContext createWebAppContext() throws IOException {
final Resource webRoot = Env.findWebRoot(context.getResourceFactory());
context.setBaseResource(webRoot);
context.setContextPath(contextRoot);
context.addServlet(servlet, "/*");

// don't add the servlet this way - the @WebServlet annotation is ignored!
// https://github.com/mvysny/vaadin-boot/issues/22
// context.addServlet(servlet, "/*");

// when the webapp fails to initialize, make sure that start() throws.
context.setThrowUnavailableOnStartupException(true);
if (!disableClasspathScanning) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package com.github.mvysny.vaadinboot;

import com.vaadin.flow.server.VaadinServlet;
import jakarta.servlet.Servlet;
import jakarta.servlet.annotation.WebInitParam;
import jakarta.servlet.annotation.WebServlet;
import org.eclipse.jetty.ee10.webapp.WebAppContext;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -127,12 +123,4 @@ public void scanTestClasspathModifiesWebAppConfig() throws Exception {
final WebAppContext ctx = new VaadinBoot().scanTestClasspath().createWebAppContext();
assertEquals(".*\\.jar|.*/classes/.*|.*/test-classes/.*", ctx.getAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern"));
}

@WebServlet(name = "myservlet", initParams = @WebInitParam(name = "foo", value = "bar"))
public static class MyServlet extends VaadinServlet {}

@Test
public void smokeCustomServlet() throws Exception {
new VaadinBoot().withServlet((Class<? extends Servlet>) MyServlet.class);
}
}

0 comments on commit a7adc39

Please sign in to comment.