use of org.osgi.service.http.HttpService in project jetty.project by eclipse.
the class TestOSGiUtil method testHttpServiceGreetings.
protected static void testHttpServiceGreetings(BundleContext bundleContext, String protocol, int port) throws Exception {
assertActiveBundle(bundleContext, "org.eclipse.jetty.osgi.boot");
assertActiveBundle(bundleContext, "org.eclipse.jetty.osgi.httpservice");
assertActiveBundle(bundleContext, "org.eclipse.equinox.http.servlet");
// in the OSGi world this would be bad code and we should use a bundle
// tracker.
// here we purposely want to make sure that the httpService is actually
// ready.
ServiceReference sr = bundleContext.getServiceReference(HttpService.class.getName());
Assert.assertNotNull("The httpServiceOSGiBundle is started and should " + "have deployed a service reference for HttpService", sr);
HttpService http = (HttpService) bundleContext.getService(sr);
http.registerServlet("/greetings", new HttpServlet() {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("Hello");
}
}, null, null);
// now test the servlet
HttpClient client = protocol.equals("https") ? new HttpClient(newSslContextFactory()) : new HttpClient();
try {
client.start();
ContentResponse response = client.GET(protocol + "://127.0.0.1:" + port + "/greetings");
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
String content = new String(response.getContent());
Assert.assertEquals("Hello", content);
} finally {
client.stop();
}
}
use of org.osgi.service.http.HttpService in project jersey by jersey.
the class Activator method start.
@Override
public synchronized void start(BundleContext bundleContext) throws Exception {
this.bc = bundleContext;
logger.info("STARTING HTTP SERVICE BUNDLE");
this.tracker = new ServiceTracker(this.bc, HttpService.class.getName(), null) {
@Override
public Object addingService(ServiceReference serviceRef) {
httpService = (HttpService) super.addingService(serviceRef);
registerServlets();
return httpService;
}
@Override
public void removedService(ServiceReference ref, Object service) {
if (httpService == service) {
unregisterServlets();
httpService = null;
}
super.removedService(ref, service);
}
};
this.tracker.open();
logger.info("HTTP SERVICE BUNDLE STARTED");
}
use of org.osgi.service.http.HttpService in project sling by apache.
the class VirtualInstance method startJetty.
public synchronized void startJetty() throws Throwable {
if (jettyServer != null) {
return;
}
servletContext = new ServletContextHandler(ServletContextHandler.NO_SECURITY);
servletContext.setContextPath("/");
TopologyConnectorServlet servlet = new TopologyConnectorServlet();
PrivateAccessor.setField(servlet, "config", config);
PrivateAccessor.setField(servlet, "clusterViewService", clusterViewService);
PrivateAccessor.setField(servlet, "announcementRegistry", announcementRegistry);
Mockery context = new JUnit4Mockery();
final HttpService httpService = context.mock(HttpService.class);
context.checking(new Expectations() {
{
allowing(httpService).registerServlet(with(any(String.class)), with(any(Servlet.class)), with(any(Dictionary.class)), with(any(HttpContext.class)));
}
});
PrivateAccessor.setField(servlet, "httpService", httpService);
ComponentContext cc = null;
PrivateAccessor.invoke(servlet, "activate", new Class[] { ComponentContext.class }, new Object[] { cc });
ServletHolder holder = new ServletHolder(servlet);
servletContext.addServlet(holder, "/system/console/topology/*");
jettyServer = new Server();
jettyServer.setHandler(servletContext);
Connector connector = new SelectChannelConnector();
jettyServer.setConnectors(new Connector[] { connector });
jettyServer.start();
}
Aggregations