Creating a Content Handler
First, create a new MIME type that will represent a new kind of file type. Suppose that this new kind of file type is an image with the following characteristics:
- The file hasthe extension
.xy.
- The file contains information about an image that supports only two colors.
- The information is used to create a
MemoryImageSource image.
- The first line in the file contains the width and height of the image in the format width| height.
- The second line of the file contains the codes for the two colors in the format
code_color_1*x*code_color_2*y (the supported codes are <!--0-black,1-red,2-blue,3-green,4-white,other_code- new Color(112,112,112)-->).
- The rest of the file contains alternative lines of the form
number_of_pixels_of_the_first_color*x and number_of_pixels_of_the_second_ color*y.
- The associate MIME type for this kind of file is image/xy.
Figure 3 shows an example file, named image.xy:

Every line of dates must be written in the file on a separate linethe file image.xy listed above should have 54 lines:
image.xy file
25|25 5-y
0*x*4*y 4-x
80-x 5-y
15-y 11-x
10-x 5-y
15-y 3-x
10-x 6-y
15-y 11-x
15-x 14-y
5-y 12-x
20-x 14-y
5-y 12-x
20-x 15-y
5-y 11-x
20-x 7-y
5-y 2-x
20-x 6-y
5-y 20-x
11-x 11-y
5-y 13-x
4-x 1-y
5-y 11-x
11-x 13-y
5-y 12-x
4-x 12-y
5-y 38-x
11-x <!--0-black,1-red,2-blue,3-green,4-white,other_code- new
Color(112,112,112)-->
Remember that this is just a simple file format example. Of course, in real life you will most likely use much more complicated formats, like .jpg or .bmp formats. Editing the image information in text is easier to understand than in binary.
Now that you have a new kind of image, no existing viewers can see it. That means that you have to create your own viewer. To do this, create a new content handler that will know how to process the information from the file and return the desirable MemoryImageSource object. Listing 3 shows a possible content handler for this kind of file.
Notice that this class is named xy.class and it is stored in the sun.net.www.content.image package. This helps Java automatically find this content handler, which saves you from having to install it manually by calling the URLConnection.setContentHandlerFactory method. It also saves you from having to define a ContentHandlerFactory factory. Notice that this class extends the content handler class and defines the getContent method. The getContent method implementation is pretty simple: it uses the StringTokenizer class to split the file into tokens and then uses those tokens to construct a color matrix that goes as an argument to the MemoryImageSource constructor. Of course, the getContent method implementation depends on the file type.
Note: The source for the MemoryImageSource can be found here.
Now you are ready to view a .xy file. The only problem is that no Internet server can return an image/xy MIME type. To solve this problem, you can either ask key people to incorpotate your new MIME type into the appropriate applications, or you can create a new Internet server that can return your MIME type. The latter is the most realistic option.
Though the details of how this server works are beyond the scope of this article, it is important to know that the lines that figure out when the MIME type is image/xy and return this MIME type are:
//find out when the file requested is an .xy one
if(ext.indexOf("xy")!=-1)contenttype="image/xy";
//write the MIME type into the header for the client
out.writeBytes("Content-type: "+contenttype+"\r\n");
Listing 4 shows you a minimal HTTP server that runs on the localhost:80 and returns an image/xy MIME type when it gets a request for an .xy file.
The final step is to test the content handler. To do so, create a simple application that uses an URLConnection to request the image.xy file to the HTTP server created above (this means that the test application is an HTTP client). The server will look for the requested resources in the C:\Jeditor\handlers directory. The server then checks the file extension and returns the proper MIME type into the header of the response. It also returns the file content as chunks of bytes information.
Listing 5 shows a test application example.
Notice that if you didn't store the xy.class in the sun.net.www.content.image package, you have to decomment the XYFactory class and the two lines in the main method (to manually install the content handler). That's because, in this case, Java will not find the proper content handler in its automatic search.
To test the new content handler on a single machine, first you have to be sure that you have the image.xy file and the HTTPServer, TestXYHandler classes in the same directory (C:\Jeditor\handlers). Also, make sure you've stored the the xy.class class in the sun.net.www.content.image package. After that, all you have to do is to run the HTTP server and the TestXYHandler application. The URL for testing this example is http://localhost/image.xy and it was fixed in the TestXYHandler application.
To see the difference between an Internet client that doesn't understand the .xy files and your TestXYHandler client, run them both, and view the results, which may look an awful lot like Figure 3.

As you can see in Figure 4, the application is acts as a viewer, but the IE client displays the File Download dialog. If you click the Open button, IE will open the file in the text mode with help from an external text viewer like Notepad or Wordpadwhich isn't what you want. If you click on Save button, IE will download the file just like an .exe application.
Remember that, even if the requested resource is a binary or a text file, the major difference in using a Java content handler consists in the cast conversion made over the object returned by the getContent method.
The second part of this article will demonstrate how to associate the created content handler created in this part with a defined protocol handler. You will also learn how Java uses the default protocol handlers that cames with the Java kit.
| Home / Articles
/ Creating Content and Protocol Handlers in Java / 1 / 2 / |
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|