Skip to content

Commit

Permalink
Add support for other ServletEnvironment instances (#499)
Browse files Browse the repository at this point in the history
The new `WebBundle#getServletEnvironment(Environment)` method allows for specifying a different `ServletEnvironment` to which dropwizard-web should be applied.

Refs #383
  • Loading branch information
joschi authored Feb 13, 2024
1 parent de51153 commit d7a1a49
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 6 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,17 @@ public class ExampleConfiguration extends Configuration {

Add a `WebBundle` to the `Boostrap` object in your `initialize` method:
```java
bootstrap.addBundle(new WebBundle<ExampleConfiguration>() {
bootstrap.addBundle(new WebBundle<>() {
@Override
public WebConfiguration getWebConfiguration(final ExampleConfiguration configuration) {
return configuration.getWebConfiguration();
}

// Optional: Override Servlet environment to apply the configuration to the admin servlets
@Override
protected ServletEnvironment getServletEnvironment(Environment environment) {
return environment.admin();
}
});
```

Expand Down Expand Up @@ -78,7 +84,7 @@ This minimal config results in the following:
Support for CORS or CSP require additional configuration.

## Maven Artifacts
This project is available on Maven Central. To add it to your project simply add the following dependencies to your
This project is available on Maven Central. To add it to your project simply add the following dependencies to your
`pom.xml`:
```xml
<dependency>
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/io/dropwizard/web/WebBundle.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.dropwizard.core.ConfiguredBundle;
import io.dropwizard.core.setup.Bootstrap;
import io.dropwizard.core.setup.Environment;
import io.dropwizard.jetty.setup.ServletEnvironment;
import io.dropwizard.web.conf.WebConfiguration;
import org.eclipse.jetty.servlets.HeaderFilter;

Expand Down Expand Up @@ -71,7 +72,7 @@ protected void configureHeaderFilter(Environment environment,
.map(entry -> "set " + entry.getKey() + ": " + entry.getValue())
.collect(Collectors.joining(","));
final Map<String, String> filterConfig = Collections.singletonMap("headerConfig", headerConfig);
final FilterRegistration.Dynamic filter = environment.servlets()
final FilterRegistration.Dynamic filter = getServletEnvironment(environment)
.addFilter("header-filter-" + uriPath, HeaderFilter.class);
filter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, urlPattern);
filter.setInitParameters(filterConfig);
Expand All @@ -85,5 +86,15 @@ private String deriveUrlPattern(String uri) {
}
}

/**
* Define which {@link ServletEnvironment} should be configured. Default is {@link Environment#servlets()}.
*
* @param environment The environment of the Dropwizard application
* @return The {@link ServletEnvironment} to configure
*/
protected ServletEnvironment getServletEnvironment(Environment environment) {
return environment.servlets();
}

public abstract WebConfiguration getWebConfiguration(T config);
}
16 changes: 14 additions & 2 deletions src/test/java/io/dropwizard/web/test/TestApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.dropwizard.core.Application;
import io.dropwizard.core.setup.Bootstrap;
import io.dropwizard.core.setup.Environment;
import io.dropwizard.jetty.setup.ServletEnvironment;
import io.dropwizard.web.WebBundle;
import io.dropwizard.web.conf.WebConfiguration;

Expand All @@ -13,18 +14,29 @@ public class TestApp extends Application<TestConfig> {

@Override
public void initialize(Bootstrap<TestConfig> bootstrap) {
bootstrap.addBundle(new WebBundle<TestConfig>() {
bootstrap.addBundle(new WebBundle<>() {
@Override
public WebConfiguration getWebConfiguration(TestConfig config) {
return config.getWeb1Configuration();
}
});
bootstrap.addBundle(new WebBundle<TestConfig>() {
bootstrap.addBundle(new WebBundle<>() {
@Override
public WebConfiguration getWebConfiguration(TestConfig config) {
return config.getWeb2Configuration();
}
});
bootstrap.addBundle(new WebBundle<>() {
@Override
public WebConfiguration getWebConfiguration(TestConfig config) {
return config.getAdminWebConfiguration();
}

@Override
protected ServletEnvironment getServletEnvironment(Environment environment) {
return environment.admin();
}
});
}

@Override
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/io/dropwizard/web/test/TestConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public class TestConfig extends Configuration {
@JsonProperty("web2")
private WebConfiguration web2Configuration = new WebConfiguration();

@Valid
@NotNull
@JsonProperty("webAdmin")
private WebConfiguration adminWebConfiguration = new WebConfiguration();

public WebConfiguration getWeb1Configuration() {
return web1Configuration;
}
Expand All @@ -26,11 +31,19 @@ public WebConfiguration getWeb2Configuration() {
return web2Configuration;
}

public WebConfiguration getAdminWebConfiguration() {
return adminWebConfiguration;
}

public void setWeb1Configuration(WebConfiguration webConfiguration) {
this.web1Configuration = webConfiguration;
}

public void setWeb2Configuration(WebConfiguration webConfiguration) {
this.web2Configuration = webConfiguration;
}

public void setAdminWebConfiguration(WebConfiguration adminWebConfiguration) {
this.adminWebConfiguration = adminWebConfiguration;
}
}
5 changes: 5 additions & 0 deletions src/test/java/io/dropwizard/web/test/WebFilterIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ public class WebFilterIT {

private Client client;
private String hostUrl;
private String adminUrl;

@BeforeEach
public void setUp() throws Exception {
client = EXT.client();
hostUrl = "http://localhost:" + EXT.getLocalPort();
adminUrl = "http://localhost:" + EXT.getAdminPort() + "/admin";
}

@AfterEach
Expand All @@ -38,10 +40,12 @@ public void testHeaders() {
// given
String uri1 = hostUrl + "/test/one";
String uri2 = hostUrl + "/test/two";
String adminUri = adminUrl + "/ping";

// when
Response response1 = client.target(uri1).request().get();
Response response2 = client.target(uri2).request().get();
Response adminResponse = client.target(adminUri).request().get();

// then
assertThat(response1.readEntity(String.class), is("test response 1"));
Expand All @@ -54,6 +58,7 @@ public void testHeaders() {
assertThat(response1.getHeaderString("X-Custom-Header-2"), is("custom value 2"));
assertThat(response2.readEntity(String.class), is("test response 2"));
assertThat(response2.getHeaderString("X-Second-Custom-Header"), is("custom value"));
assertThat(adminResponse.getHeaderString("X-Admin-Custom-Header"), is("custom value"));
}

}
6 changes: 5 additions & 1 deletion src/test/resources/conf/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ web2:
headers:
enabled: true
X-Second-Custom-Header: custom value

webAdmin:
uriPath: /
headers:
enabled: true
X-Admin-Custom-Header: custom value
server:
type: simple
applicationContextPath: /
Expand Down

0 comments on commit d7a1a49

Please sign in to comment.