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;
034:
035: import java.net.*;
036: import java.io.*;
037: import java.util.Vector;
038:
039: public class Access {
040: private File tempDir;
041: private Vector temps = new Vector();
042:
043: public Access() {
044: String tempDirName;
045:
046: try {
047: tempDirName = System
048: .getProperty("websphinx.temp.directory");
049: } catch (SecurityException e) {
050: tempDirName = null;
051: }
052:
053: if (tempDirName == null) {
054: String os = System.getProperty("os.name");
055: tempDirName = (os.startsWith("Windows")) ? "c:\\temp\\"
056: : "/tmp/";
057: }
058:
059: if (!(tempDirName.endsWith("/") || tempDirName
060: .endsWith(File.separator)))
061: tempDirName += "/";
062:
063: tempDir = new File(tempDirName);
064: }
065:
066: public URLConnection openConnection(URL url) throws IOException {
067: URLConnection conn = url.openConnection();
068: conn.connect();
069: return conn;
070: }
071:
072: public URLConnection openConnection(Link link) throws IOException {
073: // get the URL
074: int method = link.getMethod();
075: URL url;
076: switch (method) {
077: case Link.GET:
078: url = link.getPageURL();
079: break;
080: case Link.POST:
081: url = link.getServiceURL();
082: break;
083: default:
084: throw new IOException("Unknown HTTP method "
085: + link.getMethod());
086: }
087:
088: // open a connection to the URL
089: URLConnection conn = url.openConnection();
090:
091: // set up request headers
092: DownloadParameters dp = link.getDownloadParameters();
093: if (dp != null) {
094: conn.setAllowUserInteraction(dp.getInteractive());
095: conn.setUseCaches(dp.getUseCaches());
096:
097: String userAgent = dp.getUserAgent();
098: if (userAgent != null)
099: conn.setRequestProperty("User-Agent", userAgent);
100:
101: String types = dp.getAcceptedMIMETypes();
102: if (types != null)
103: conn.setRequestProperty("accept", types);
104: }
105:
106: // submit the query if it's a POST (GET queries are encoded in the URL)
107: if (method == Link.POST) {
108: //#ifdef JDK1.1
109: if (conn instanceof HttpURLConnection)
110: ((HttpURLConnection) conn).setRequestMethod("POST");
111: //#endif JDK1.1
112:
113: String query = link.getQuery();
114: if (query.startsWith("?"))
115: query = query.substring(1);
116:
117: conn.setDoOutput(true);
118: conn.setRequestProperty("Content-type",
119: "application/x-www-form-urlencoded");
120: conn.setRequestProperty("Content-length", String
121: .valueOf(query.length()));
122:
123: // commence request
124: //#ifdef JDK1.1
125: PrintStream out = new PrintStream(conn.getOutputStream());
126: //#endif JDK1.1
127: /*#ifdef JDK1.0
128: PrintStream out = new PrintStream (conn.getOutputStream ());
129: #endif JDK1.0*/
130: out.print(query);
131: out.flush();
132: }
133:
134: conn.connect();
135: return conn;
136: }
137:
138: public InputStream readFile(File file) throws IOException {
139: return new FileInputStream(file);
140: }
141:
142: public OutputStream writeFile(File file, boolean append)
143: throws IOException {
144: //#ifdef JDK1.1
145: return new FileOutputStream(file.toString(), append);
146: //#endif JDK1.1
147: /*#ifdef JDK1.0
148: if (append)
149: throw new IOException ("Can't append to files under JDK 1.0");
150: else
151: return new FileOutputStream (file.toString());
152: #endif JDK1.0*/
153: }
154:
155: public RandomAccessFile readWriteFile(File file) throws IOException {
156: return new RandomAccessFile(file, "rw");
157: }
158:
159: public void makeDir(File file) throws IOException {
160: file.mkdirs();
161: }
162:
163: public File getTemporaryDirectory() {
164: return tempDir;
165: }
166:
167: public File makeTemporaryFile(String basename, String extension) {
168: File dir = getTemporaryDirectory();
169: File f;
170: synchronized (temps) {
171: do
172: f = new File(
173: dir,
174: basename
175: + String
176: .valueOf((int) (Math.random() * 999999))
177: + extension);
178: while (temps.contains(f) || f.exists());
179:
180: temps.addElement(f);
181: }
182: return f;
183: }
184:
185: public void deleteAllTempFiles() {
186: synchronized (temps) {
187: for (int i = 0; i < temps.size(); ++i) {
188: File f = (File) temps.elementAt(i);
189: f.delete();
190: }
191: temps.setSize(0);
192: }
193: }
194:
195: /*
196: * Global access object
197: *
198: */
199:
200: private static Access theAccess = new Access();
201:
202: public static Access getAccess() {
203: return theAccess;
204: }
205:
206: public static void setAccess(Access access) {
207: theAccess = access;
208: }
209:
210: }
|