/** * Called when the OSGi framework starts our bundle */ public void start(BundleContext bc) throws Exception { bundleContext = bc; //preparing special HTTP Context with HTTP Connector Hashtable<String, String> props = new Hashtable<String, String>(); props.put( ExtenderConstants.PROPERTY_HTTP_CONTEXT_ID, "httpConnector" ); HashMap<String,String> contextMappingParams = new HashMap<String,String>(); contextMappingParams.put(ExtenderConstants.PROPERTY_HTTP_VIRTUAL_HOSTS, "localhost"); contextMappingParams.put(ExtenderConstants.PROPERTY_HTTP_CONNECTORS, "alternateConnector"); contextMappingReg = bundleContext.registerService( HttpContextMapping.class, new WhiteboardHttpContextMapping("httpConnector", "whiteboard", contextMappingParams), props ); //Registering Servlet props.put(ExtenderConstants.PROPERTY_ALIAS, "/connector"); servletReg = bundleContext.registerService(Servlet.class, new HelloServlet(), props); }
public void start(BundleContext context) throws Exception { ServiceReference<WebContainer> serviceReference = context .getServiceReference(WebContainer.class); while (serviceReference == null) { serviceReference = context.getServiceReference(WebContainer.class); } WebContainer service = (WebContainer) context .getService(serviceReference); HttpContext httpContext = service.getDefaultSharedHttpContext(); Dictionary<String, String> props; // register a custom http context for sharing props = new Hashtable<String, String>(); props.put(ExtenderConstants.PROPERTY_HTTP_CONTEXT_ID, "shared"); httpContextReg = context.registerService(HttpContext.class, httpContext, props); // and an servlet that cannot be accessed due to the above context props = new Hashtable<String, String>(); props.put( ExtenderConstants.PROPERTY_ALIAS, "/extfilter" ); props.put("servlet-name", "FilteredServlet"); props.put(ExtenderConstants.PROPERTY_HTTP_CONTEXT_ID, "shared"); registerService = context.registerService( Servlet.class, new HelloServlet(), props ); }
servletReg = bundleContext.registerService(Servlet.class, new HelloServlet(), props); props.put(ExtenderConstants.PROPERTY_HTTP_CONTEXT_ID, "forbidden"); forbiddenServletReg = bundleContext.registerService(Servlet.class, new HelloServlet(), props);
public HttpService addingService(ServiceReference<HttpService> reference) { final HttpService httpService = (HttpService) bundleContext .getService(reference); if (httpService != null) { // create a default context to share between registrations final HttpContext httpContext = httpService.createDefaultHttpContext(); final Dictionary<String, Object> initParams = new Hashtable<String, Object>(); try { httpService.registerServlet("/hello", new HelloServlet(), initParams, httpContext); httpService.registerServlet("/hello/logo", new HelloImage(), initParams, httpContext); httpService.registerResources("/images", "/images", httpContext); } catch (ServletException e) { e.printStackTrace(); } catch (NamespaceException e) { e.printStackTrace(); } } return httpService; }