servletPathLocal += "*";
} else {
servletPathLocal += "/*";
}
final WebappContext context = new WebappContext("TestContext", contextPathLocal);
// servlet class and servlet instance can be both null or one of them is specified exclusively.
final HttpServlet servletInstance = deploymentContext.getServletInstance();
final Class<? extends HttpServlet> servletClass = deploymentContext.getServletClass();
if (servletInstance != null || servletClass != null) {
final ServletRegistration registration;
if (servletInstance != null) {
registration = context.addServlet(servletInstance.getClass().getName(), servletInstance);
} else {
registration = context.addServlet(servletClass.getName(), servletClass);
}
registration.setInitParameters(deploymentContext.getInitParams());
registration.addMapping(servletPathLocal);
}
for (final Class<? extends EventListener> eventListener : deploymentContext.getListeners()) {
context.addListener(eventListener);
}
final Map<String, String> contextParams = deploymentContext.getContextParams();
for (final String contextParamName : contextParams.keySet()) {
context.addContextInitParameter(contextParamName, contextParams.get(contextParamName));
}
// Filter support
if (deploymentContext.getFilters() != null) {
for (final ServletDeploymentContext.FilterDescriptor filterDescriptor : deploymentContext.getFilters()) {
final FilterRegistration filterRegistration =
context.addFilter(filterDescriptor.getFilterName(), filterDescriptor.getFilterClass());
filterRegistration.setInitParameters(filterDescriptor.getInitParams());
filterRegistration.addMappingForUrlPatterns(
grizzlyDispatcherTypes(filterDescriptor.getDispatcherTypes()),
true,
servletPathLocal);
}
}
try {
server = GrizzlyHttpServerFactory.createHttpServer(baseUri, (GrizzlyHttpContainer) null, false, null, false);
context.deploy(server);
} catch (final ProcessingException ex) {
throw new TestContainerException(ex);
}
}