KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > websphinx > Access


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;
34
35 import java.net.*;
36 import java.io.*;
37 import java.util.Vector JavaDoc;
38
39 public class Access {
40     private File tempDir;
41     private Vector JavaDoc temps = new Vector JavaDoc ();
42
43     public Access () {
44         String JavaDoc tempDirName;
45         
46         try {
47             tempDirName = System.getProperty ("websphinx.temp.directory");
48         } catch (SecurityException JavaDoc e) {
49             tempDirName = null;
50         }
51
52         if (tempDirName == null) {
53             String JavaDoc os = System.getProperty("os.name");
54             tempDirName = (os.startsWith ("Windows"))
55                             ? "c:\\temp\\"
56                             : "/tmp/";
57         }
58         
59         if (!(tempDirName.endsWith ("/") ||
60               tempDirName.endsWith (File.separator)))
61             tempDirName += "/";
62         
63         tempDir = new File (tempDirName);
64     }
65
66     public URLConnection openConnection (URL url) throws IOException {
67         URLConnection conn = url.openConnection ();
68         conn.connect ();
69         return conn;
70     }
71
72     public URLConnection openConnection (Link link) throws IOException {
73         // get the URL
74
int method = link.getMethod();
75         URL url;
76         switch (method) {
77             case Link.GET:
78                 url = link.getPageURL();
79                 break;
80             case Link.POST:
81                 url = link.getServiceURL();
82                 break;
83             default:
84                 throw new IOException ("Unknown HTTP method " + link.getMethod());
85         }
86
87         // open a connection to the URL
88
URLConnection conn = url.openConnection ();
89
90         // set up request headers
91
DownloadParameters dp = link.getDownloadParameters ();
92         if (dp != null) {
93             conn.setAllowUserInteraction (dp.getInteractive ());
94             conn.setUseCaches (dp.getUseCaches ());
95
96             String JavaDoc userAgent = dp.getUserAgent ();
97             if (userAgent != null)
98                 conn.setRequestProperty ("User-Agent", userAgent);
99
100             String JavaDoc types = dp.getAcceptedMIMETypes ();
101             if (types != null)
102                 conn.setRequestProperty ("accept", types);
103         }
104
105         // submit the query if it's a POST (GET queries are encoded in the URL)
106
if (method == Link.POST) {
107 //#ifdef JDK1.1
108
if (conn instanceof HttpURLConnection)
109                 ((HttpURLConnection)conn).setRequestMethod ("POST");
110 //#endif JDK1.1
111

112             String JavaDoc query = link.getQuery ();
113             if (query.startsWith ("?"))
114                 query = query.substring (1);
115
116             conn.setDoOutput (true);
117             conn.setRequestProperty ("Content-type",
118                                      "application/x-www-form-urlencoded");
119             conn.setRequestProperty ("Content-length", String.valueOf(query.length()));
120
121             // commence request
122
//#ifdef JDK1.1
123
PrintStream out = new PrintStream (conn.getOutputStream ());
124 //#endif JDK1.1
125
/*#ifdef JDK1.0
126             PrintStream out = new PrintStream (conn.getOutputStream ());
127 #endif JDK1.0*/

128             out.print (query);
129             out.flush ();
130         }
131
132         conn.connect ();
133         return conn;
134     }
135
136   public InputStream readFile (File file) throws IOException {
137     return new FileInputStream (file);
138   }
139
140   public OutputStream writeFile (File file, boolean append) throws IOException {
141     //#ifdef JDK1.1
142
return new FileOutputStream (file.toString(), append);
143     //#endif JDK1.1
144
/*#ifdef JDK1.0
145     if (append)
146         throw new IOException ("Can't append to files under JDK 1.0");
147     else
148         return new FileOutputStream (file.toString());
149     #endif JDK1.0*/

150   }
151
152     public RandomAccessFile readWriteFile (File file) throws IOException {
153         return new RandomAccessFile (file, "rw");
154     }
155     
156     public void makeDir (File file) throws IOException {
157         file.mkdirs ();
158     }
159
160     public File getTemporaryDirectory () {
161         return tempDir;
162     }
163
164     public File makeTemporaryFile (String JavaDoc basename, String JavaDoc extension) {
165         File dir = getTemporaryDirectory ();
166         File f;
167         synchronized (temps) {
168             do
169                 f = new File (dir,
170                               basename
171                               + String.valueOf ((int)(Math.random() * 999999))
172                               + extension);
173             while (temps.contains (f) || f.exists());
174
175             temps.addElement (f);
176         }
177         return f;
178     }
179
180     public void deleteAllTempFiles () {
181         synchronized (temps) {
182             for (int i=0; i<temps.size(); ++i) {
183                 File f = (File)temps.elementAt(i);
184                 f.delete ();
185             }
186             temps.setSize (0);
187         }
188     }
189
190   /*
191    * Global access object
192    *
193    */

194
195     private static Access theAccess = new Access ();
196
197     public static Access getAccess () {
198         return theAccess;
199     }
200
201     public static void setAccess (Access access) {
202         theAccess = access;
203     }
204
205 }
206
Popular Tags