1 32 33 package websphinx; 34 35 import java.net.*; 36 import java.io.*; 37 import java.util.Vector ; 38 39 public class Access { 40 private File tempDir; 41 private Vector temps = new Vector (); 42 43 public Access () { 44 String tempDirName; 45 46 try { 47 tempDirName = System.getProperty ("websphinx.temp.directory"); 48 } catch (SecurityException e) { 49 tempDirName = null; 50 } 51 52 if (tempDirName == null) { 53 String 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 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 URLConnection conn = url.openConnection (); 89 90 DownloadParameters dp = link.getDownloadParameters (); 92 if (dp != null) { 93 conn.setAllowUserInteraction (dp.getInteractive ()); 94 conn.setUseCaches (dp.getUseCaches ()); 95 96 String userAgent = dp.getUserAgent (); 97 if (userAgent != null) 98 conn.setRequestProperty ("User-Agent", userAgent); 99 100 String types = dp.getAcceptedMIMETypes (); 101 if (types != null) 102 conn.setRequestProperty ("accept", types); 103 } 104 105 if (method == Link.POST) { 107 if (conn instanceof HttpURLConnection) 109 ((HttpURLConnection)conn).setRequestMethod ("POST"); 110 112 String 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 PrintStream out = new PrintStream (conn.getOutputStream ()); 124 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 return new FileOutputStream (file.toString(), append); 143 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 basename, String 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 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 |