Beruflich Dokumente
Kultur Dokumente
Certainly, you've noticed that when you "ask" for an Internet file, your browser has different ways to access it, depending on the file type. For
example, if the file is a simple HTML or a common image type ( .gif, .jpg), the browser becomes a viewer for it. However, if the file is a
.pdf, a movie, or some other complex file type, the browser calls for external help. The browser identifies the file types, knows how to process
the "simple" ones, and knows it needs special viewers or some plug-ins to handle complex file types.
Browsers also know how to "talk" with different kinds of Internet serversHTTP servers, FTP servers, and any other kind of familiar server,
depending on the kind of URL that's supplied.
Did you know that it's possible to simulate this behavior in Java? This article will show you how to create the Internet client, the Internet server (a
minimal HTTP server), and a dedicated viewer for a new image typealso known as a content handler. You'll also learn how to use an existing
content handler.
Java allows you to create a network application that uses a URL to access a Web resource through the java.net.URLConnection
class. Similar to a Web browser, this class inspects the MIME type of the received file and determines how to process it. It then calls a specialized
class that understands the file format and puts that format into a "readable" form. These specialized classes are known as "content handlers" and
they are subclasses of the java.net.ContentHandler abstract class. The practical purpose of a content handler is to extract an
object specific to a MIME type from an URLConnection. You can extend this abstract class for new viewers, new MIME types, or for existing
types that are not, by default, supported by Java. Java offers a default set of content handlers that cover the most used MIME types. The content
handlers' class names-along with the names of the packages where you can find themreflects the MIME type name. For instance, the MIME type
image/png's content handler is in the sun.net.www.content.image package and its name is png.class. So, the
sun.net.www.content path is fixed and the rest of it depends on the MIME type.
Note: You can usually find the sun.net.www.content... path in the <JDK_HOME>/jre/lib/rt.jar.
Table 1: Java correspondence between MIME types and their content handlers.
When you access an Internet resource through URLConnection, Java joins the proper content handler with the resource MIME type. Java
determines the proper content handler by using the MIME type to find the class it implements.
To create a content handler, you must first create a subclass of the ContentHandler class and override the getContent method.
This method gets a URLConnection argument and returns an Object object that fits with the MIME type. The most effective way to use
the object returned by this method is to make a cast conversion to the right object. Here's the syntax for the getContent method:
To join a content handler with a MIME type, you must use the java.net.Content HandlerFactory interface. Implementing this
interface forces you to implement a behavior to the ContentHandlerFactory.createContentHandler method. This
method is gets a string argument representing the MIME type and uses this string for returning the right content handler:
As shown in Figure 1, IE displays the same result as the JPGViewer application. This is because the image/jpeg MIME type is a "member" of
the Internet MIME types standard.
Listing 2 coresponds to the MIME type, text/plain and can be used for viewing the content of any text file. The Java content handler for this MIME
type is plain.class and is located in the sun.net.www.content.text package. This example uses the same
HTTPServer and the URL http://localhost/test.txt. The text file is named test.txt and contains the text "This
is a text file...". Of course, you can use any other text filejust remember to copy the text file into the
C:\\JEditor\handlers directory, so that HTTPServer can find it.
Figure 2 shows how the test.txt file content is viewed by IE and in the ListTextFiles application.
ListTextFiles
Home / Articles / Creating Content and Protocol Handlers in Java / 1 Next Page
Part 1 of this series showed you how to work with an existing content handler and how to create a new one. It also discussed how
to define a new MIME type for a new kind of file (the .xy file) and how to transfer it from a simple HTTP server to a Java client.
In principle, the Internet browser (Internet Explorer 6.0) and the Java client that you have developed for the new content handler
use the same ideathat is, both of them use the HTTP specification protocol for connecting and sending a request to the HTTP
server HTTPServer.java (the source code for this minimal HTTP server can be viewed in Part 1). When they get the
sever response, they process it using their appropriate content handlers.
The question is: how does the Java client (or the browser) know that it will "talk" with an HTTP server and how does it know what
to "say"? The answer is that the Java client has no idea what kind of server it will "talk" to. The Java client just parses the provided
URL and extracts the protocol name (http, ftp, etc.). After that, it associates the protocol name with the corresponding
protocol implementation. These tasks are accomplished by the protocol handler.
In this article, you will learn how to work with an existing Java protocol handler and how to create a new one. Of course, when
you're writing your own protocol handler, it will not be recognized by Internet servers, or any other kind of servers, because they
can't possibly recognize it. For testing purposes, these examples use the HTTPServer.java you developed in Part 1.
This server will recognize the protocol handler defined in Part 2 and the content handler defined in Part 1.
In Java, URL objects use protocol handlers to open connections to specified servers. By default, Java supports a set of protocol
handlers that have been developed for the most common protocols, but Java also offers you support for any new protocol
handlers you should create. Such support can be found in the java.net.URLStreamHandler and
java.net.URLConnection classes.
The default protocol handlers' class names, along with their associated package names, reflect the protocol names themselves.
For example, the http protocol's proper protocol handler is in the sun.net.www.protocol.http package and its
name is Handler.class. The sun.net.www.protocol path is fixed and the rest depends on the protocol
name. The table below contains the correspondence between some protocol names and their protocol handlers (J2SE 1.5):
Listing 1 demonstrates how to create a simple Java client that uses the protocol handler specific to the HTTP protocol for
connecting and communicating with an HTTP server.
As you can see, the default HTTP protocol handler eliminates the need to write any HTTP commands because Java knows how to
implement it in the sun.net.www.protocol.http package.
Home / Articles / Creating Content and Protocol Handlers in Java, Part 2 / 1 Next Page