Interface HttpService
-
public interface HttpService
The Http Service allows other bundles in the OSGi environment to dynamically register resources and servlets into the URI namespace of Http Service. A bundle may later unregister its resources or servlets.- See Also:
HttpContext
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description HttpContext
createDefaultHttpContext()
Creates a defaultHttpContext
for registering servlets or resources with the HttpService, a newHttpContext
object is created each time this method is called.void
registerResources(java.lang.String alias, java.lang.String name, HttpContext context)
Registers resources into the URI namespace.void
registerServlet(java.lang.String alias, Servlet servlet, java.util.Dictionary initparams, HttpContext context)
Registers a servlet into the URI namespace.void
unregister(java.lang.String alias)
Unregisters a previous registration done byregisterServlet
orregisterResources
methods.
-
-
-
Method Detail
-
registerServlet
void registerServlet(java.lang.String alias, Servlet servlet, java.util.Dictionary initparams, HttpContext context) throws ServletException, NamespaceException
Registers a servlet into the URI namespace.The alias is the name in the URI namespace of the Http Service at which the registration will be mapped.
An alias must begin with slash ('/') and must not end with slash ('/'), with the exception that an alias of the form "/" is used to denote the root alias. See the specification text for details on how HTTP requests are mapped to servlet and resource registrations.
The Http Service will call the servlet's
init
method before returning.httpService.registerServlet("/myservlet", servlet, initparams, context);
Servlets registered with the same
HttpContext
object will share the sameServletContext
. The Http Service will call thecontext
argument to support theServletContext
methodsgetResource
,getResourceAsStream
andgetMimeType
, and to handle security for requests. If thecontext
argument isnull
, a defaultHttpContext
object is used (seecreateDefaultHttpContext()
).- Parameters:
alias
- name in the URI namespace at which the servlet is registeredservlet
- the servlet object to registerinitparams
- initialization arguments for the servlet ornull
if there are none. This argument is used by the servlet'sServletConfig
object.context
- theHttpContext
object for the registered servlet, ornull
if a defaultHttpContext
is to be created and used.- Throws:
NamespaceException
- if the registration fails because the alias is already in use.ServletException
- if the servlet'sinit
method throws an exception, or the given servlet object has already been registered at a different alias.java.lang.IllegalArgumentException
- if any of the arguments are invalid
-
registerResources
void registerResources(java.lang.String alias, java.lang.String name, HttpContext context) throws NamespaceException
Registers resources into the URI namespace.The alias is the name in the URI namespace of the Http Service at which the registration will be mapped. An alias must begin with slash ('/') and must not end with slash ('/'), with the exception that an alias of the form "/" is used to denote the root alias. The name parameter must also not end with slash ('/') with the exception that a name of the form "/" is used to denote the root of the bundle. See the specification text for details on how HTTP requests are mapped to servlet and resource registrations.
For example, suppose the resource name /tmp is registered to the alias /files. A request for /files/foo.txt will map to the resource name /tmp/foo.txt.
httpservice.registerResources("/files", "/tmp", context);
The Http Service will call theHttpContext
argument to map resource names to URLs and MIME types and to handle security for requests. If theHttpContext
argument isnull
, a defaultHttpContext
is used (seecreateDefaultHttpContext()
).- Parameters:
alias
- name in the URI namespace at which the resources are registeredname
- the base name of the resources that will be registeredcontext
- theHttpContext
object for the registered resources, ornull
if a defaultHttpContext
is to be created and used.- Throws:
NamespaceException
- if the registration fails because the alias is already in use.java.lang.IllegalArgumentException
- if any of the parameters are invalid
-
unregister
void unregister(java.lang.String alias)
Unregisters a previous registration done byregisterServlet
orregisterResources
methods.After this call, the registered alias in the URI name-space will no longer be available. If the registration was for a servlet, the Http Service must call the
destroy
method of the servlet before returning.If the bundle which performed the registration is stopped or otherwise "unget"s the Http Service without calling
unregister(String)
then Http Service must automatically unregister the registration. However, if the registration was for a servlet, thedestroy
method of the servlet will not be called in this case since the bundle may be stopped.unregister(String)
must be explicitly called to cause thedestroy
method of the servlet to be called. This can be done in theBundleActivator.stop
method of the bundle registering the servlet.- Parameters:
alias
- name in the URI name-space of the registration to unregister- Throws:
java.lang.IllegalArgumentException
- if there is no registration for the alias or the calling bundle was not the bundle which registered the alias.
-
createDefaultHttpContext
HttpContext createDefaultHttpContext()
Creates a defaultHttpContext
for registering servlets or resources with the HttpService, a newHttpContext
object is created each time this method is called.The behavior of the methods on the default
HttpContext
is defined as follows:getMimeType
- Does not define any customized MIME types for the Content-Type header in the response, and always returnsnull
.handleSecurity
- Performs implementation-defined authentication on the request.getResource
- Assumes the named resource is in the context bundle; this method calls the context bundle'sBundle.getResource
method, and returns the appropriate URL to access the resource. On a Java runtime environment that supports permissions, the Http Service needs to be grantedorg.osgi.framework.AdminPermission[*,RESOURCE]
.
- Returns:
- a default
HttpContext
object. - Since:
- 1.1
-
-