20-CS-694 Advanced Programming Techniques Spring 2012
Networking - Protocol Handler

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

Prev     Next     ProtoTest.java     Rotate.java     Handler.java
Prev     Next     CryptURLConnection.java     CryptInputStream.java      All lectures

Protocol Handler

This application makes use of the crypt:rotate protocol handler given in the following slides. Java natively has some handlers that support processing certain protocols such as http and ftp. This example adds a protocol handler for a new protocol that simply rotates the ascii value of received bytes. Compile ProtoTest.java and run it like this after the compiled classes of the following slides have been placed in the proper directory:

    java ProtoTest
then select one of two items from the JComboBox and click the "Run It!" button. One of the items requests an http transaction, the other uses the new crypt:rotate protocol.

The protocol handler comprises several class files that must be developed. These are shown in the following slides. The files are placed in directory protocolhandlers/crypt where protocolhanders is in the classpath. The name crypt is mandatory and represents the protocol type crypt. The subprotocol rotate is due to new CryptURLConnection that is returned in the Handler class.

import java.lang.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import protocolhandlers.crypt.*;

class PTF extends Thread {
   String query;
   boolean running = false;
   ProtoTestFrame ptf;

   public PTF (ProtoTestFrame ptf, String q) { this.ptf = ptf; query = q; }

   public void run () {
      running = true;
      try {
         // query = http://gauss.ececs.uc.edu/index.html
         // query = crypt:rotate://gauss.ececs.uc.edu/index.html
         URL url = new URL(query);    // Handler.java -> URLStreamHandler
         ptf.text.append("Start application:\n");
         InputStream is = (InputStream)url.getContent(); // Handler.java
         ptf.text.append("\nApplication: got content...outputting\n");
         try {
            int ch;
            while ((ch = is.read()) >= 0 && running)
               ptf.text.append(String.valueOf((char)ch));
         } catch (Exception e) { 
	    ptf.text.append("done\n");
	 }
      } catch (Exception e) {
         ptf.text.append("Exception: "+e.toString()+"\n");
      }
   }
}

class ProtoTestFrame extends JFrame implements ActionListener {
   JTextArea text;
   JTextField msg;
   JButton doit;
   String query;
   JComboBox cb;
   PTF ptf = null;

   public ProtoTestFrame () {
      System.setProperty("java.protocol.handler.pkgs","protocolhandlers");
      setLayout(new BorderLayout());
      add("Center", new JScrollPane(text = new JTextArea()));
      JPanel q = new JPanel();
      q.setLayout(new GridLayout(2,1));
      JPanel p = new JPanel();
      p.setLayout(new FlowLayout());
      p.add(new JLabel("java.protocol.handler.pkgs:", JLabel.RIGHT));
      p.add(msg = new JTextField(10));
      p.add(doit = new JButton("Run It!"));
      q.add(p);
      q.add(cb = new JComboBox());
      cb.addItem("http://gauss.ececs.uc.edu/index.html");
      cb.addItem("crypt:rotate://gauss.ececs.uc.edu/index.html");
      add("South", q);
      doit.addActionListener(this);
      msg.setText(System.getProperty("java.protocol.handler.pkgs"));
   }

   public void actionPerformed (ActionEvent evt) { 
      if (ptf != null) ptf.running = false;
      ptf = new PTF(this, (String)cb.getSelectedItem());
      ptf.start();
   }
}

public class ProtoTest {
   public static void main (String args[]) {
      ProtoTestFrame pf = new ProtoTestFrame();
      pf.setSize(500,600);
      pf.setVisible(true);
   }
}