home *** CD-ROM | disk | FTP | other *** search
/ Australian PC Authority 1999 May / may1999.iso / INTERNET / COMMUNIC / NETCAST.Z / marimb10.jar / netscape / netcast / NSTunerURLStreamHandler.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-02-25  |  4.7 KB  |  124 lines

  1. package netscape.netcast;
  2.  
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.net.URL;
  6. import java.net.URLConnection;
  7. import java.net.URLStreamHandler;
  8. import marimba.castanet.client.CastanetChannel;
  9. import marimba.castanet.client.CastanetFile;
  10. import marimba.castanet.client.CastanetObject;
  11. import marimba.castanet.client.CastanetTransmitter;
  12. import marimba.castanet.client.CastanetWorkspace;
  13. import netscape.applet.CastanetChannelInfo;
  14. import netscape.security.PrivilegeManager;
  15. import netscape.security.Target;
  16.  
  17. public class NSTunerURLStreamHandler extends URLStreamHandler {
  18.    public NSTunerURLStreamHandler() {
  19.       NSCastanetWorkspace.getCastanetWorkspace();
  20.    }
  21.  
  22.    NSTunerStringConnection status(URL url, String txname, String chname, String msg) {
  23.       CastanetWorkspace ws = NSCastanetWorkspace.getCastanetWorkspace();
  24.       CastanetTransmitter transmitter = ws.getTransmitter(txname);
  25.       if (transmitter != null) {
  26.          CastanetChannel channel = transmitter.getChannel(chname);
  27.          if (channel != null) {
  28.             chname = ((CastanetObject)channel).getName();
  29.          }
  30.       }
  31.  
  32.       return new NSTunerStringConnection(url, "<title>" + chname + "</title><h1>" + msg + "</h1>");
  33.    }
  34.  
  35.    protected URLConnection openConnection(URL url) throws IOException {
  36.       CastanetWorkspace ws = NSCastanetWorkspace.getCastanetWorkspace();
  37.       PrivilegeManager privMgr = PrivilegeManager.getPrivilegeManager();
  38.       if (privMgr != null) {
  39.          Target target = Target.findTarget("MarimbaAppContextTarget");
  40.          if (target != null) {
  41.             privMgr.enablePrivilege(target);
  42.          }
  43.       }
  44.  
  45.       CastanetChannelInfo.setWorkspacePath(((CastanetObject)ws).getDirectory().getAbsolutePath());
  46.       int port = url.getPort();
  47.       if (port < 0) {
  48.          port = 80;
  49.       }
  50.  
  51.       String txname = url.getHost() + ":" + port;
  52.       String path = url.getFile();
  53.       if (path.startsWith("/")) {
  54.          path = path.substring(1);
  55.       }
  56.  
  57.       String cmd = null;
  58.       int qmark = path.lastIndexOf(63);
  59.       if (qmark >= 0) {
  60.          cmd = path.substring(qmark + 1);
  61.          path = path.substring(0, qmark);
  62.       }
  63.  
  64.       int slash = path.indexOf(47);
  65.       if (slash < 0) {
  66.          slash = path.length();
  67.       }
  68.  
  69.       String chname = path.substring(0, slash);
  70.  
  71.       for(path = path.substring(slash); path.startsWith("/"); path = path.substring(1)) {
  72.       }
  73.  
  74.       if (cmd != null) {
  75.          if (cmd.equals("subscribe")) {
  76.             new TunerCommand(3, txname, chname);
  77.             return this.status(url, txname, chname, "subscribing: " + chname);
  78.          } else if (cmd.equals("start")) {
  79.             new TunerCommand(0, txname, chname);
  80.             return this.status(url, txname, chname, "starting: " + chname);
  81.          } else if (cmd.equals("stop")) {
  82.             new TunerCommand(2, txname, chname);
  83.             return this.status(url, txname, chname, "stopping channel: " + chname);
  84.          } else if (cmd.equals("update")) {
  85.             new TunerCommand(5, txname, chname);
  86.             return this.status(url, txname, chname, "updating channel: " + chname);
  87.          } else {
  88.             throw new FileNotFoundException("Invalid Command: '" + cmd + "'");
  89.          }
  90.       } else {
  91.          CastanetTransmitter transmitter = ws.getTransmitter(txname);
  92.          if (transmitter == null) {
  93.             throw new FileNotFoundException("Transmitter not found: '" + txname + "'");
  94.          } else {
  95.             CastanetChannel channel = transmitter.getChannel(chname);
  96.             if (channel == null) {
  97.                throw new FileNotFoundException("Channel not found: '" + chname + "'");
  98.             } else {
  99.                if (path.startsWith("../")) {
  100.                   path = path.substring(3);
  101.                   slash = path.indexOf(47);
  102.                   chname = slash < 0 ? path : path.substring(0, slash);
  103.                   path = path.substring(slash + 1);
  104.                   channel = transmitter.getChannel(chname);
  105.                   if (channel == null) {
  106.                      throw new FileNotFoundException("Channel not found: '" + chname + "'");
  107.                   }
  108.                }
  109.  
  110.                CastanetFile file = channel.getFile(path);
  111.                if (file == null) {
  112.                   path = path.replace('\\', '/');
  113.                   if ((file = channel.getFile(path)) == null) {
  114.                      throw new FileNotFoundException("File not found: '" + path + "'");
  115.                   }
  116.                }
  117.  
  118.                return new NSTunerURLConnection(url, file, path, (String)null);
  119.             }
  120.          }
  121.       }
  122.    }
  123. }
  124.