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, for which Oxygen XML Developer usually provides specific fixed URL stream handlers. If it is set to
handle connections for a specific protocol, this extension will be asked to provide the URL
stream handler for each opened connection of a URL having that protocol.
- boolean canHandleProtocol(String protocol) - This method
checks if the plugin can handle a specific protocol. If this method returns
truefor a specific protocol, the getURLStreamHandler(URL) method will be called for each opened 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 opened connection
of a URL with a protocol for which the canHandleProtocol(String) method
returns
true.If this method returns
null, the default Oxygen XML Developer URLStreamHandler is used.
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 Developer 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 Developer) 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 = new sun.net.www.protocol.http.Handler();
} else {
handler = new sun.net.www.protocol.https.Handler();
}
}
return handler;
}
}