Class ExtendableProtocolURLStreamHandler
- java.lang.Object
-
- java.net.URLStreamHandler
-
- io.microsphere.net.ExtendableProtocolURLStreamHandler
-
public abstract class ExtendableProtocolURLStreamHandler extends java.net.URLStreamHandler
Extendable ProtocolURLStreamHandler
class supports the sub-protocols, like :{protocol}:{sub-protocols[0]}: ... :{sub-protocols[n]}://...
,- {protocol} : The protocol of
URLStreamHandler
is recognized byURL
(required) - {sub-protocols} : the list of sub-protocols that is
resolved
fromURL
(optional)
The method
initSubProtocolURLConnectionFactories(List)
that is overridden allows the sub-protocols to be extended, the prerequisite is the methodinit()
being invoked later.If no
SubProtocolURLConnectionFactory
initialized orURLConnection
open, thefallback strategy
will be applied.If there is no requirement to support the sub-protocol, the subclass only needs to override
openConnection(URL, Proxy)
method.If an instance is instantiated by the default constructor, the implementation class must the obey conventions as follow:
- The class must be the top level
- The simple class name must be "Handler"
- The class must not be present in the "default" or builtin package()
A new instance also can specify some protocol via
the constructor with the protocol argument
.Node: these methods are overridden making final:
openConnection(URL)
parseURL(URL, String, int, int)
equals(URL, URL)
hostsEqual(URL, URL)
hashCode(URL)
toExternalForm(URL)
Example Usage
Consider a custom protocol handler for "jdbc" supporting sub-protocols like "mysql", "postgresql", etc.:{@code public class Handler extends ExtendableProtocolURLStreamHandler { public Handler() { super("jdbc"); }
- Since:
- 1.0.0
- Author:
- Mercy
- See Also:
SubProtocolURLConnectionFactory
,URLStreamHandler
- {protocol} : The protocol of
-
-
Constructor Summary
Constructors Constructor Description ExtendableProtocolURLStreamHandler()
The default constructor must obey the following conventions: The class must be the top level The simple class name must be "Handler" The class must not be present in the "default" or builtin package("sun.net.www.protocol"
)ExtendableProtocolURLStreamHandler(java.lang.String protocol)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
customizeSubProtocolURLConnectionFactories(java.util.function.Consumer<java.util.List<SubProtocolURLConnectionFactory>> factoriesCustomizer)
CustomizeSubProtocolURLConnectionFactories
protected boolean
equals(java.net.URL u1, java.net.URL u2)
static java.util.Set<java.lang.String>
getHandlePackages()
static java.lang.String
getHandlePackagesPropertyValue()
Get theSystem
property value of the packages ofURLStreamHandlers
.java.lang.String
getProtocol()
protected int
hashCode(java.net.URL u)
protected boolean
hostsEqual(java.net.URL u1, java.net.URL u2)
void
init()
protected void
initSubProtocolURLConnectionFactories(java.util.List<SubProtocolURLConnectionFactory> factories)
InitializeSubProtocolURLConnectionFactories
protected java.net.URLConnection
openConnection(java.net.URL u)
java.net.URLConnection
openConnection(java.net.URL u, java.net.Proxy p)
protected java.net.URLConnection
openFallbackConnection(java.net.URL url, java.net.Proxy proxy)
The subclass can override this method to open the fallbackURLConnection
if anySubProtocolURLConnectionFactory
does not create theURLConnection
.protected void
parseURL(java.net.URL u, java.lang.String spec, int start, int limit)
protected java.lang.String
reformSpec(java.net.URL url, java.lang.String spec, int start, int end, int limit)
Reform the string of specifiedURL
if its' scheme presents the sub-protocol, e,g.protected java.lang.String
resolveAuthority(java.net.URL url)
protected java.lang.String
resolvePath(java.net.URL url)
protected java.util.List<java.lang.String>
resolveSubProtocols(java.net.URL url)
Get the sub-protocols from the specifiedURL
protected java.lang.String
toExternalForm(java.net.URL u)
Reuses the algorithm ofURLStreamHandler.toExternalForm(URL)
using theStringBuilder
to theStringBuilder
.java.lang.String
toString()
-
-
-
Constructor Detail
-
ExtendableProtocolURLStreamHandler
public ExtendableProtocolURLStreamHandler()
The default constructor must obey the following conventions:- The class must be the top level
- The simple class name must be "Handler"
- The class must not be present in the "default" or builtin package(
"sun.net.www.protocol"
)
-
ExtendableProtocolURLStreamHandler
public ExtendableProtocolURLStreamHandler(java.lang.String protocol)
-
-
Method Detail
-
getHandlePackages
@Nonnull @Immutable public static java.util.Set<java.lang.String> getHandlePackages()
-
getHandlePackagesPropertyValue
public static java.lang.String getHandlePackagesPropertyValue()
Get theSystem
property value of the packages ofURLStreamHandlers
.- Returns:
null
if absent
-
init
public void init()
-
initSubProtocolURLConnectionFactories
protected void initSubProtocolURLConnectionFactories(java.util.List<SubProtocolURLConnectionFactory> factories)
InitializeSubProtocolURLConnectionFactories
- Parameters:
factories
- the collection ofSubProtocolURLConnectionFactories
-
customizeSubProtocolURLConnectionFactories
public void customizeSubProtocolURLConnectionFactories(java.util.function.Consumer<java.util.List<SubProtocolURLConnectionFactory>> factoriesCustomizer)
CustomizeSubProtocolURLConnectionFactories
- Parameters:
factoriesCustomizer
- the customizer for collection ofSubProtocolURLConnectionFactories
-
openConnection
protected final java.net.URLConnection openConnection(java.net.URL u) throws java.io.IOException
- Specified by:
openConnection
in classjava.net.URLStreamHandler
- Throws:
java.io.IOException
-
openConnection
public java.net.URLConnection openConnection(java.net.URL u, java.net.Proxy p) throws java.io.IOException
- Overrides:
openConnection
in classjava.net.URLStreamHandler
- Throws:
java.io.IOException
-
openFallbackConnection
protected java.net.URLConnection openFallbackConnection(java.net.URL url, java.net.Proxy proxy) throws java.io.IOException
The subclass can override this method to open the fallbackURLConnection
if anySubProtocolURLConnectionFactory
does not create theURLConnection
.- Parameters:
url
- the URL that this connects toproxy
-Proxy
the proxy through which the connection will be made. If direct connection is desired, Proxy.NO_PROXY should be specified.- Returns:
null
as default- Throws:
java.io.IOException
-
equals
protected final boolean equals(java.net.URL u1, java.net.URL u2)
- Overrides:
equals
in classjava.net.URLStreamHandler
-
hashCode
protected final int hashCode(java.net.URL u)
- Overrides:
hashCode
in classjava.net.URLStreamHandler
-
hostsEqual
protected final boolean hostsEqual(java.net.URL u1, java.net.URL u2)
- Overrides:
hostsEqual
in classjava.net.URLStreamHandler
-
toExternalForm
protected final java.lang.String toExternalForm(java.net.URL u)
Reuses the algorithm ofURLStreamHandler.toExternalForm(URL)
using theStringBuilder
to theStringBuilder
.- Overrides:
toExternalForm
in classjava.net.URLStreamHandler
- Parameters:
u
- the URL.- Returns:
- a string representation of the URL argument.
-
parseURL
protected final void parseURL(java.net.URL u, java.lang.String spec, int start, int limit)
- Overrides:
parseURL
in classjava.net.URLStreamHandler
-
reformSpec
protected java.lang.String reformSpec(java.net.URL url, java.lang.String spec, int start, int end, int limit)
Reform the string of specifiedURL
if its' scheme presents the sub-protocol, e,g. A string representing the URL is "jdbc:mysql://localhost:3307/mydb?charset=UTF-8#top", its'- scheme : "jdbc:mysql"
- host : "localhost"
- port : 3307
- path : "/mydb"
- query : "charset=UTF-8"
- ref : "top"
This scheme contains two parts, the former is "jdbc" as the protocol, the later is "mysql" called sub-protocol which is convenient to extend the fine-grain
URLStreamHandler
. In this case, the reformed string of specifiedURL
will be "jdbc://localhost:3307/mydb;_sp=mysql?charset=UTF-8#top".- Parameters:
url
- theURL
to receive the result of parsing the spec.spec
- theString
representing the URL that must be parsed.start
- the character index at which to begin parsing. This is just past the ':
' (if there is one) that specifies the determination of the protocol name.end
- the index of the string "://" present in the URL from thestart
index, its' value is greater or equal 0.limit
- the character position to stop parsing at. This is the end of the string or the position of the "#
" character, if present. All information after the sharp sign indicates an anchor.- Returns:
- reformed the string of specified
URL
if the suffix o
-
resolveSubProtocols
protected java.util.List<java.lang.String> resolveSubProtocols(java.net.URL url)
Get the sub-protocols from the specifiedURL
- Parameters:
url
-URL
- Returns:
- non-null
-
resolveAuthority
protected java.lang.String resolveAuthority(java.net.URL url)
-
resolvePath
protected java.lang.String resolvePath(java.net.URL url)
-
getProtocol
public final java.lang.String getProtocol()
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-