org.clazzes.util.http.osgi
Class BasicHttpContext

java.lang.Object
  extended by org.clazzes.util.http.osgi.BasicHttpContext
All Implemented Interfaces:
org.osgi.service.http.HttpContext

public class BasicHttpContext
extends Object
implements org.osgi.service.http.HttpContext

A HttpContext implementation with an extension to MIME type mapping and resources received from a specified bundle, which is usually injected using blueprint.

The behavior of the HttpContext methods is defined as follows:

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 ClassNotFoundExceptions.


Field Summary
 
Fields inherited from interface org.osgi.service.http.HttpContext
AUTHENTICATION_TYPE, AUTHORIZATION, REMOTE_USER
 
Constructor Summary
BasicHttpContext()
           
 
Method Summary
 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)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BasicHttpContext

public BasicHttpContext()
Method Detail

handleSecurity

public boolean handleSecurity(javax.servlet.http.HttpServletRequest request,
                              javax.servlet.http.HttpServletResponse response)
                       throws IOException
Specified by:
handleSecurity in interface org.osgi.service.http.HttpContext
Throws:
IOException

getResource

public URL getResource(String name)
Specified by:
getResource in interface org.osgi.service.http.HttpContext

getMimeType

public String getMimeType(String name)
Tries to detect the mime type for the file name given. When the local map getExtensionMimeTypes() fails, DefaultHttpContext.getMimeType(String) is asked for advice.

Specified by:
getMimeType in interface org.osgi.service.http.HttpContext
Returns:
The mime type of the path given, or null if no mapping is available.

getResourceBundle

public org.osgi.framework.Bundle getResourceBundle()
Returns:
the bundle used to resolve resources.

setResourceBundle

public void setResourceBundle(org.osgi.framework.Bundle resourceBundle)
Parameters:
resourceBundle - the bundle used to resolve resources to set.

getExtensionMimeTypes

public Map<String,String> getExtensionMimeTypes()
Returns:
A map containing file extension as keys and MIME types as values. Note that DefaultHttpContext.getExtensionMimeTypes() also may be used by getMimeType(String).

setExtensionMimeTypes

public void setExtensionMimeTypes(Map<String,String> extensionMimeTypes)
Parameters:
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).

getResourcePrefix

public String getResourcePrefix()
Returns:
the prefix to attach when searching resource paths in getResource(String).

setResourcePrefix

public void setResourcePrefix(String resourcePrefix)
Parameters:
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 © 2012. All Rights Reserved.