Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to subclass URLConnection?  RSS feed

 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to extend Java's URL handling so that it will handle my own custom protocol. I have got as far as registering my URLStreamHandler, so that Java will call my openConnection() method, when it encounters a URL with my protocol. But what to do now?

It looks like I need to return a subclass of URLConnection. However, that class has some of the least helpful javadoc I have ever seen in the JDK. It certainly says almost nothing about how to subclass it.

Can anyone point me to any articles or examples that will help me to do the right thing?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess you could download the source for the library, examine URLConnection and see how HttpURLConnection extends it. There are a boatload of methods for sure and it's hard to guess which ones you'll have to override from the doc.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did already look at the source of URLConnection and its two subclasses HttpURLConnection and JarURLConnection. Unfortunately, they shed very little light on what I should do in my situation.

However, by pure experiment, I have found that, for my particular application, it seems to suffice to override connect() and getInputStream(). I use the inherited field "connected" to note whether the connection has been connected.

It "works", but it would be nice to know whether I have made a legitimate URLConnection class, or whether there is other stuff I should have done.

FWIW I think URLConnection is a badly-designed class. It comes from JDK 1.0, and one can certainly tell. For instance, the base class includes a whole load of concepts, like headers, that are clearly only really applicable to HTTP connections, and ought to be in the subclass, therefore.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!