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.

 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 43
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%.

Remember Figlets? They're Back With Zend
Microsoft Readies an App Store Competitor?
Google: Chrome Browser Will Make Money
Sam Ramji: Microsoft's Man in Open Source
Google to Shake Up Browsers With Own Launch
Mozilla's Ubquity Mashup: For The Masses?
iPhone Users Just Want to Have Fun
Oops! I Fixed the Linux Kernel
Jim Zemlin: The New Center of Linux Gravity
Microsoft's Novell Investment Tops $340M

Writing Functional Code with RDFa
BitLocker Brings Encryption to Windows Server 2008
Network Know-How: Exploring Network Algorithms
Create a Durable and Reliable WCF Service with MSMQ 4.0
The Baker's Dozen: 13 Tips for SQL Server 2008 and SSRS 2008
Book Excerpt: Microsoft Expression Blend Unleashed
Develop a Mobile RSS Feed the Easy Way
State of the Semantic Web: Know Where to Look
A 3D Exploration of the HTML Canvas Element
Setting Up and Running Subversion and Tortoise SVN with Visual Studio and .NET

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
Intel PDF: Virtualization Delivers Data Center Efficiency
Intel eBook: Managing the Evolving Data Center
Microsoft Article: BitLocker Brings Encryption to Windows Server 2008
Symantec eBook: The Guide to E-Mail Archiving and Management
Microsoft Article: RODCs Transform Branch Office Security
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
Avaya Article: Advancing the State of the Art in Customer Service
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Avaya Article: Avaya AE Services Provide Rapid Telephony Integration with Facebook
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Seminar: Efficiencies in Hardware/Software Virtualization
HP Webcast: Disaster Recovery Planning
Go Parallel Video: Performance and Threading Tools for Game Developers
HP Video: StorageWorks EVA4400 and Oracle
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
IBM TCO eKIT: Your IT Budget is Under Attack, Get in Control
IBM Energy Efficiency eKIT: Learn How to Reduce Costs
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt and free High-Performance SQL Code eBook
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
Microsoft Article: Silverlight Streaming--Free Video Hosting for All
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
HP Demo: StorageWorks EVA4400
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES