advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Tutorials : Creating Content and Protocol Handlers in Java :

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 line—the 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 Wordpad—which 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 /

How to Add Java Applets to Your Site

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.

 Avaya DevConnect Center
 Service Component Architecture/Service Data Objects Solution Center
 Intel Go Parallel Portal
 Internet.com eBook Library
 IBM Software Construction Toolbox
 Microsoft RIA Development Center
 Destination .NET
XML error: not well-formed (invalid token) at line 53
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Is .NET on Linux Finally Ready?
Red Hat Takes on HPC Market, Microsoft
Python's New Release Bridges the Gap
No Flash Seen on iPhone Horizon
Apple Yields to Complaints Over iPhone NDA
Microsoft Shows Some Ankle With Visual Studio
Gentoo Linux Cancels Distribution
It's Official: Windows 7 at PDC, WinHEC
Oracle Keeps Building on Spoils From BEA
Intel, Oracle Head For 'The Cloud'

"Supply Chain" SOA with SKOS
Service Component Architecture in Real Life
C++Ox: The Dawning of a New Standard
Getting Started with Virtualization
Master Complex Builds with MSBuild
eCryptfs: Single-File Encryption in Linux
CCXML in Action: A CCXML Auto Attendant
Ballmer: Current Woes Won't Halt Tech, Microsoft
Microsoft Uses VMworld to Hype Its Hypervisor
Microsoft Charges Ahead in Virtualization

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES