001: /*
002: * WebSphinx web-crawling toolkit
003: *
004: * Copyright (c) 1998-2002 Carnegie Mellon University. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
020: * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
021: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
022: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
023: * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
024: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
025: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
026: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
027: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
028: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
029: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030: *
031: */
032:
033: package websphinx.workbench;
034:
035: import websphinx.*;
036: import java.net.URL;
037: import java.net.URLConnection;
038: import java.io.File;
039: import java.io.OutputStream;
040: import java.io.InputStream;
041: import java.io.IOException;
042: import java.io.RandomAccessFile;
043: import netscape.security.PrivilegeManager;
044: import netscape.security.ForbiddenTargetException;
045:
046: public class Netscape4Access extends Access {
047:
048: private boolean isLocalURL(URL url) {
049: return (url.getProtocol().equals("file") && url.getHost()
050: .equals(""));
051: }
052:
053: public URLConnection openConnection(URL url) throws IOException {
054: try {
055: PrivilegeManager
056: .enablePrivilege("UniversalConnectWithRedirect");
057: if (isLocalURL(url))
058: PrivilegeManager.enablePrivilege("UniversalFileRead");
059: } catch (ForbiddenTargetException e) {
060: throw new IOException("connection forbidden");
061: }
062:
063: return super .openConnection(url);
064: }
065:
066: public URLConnection openConnection(Link link) throws IOException {
067: try {
068: PrivilegeManager
069: .enablePrivilege("UniversalConnectWithRedirect");
070: } catch (ForbiddenTargetException e) {
071: throw new IOException("connection forbidden");
072: }
073:
074: if (isLocalURL(link.getURL()))
075: PrivilegeManager.enablePrivilege("UniversalFileRead");
076: return super .openConnection(link);
077: }
078:
079: public InputStream readFile(File file) throws IOException {
080: try {
081: PrivilegeManager.enablePrivilege("UniversalFileRead");
082: } catch (ForbiddenTargetException e) {
083: throw new IOException("file read forbidden");
084: }
085:
086: return super .readFile(file);
087: }
088:
089: public OutputStream writeFile(File file, boolean append)
090: throws IOException {
091: try {
092: PrivilegeManager.enablePrivilege("UniversalFileWrite");
093: } catch (ForbiddenTargetException e) {
094: throw new IOException("file write forbidden");
095: }
096:
097: return super .writeFile(file, append);
098: }
099:
100: public RandomAccessFile readWriteFile(File file) throws IOException {
101: try {
102: PrivilegeManager.enablePrivilege("UniversalFileWrite");
103: PrivilegeManager.enablePrivilege("UniversalFileRead");
104: } catch (ForbiddenTargetException e) {
105: throw new IOException("file read/write forbidden");
106: }
107:
108: return super .readWriteFile(file);
109: }
110:
111: public void makeDir(File file) throws IOException {
112: try {
113: PrivilegeManager.enablePrivilege("UniversalFileWrite");
114: PrivilegeManager.enablePrivilege("UniversalFileRead");
115: // mkdirs needs UniversalFileRead to check whether a
116: // directory already exists (I guess)
117: } catch (ForbiddenTargetException e) {
118: throw new IOException("make-directory forbidden");
119: }
120:
121: super .makeDir(file);
122: }
123:
124: public File makeTemporaryFile(String basename, String extension) {
125: try {
126: PrivilegeManager.enablePrivilege("UniversalFileRead");
127: // need UniversalFileRead to check whether a filename
128: // already exists
129: // FIX: should I bother with that check?
130: } catch (ForbiddenTargetException e) {
131: throw new SecurityException("temp file check forbidden");
132: }
133:
134: return super.makeTemporaryFile(basename, extension);
135: }
136: }
|