Content of FetchURL.java extracted from checkweb.jar
// // fetchURL: class used to check a URL via http connection // // used by checkweb.java // requires HttpConnection.java // // Only textfiles are fetched, other content-types are ignored // // version 0.1 // version 0.2 updated in order to use the class URL instead of class URI, updated naming of files in cache // version 0.3 changed the name files are named, see tag TAG-0001 // version 0.4: adapted to package concept package checkweb; import javax.swing.JTable; import java.io.*; import java.net.*; public class FetchURL extends Thread { String urlName; JTable table; int row; OutputStream f, content = null; // constructor public FetchURL(String urlName, int row, JTable table) { this.urlName=urlName; this.table = table; this.row = row; start(); } synchronized void println(String s) { if (table == null) System.out.println(urlName+"--->"+s); else table.setValueAt(s,row,1); } public void run() { try{ HttpConnection server = new HttpConnection(urlName); int responseCode = server.getResponseCode(); if (responseCode != 200) { println("HTTP response code: " + String.valueOf(responseCode)+" "+server.getResponseMessage()); server.close(); return; } if ((server.getContentType()!=null)&&(!server.getContentType().startsWith("text"))) { println ("Content type is different from <text>"); server.close(); return; } println("server contacted"); long webtime = server.getLastModified(); if (webtime == 0) { webtime = System.currentTimeMillis(); } // Date time = new Date(webtime); System.out.println(time.toString()); URL url = new URL(urlName); String fileName = url.getPath(); if (fileName.length() == 0) fileName = "/"; // small correction, in case filename is empty if (fileName.endsWith("/")) { fileName += "index.html"; } File dir = new File(Main.cache+"/"+url.getHost()); if (!dir.exists()) dir.mkdir(); // TAG-0001 // fileName = dir.toString()+fileName.charAt(0)+fileName.substring(1).replace('/','.')+((url.getQuery()!=null ? "^"+url.getQuery().hashCode() : "")); fileName = dir.toString()+fileName.charAt(0)+fileName.substring(1).replace('/','.')+((url.getQuery()!=null ? "^"+url.getQuery().replace('*','.').replace(':','.')+".html" : "")); File target = new File(fileName); LineNumberReader in = null; boolean equal = false; if (target.exists()) { if (webtime == target.lastModified()) { println("No changes"); return; } in = new LineNumberReader(new FileReader(target)); equal = true; // later on a check is performed! }; File tempFile = File.createTempFile("check",null,dir); PrintWriter outStream = new PrintWriter(new FileWriter(tempFile)); String line; while ((line = server.readLine())!= null) { outStream.println(line); if (equal) { String line2 = in.readLine(); if (line2 == null) equal = false; else if (line.compareTo(line2) != 0) equal = false; } }; server.close(); outStream.close(); if (in != null) { in.close(); } if (equal) { tempFile.delete(); println("No changes"); } else { target.delete(); tempFile.renameTo(target); target.setLastModified(webtime); println("Updated!");} }catch (MalformedURLException ex){ println("Bad URL"); }catch (UnknownHostException ex){ println("Unknown host"); }catch (ConnectException ex){ println("Unable to connect"); }catch (IOException ex){ println(ex.toString()); } } }