FileDocCategorySizeDatePackage
UrlInterceptHandlerGears.javaAPI DocAndroid 1.5 API14685Wed May 06 22:41:56 BST 2009android.webkit.gears

UrlInterceptHandlerGears

public class UrlInterceptHandlerGears extends Object implements android.webkit.UrlInterceptHandler
Services requests to handle URLs coming from the browser or HttpRequestAndroid. This registers itself with the UrlInterceptRegister in Android so we get a chance to service all URLs passing through the browser before anything else.

Fields Summary
private static UrlInterceptHandlerGears
instance
Singleton instance.
private static final String
LOG_TAG
Debug logging tag.
private static final int
BUFFER_SIZE
Buffer size for reading/writing streams.
private static boolean
logEnabled
Enable/disable all logging in this class.
public static final int
HEADERS_MAP_INDEX_KEY
The unmodified (case-sensitive) key in the headers map is the same index as used by HttpRequestAndroid.
public static final int
HEADERS_MAP_INDEX_VALUE
The associated value in the headers map is the same index as used by HttpRequestAndroid.
Constructors Summary
public UrlInterceptHandlerGears()
Construct and initialize the singleton instance.

    if (instance != null) {
      Log.e(LOG_TAG, "UrlInterceptHandlerGears singleton already constructed");
      throw new RuntimeException();
    }
    instance = this;
  
Methods Summary
public static voidenableLogging(boolean on)
Turn on/off logging in this class.

param
on Logging enable state.

    logEnabled = on;
  
public static android.webkit.gears.UrlInterceptHandlerGearsgetInstance()
Get the singleton instance.

return
The singleton instance.

    return instance;
  
public android.webkit.PluginDatagetPluginData(java.lang.String url, java.util.Map requestHeaders)
Given an URL, returns a PluginData instance which contains the response for the request. This implements the UrlInterceptHandler interface.

param
url The fully qualified URL being requested.
param
requestHeaders The request headers for this URL.
return
a PluginData object.

    // Thankfully the browser does call us with case-sensitive
    // headers. We just need to map it case-insensitive.
    Map<String, String[]> lowercaseRequestHeaders =
        new HashMap<String, String[]>();
    Iterator<Map.Entry<String, String>> requestHeadersIt =
        requestHeaders.entrySet().iterator();
    while (requestHeadersIt.hasNext()) {
      Map.Entry<String, String> entry = requestHeadersIt.next();
      String key = entry.getKey();
      String mapValue[] = { key, entry.getValue() };
      lowercaseRequestHeaders.put(key.toLowerCase(), mapValue);
    }
    ServiceResponse response = getServiceResponse(url, lowercaseRequestHeaders);
    if (response == null) {
      // No result for this URL.
      return null;
    }
    return new PluginData(response.getInputStream(),
                          response.getContentLength(),
                          response.getResponseHeaders(),
                          response.getStatusCode());
  
public android.webkit.gears.UrlInterceptHandlerGears$ServiceResponsegetServiceResponse(java.lang.String url, java.util.Map requestHeaders)
Given an URL, returns a CacheResult and headers which contain the response for the request.

param
url The fully qualified URL being requested.
param
requestHeaders The request headers for this URL.
return
If a response can be crafted, a ServiceResponse is created which contains all response headers and an InputStream attached to the body. If there is no response, null is returned.

    if (!url.startsWith("http://") && !url.startsWith("https://")) {
      // Don't know how to service non-HTTP URLs
      return null;
    }
    // Call the native handler to craft a response for this URL.
    return nativeService(new ServiceRequest(url, requestHeaders));
  
private voidlog(java.lang.String str)
Convenience debug function. Calls the Android logging mechanism. logEnabled is not a constant, so if the string evaluation is potentially expensive, the caller also needs to check it.

param
str String to log to the Android console.

    if (logEnabled) {
      Log.i(LOG_TAG, str);
    }
  
private static native android.webkit.gears.UrlInterceptHandlerGears$ServiceResponsenativeService(android.webkit.gears.UrlInterceptHandlerGears$ServiceRequest request)
Native method which handles the bulk of the request in LocalServer.

param
request A ServiceRequest object containing information about the request.
return
If serviced, a ServiceResponse object containing all the information to provide a response for the URL, or null if no response available for this URL.

public synchronized voidregister()
Register the singleton instance with the browser's interception mechanism.

    UrlInterceptRegistry.registerHandler(this);
  
public android.webkit.CacheManager.CacheResultservice(java.lang.String url, java.util.Map headers)
Given an URL, returns the CacheResult which contains the surrogate response for the request, or null if the handler is not interested.

param
url URL string.
param
headers The headers associated with the request. May be null.
return
The CacheResult containing the surrogate response.
Deprecated
Use PluginData getPluginData(String url, Map headers); instead

      throw new UnsupportedOperationException("unimplemented");
    
public synchronized voidunregister()
Unregister the singleton instance from the browser's interception mechanism.

    UrlInterceptRegistry.unregisterHandler(this);