Edit online

Targeted URL Stream Handler Plugin Extension

This type of plugin can be used when it is necessary to impose custom URL stream handlers for specific URLs.

This plugin extension can handle the following protocols: http, https, ftp or sftp. Oxygen XML Editor usually provides specific fixed URL stream handlers. If it is set to handle connections for a specific protocol, this extension prompts for the URL stream handler for each open connection of a URL that has that protocol.

To use this type of plugin, you have to implement the ro.sync.exml.plugin.urlstreamhandler.TargetedURLStreamHandlerPluginExtension interface that provides the following methods:

boolean canHandleProtocol(String protocol)
This method checks if the plugin can handle a specific protocol. If this method returns true for a specific protocol, the getURLStreamHandler(URL) method will be called for each open connection of a URL having this protocol.
URLStreamHandler getURLStreamHandler(URL url)

This method provides the URL handler for the specified URL and it is called for each open connection of a URL with a protocol that has the canHandleProtocol(String) method return true.

If this method returns null, the default Oxygen XML Editor URLStreamHandler is used.

To use this type of extension in your plugin, create an extension of TargetedURLHandler type in your plugin.xml file and specify the class that implements TargetedURLStreamHandlerPluginExtension:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plugin SYSTEM "../plugin.dtd">
<plugin name="CustomTargetedURLStreamHandlerPlugin" ..............>
 <runtime>
  ........
 </runtime>
 
 <extension type="TargetedURLHandler" 
          class="CustomTargetedURLStreamHandlerPluginExtension"/>
 ...............
  
</plugin>

This extension can be useful in situations when connections opened from a specific host must be handled in a particular way. For example, the Oxygen XML Editor HTTP URLStreamHandler may not be compatible for sending and receiving SOAP using the SUN Web Services implementation. In this case, you can override the stream handler (set by Oxygen XML Editor) to use the default SUN URLStreamHandler, since it is more compatible with sending and receiving SOAP requests.

public class CustomTargetedURLStreamHandlerPluginExtension 
  implements TargetedURLStreamHandlerPluginExtension {

  @Override
  public boolean canHandleProtocol(String protocol) {
    boolean handleProtocol = false;
    if ("http".equals(protocol) || "https".equals(protocol)) {
      // This extension handles both HTTP and HTTPS protocols
      handleProtocol = true;
    }
    return handleProtocol;
  }

  @Override
  public URLStreamHandler getURLStreamHandler(URL url) {
    // This method is called only for the URLs with a protocol 
    // where canHandleProtocol(String) method returns true (HTTP and HTTPS)

    URLStreamHandler handler = null;
      
    String host = url.getHost();
    String protocol = url.getProtocol();
    if ("some_host".equals(host)) {
      // When there are connections opened from some_host, the SUN HTTP(S) 
      // handlers are used
      if ("http".equals(protocol)) {
        handler = (URLStreamHandler) Class.forName("sun.net.www.protocol.http.Handler")
.getConstructor(new Class[0]).newInstance(new Object[0]);
      } else {
        handler = (URLStreamHandler) Class.forName("sun.net.www.protocol.https.Handler")
.getConstructor(new Class[0]).newInstance(new Object[0]);
      }
    }
    return handler;
  }
}