Sie sind auf Seite 1von 4

Tutorials : Creating Content and Protocol Handlers in Java :

Creating Content and Protocol Handlers in


Java
by Anghel Leonard

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.

Working with an Existing Content Handler


When you "ask" for an Internet file, the browser usually inspects the MIME (Multipurpose Internet Mail Extensions) type of that file to determine
how to process it. MIME types provide the browser with information about the file typefor example, if the file is HTML, then the MIME type will be
text/html. If the file is a .jpg, then the MIME type will be image/jpeg, if the file type is a video MPEG, then the MIME type will be video/mpeg,
etc.) Click here for a detailed description of existing MIME types.

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.

MIME TYPE Class package


audio/aiff aiff.class sun.net.www.content.audio
audio/basic basic.class sun.net.www.content.audio
audio/wav wav.class sun.net.www.content.audio
audio/x_aiff x_aiff.class sun.net.www.content.audio
image/gif gif.class sun.net.www.content.image
image/jpeg jpeg.class sun.net.www.content.image
image/png png.class sun.net.www.content.image
text/plain plain.class sun.net.www.content.text

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:

public abstract Object getContent(URLConnection URLcon) throws IOException

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:

ContentHandler createContentHandler(String MIME_type)

To install a ContentHandlerFactory as your default type, you must call the


URLConnection.setContentHandlerFactory method:

public static void setContentHandlerFactory(ContentHandlerFactory CHF)

.jpg image who's type matches a


To create a viewer for a MIME type for which a default content handler already exists, choose an ordinary
default content handler. Request the .jpg file from an HTTP server using the proper URL. Listing 1 shows the use of a minimal HTTP server
(HTTPServer) that runs on the local host on port 80. It's using the URL http://localhost/image.jpg (though you can use any
other .jpg image as long as you put it into the C:\JEditor\handlers directorythis is the default directory that is used by
the HTTPServer for locating the requested resource). Of course, you can make minimal changes to the application for access any .jpg
image on the Internet. All you have to do in this case, is to change the URL and eventually configure your application to pass through a proxy
server.

HTTPServer and JPGViewer in C:\JEditor\handlers. You


To test this application, make sure you have in the classes for
also need in the same directory a .jpg file named image that can be any JPG image.

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

Applet Index The Java Source


(sorted alphabetically) (applets w/source code)
ABCDEFGHIJK ABCDEFGHIJK
LMNOPQRSTU LMNOPQRSTU
V W X Y Z #s VWXYZ

How to Add Java Applets to Your Site

Creating Content and Protocol


Handlers in Java, Part 2
by Anghel Leonard

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.

Working with an Existing Protocol Handler


Whenever you "ask" for an Internet resource, the browser must complete three essential tasks.
1. The first task is to parse the specified URL (conforming to URL specifications) and extract the protocol name.
2. The second task is based on the protocol namethe browser is choosing "the right words" for connecting and
communicating with the requested server. The final task is to put the received resource into a readable form. These first
two tasks are accomplished by the protocol handler.
3. The final task is accomplished by the content handler. The browser has a protocol handler for every kind of protocol, so
the HTTP protocol handler know how to "talk" with an HTTP server, a FTP protocol handler know how to "talk" with an
FTP server, etc.

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):

Protocol name path class


doc sun.net.www.protocol.doc Handler.class
file sun.net.www.protocol.file Handler.class
ftp sun.net.www.protocol.ftp Handler.class
gopher sun.net.www.protocol.gopher Handler.class
http sun.net.www.protocol.http Handler.class
jar sun.net.www.protocol.jar Handler.class
mailto sun.net.www.protocol.mailto Handler.class
netdoc sun.net.www.protocol.netdoc Handler.class
systemresource sun.net.www.protocol.systemresor Handler.class
ce
verbatim sun.net.www.protocol.verbatim Handler.class
Xy (your protocol handler) sun.net.www.protocol.xy Handler.class

Note: Usually, the sun.net.www.protocol... path is in the <JDK_HOME>/jre/lib/rt.jar.

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

Applet Index The Java Source


(sorted alphabetically) (applets w/source code)
ABCDEFGHIJK ABCDEFGHIJK
LMNOPQRSTU
V W X Y Z #s

Das könnte Ihnen auch gefallen