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

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

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

Protocol Handler - CryptURLConnection.java

This class file is placed in protocolhandlers/crypt.
package protocolhandlers.crypt;
import java.net.*;
import java.io.*;

class CryptURLConnection extends URLConnection {
   static int defaultport = 80;
   CryptInputStream cis;
   
   public String getContentType() {
      System.out.println("CryptURLConnection: getContentType(): "+guessContentTypeFromName(url.getFile()));
      return guessContentTypeFromName (url.getFile());
   }
   
   CryptURLConnection (URL url, String crypType) throws IOException {
      super (url);
      try {
         String classname
            = "protocolhandlers.crypt."+crypType+"CryptInputStream";
         System.out.println("CyrptURLConnection: Contruct: class=["+classname+"]");
         cis = (CryptInputStream)Class.forName(classname).newInstance();
      } catch (Exception e) {
         throw new IOException("Crypt Class Not Found: "+e.toString());
      }
   }

   public void connect () throws IOException {
      int port = (url.getPort() == -1) ? defaultport : url.getPort();
      System.out.println("CryptURLConnection: connect(): port=["+port+"]");
      Socket s = new Socket (url.getHost(), port);
      OutputStream server = s.getOutputStream();
      new PrintWriter (new OutputStreamWriter(server,"8859_1"),true).println("GET "+url.getFile());
      cis.set(s.getInputStream(), server);
      System.out.println("  All set, connecting...");
      connected = true;
   }
   
   public InputStream getInputStream() throws IOException {
      if (!connected) connect();
      return cis;
   }
}