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

  1. package netscape.netcast;
  2.  
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.net.MalformedURLException;
  6. import java.net.URL;
  7. import java.net.URLConnection;
  8. import java.net.URLStreamHandler;
  9. import java.util.ResourceBundle;
  10. import java.util.StringTokenizer;
  11. import java.util.Vector;
  12. import marimba.castanet.client.CastanetChannel;
  13. import marimba.castanet.client.CastanetFile;
  14. import marimba.castanet.client.CastanetObject;
  15. import marimba.castanet.client.CastanetTransmitter;
  16. import marimba.castanet.client.CastanetWorkspace;
  17. import netscape.applet.CastanetChannelInfo;
  18. import netscape.security.PrivilegeManager;
  19. import netscape.security.Target;
  20.  
  21. public class NSCastanetTunerURLStreamHandler extends URLStreamHandler {
  22.    public NSCastanetTunerURLStreamHandler() {
  23.       NSCastanetWorkspace.getCastanetWorkspace();
  24.    }
  25.  
  26.    NSTunerStringConnection status(URL url, String txname, String chname, String msg) {
  27.       CastanetWorkspace ws = NSCastanetWorkspace.getCastanetWorkspace();
  28.       CastanetTransmitter transmitter = ws.getTransmitter(txname);
  29.       if (transmitter != null) {
  30.          CastanetChannel channel = transmitter.getChannel(chname);
  31.          if (channel != null) {
  32.             chname = ((CastanetObject)channel).getName();
  33.          }
  34.       }
  35.  
  36.       return new NSTunerStringConnection(url, "<title>" + chname + "</title><h1>" + msg + "</h1>");
  37.    }
  38.  
  39.    protected URLConnection openConnection(URL url) throws IOException {
  40.       CastanetWorkspace ws = NSCastanetWorkspace.getCastanetWorkspace();
  41.       PrivilegeManager privMgr = PrivilegeManager.getPrivilegeManager();
  42.       if (privMgr != null) {
  43.          Target target = Target.findTarget("MarimbaAppContextTarget");
  44.          if (target != null) {
  45.             privMgr.enablePrivilege(target);
  46.          }
  47.       }
  48.  
  49.       CastanetChannelInfo.setWorkspacePath(((CastanetObject)ws).getDirectory().getAbsolutePath());
  50.       int port = url.getPort();
  51.       if (port < 0) {
  52.          port = 80;
  53.       }
  54.  
  55.       String txname = url.getHost() + ":" + port;
  56.       String path = url.getFile();
  57.       if (path.startsWith("/")) {
  58.          path = path.substring(1);
  59.       }
  60.  
  61.       String cmd = null;
  62.       int qmark = path.lastIndexOf(63);
  63.       if (qmark >= 0) {
  64.          cmd = path.substring(qmark + 1);
  65.          path = path.substring(0, qmark);
  66.       }
  67.  
  68.       int slash = path.indexOf(47);
  69.       if (slash < 0) {
  70.          slash = path.length();
  71.       }
  72.  
  73.       String chname = path.substring(0, slash);
  74.       path = path.substring(slash);
  75.       if (path.length() == 0) {
  76.          path = "index.html";
  77.       } else if (path.endsWith("/")) {
  78.          path = path + "index.html";
  79.       }
  80.  
  81.       if (path.startsWith("/")) {
  82.          path = path.substring(1);
  83.       }
  84.  
  85.       if (cmd != null) {
  86.          if (cmd.equals("subscribe")) {
  87.             new TunerCommand(3, txname, chname);
  88.             throw new NSStatusException(ResourceBundle.getBundle("netscape.netcast.NetcasterCastanetResources").getString("Subscribing Castanet channel: ") + chname);
  89.          }
  90.  
  91.          if (cmd.equals("start")) {
  92.             new TunerCommand(0, txname, chname);
  93.             throw new NSStatusException(ResourceBundle.getBundle("netscape.netcast.NetcasterCastanetResources").getString("Starting Castanet channel: ") + chname);
  94.          }
  95.       }
  96.  
  97.       CastanetTransmitter transmitter = ws.getTransmitter(txname);
  98.       if (transmitter == null) {
  99.          throw new FileNotFoundException(ResourceBundle.getBundle("netscape.netcast.NetcasterCastanetResources").getString("Transmitter not found: '") + txname + "'");
  100.       } else {
  101.          CastanetChannel channel = transmitter.getChannel(chname);
  102.          if (channel == null) {
  103.             throw new FileNotFoundException(ResourceBundle.getBundle("netscape.netcast.NetcasterCastanetResources").getString("Channel not found: '") + chname + "'");
  104.          } else {
  105.             while(path.startsWith("/")) {
  106.                path = path.substring(1);
  107.             }
  108.  
  109.             if (cmd == null) {
  110.                ((CastanetObject)channel).notify(703, path);
  111.                if (path.startsWith("../")) {
  112.                   path = path.substring(3);
  113.                   slash = path.indexOf(47);
  114.                   chname = slash < 0 ? path : path.substring(0, slash);
  115.                   path = path.substring(slash + 1);
  116.                   channel = transmitter.getChannel(chname);
  117.                   if (channel == null) {
  118.                      throw new FileNotFoundException(ResourceBundle.getBundle("netscape.netcast.NetcasterCastanetResources").getString("Channel not found: '") + chname + "'");
  119.                   }
  120.                }
  121.  
  122.                CastanetFile file = channel.getFile(path);
  123.                if (file == null) {
  124.                   path = path.replace('\\', '/');
  125.                   if ((file = channel.getFile(path)) == null) {
  126.                      throw new FileNotFoundException(ResourceBundle.getBundle("netscape.netcast.NetcasterCastanetResources").getString("File not found: '") + path + "'");
  127.                   }
  128.                }
  129.  
  130.                String mime = null;
  131.                slash = path.lastIndexOf(47);
  132.                int dot = path.lastIndexOf(46);
  133.                if (dot > slash) {
  134.                   String extention = path.substring(dot + 1);
  135.                   mime = channel.getChannelProperty("mime.type." + extention);
  136.                }
  137.  
  138.                return new NSTunerURLConnection(url, file, path, mime);
  139.             } else if (cmd.equals("stop")) {
  140.                new TunerCommand(2, txname, chname);
  141.                throw new NSStatusException(ResourceBundle.getBundle("netscape.netcast.NetcasterCastanetResources").getString("Stopping Castanet channel: ") + chname);
  142.             } else if (cmd.equals("update")) {
  143.                new TunerCommand(5, txname, chname);
  144.                throw new NSStatusException(ResourceBundle.getBundle("netscape.netcast.NetcasterCastanetResources").getString("Updating Castanet channel: ") + chname);
  145.             } else if (cmd.startsWith("log=")) {
  146.                cmd = cmd.substring(4);
  147.                int i = cmd.indexOf(44);
  148.                String logstr = i < 0 ? cmd : cmd.substring(0, i);
  149.                String urlstr = i < 0 ? cmd : cmd.substring(i + 1);
  150.  
  151.                try {
  152.                   if (urlstr.startsWith("castanet:")) {
  153.                      url = new URL(url.getProtocol() + ":" + urlstr.substring(9));
  154.                   } else if (urlstr.indexOf(58) < 0) {
  155.                      if (txname.endsWith(":80")) {
  156.                         txname = url.getHost();
  157.                      }
  158.  
  159.                      i = path.lastIndexOf(47);
  160.                      if (i >= 0) {
  161.                         urlstr = path.substring(0, i) + "/" + urlstr;
  162.                      }
  163.  
  164.                      url = new URL(url.getProtocol() + "://" + txname + "/" + chname + "/" + urlstr);
  165.                   } else {
  166.                      url = new URL(urlstr);
  167.                   }
  168.  
  169.                   int strlen = logstr.length();
  170.                   byte[] data = new byte[strlen];
  171.                   logstr.getBytes(0, strlen, data, 0);
  172.                   channel.appendLog(data);
  173.                   return url.openConnection();
  174.                } catch (MalformedURLException var18) {
  175.                   throw new FileNotFoundException(ResourceBundle.getBundle("netscape.netcast.NetcasterCastanetResources").getString("Malformed URL: ") + urlstr);
  176.                }
  177.             } else if (!cmd.startsWith("start,")) {
  178.                throw new FileNotFoundException(ResourceBundle.getBundle("netscape.netcast.NetcasterCastanetResources").getString("No such command: '") + cmd + "'");
  179.             } else {
  180.                String argstr = cmd.substring(6);
  181.                Vector v = new Vector();
  182.                StringTokenizer t = new StringTokenizer(argstr, ",");
  183.  
  184.                while(t.hasMoreTokens()) {
  185.                   v.addElement(t.nextToken());
  186.                }
  187.  
  188.                String[] argv = new String[v.size()];
  189.                v.copyInto(argv);
  190.                new TunerCommand(0, txname, chname, argv);
  191.                throw new NSStatusException(ResourceBundle.getBundle("netscape.netcast.NetcasterCastanetResources").getString("Starting Castanet channel: ") + chname);
  192.             }
  193.          }
  194.       }
  195.    }
  196. }
  197.