ServletRegistration registration; if (c != null) { registration = context.addServlet(c.getName(), c); } else { registration = context.addServlet(servlet.getClass().getName(), servlet);
final ServletRegistration registration; if (servletInstance != null) { registration = context.addServlet(servletInstance.getClass().getName(), servletInstance); } else { registration = context.addServlet(servletClass.getName(), servletClass);
public WebServer(URI endpoint) throws IOException { this.server = GrizzlyServerFactory.createHttpServer(endpoint, new HttpHandler() { @Override public void service(Request rqst, Response rspns) throws Exception { rspns.setStatus(HttpStatus.NOT_FOUND_404.getStatusCode(), "Not found"); rspns.getWriter().write("404: not found"); } }); WebappContext context = new WebappContext("WebappContext", BASE_PATH); ServletRegistration registration = context.addServlet("ServletContainer", ServletContainer.class); registration.setInitParameter(ServletContainer.RESOURCE_CONFIG_CLASS, PackagesResourceConfig.class.getName()); StringJoiner sj = new StringJoiner(","); for (String s : PACKAGES) { sj.add(s); } registration.setInitParameter(PackagesResourceConfig.PROPERTY_PACKAGES, sj.toString()); registration.addMapping(BASE_PATH); context.deploy(server); }
WebappContext webappContext = new WebappContext("myWebappContext"); webappContext.addListener(new ServletContextListener() { @Override public void contextInitialized(ServletContextEvent sce) { sce.getServletContext().setAttribute(ServletProperties.SERVICE_LOCATOR, MY_SERVICE_LOCATOR); } @Override public void contextDestroyed(ServletContextEvent sce) { } }); ServletRegistration servlet = webappContext.addServlet("myAppplication", new ServletContainer(resourceConfig)); servlet.addMapping("/application/*"); ServletRegistration hello = webappContext.addServlet("myServlet", MyServlet.class); hello.addMapping("/servlet/*"); HttpServer createHttpServer = GrizzlyHttpServerFactory.createHttpServer(MY_URI, false); webappContext.deploy(createHttpServer); createHttpServer.start();
@Nonnull private static WebappContext _createContext (final URI u, final Class <? extends Servlet> aServletClass, final Servlet aServlet, final Map <String, String> aInitParams, final Map <String, String> aContextInitParams) { String path = u.getPath (); if (path == null) throw new IllegalArgumentException ("The URI path, of the URI " + u + ", must be non-null"); if (path.isEmpty ()) throw new IllegalArgumentException ("The URI path, of the URI " + u + ", must be present"); if (path.charAt (0) != '/') throw new IllegalArgumentException ("The URI path, of the URI " + u + ". must start with a '/'"); path = String.format ("/%s", UriComponent.decodePath (u.getPath (), true).get (1).toString ()); final WebappContext aContext = new WebappContext ("GrizzlyContext", path); ServletRegistration aRegistration; if (aServletClass != null) aRegistration = aContext.addServlet (aServletClass.getName (), aServletClass); else aRegistration = aContext.addServlet (aServlet.getClass ().getName (), aServlet); aRegistration.addMapping ("/*"); if (aContextInitParams != null) for (final Map.Entry <String, String> e : aContextInitParams.entrySet ()) aContext.setInitParameter (e.getKey (), e.getValue ()); if (aInitParams != null) aRegistration.setInitParameters (aInitParams); return aContext; }
private static HttpServer create(URI u, Servlet servlet) throws IOException { String path = u.getPath(); path = String.format("/%s", UriComponent.decodePath(u.getPath(), true) .get(1).toString()); WebappContext context = new WebappContext("GrizzlyContext", path); context.addListener(MyListener.class); ServletRegistration registration; registration = context.addServlet(servlet.getClass().getName(), servlet); registration.addMapping("/*"); HttpServer server = GrizzlyHttpServerFactory.createHttpServer(u); context.deploy(server); return server; }
@Nonnull private static WebappContext _createContext (final URI u, final Class <? extends Servlet> aServletClass, final Servlet aServlet, final Map <String, String> aInitParams, final Map <String, String> aContextInitParams) { String path = u.getPath (); if (path == null) throw new IllegalArgumentException ("The URI path, of the URI " + u + ", must be non-null"); if (path.isEmpty ()) throw new IllegalArgumentException ("The URI path, of the URI " + u + ", must be present"); if (path.charAt (0) != '/') throw new IllegalArgumentException ("The URI path, of the URI " + u + ". must start with a '/'"); path = String.format ("/%s", UriComponent.decodePath (u.getPath (), true).get (1).toString ()); final WebappContext aContext = new WebappContext ("GrizzlyContext", path); ServletRegistration aRegistration; if (aServletClass != null) aRegistration = aContext.addServlet (aServletClass.getName (), aServletClass); else aRegistration = aContext.addServlet (aServlet.getClass ().getName (), aServlet); aRegistration.addMapping ("/*"); if (aContextInitParams != null) for (final Map.Entry <String, String> e : aContextInitParams.entrySet ()) aContext.setInitParameter (e.getKey (), e.getValue ()); if (aInitParams != null) aRegistration.setInitParameters (aInitParams); return aContext; }
HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(getBaseURI()); WebappContext context = new WebappContext("WebappContext", "/api"); ServletRegistration registration = context.addServlet("ServletContainer", new ServletContainer(config)); registration.addMapping("/*"); context.deploy(httpServer);
@Before public void setup() throws Exception { if (server == null) { System.out.println("Initializing an instance of Grizzly Container ..."); final ResourceConfig rc = new ResourceConfig(ResourceEndpointIntegrationTest.class, ..., ..., ...); //update WebappContext ctx = new WebappContext("IntegrationTestContext"); //register your listeners from web.xml in here ctx.addListener("com.xxx.yyy.XEndpointServletContextListener"); //register your applicationContext.xml here ctx.addContextInitParameter("contextConfigLocation", "classpath:applicationContext.xml"); //ServletRegistration is needed to load the ResourceConfig rc inside ServletContainer or you will have no //Servlet-based features available ServletRegistration registration = ctx.addServlet("ServletContainer", new ServletContainer(rc)); //Initialize the Grizzly server passing it base URL server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI)); //Deploy the server using our custom context ctx.deploy(server); } }
WebappContext context = new WebappContext("context"); ServletRegistration registration = context.addServlet("ServletContainer", ServletContainer.class); registration.setInitParameter("com.sun.jersey.config.property.packages", "com.sun.jersey.samples.https_grizzly.resource;com.sun.jersey.samples.https_grizzly.auth"); // add security filter (which handles http basic authentication) registration.setInitParameter(ResourceConfig.PROPERTY_CONTAINER_REQUEST_FILTERS, "com.sun.jersey.samples.https_grizzly.auth.SecurityFilter;com.sun.jersey.api.container.filter.LoggingFilter"); registration.setInitParameter(ResourceConfig.PROPERTY_CONTAINER_RESPONSE_FILTERS, LoggingFilter.class.getName()); try { webServer = GrizzlyServerFactory.createHttpServer( getBaseURI() ); // start Grizzly embedded server // System.out.println("Jersey app started. Try out " + BASE_URI + "\nHit CTRL + C to stop it..."); context.deploy(webServer); webServer.start(); } catch (Exception ex) { System.out.println(ex.getMessage()); }
context.addServlet(name, new ServletContainer(rc)); registration.setInitParameter("swagger.context.id", name); registration.addMapping(String.format("%s/*", path));
WebappContext webappContext = new WebappContext("grizzly web context", ""); FilterRegistration testFilterReg = webappContext.addFilter("TestFilter", TestFilter.class); testFilterReg.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), "/*"); ServletRegistration servletRegistration = webappContext.addServlet("Jersey", org.glassfish.jersey.servlet.ServletContainer.class); servletRegistration.addMapping("/myapp/*"); servletRegistration.setInitParameter("jersey.config.server.provider.packages", "com.example"); HttpServer server = HttpServer.createSimpleServer(); webappContext.deploy(server); server.start();
final ServletRegistration reg = ctx.addServlet("spring", new SpringServlet()); reg.addMapping("/*"); ctx.addContextInitParameter("contextConfigLocation", "classpath:spring-context.xml");
private static String API_PACKAGE = "package where TestRESTService class"; public static final URI BASE_URI = UriBuilder .fromUri("http://localhost/") .port(8000) .build(); private static HttpServer initServer() throws IOException { System.out.println("Starting grizzly... " + BASE_URI); HttpServer httpServer = GrizzlyServerFactory.createHttpServer(BASE_URI, new HttpHandler() { @Override public void service(Request rqst, Response rspns) throws Exception { rspns.sendError(404); } }); // Initialize and register Jersey Servlet WebappContext context = new WebappContext("GrizzlyContext", "/"); ServletRegistration registration = context.addServlet( ServletContainer.class.getName(), ServletContainer.class); registration.setInitParameter(ServletContainer.RESOURCE_CONFIG_CLASS, PackagesResourceConfig.class.getName()); registration.setInitParameter(PackagesResourceConfig.PROPERTY_PACKAGES, API_PACKAGE); registration.addMapping("/*"); context.deploy(httpServer); return httpServer; }
final ServletRegistration registration = ctx.addServlet("ServletContainer", new ServletContainer(config)); registration.addMapping("/*");
final DefaultServlet s = new DefaultServlet((StaticHttpHandlerBase) h); final ServletRegistration registration = addServlet("DefaultServlet", s); registration.addMapping("/"); final ServletConfigImpl sConfig =
final DefaultServlet s = new DefaultServlet((StaticHttpHandlerBase) h); final ServletRegistration registration = addServlet("DefaultServlet", s); registration.addMapping("/"); final ServletConfigImpl sConfig =
final DefaultServlet s = new DefaultServlet((StaticHttpHandlerBase) h); final ServletRegistration registration = addServlet("DefaultServlet", s); registration.addMapping("/"); final ServletConfigImpl sConfig =
final DefaultServlet s = new DefaultServlet((StaticHttpHandlerBase) h); final ServletRegistration registration = addServlet("DefaultServlet", s); registration.addMapping("/"); final ServletConfigImpl sConfig =
.setAttribute("com.codahale.metrics.servlets.MetricsServlet.registry", (metricsService.getMetricRegistry())); adminCtx.setAttribute("com.codahale.metrics.servlets.HealthCheckServlet.registry", metricsService.getHealthCheck()); final ServletRegistration sgMetrics = adminCtx.addServlet("admin", new AdminServlet()); sgMetrics.addMapping("/admin/*"); adminCtx.deploy(server);