home *** CD-ROM | disk | FTP | other *** search
- package netscape.netcast;
-
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.net.URL;
- import java.net.URLConnection;
- import java.net.URLStreamHandler;
- import marimba.castanet.client.CastanetChannel;
- import marimba.castanet.client.CastanetFile;
- import marimba.castanet.client.CastanetObject;
- import marimba.castanet.client.CastanetTransmitter;
- import marimba.castanet.client.CastanetWorkspace;
- import netscape.applet.CastanetChannelInfo;
- import netscape.security.PrivilegeManager;
- import netscape.security.Target;
-
- public class NSTunerURLStreamHandler extends URLStreamHandler {
- public NSTunerURLStreamHandler() {
- NSCastanetWorkspace.getCastanetWorkspace();
- }
-
- NSTunerStringConnection status(URL url, String txname, String chname, String msg) {
- CastanetWorkspace ws = NSCastanetWorkspace.getCastanetWorkspace();
- CastanetTransmitter transmitter = ws.getTransmitter(txname);
- if (transmitter != null) {
- CastanetChannel channel = transmitter.getChannel(chname);
- if (channel != null) {
- chname = ((CastanetObject)channel).getName();
- }
- }
-
- return new NSTunerStringConnection(url, "<title>" + chname + "</title><h1>" + msg + "</h1>");
- }
-
- protected URLConnection openConnection(URL url) throws IOException {
- CastanetWorkspace ws = NSCastanetWorkspace.getCastanetWorkspace();
- PrivilegeManager privMgr = PrivilegeManager.getPrivilegeManager();
- if (privMgr != null) {
- Target target = Target.findTarget("MarimbaAppContextTarget");
- if (target != null) {
- privMgr.enablePrivilege(target);
- }
- }
-
- CastanetChannelInfo.setWorkspacePath(((CastanetObject)ws).getDirectory().getAbsolutePath());
- int port = url.getPort();
- if (port < 0) {
- port = 80;
- }
-
- String txname = url.getHost() + ":" + port;
- String path = url.getFile();
- if (path.startsWith("/")) {
- path = path.substring(1);
- }
-
- String cmd = null;
- int qmark = path.lastIndexOf(63);
- if (qmark >= 0) {
- cmd = path.substring(qmark + 1);
- path = path.substring(0, qmark);
- }
-
- int slash = path.indexOf(47);
- if (slash < 0) {
- slash = path.length();
- }
-
- String chname = path.substring(0, slash);
-
- for(path = path.substring(slash); path.startsWith("/"); path = path.substring(1)) {
- }
-
- if (cmd != null) {
- if (cmd.equals("subscribe")) {
- new TunerCommand(3, txname, chname);
- return this.status(url, txname, chname, "subscribing: " + chname);
- } else if (cmd.equals("start")) {
- new TunerCommand(0, txname, chname);
- return this.status(url, txname, chname, "starting: " + chname);
- } else if (cmd.equals("stop")) {
- new TunerCommand(2, txname, chname);
- return this.status(url, txname, chname, "stopping channel: " + chname);
- } else if (cmd.equals("update")) {
- new TunerCommand(5, txname, chname);
- return this.status(url, txname, chname, "updating channel: " + chname);
- } else {
- throw new FileNotFoundException("Invalid Command: '" + cmd + "'");
- }
- } else {
- CastanetTransmitter transmitter = ws.getTransmitter(txname);
- if (transmitter == null) {
- throw new FileNotFoundException("Transmitter not found: '" + txname + "'");
- } else {
- CastanetChannel channel = transmitter.getChannel(chname);
- if (channel == null) {
- throw new FileNotFoundException("Channel not found: '" + chname + "'");
- } else {
- if (path.startsWith("../")) {
- path = path.substring(3);
- slash = path.indexOf(47);
- chname = slash < 0 ? path : path.substring(0, slash);
- path = path.substring(slash + 1);
- channel = transmitter.getChannel(chname);
- if (channel == null) {
- throw new FileNotFoundException("Channel not found: '" + chname + "'");
- }
- }
-
- CastanetFile file = channel.getFile(path);
- if (file == null) {
- path = path.replace('\\', '/');
- if ((file = channel.getFile(path)) == null) {
- throw new FileNotFoundException("File not found: '" + path + "'");
- }
- }
-
- return new NSTunerURLConnection(url, file, path, (String)null);
- }
- }
- }
- }
- }
-