org.clazzes.util.http.osgi
public class BasicHttpContext extends Object implements org.osgi.service.http.HttpContext
The behavior of the HttpContext
methods is
defined as follows:
getMimeType(String)
returns a MIME type based on the file extension of
the given path, using getExtensionMimeTypes()
first, then DefaultHttpContext.getMimeType(String)
as failback.
If no mapping can be made, null
is returned.
handleSecurity(HttpServletRequest, HttpServletResponse)
Always return true
.
getResource(String)
assumes the named resource is in the
context bundle; this method calls the context bundle's
Bundle.getResource(String)
method using the supplied resource prefix,
and returns the appropriate URL to
access the resource. On a Java runtime environment that supports
permissions, the Http Service needs to be granted
AdminPermission
[*,RESOURCE]
.
If you trust the default mime type map of DefaultHttpContext
,
the bean can be setup inside a blueprint container using this example:
<bean id="httpContext" class="org.clazzes.util.http.osgi.BasicHttpContext"> <property name="resourceBundle" ref="blueprintBundle"/> </bean>
To take control over mime types important for the application start with this example:
<bean id="httpContext" class="org.clazzes.util.http.osgi.BasicHttpContext"> <property name="resourceBundle" ref="blueprintBundle"/> <property name="extensionMimeTypes"> <map> <entry key="html" value="text/html"/> <entry key="xml" value="text/xml"/> <entry key="js" value="application/javascript"/> <entry key="json" value="application/json"/> <entry key="css" value="text/css"/> <entry key="png" value="image/png"/> <entry key="jpg" value="image/jpeg"/> <entry key="jpeg" value="image/jpeg"/> <entry key="gif" value="image/gif"/> <entry key="tif" value="image/tiff"/> </map> </property> </bean>
Please do not forget to manually import the org.clazzes.util.http.osgi
package in blueprint bundles, otherwise you will run into ClassNotFoundException
s.
Constructor and Description |
---|
BasicHttpContext() |
Modifier and Type | Method and Description |
---|---|
Map<String,String> |
getExtensionMimeTypes() |
String |
getMimeType(String name)
Tries to detect the mime type for the file name given.
|
URL |
getResource(String name) |
org.osgi.framework.Bundle |
getResourceBundle() |
String |
getResourcePrefix() |
boolean |
handleSecurity(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response) |
void |
setExtensionMimeTypes(Map<String,String> extensionMimeTypes) |
void |
setResourceBundle(org.osgi.framework.Bundle resourceBundle) |
void |
setResourcePrefix(String resourcePrefix) |
public boolean handleSecurity(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws IOException
handleSecurity
in interface org.osgi.service.http.HttpContext
IOException
public URL getResource(String name)
getResource
in interface org.osgi.service.http.HttpContext
public String getMimeType(String name)
getExtensionMimeTypes()
fails,
DefaultHttpContext.getMimeType(String)
is asked for advice.getMimeType
in interface org.osgi.service.http.HttpContext
public org.osgi.framework.Bundle getResourceBundle()
public void setResourceBundle(org.osgi.framework.Bundle resourceBundle)
resourceBundle
- the bundle used to resolve resources to set.public Map<String,String> getExtensionMimeTypes()
DefaultHttpContext.getExtensionMimeTypes()
also may be used by getMimeType(String)
.public void setExtensionMimeTypes(Map<String,String> extensionMimeTypes)
extensionMimeTypes
- A map containing file extension as keys and MIME types
as values to set. The extensions are not case-sensitive, please supply
supply the file extension in lower case.
Note that DefaultHttpContext.getExtensionMimeTypes()
also may be used by getMimeType(String)
.public String getResourcePrefix()
getResource(String)
.public void setResourcePrefix(String resourcePrefix)
resourcePrefix
- the prefix to attach when searching resource paths in
getResource(String)
to set. If you set this property to
WEB-INF/
, all resources are searched inside the WEB-INF
folder of your OSGi bundle, which is the recommended approach.Copyright © 2013 Clazzes.org. All Rights Reserved.