Search in sources :

Example 1 with HttpService

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();
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpService(org.osgi.service.http.HttpService) HttpServlet(javax.servlet.http.HttpServlet) HttpClient(org.eclipse.jetty.client.HttpClient) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ServiceReference(org.osgi.framework.ServiceReference)

Example 2 with HttpService

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");
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) HttpService(org.osgi.service.http.HttpService) ServiceReference(org.osgi.framework.ServiceReference)

Example 3 with HttpService

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();
}
Also used : Expectations(org.jmock.Expectations) Dictionary(java.util.Dictionary) SelectChannelConnector(org.eclipse.jetty.server.nio.SelectChannelConnector) Connector(org.eclipse.jetty.server.Connector) ComponentContext(org.osgi.service.component.ComponentContext) Server(org.eclipse.jetty.server.Server) TopologyConnectorServlet(org.apache.sling.discovery.base.connectors.ping.TopologyConnectorServlet) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpContext(org.osgi.service.http.HttpContext) Mockery(org.jmock.Mockery) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) SelectChannelConnector(org.eclipse.jetty.server.nio.SelectChannelConnector) HttpService(org.osgi.service.http.HttpService) TopologyConnectorServlet(org.apache.sling.discovery.base.connectors.ping.TopologyConnectorServlet) Servlet(javax.servlet.Servlet) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Aggregations

HttpService (org.osgi.service.http.HttpService)3 ServiceReference (org.osgi.framework.ServiceReference)2 IOException (java.io.IOException)1 Dictionary (java.util.Dictionary)1 Servlet (javax.servlet.Servlet)1 ServletException (javax.servlet.ServletException)1 HttpServlet (javax.servlet.http.HttpServlet)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 TopologyConnectorServlet (org.apache.sling.discovery.base.connectors.ping.TopologyConnectorServlet)1 HttpClient (org.eclipse.jetty.client.HttpClient)1 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)1 Connector (org.eclipse.jetty.server.Connector)1 Server (org.eclipse.jetty.server.Server)1 SelectChannelConnector (org.eclipse.jetty.server.nio.SelectChannelConnector)1 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)1 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)1 Expectations (org.jmock.Expectations)1 Mockery (org.jmock.Mockery)1 JUnit4Mockery (org.jmock.integration.junit4.JUnit4Mockery)1