use of org.glassfish.jersey.servlet.ServletContainer in project hbase by apache.
the class HBaseRESTTestingUtility method startServletContainer.
public void startServletContainer(Configuration conf) throws Exception {
if (server != null) {
LOG.error("ServletContainer already running");
return;
}
// Inject the conf for the test by being first to make singleton
RESTServlet.getInstance(conf, UserProvider.instantiate(conf));
// set up the Jersey servlet container for Jetty
ResourceConfig app = new ResourceConfig().packages("org.apache.hadoop.hbase.rest").register(Jackson1Feature.class);
ServletHolder sh = new ServletHolder(new ServletContainer(app));
// set up Jetty and run the embedded server
server = new Server(0);
LOG.info("configured " + ServletContainer.class.getName());
HttpConfiguration httpConfig = new HttpConfiguration();
httpConfig.setSendDateHeader(false);
httpConfig.setSendServerVersion(false);
ServerConnector serverConnector = new ServerConnector(server, new HttpConnectionFactory(httpConfig));
serverConnector.setPort(testServletPort);
server.addConnector(serverConnector);
// set up context
ServletContextHandler ctxHandler = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
ctxHandler.addServlet(sh, "/*");
// Load filters specified from configuration.
String[] filterClasses = conf.getStrings(Constants.FILTER_CLASSES, ArrayUtils.EMPTY_STRING_ARRAY);
for (String filter : filterClasses) {
filter = filter.trim();
ctxHandler.addFilter(filter, "/*", EnumSet.of(DispatcherType.REQUEST));
}
LOG.info("Loaded filter classes :" + filterClasses);
conf.set(RESTServer.REST_CSRF_BROWSER_USERAGENTS_REGEX_KEY, ".*");
RESTServer.addCSRFFilter(ctxHandler, conf);
HttpServerUtil.constrainHttpMethods(ctxHandler);
// start the server
server.start();
// get the port
testServletPort = ((ServerConnector) server.getConnectors()[0]).getLocalPort();
LOG.info("started " + server.getClass().getName() + " on port " + testServletPort);
}
use of org.glassfish.jersey.servlet.ServletContainer in project kafka by apache.
the class RestServer method start.
public void start(Herder herder) {
log.info("Starting REST server");
ResourceConfig resourceConfig = new ResourceConfig();
resourceConfig.register(new JacksonJsonProvider());
resourceConfig.register(RootResource.class);
resourceConfig.register(new ConnectorsResource(herder));
resourceConfig.register(new ConnectorPluginsResource(herder));
resourceConfig.register(ConnectExceptionMapper.class);
ServletContainer servletContainer = new ServletContainer(resourceConfig);
ServletHolder servletHolder = new ServletHolder(servletContainer);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
context.addServlet(servletHolder, "/*");
String allowedOrigins = config.getString(WorkerConfig.ACCESS_CONTROL_ALLOW_ORIGIN_CONFIG);
if (allowedOrigins != null && !allowedOrigins.trim().isEmpty()) {
FilterHolder filterHolder = new FilterHolder(new CrossOriginFilter());
filterHolder.setName("cross-origin");
filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, allowedOrigins);
String allowedMethods = config.getString(WorkerConfig.ACCESS_CONTROL_ALLOW_METHODS_CONFIG);
if (allowedMethods != null && !allowedOrigins.trim().isEmpty()) {
filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, allowedMethods);
}
context.addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST));
}
RequestLogHandler requestLogHandler = new RequestLogHandler();
Slf4jRequestLog requestLog = new Slf4jRequestLog();
requestLog.setLoggerName(RestServer.class.getCanonicalName());
requestLog.setLogLatency(true);
requestLogHandler.setRequestLog(requestLog);
HandlerCollection handlers = new HandlerCollection();
handlers.setHandlers(new Handler[] { context, new DefaultHandler(), requestLogHandler });
/* Needed for graceful shutdown as per `setStopTimeout` documentation */
StatisticsHandler statsHandler = new StatisticsHandler();
statsHandler.setHandler(handlers);
jettyServer.setHandler(statsHandler);
jettyServer.setStopTimeout(GRACEFUL_SHUTDOWN_TIMEOUT_MS);
jettyServer.setStopAtShutdown(true);
try {
jettyServer.start();
} catch (Exception e) {
throw new ConnectException("Unable to start REST server", e);
}
log.info("REST server listening at " + jettyServer.getURI() + ", advertising URL " + advertisedUrl());
}
use of org.glassfish.jersey.servlet.ServletContainer in project jersey by jersey.
the class JerseyServletContainerInitializer method addServletWithExistingRegistration.
/**
* Enhance existing servlet configuration.
*/
private static void addServletWithExistingRegistration(final ServletContext context, ServletRegistration registration, final Class<? extends Application> clazz, final Set<Class<?>> classes) throws ServletException {
// create a new servlet container for a given app.
final ResourceConfig resourceConfig = ResourceConfig.forApplicationClass(clazz, classes).addProperties(getInitParams(registration)).addProperties(Utils.getContextParams(context));
if (registration.getClassName() != null) {
// class name present - complete servlet registration from container point of view
Utils.store(resourceConfig, context, registration.getName());
} else {
// no class name - no complete servlet registration from container point of view
final ServletContainer servlet = new ServletContainer(resourceConfig);
final ServletRegistration.Dynamic dynamicRegistration = context.addServlet(clazz.getName(), servlet);
dynamicRegistration.setAsyncSupported(true);
dynamicRegistration.setLoadOnStartup(1);
registration = dynamicRegistration;
}
if (registration.getMappings().isEmpty()) {
final ApplicationPath ap = clazz.getAnnotation(ApplicationPath.class);
if (ap != null) {
final String mapping = createMappingPath(ap);
if (!mappingExists(context, mapping)) {
registration.addMapping(mapping);
LOGGER.log(Level.CONFIG, LocalizationMessages.JERSEY_APP_REGISTERED_MAPPING(clazz.getName(), mapping));
} else {
LOGGER.log(Level.WARNING, LocalizationMessages.JERSEY_APP_MAPPING_CONFLICT(clazz.getName(), mapping));
}
} else {
// Error
LOGGER.log(Level.WARNING, LocalizationMessages.JERSEY_APP_NO_MAPPING_OR_ANNOTATION(clazz.getName(), ApplicationPath.class.getSimpleName()));
}
} else {
LOGGER.log(Level.CONFIG, LocalizationMessages.JERSEY_APP_REGISTERED_APPLICATION(clazz.getName()));
}
}
use of org.glassfish.jersey.servlet.ServletContainer in project jersey by jersey.
the class JerseyServletContainerInitializer method addServletWithApplication.
/**
* Add new servlet according to {@link Application} subclass with {@link ApplicationPath} annotation or existing
* {@code servlet-mapping}.
*/
private static void addServletWithApplication(final ServletContext context, final Class<? extends Application> clazz, final Set<Class<?>> defaultClasses) throws ServletException {
final ApplicationPath ap = clazz.getAnnotation(ApplicationPath.class);
if (ap != null) {
// App is annotated with ApplicationPath
final ResourceConfig resourceConfig = ResourceConfig.forApplicationClass(clazz, defaultClasses).addProperties(Utils.getContextParams(context));
final ServletContainer s = new ServletContainer(resourceConfig);
final ServletRegistration.Dynamic dsr = context.addServlet(clazz.getName(), s);
dsr.setAsyncSupported(true);
dsr.setLoadOnStartup(1);
final String mapping = createMappingPath(ap);
if (!mappingExists(context, mapping)) {
dsr.addMapping(mapping);
LOGGER.log(Level.CONFIG, LocalizationMessages.JERSEY_APP_REGISTERED_MAPPING(clazz.getName(), mapping));
} else {
LOGGER.log(Level.WARNING, LocalizationMessages.JERSEY_APP_MAPPING_CONFLICT(clazz.getName(), mapping));
}
}
}
use of org.glassfish.jersey.servlet.ServletContainer in project uavstack by uavorg.
the class JerseyConfig method jerseyServlet.
@Bean
public ServletRegistrationBean jerseyServlet() {
ServletRegistrationBean registration = new ServletRegistrationBean(new ServletContainer(), "/rest/*");
// our rest resources will be available in the path /rest/*
registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, JerseyConfig.class.getName());
return registration;
}
Aggregations