1 6 7 package com.hp.hpl.jena.util; 8 9 import java.io.BufferedInputStream ; 10 import java.io.IOException ; 11 import java.io.InputStream ; 12 import java.net.* ; 13 14 import org.apache.commons.logging.*; 15 16 21 22 public class LocatorURL implements Locator 23 { 24 static Log log = LogFactory.getLog(LocatorURL.class) ; 25 static final String acceptHeader = "application/rdf+xml,application/xml;q=0.9,*/*;q=0.5" ; 26 27 public InputStream open(String filenameOrURI) 28 { 29 if ( ! hasScheme(filenameOrURI, "http:") 30 ) 32 { 33 if ( FileManager.logAllLookups && log.isTraceEnabled() ) 34 log.trace("Not found: "+filenameOrURI) ; 35 return null; 36 } 37 38 try 39 { 40 URL url = new URL(filenameOrURI); 41 URLConnection conn = (HttpURLConnection) url.openConnection(); 42 conn.setRequestProperty("Accept", acceptHeader) ; 43 conn.setRequestProperty("Accept-Charset", "utf-8,*") ; 44 conn.setDoInput(true) ; 45 conn.setDoOutput(false) ; 46 conn.connect() ; 49 InputStream in = new BufferedInputStream (conn.getInputStream()); 50 51 if ( in == null ) 52 { 53 if ( FileManager.logAllLookups && log.isTraceEnabled() ) 54 log.trace("Not found: "+filenameOrURI) ; 55 return null ; 56 } 57 if ( FileManager.logAllLookups && log.isTraceEnabled() ) 58 log.trace("Found: "+filenameOrURI) ; 59 return in; 60 } 61 catch (java.io.FileNotFoundException ex) 62 { 63 if ( FileManager.logAllLookups && log.isTraceEnabled() ) 64 log.trace("LocatorURL: not found: "+filenameOrURI) ; 65 return null ; 66 } 67 catch (MalformedURLException ex) 68 { 69 log.warn("Malformed URL: " + filenameOrURI); 70 return null; 71 } 72 catch (IOException ex) 73 { 74 if ( ex instanceof ConnectException ) 75 { 76 if ( FileManager.logAllLookups && log.isTraceEnabled() ) 77 log.trace("LocatorURL: not found: "+filenameOrURI) ; 78 } 79 else 80 log.warn("IO Exception opening URL: " + filenameOrURI+" "+ex.getMessage()); 81 return null; 82 } 83 } 84 public String getName() { return "LocatorURL" ; } 85 86 private boolean hasScheme(String uri, String scheme) 87 { 88 String actualScheme = getScheme(uri) ; 89 if ( actualScheme == null ) 90 return false ; 91 return actualScheme.equalsIgnoreCase(scheme) ; 92 } 93 94 private String getScheme(String uri) 96 { 97 int ch = uri.indexOf(':') ; 98 if ( ch < 0 ) 99 return null ; 100 101 return uri.substring(0,ch+1) ; 103 } 104 105 } 106 | Popular Tags |