-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose API for configuring the WebAppContext in Jetty (#29)
- Loading branch information
1 parent
5801f7f
commit f241988
Showing
8 changed files
with
133 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/main/java/io/logz/guice/jersey/configuration/JerseyWebApplicationConfigurator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package io.logz.guice.jersey.configuration; | ||
|
||
import org.eclipse.jetty.webapp.WebAppContext; | ||
|
||
/** | ||
* Implement to provide additional configuration for the underlying {@link org.eclipse.jetty.webapp.WebAppContext}, | ||
* like adding a {@link javax.servlet.Filter} or {@link java.util.EventListener}, etc. | ||
*/ | ||
@FunctionalInterface | ||
public interface JerseyWebApplicationConfigurator { | ||
void configure(WebAppContext webAppContext); | ||
} |
45 changes: 45 additions & 0 deletions
45
src/test/java/io/logz/guice/jersey/JettyFilterRegistrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package io.logz.guice.jersey; | ||
|
||
import io.logz.guice.jersey.configuration.JerseyConfiguration; | ||
import io.logz.guice.jersey.configuration.JerseyConfigurationBuilder; | ||
import io.logz.guice.jersey.filters.AddHeaderJettyFilter; | ||
import io.logz.guice.jersey.resources.TestResource; | ||
import io.logz.guice.jersey.supplier.JerseyServerSupplier; | ||
import org.eclipse.jetty.server.Server; | ||
import org.eclipse.jetty.servlet.FilterHolder; | ||
import org.junit.Test; | ||
|
||
import javax.servlet.DispatcherType; | ||
import javax.ws.rs.core.Response; | ||
import java.util.EnumSet; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class JettyFilterRegistrationTest { | ||
|
||
@Test | ||
public void testJettyFilter() throws Exception { | ||
JerseyConfigurationBuilder configurationBuilder = JerseyConfiguration.builder() | ||
.addResourceClass(TestResource.class); | ||
|
||
String myTestValue = "param"; | ||
|
||
JerseyServerSupplier.Tester tester = target -> { | ||
Response response = target.path(TestResource.PATH).request().get(); | ||
String headerParamValue = response.getHeaderString(AddHeaderJettyFilter.TEST_HEADER); | ||
String responseBody = response.readEntity(String.class); | ||
assertEquals(TestResource.MESSAGE, responseBody); | ||
assertEquals(headerParamValue, myTestValue); | ||
}; | ||
|
||
JerseyServerSupplier.createServerAndTest(configurationBuilder, Server::new, tester, webAppContext -> { | ||
FilterHolder filterHolder = new FilterHolder(AddHeaderJettyFilter.class); | ||
Map<String, String> initParams = new HashMap<>(); | ||
initParams.put(AddHeaderJettyFilter.INIT_PARAM_KEY, myTestValue); | ||
filterHolder.setInitParameters(initParams); | ||
webAppContext.addFilter(filterHolder, "/*", EnumSet.allOf(DispatcherType.class)); | ||
}); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/test/java/io/logz/guice/jersey/filters/AddHeaderJettyFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.logz.guice.jersey.filters; | ||
|
||
import javax.servlet.Filter; | ||
import javax.servlet.FilterChain; | ||
import javax.servlet.FilterConfig; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
|
||
public class AddHeaderJettyFilter implements Filter { | ||
|
||
public static final String TEST_HEADER = "X-JETTY-FILTER-TEST-HEADER"; | ||
public static final String INIT_PARAM_KEY = "INIT_PARAM_KEY"; | ||
private String initParamValue; | ||
|
||
@Override | ||
public void init(FilterConfig filterConfig) { | ||
initParamValue = filterConfig.getInitParameter(INIT_PARAM_KEY); | ||
} | ||
|
||
@Override | ||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { | ||
((HttpServletResponse) response).addHeader(TEST_HEADER, initParamValue); | ||
chain.doFilter(request, response); | ||
} | ||
|
||
@Override | ||
public void destroy() { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters