public static HttpContext createHttpContext(GrizzlyWebServer server, String contextPath, String path) { GrizzlyHttpContext context = new GrizzlyHttpContext(contextPath, path); server.addGrizzlyAdapter(new HttpSpiAdapter(context), new String[] {contextPath+path}); return context; }
/** * Deploy {@link Deployable} to gws. * * @param gws Grizzly to deploy to. * @param toDeploy Deployable to be deployed. * @param configuration Configuration of deployment. * * @return Deployment identification. * * @throws DeployException Error in deployment. */ public final DeploymentID deploy(GrizzlyWebServer gws, V toDeploy, T configuration) throws DeployException { Map<GrizzlyAdapter, Set<String>> map = convert(toDeploy, configuration); if (map == null || map.isEmpty()) { throw new DeployException("No GrizzlyAdapters created for: " + toDeploy); } for (Map.Entry<GrizzlyAdapter, Set<String>> adapterEntry : map.entrySet()) { GrizzlyAdapter adapter = adapterEntry.getKey(); Set<String> mappings = adapterEntry.getValue(); if (logger.isLoggable(Level.FINEST)) { logger.log(Level.FINEST, String.format("Deploying '%s' to %s", adapter, mappings)); } gws.addGrizzlyAdapter(adapter, mappings.toArray(new String[mappings.size()])); } final DeploymentID deploymentId = new DeploymentID(toDeploy.hashCode()); deployed.put(deploymentId, map.keySet()); return deploymentId; }
@Override public MkContainer start(final int prt) throws IOException { if (this.port != 0) { throw new IllegalStateException( String.format( "already listening on port %d, use #stop() first", this.port ) ); } this.port = prt; this.gws = new GrizzlyWebServer(this.port); this.gws.addGrizzlyAdapter(this.adapter, new String[] {"/"}); this.gws.start(); Logger.info(this, "started on port #%s", prt); return this; }
/** * Return an already configured {@link GrizzlyWebServer} that can serves * static resources. * @param path the directory to serve static resource from. * @return a ready to use {@link GrizzlyWebServer} that listen on port 8080 */ public final static GrizzlyWebServer newConfiguredInstance(String path){ GrizzlyWebServer ws = new GrizzlyWebServer(8080); ws.addGrizzlyAdapter(new GrizzlyAdapter(path){ { setHandleStaticResources(true); } @Override public void service(GrizzlyRequest request, GrizzlyResponse response) { try { response.setStatus(404); response.flushBuffer(); } catch (IOException ex) { Logger.getLogger(GrizzlyWebServer.class.getName()).log( Level.SEVERE, null, ex); } } }); return ws; }
/** * Return an already configured {@link GrizzlyWebServer} that can serves * static resources. * @param path the directory to serve static resource from. * @return a ready to use {@link GrizzlyWebServer} that listen on port 8080 */ public static GrizzlyWebServer newConfiguredInstance(String path){ GrizzlyWebServer ws = new GrizzlyWebServer(8080); ws.addGrizzlyAdapter(new GrizzlyAdapter(path){ { setHandleStaticResources(true); } @Override public void service(GrizzlyRequest request, GrizzlyResponse response) { try { response.setStatus(404); response.flushBuffer(); } catch (IOException ex) { final Logger localLogger = LoggerUtils.getLogger(); if (localLogger.isLoggable(Level.SEVERE)) { localLogger.log(Level.SEVERE, LogMessages.SEVERE_GRIZZLY_HTTP_GWS_IO_ERROR(), ex); } } } }); return ws; }
GrizzlyWebServer ws = new GrizzlyWebServer("/var/www"); try { ServletAdapter sa = new ServletAdapter(); sa.setRootFolder("/Path/To/Exploded/War/File"); sa.setServlet(new MyServlet()); ws.addGrizzlyAdapter(sa); ws.start(); } catch (IOException ex) { // Something when wrong. }
private static GrizzlyWebServer getServer(int port, String webResourcesPath, String contextPath) { GrizzlyWebServer gws = new GrizzlyWebServer(port, webResourcesPath); ServletAdapter sa = new ServletAdapter(); /* here they are added statically */ sa.addInitParameter("javax.ws.rs.Application", "com.rest.MyApplication"); sa.setContextPath(contextPath); sa.setServletInstance(new ServletContainer()); sa.setProperty("load-on-startup", 1); gws.addGrizzlyAdapter(sa, new String[] { contextPath }); return gws;