Class ExtendableProtocolURLStreamHandler
- java.lang.Object
-
- java.net.URLStreamHandler
-
- io.microsphere.net.ExtendableProtocolURLStreamHandler
-
public abstract class ExtendableProtocolURLStreamHandler extends java.net.URLStreamHandlerExtendable ProtocolURLStreamHandlerclass supports the sub-protocols, like :{protocol}:{sub-protocols[0]}: ... :{sub-protocols[n]}://...,- {protocol} : The protocol of
URLStreamHandleris recognized byURL(required) - {sub-protocols} : the list of sub-protocols that is
resolvedfromURL(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
SubProtocolURLConnectionFactoryinitialized orURLConnectionopen, thefallback strategywill 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 voidcustomizeSubProtocolURLConnectionFactories(java.util.function.Consumer<java.util.List<SubProtocolURLConnectionFactory>> factoriesCustomizer)CustomizeSubProtocolURLConnectionFactoriesprotected booleanequals(java.net.URL u1, java.net.URL u2)static java.util.Set<java.lang.String>getHandlePackages()static java.lang.StringgetHandlePackagesPropertyValue()Get theSystemproperty value of the packages ofURLStreamHandlers.java.lang.StringgetProtocol()protected inthashCode(java.net.URL u)protected booleanhostsEqual(java.net.URL u1, java.net.URL u2)voidinit()protected voidinitSubProtocolURLConnectionFactories(java.util.List<SubProtocolURLConnectionFactory> factories)InitializeSubProtocolURLConnectionFactoriesprotected java.net.URLConnectionopenConnection(java.net.URL u)java.net.URLConnectionopenConnection(java.net.URL u, java.net.Proxy p)protected java.net.URLConnectionopenFallbackConnection(java.net.URL url, java.net.Proxy proxy)The subclass can override this method to open the fallbackURLConnectionif anySubProtocolURLConnectionFactorydoes not create theURLConnection.protected voidparseURL(java.net.URL u, java.lang.String spec, int start, int limit)protected java.lang.StringreformSpec(java.net.URL url, java.lang.String spec, int start, int end, int limit)Reform the string of specifiedURLif its' scheme presents the sub-protocol, e,g.protected java.lang.StringresolveAuthority(java.net.URL url)protected java.lang.StringresolvePath(java.net.URL url)protected java.util.List<java.lang.String>resolveSubProtocols(java.net.URL url)Get the sub-protocols from the specifiedURLprotected java.lang.StringtoExternalForm(java.net.URL u)Reuses the algorithm ofURLStreamHandler.toExternalForm(URL)using theStringBuilderto theStringBuilder.java.lang.StringtoString()
-
-
-
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 theSystemproperty value of the packages ofURLStreamHandlers.- Returns:
nullif 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:
openConnectionin 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:
openConnectionin classjava.net.URLStreamHandler- Throws:
java.io.IOException
-
openFallbackConnection
protected java.net.URLConnection openFallbackConnection(java.net.URL url, java.net.Proxy proxy) throws java.io.IOExceptionThe subclass can override this method to open the fallbackURLConnectionif anySubProtocolURLConnectionFactorydoes not create theURLConnection.- Parameters:
url- the URL that this connects toproxy-Proxythe proxy through which the connection will be made. If direct connection is desired, Proxy.NO_PROXY should be specified.- Returns:
nullas default- Throws:
java.io.IOException
-
equals
protected final boolean equals(java.net.URL u1, java.net.URL u2)- Overrides:
equalsin classjava.net.URLStreamHandler
-
hashCode
protected final int hashCode(java.net.URL u)
- Overrides:
hashCodein classjava.net.URLStreamHandler
-
hostsEqual
protected final boolean hostsEqual(java.net.URL u1, java.net.URL u2)- Overrides:
hostsEqualin classjava.net.URLStreamHandler
-
toExternalForm
protected final java.lang.String toExternalForm(java.net.URL u)
Reuses the algorithm ofURLStreamHandler.toExternalForm(URL)using theStringBuilderto theStringBuilder.- Overrides:
toExternalFormin 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:
parseURLin 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 specifiedURLif 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 specifiedURLwill be "jdbc://localhost:3307/mydb;_sp=mysql?charset=UTF-8#top".- Parameters:
url- theURLto receive the result of parsing the spec.spec- theStringrepresenting 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 thestartindex, 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
URLif 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:
toStringin classjava.lang.Object
-
-