KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > websphinx > workbench > Netscape4Access


1 /*
2  * WebSphinx web-crawling toolkit
3  *
4  * Copyright (c) 1998-2002 Carnegie Mellon University. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
20  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
23  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  */

32
33 package websphinx.workbench;
34
35 import websphinx.*;
36 import java.net.URL JavaDoc;
37 import java.net.URLConnection JavaDoc;
38 import java.io.File JavaDoc;
39 import java.io.OutputStream JavaDoc;
40 import java.io.InputStream JavaDoc;
41 import java.io.IOException JavaDoc;
42 import java.io.RandomAccessFile JavaDoc;
43 import netscape.security.PrivilegeManager;
44 import netscape.security.ForbiddenTargetException;
45
46 public class Netscape4Access extends Access {
47
48     private boolean isLocalURL (URL JavaDoc url) {
49         return (url.getProtocol().equals ("file")
50                      && url.getHost().equals (""));
51     }
52
53     public URLConnection JavaDoc openConnection (URL JavaDoc url) throws IOException JavaDoc {
54         try {
55             PrivilegeManager.enablePrivilege ("UniversalConnectWithRedirect");
56             if (isLocalURL (url))
57                 PrivilegeManager.enablePrivilege ("UniversalFileRead");
58         } catch (ForbiddenTargetException e) {
59           throw new IOException JavaDoc ("connection forbidden");
60         }
61
62         return super.openConnection (url);
63     }
64
65     public URLConnection JavaDoc openConnection (Link link) throws IOException JavaDoc {
66         try {
67             PrivilegeManager.enablePrivilege ("UniversalConnectWithRedirect");
68         } catch (ForbiddenTargetException e) {
69           throw new IOException JavaDoc ("connection forbidden");
70         }
71
72         if (isLocalURL (link.getURL()))
73           PrivilegeManager.enablePrivilege ("UniversalFileRead");
74         return super.openConnection (link);
75     }
76
77   public InputStream JavaDoc readFile (File JavaDoc file) throws IOException JavaDoc {
78     try {
79       PrivilegeManager.enablePrivilege("UniversalFileRead");
80     } catch (ForbiddenTargetException e) {
81       throw new IOException JavaDoc ("file read forbidden");
82     }
83
84     return super.readFile (file);
85   }
86
87   public OutputStream JavaDoc writeFile (File JavaDoc file, boolean append) throws IOException JavaDoc {
88     try {
89       PrivilegeManager.enablePrivilege("UniversalFileWrite");
90     } catch (ForbiddenTargetException e) {
91       throw new IOException JavaDoc ("file write forbidden");
92     }
93
94     return super.writeFile (file, append);
95   }
96
97   public RandomAccessFile JavaDoc readWriteFile (File JavaDoc file) throws IOException JavaDoc {
98     try {
99       PrivilegeManager.enablePrivilege("UniversalFileWrite");
100       PrivilegeManager.enablePrivilege("UniversalFileRead");
101     } catch (ForbiddenTargetException e) {
102       throw new IOException JavaDoc ("file read/write forbidden");
103     }
104     
105     return super.readWriteFile (file);
106   }
107
108     public void makeDir (File JavaDoc file) throws IOException JavaDoc {
109         try {
110           PrivilegeManager.enablePrivilege("UniversalFileWrite");
111           PrivilegeManager.enablePrivilege("UniversalFileRead");
112             // mkdirs needs UniversalFileRead to check whether a
113
// directory already exists (I guess)
114
} catch (ForbiddenTargetException e) {
115           throw new IOException JavaDoc ("make-directory forbidden");
116         }
117         
118         super.makeDir (file);
119     }
120
121     
122   public File JavaDoc makeTemporaryFile (String JavaDoc basename, String JavaDoc extension) {
123     try {
124       PrivilegeManager.enablePrivilege("UniversalFileRead");
125             // need UniversalFileRead to check whether a filename
126
// already exists
127
// FIX: should I bother with that check?
128
} catch (ForbiddenTargetException e) {
129       throw new SecurityException JavaDoc ("temp file check forbidden");
130     }
131       
132     return super.makeTemporaryFile (basename, extension);
133   }
134 }
135
Popular Tags