15.7 Content Handlers


The URLConnection class is intimately tied to Java's protocol and content handler mechanism. The protocol handler is responsible for making connections, exchanging headers, requesting particular documents, and so forth. It handles all the overhead of the protocol for requesting files. The content handler deals only with the actual data. It takes the raw input after all headers and so forth are stripped and converts it to the right kind of object for Java to deal with; for instance, an InputStream or an ImageProducer .

15.7.1 Getting Content

The getContent( ) methods of URLConnection use a content handler to turn the raw data of a connection into a Java object. public Object getContent( ) throws IOException

This method is virtually identical to the getContent() method of the URL class. In fact, that method just calls this method. getContent( ) downloads the object selected by the URL of this URLConnection . For getContent() to work, the virtual machine needs to recognize and understand the content type. The exact content types supported vary from one VM and version to the next . Sun's JDK 1.5 supports text/plain , image/gif , image/jpeg , image/png , audio/aiff , audio/basic , audio/wav , and a few others. Different VMs and applications may support additional types. For instance, HotJava 3.0 includes a PDF content handler. Furthermore, you can install additional content handlers that understand other content types.

getContent( ) works only for protocols like HTTP, which has a clear understanding of MIME content types. If the content type is unknown or the protocol doesn't understand content types, getContent( ) throws an UnknownServiceException . public Object getContent(Class[] classes) throws IOException // Java 1.3

This overloaded variant of the getContent( ) method lets you choose what class you'd like the content returned as in order to provide different object representations of data. The method attempts to return the content in the form of one of the classes in the classes array. The order of preference is the order of the array. For instance, if you'd prefer an HTML file to be returned as a String but your second choice is a Reader and your third choice is an InputStream , you would write:

 URL u = new URL("http://www.thehungersite.com/"); URLConnection uc = u.openConnection( ) Class[] types = {String.class, Reader.class, InputStream.class}; Object o = uc.getContent(types); 

Then test for the type of the returned object using instanceof . For example:

 if (o instanceof String) {   System.out.println(o);  } else if (o instanceof Reader) {   int c;   Reader r = (Reader) o;   while ((c = r.read( )) != -1) System.out.print((char) c);  } else if (o instanceof InputStream) {   int c;   InputStream in = (InputStream) o;   while ((c = in.read( )) != -1) System.out.write(c);          } else if (o == null) {   System.out.println("None of the requested types were available.");  } else {   System.out.println("Error: unexpected type " + o.getClass( ));  } 

That last else clause shouldn't be reached. If none of the requested types are available, this method is supposed to return null rather than returning an unexpected type.

15.7.2 ContentHandlerFactory

The URLConnection class contains a static Hashtable of ContentHandler objects. Whenever the getContent( ) method of URLConnection is invoked, Java looks in this Hashtable to find the right content handler for the current URL, as indicated by the URL's Content-type. If it doesn't find a ContentHandler object for the MIME type, it tries to create one using a ContentHandlerFactory (which you'll learn more about in Chapter 17). That is, a content handler factory tells the program where it can find a content handler for a text/html file, an image/gif file, or some other kind of file. You can set the ContentHandlerFactory by passing an instance of the java.net.ContentHandlerFactory interface to the setContentHandlerFactory( ) method:

 public static void setContentHandlerFactory(ContentHandlerFactory factory)   throws SecurityException, Error 

You may set the ContentHandlerFactory only once per application; this method throws a generic Error if it is called a second time. As with most other setFactory( ) methods, untrusted applets will generally not be allowed to set the content handler factory whether one has already been set or not. Attempting to do so throws a SecurityException .

Java Network Programming
Java Network Programming, Third Edition
ISBN: 0596007213
EAN: 2147483647
Year: 2003
Pages: 164

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net