ServletHolder h = new ServletHolder(new ServletContainer(makeJerseyConfig())); root.addServlet(h, "/" + SERVLET_PATH + "/*");
public ExhibitorMain(BackupProvider backupProvider, ConfigProvider configProvider, ExhibitorArguments.Builder builder, int httpPort, String listenAddress, SecurityHandler security, SecurityArguments securityArguments) throws Exception { HashUserRealm realm = makeRealm(securityArguments); if ( securityArguments.getRemoteAuthSpec() != null ) { addRemoteAuth(builder, securityArguments.getRemoteAuthSpec()); } builder.shutdownProc(makeShutdownProc(this)); exhibitor = new Exhibitor(configProvider, null, backupProvider, builder.build()); exhibitor.start(); DefaultResourceConfig application = JerseySupport.newApplicationConfig(new UIContext(exhibitor)); ServletContainer container = new ServletContainer(application); server = new Server(); SocketConnector http = new SocketConnector(); http.setHost(listenAddress); http.setPort(httpPort); server.addConnector(http); Context root = new Context(server, "/", Context.SESSIONS); root.addFilter(ExhibitorServletFilter.class, "/", Handler.ALL); root.addServlet(new ServletHolder(container), "/*"); if ( security != null ) { root.setSecurityHandler(security); } else if ( securityArguments.getSecurityFile() != null ) { addSecurityFile(realm, securityArguments.getSecurityFile(), root); } }
public Filter getFilter() { return new ServletContainer(); }
@Override public Object addingService(ServiceReference serviceRef) { httpService = (HttpService)super.addingService(serviceRef); Dictionary<String, String> jerseyServletParams = new Hashtable<String, String>(); jerseyServletParams.put("javax.ws.rs.Application", CorpusServerApplication.class.getName()); jerseyServletParams.put("com.sun.jersey.api.json.POJOMappingFeature", "true"); try { httpService.registerServlet("/rest", new ServletContainer(), jerseyServletParams, null); } catch (ServletException e) { throw new RuntimeException(e); } catch (NamespaceException e) { throw new RuntimeException(e); } return httpService; }
@Override public void run() { LOG.info("Running"); Map<String, String> initParams = Maps.newHashMap(); initParams.put("com.sun.jersey.config.property.packages", "com.twitter.hraven.rest"); initParams.put("com.sun.jersey.api.json.POJOMappingFeature", "true"); httpServer.registerHandler("/", new com.sun.jersey.spi.container.servlet.ServletContainer(), initParams, false); // export a metric that printouts the epoch time this service came up // metrics can be viewed at hostname:portnumber/vars or // hostname:portnumber/vars.json Stats.exportLong("hravenRestService_StartTimestamp", System.currentTimeMillis()); // await shutdown lifecycle.awaitShutdown(); }
private static ServletContextHandler generateServletContextHandler() throws IOException { ServletContextHandler contextHandler = new ServletContextHandler(); contextHandler.setContextPath("/"); ResourceConfig rc = new WebAppResourceConfig(Collections.<String, Object>emptyMap(), contextHandler.getServletContext()); rc.getSingletons().add(new SampleResource()); ServletContainer jerseyServletContainer = new ServletContainer(rc); contextHandler.addServlet(SampleResource.SampleAsyncServlet.class, SampleResource.ASYNC_PATH); contextHandler.addServlet(SampleResource.SampleBlockingForwardServlet.class, SampleResource.BLOCKING_FORWARD_PATH); contextHandler.addServlet(SampleResource.SampleAsyncForwardServlet.class, SampleResource.ASYNC_FORWARD_PATH); contextHandler.addServlet(SampleResource.SampleAsyncTimeoutServlet.class, SampleResource.ASYNC_TIMEOUT_PATH); contextHandler.addServlet(SampleResource.SampleAsyncErrorServlet.class, SampleResource.ASYNC_ERROR_PATH); contextHandler.addServlet(new ServletHolder(jerseyServletContainer), "/*"); contextHandler.addFilter(RequestTracingFilter.class, "/*", EnumSet.allOf(DispatcherType.class)); return contextHandler; }
/** * Return the base URL of the HTTP server. <code>http://host:port</code> public URL getBaseUrl() { return baseUrl; } * * Call in @BeforeClass annotated method. */ public void setUpServer() { try { // cut of any base pathbasestUrl might have int port = baseUrl.getPort(); logger.info("Start http server at port " + port); hs = new Server(port); ServletContainer servletContainer = cfg.isSome() ? new ServletContainer(cfg.get()) : new ServletContainer(); ServletHolder jerseyServlet = new ServletHolder(servletContainer); ServletContextHandler context = new ServletContextHandler(hs, "/"); context.addServlet(jerseyServlet, "/*"); hs.start(); } catch (Exception e) { chuck(e); } }
/** * Register the Jersey filter */ @Bean public FilterRegistrationBean jerseyFilterRegistration( javax.ws.rs.core.Application eurekaJerseyApp) { FilterRegistrationBean bean = new FilterRegistrationBean(); bean.setFilter(new ServletContainer(eurekaJerseyApp)); bean.setOrder(Ordered.LOWEST_PRECEDENCE); bean.setUrlPatterns( Collections.singletonList(EurekaConstants.DEFAULT_PREFIX + "/*")); return bean; }
@Override public void run() { LOG.info("Running"); Map<String, String> initParams = Maps.newHashMap(); initParams.put("com.sun.jersey.config.property.packages", "com.twitter.hraven.rest"); initParams.put("com.sun.jersey.api.json.POJOMappingFeature", "true"); httpServer.registerHandler("/", new com.sun.jersey.spi.container.servlet.ServletContainer(), initParams, false); // export a metric that printouts the epoch time this service came up // metrics can be viewed at hostname:portnumber/vars or // hostname:portnumber/vars.json Stats.exportLong("hravenRestService_StartTimestamp", System.currentTimeMillis()); // await shutdown lifecycle.awaitShutdown(); }
/** * Register the Jersey filter */ @Bean public FilterRegistrationBean jerseyFilterRegistration( javax.ws.rs.core.Application eurekaJerseyApp) { FilterRegistrationBean bean = new FilterRegistrationBean(); bean.setFilter(new ServletContainer(eurekaJerseyApp)); bean.setOrder(Ordered.LOWEST_PRECEDENCE); bean.setUrlPatterns( Collections.singletonList(EurekaConstants.DEFAULT_PREFIX + "/*")); return bean; }
public static void installAsServletFilter(ServletContextHandler context) { ResourceConfig config = new DefaultResourceConfig(); // load all our REST API modules, JSON, and Swagger for (Object r: BrooklynRestApi.getAllResources()) config.getSingletons().add(r); // configure to match empty path, or any thing which looks like a file path with /assets/ and extension html, css, js, or png // and treat that as static content config.getProperties().put(ServletContainer.PROPERTY_WEB_PAGE_CONTENT_REGEX, "(/?|[^?]*/asserts/[^?]+\\.[A-Za-z0-9_]+)"); // and anything which is not matched as a servlet also falls through (but more expensive than a regex check?) config.getFeatures().put(ServletContainer.FEATURE_FILTER_FORWARD_ON_404, true); // finally create this as a _filter_ which falls through to a web app or something (optionally) FilterHolder filterHolder = new FilterHolder(new ServletContainer(config)); context.addFilter(filterHolder, "/*", EnumSet.allOf(DispatcherType.class)); }
protected void addServlet() { ResourceConfig config = new DefaultResourceConfig(); // load all our REST API modules, JSON, and Swagger for (Object r: NatServiceRestApi.getAllResources()) config.getSingletons().add(r); // Accept gzipped requests and responses config.getProperties().put(ResourceConfig.PROPERTY_CONTAINER_REQUEST_FILTERS, GZIPContentEncodingFilter.class.getName()); config.getProperties().put(ResourceConfig.PROPERTY_CONTAINER_RESPONSE_FILTERS, GZIPContentEncodingFilter.class.getName()); // configure to match empty path, or any thing which looks like a file path with /assets/ and extension html, css, js, or png // and treat that as static content config.getProperties().put(ServletContainer.PROPERTY_WEB_PAGE_CONTENT_REGEX, "(/?|[^?]*/assets/[^?]+\\.[A-Za-z0-9_]+)"); // and anything which is not matched as a servlet also falls through (but more expensive than a regex check?) config.getFeatures().put(ServletContainer.FEATURE_FILTER_FORWARD_ON_404, true); ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); ServletHolder servletHolder = new ServletHolder(new ServletContainer(config)); servletHolder.setInitParameter(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE.toString()); context.addServlet(servletHolder,"/*"); context.setContextPath("/"); context.setAttribute(AbstractRestResource.NAT_SERVICE_DISPATCHER, dispatcher); server.setHandler(context); }
protected ServletContainer createServletContainer(ApplicationHost app) { ApplicationAdapter adapter = new ApplicationAdapter(app); // disable wadl since we got class loader pb in JAXB under equinox adapter.getFeatures().put(ResourceConfig.FEATURE_DISABLE_WADL, Boolean.TRUE); // copy all features recorded in app adapter.getFeatures().putAll(app.getFeatures()); return new ServletContainer(adapter); }
/** * Manage a newly registered {@code Application} service. * @param application REST application * @param reference the associated {@code ServiceReference}. */ public void bindApplication(final Application application, final ServiceReference reference) { // Compute the Web context path String context = PathUtils.getContextPath(application, reference); try { // Try to register the Servlet httpService.registerServlet(context, new ServletContainer(application), null, null); aliases.put(reference, context); LOGGER.debug("REST Application {0} published under context ''{1}''", application.getClass().getName(), context); } catch (ServletException e) { LOGGER.error("REST Application {0} in error", application.getClass().getName(), e); } catch (NamespaceException e) { LOGGER.error("REST Application {0} is using an already bound web context name ''{}''", application.getClass().getName(), context, e); } }
private void addServletWithApplication(ServletContext sc, Class<? extends Application> a, Set<Class<?>> classes) { final ApplicationPath ap = a.getAnnotation(ApplicationPath.class); if (ap != null) { // App is annotated with ApplicationPath final ServletContainer s = new ServletContainer( new DeferredResourceConfig(a, getRootResourceAndProviderClasses(classes))); final String mapping = createMappingPath(ap); if (!mappingExists(sc, mapping)) { sc.addServlet(a.getName(), s). addMapping(mapping); LOGGER.info("Registering the Jersey servlet application, named " + a.getName() + ", at the servlet mapping, " + mapping + ", with the Application class of the same name"); } else { LOGGER.severe("Mapping conflict. " + "A Servlet declaration exists with same mapping as the Jersey servlet application, named " + a.getName() + ", at the servlet mapping, " + mapping + ". The Jersey servlet is not deployed."); } } }
private void addServletWithApplication(ServletContext sc, Class<? extends Application> a, Set<Class<?>> classes) { final ApplicationPath ap = a.getAnnotation(ApplicationPath.class); if (ap != null) { // App is annotated with ApplicationPath final ServletContainer s = new ServletContainer( new DeferredResourceConfig(a, getRootResourceAndProviderClasses(classes))); final String mapping = createMappingPath(ap); if (!mappingExists(sc, mapping)) { sc.addServlet(a.getName(), s). addMapping(mapping); LOGGER.info("Registering the Jersey servlet application, named " + a.getName() + ", at the servlet mapping, " + mapping + ", with the Application class of the same name"); } else { LOGGER.severe("Mapping conflict. " + "A Servlet declaration exists with same mapping as the Jersey servlet application, named " + a.getName() + ", at the servlet mapping, " + mapping + ". The Jersey servlet is not deployed."); } } }
private void addServletWithDefaultConfiguration(ServletContext sc, Set<Class<?>> classes) { ServletRegistration appReg = sc.getServletRegistration(Application.class.getName()); if (appReg != null && appReg.getClassName() == null) { final Set<Class<?>> x = getRootResourceAndProviderClasses(classes); final ServletContainer s = new ServletContainer( new DefaultResourceConfig(x)); appReg = sc.addServlet(appReg.getName(), s); if (appReg.getMappings().isEmpty()) { // Error LOGGER.severe("The Jersey servlet application, named " + appReg.getName() + ", has no servlet mapping"); } else { LOGGER.info("Registering the Jersey servlet application, named " + appReg.getName() + ", with the following root resource and provider classes: " + x); } } }
private void addServletWithDefaultConfiguration(ServletContext sc, Set<Class<?>> classes) { ServletRegistration appReg = sc.getServletRegistration(Application.class.getName()); if (appReg != null && appReg.getClassName() == null) { final Set<Class<?>> x = getRootResourceAndProviderClasses(classes); final ServletContainer s = new ServletContainer( new DefaultResourceConfig(x)); appReg = sc.addServlet(appReg.getName(), s); if (appReg.getMappings().isEmpty()) { // Error LOGGER.severe("The Jersey servlet application, named " + appReg.getName() + ", has no servlet mapping"); } else { LOGGER.info("Registering the Jersey servlet application, named " + appReg.getName() + ", with the following root resource and provider classes: " + x); } } }
private void addServletWithDefaultConfiguration(ServletContext sc, Set<Class<?>> classes) { ServletRegistration appReg = sc.getServletRegistration(Application.class.getName()); if (appReg != null && appReg.getClassName() == null) { final Set<Class<?>> x = getRootResourceAndProviderClasses(classes); final ServletContainer s = new ServletContainer( new DefaultResourceConfig(x)); appReg = sc.addServlet(appReg.getName(), s); if (appReg.getMappings().isEmpty()) { // Error LOGGER.severe("The Jersey servlet application, named " + appReg.getName() + ", has no servlet mapping"); } else { LOGGER.info("Registering the Jersey servlet application, named " + appReg.getName() + ", with the following root resource and provider classes: " + x); } } }
private void startHttpServer() throws Exception { server = new Server(); SelectChannelConnector selectChannelConnector = new SelectChannelConnector(); selectChannelConnector.setPort(11060); server.setConnectors(new Connector[]{selectChannelConnector}); PackagesResourceConfig packagesResourceConfig = new PackagesResourceConfig("com/ngdata/hbaseindexer/rest"); ServletHolder servletHolder = new ServletHolder(new ServletContainer(packagesResourceConfig)); servletHolder.setName("HBase-Indexer"); Context context = new Context(server, "/", Context.NO_SESSIONS); context.addServlet(servletHolder, "/*"); context.setContextPath("/"); context.setAttribute("indexerModel", indexerModel); context.setAttribute("indexerSupervisor", indexerSupervisor); server.setHandler(context); server.start(); }