Interface ContextRelated
-
- All Known Subinterfaces:
ErrorPageMapping,FilterMapping,JspMapping,ListenerMapping,ResourceMapping,ServletMapping,WebSocketMapping,WelcomeFileMapping
public interface ContextRelatedSuper interface extended by all explicit whiteboard elements that can be registered by targeting selected context.
Context is something different in
HttpServicecase (it's represented byHttpContext) and in Whiteboard case (it's represented byServletContextHelper). In both cases, eventually this context is actually backed by a realServletContextfrom Java Servlet API. Though it's not 1:1 relation...Both
HttpContextandServletContextHelperdo not specify a context path. It can be specified only by:- (Pax Web legacy Whiteboard)
httpContext.pathservice registration property when whiteboard-registeringHttpContextservice - (Pax Web legacy Whiteboard) whiteboard-registering a
HttpContextMappingservice with a path - (OSGI CMPN Whiteboard)
HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATHservice registration property when registeringServletContextHelperservice
The referenced context doesn't have to be unique wrt
ServletContext, there may be many contexts registered (as mentioned above) for given context path but using different name. Servlet (or e.g., filter) may be associated with one (or more)ServletContextHelper(new Whiteboard) instance for given context path, but actual (server specific) context (or web application) may be supported by differentServletContextHelperinstances.Pax Web will unify behavior of Http Service and Whiteboard style contexts (knowing that underneath there's actual, server-specific
ServletContext) and uniqueness will be checked by String ID (and bundle for bundle-scoped access). Additionally for old-styleHttpContext, a shared flag will be checked to determine whether context may be used by different bundles. Whiteboard (new-style) context is shared by default.In
HttpServicecase (no whiteboard), equality ofHttpContextcreated by users is implied to be instance equality (same object). Pax Web wraps such contexts and setscustomcontext ID in the wrapper.In non-whiteboard approach, servlets are always registered together with associated
HttpContextwhen calling method likeHttpService.registerServlet(java.lang.String, javax.servlet.Servlet, java.util.Dictionary<?, ?>, org.osgi.service.http.HttpContext). User may also provideWebContainerContextorMultiBundleWebContainerContextwhen registering a servlet. That means (assuming Pax Web specific shared contexts) it's hard to reference common context without using actual instance of the context, so such instance has to be shared through OSGi registry.To support real sharing of
HttpContextwhen using oldHttpServicemethods, user first needs to registerHttpContextMappingOSGi service orHttpContextservice withhttpContext.idregistration property:// register pure HttpContext service props.put("httpContext.id", "my-context"); bundleContext.registerService(HttpContext.class, aContext, props); // or register HttpContextMapping using "explicit whiteboard approach" aContextMapping = new HttpContextMappingImpl(); aContextMapping.setContextId("my-context"); aContextMapping.setHttpContext(new HttpContext() { ... }); bundleContext.registerService(HttpContextMapping.class, aContextMapping, null);Then, an element (like servlet) may be registered like this:
context = new DefaultHttpContext(bundleContext.getBundle(), "my-context"); httpService.registerServlet("/alias", servlet, null, context);This trick is based on assumed identity of DefaultHttpContext, which is name+bundle and separates the identity from context behavior (
HttpContext.handleSecurity(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)), which should not be specified (or emphasized) in every registration.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description StringgetContextId()Get an ID of the context in which the servlet should be registered.StringgetContextSelectFilter()Get an LDAP-style filter to selectServletContextHelperinstances to use when registering given element.
-
-
-
Method Detail
-
getContextSelectFilter
String getContextSelectFilter()
Get an LDAP-style filter to select
ServletContextHelperinstances to use when registering given element. This is generic, defined in Whiteboard specification, way of associating the servlet (and filter, and listener, ...) with a context.In generic scenario, when custom context (
ServletContextHelperin case of Whiteboard) is registered using:DictionaryServlet may be associated with such context using:props = new Hashtable<>(); props.put("osgi.http.whiteboard.context.name", "my-context"); props.put("osgi.http.whiteboard.context.path", "/my-context"); context.registerService(ServletContextHelper.class, ..., props); Dictionaryprops = new Hashtable<>(); props.put("osgi.http.whiteboard.context.select", "(osgi.http.whiteboard.context.name=my-context)"); context.registerService(Servlet.class, ..., props); Special Whiteboard-Http Service scenario (OSGi R7 CMPN 140.10 "Integration with Http Service Contexts") mentions that a whiteboard element (excluding servlet, but let's not add such restriction in Pax Web) may be associated with a context representing an old
HttpContextusing special context selection:Dictionaryprops = new Hashtable<>(); props.put("osgi.http.whiteboard.context.select", "(osgi.http.whiteboard.context.httpservice=*)"); context.registerService(Servlet.class, ..., props); This specification fragment says:
A Http Whiteboard service which should be registered with a Http Context from the Http Service can achieve this by targeting a
ServletContextHelperwith the registration propertyosgi.http.whiteboard.context.httpservice.This seems a bit artificial:
- We can't distinguish actual old-style context (
HttpContext) - Specification requires the implementation to register special
ServletContextHelperrepresenting aHttpContext
- Returns:
- We can't distinguish actual old-style context (
-
getContextId
String getContextId()
Get an ID of the context in which the servlet should be registered. This is handy simplification of context association if no
getContextSelectFilter()is specified.In Whiteboard scenario, the context (actually,
ServletContextHelper) is selected using an LDAP-like filter, seegetContextSelectFilter(), which allows association with many servlet contexts. To simplify things, this method may just indicate singleServletContextHelperby name. It can be done using one of (in order of decreasing priority):- standard (Whiteboard)
osgi.http.whiteboard.context.select=(osgi.http.whiteboard.context.name=name)service registration property - legacy (Pax Web specific)
httpContext.id=nameservice registration property - this method (if user registers an instance of
ContextRelated.
- Returns:
- id of single context this whiteboard element should be associated with
- standard (Whiteboard)
-
-