1 8 package org.apache.avalon.excalibur.source; 9 10 import org.apache.avalon.framework.component.Composable; 11 import org.apache.avalon.framework.component.ComponentManager; 12 import org.apache.avalon.excalibur.monitor.FileResource; 13 import org.apache.avalon.excalibur.monitor.Monitorable; 14 import org.apache.avalon.excalibur.monitor.Resource; 15 import org.apache.avalon.excalibur.monitor.SourceResource; 16 import org.apache.avalon.excalibur.source.validity.TimeStampValidity; 17 import org.apache.avalon.excalibur.xml.Parser; 18 import org.apache.avalon.excalibur.xml.XMLConsumer; 19 import org.apache.avalon.excalibur.xml.XMLizable; 20 import org.xml.sax.ContentHandler ; 21 import org.xml.sax.InputSource ; 22 import org.xml.sax.SAXException ; 23 import org.xml.sax.ext.LexicalHandler ; 24 25 import java.io.*; 26 import java.lang.reflect.Method ; 27 import java.net.URL ; 28 import java.net.URLConnection ; 29 30 36 37 public final class URLSource 38 implements Composable, ModifiableSource, XMLizable, Monitorable { 39 40 41 private final String FILE = "file:"; 42 43 44 private long lastModificationDate; 45 46 47 private long contentLength; 48 49 50 private String systemId; 51 52 53 private URL url; 54 55 56 private URLConnection connection; 57 58 59 private boolean isFile; 60 61 62 private boolean gotInfos; 63 64 65 private ComponentManager manager; 66 67 71 public URLSource(URL url, 72 SourceParameters parameters) 73 throws IOException { 74 this.systemId = url.toExternalForm(); 75 this.isFile = systemId.startsWith(FILE); 76 this.url = url; 77 this.gotInfos = false; 78 } 79 80 public void compose(ComponentManager manager ) 81 { 82 this.manager = manager; 83 } 84 85 89 private void getInfos() { 90 if (this.gotInfos == false) { 91 if (this.isFile == true) { 92 File file = new File(this.systemId.substring(FILE.length())); 93 this.lastModificationDate = file.lastModified(); 94 this.contentLength = file.length(); 95 } else { 96 try { 97 if (this.connection == null) { 98 this.connection = this.url.openConnection(); 99 String userInfo = this.getUserInfo(); 100 if (this.url.getProtocol().startsWith("http") == true && userInfo != null) { 101 this.connection.setRequestProperty("Authorization","Basic "+SourceUtil.encodeBASE64(userInfo)); 102 } 103 } 104 this.lastModificationDate = this.connection.getLastModified(); 105 this.contentLength = this.connection.getContentLength(); 106 } catch (IOException ignore) { 107 this.lastModificationDate = 0; 108 this.contentLength = -1; 109 } 110 } 111 this.gotInfos = true; 112 } 113 } 114 115 119 public long getLastModified() { 120 this.getInfos(); 121 return this.lastModificationDate; 122 } 123 124 127 public Resource getResource() 128 throws Exception { 129 this.getInfos(); 130 if (this.isFile == true) { 131 return new FileResource(this.systemId.substring(FILE.length())); 132 } else { 133 return new SourceResource(this); 134 } 135 } 136 137 141 public long getContentLength() { 142 this.getInfos(); 143 return this.contentLength; 144 } 145 146 153 public InputStream getInputStream() 154 throws IOException { 155 this.getInfos(); 156 InputStream input = null; 157 if (this.isFile == true) { 158 input = new FileInputStream(this.systemId.substring(FILE.length())); 159 } else { 160 if (this.connection == null) { 161 this.connection = this.url.openConnection(); 162 163 String userInfo = this.getUserInfo(); 164 if (this.url.getProtocol().startsWith("http") == true && userInfo != null) { 165 this.connection.setRequestProperty("Authorization","Basic "+SourceUtil.encodeBASE64(userInfo)); 166 } 167 } 168 169 input = this.connection.getInputStream(); 170 this.connection = null; } 172 return input; 173 } 174 175 private static boolean checkedURLClass = false; 176 private static boolean urlSupportsGetUserInfo = false; 177 private static Method urlGetUserInfo = null; 178 private static Object [] emptyParams = new Object [0]; 179 180 184 private String getUserInfo() { 185 if (URLSource.checkedURLClass == true) { 186 if (URLSource.urlSupportsGetUserInfo == true) { 187 try { 188 return (String ) URLSource.urlGetUserInfo.invoke(this.url, URLSource.emptyParams); 189 } catch (Exception e){ 190 } 192 } 193 return null; 194 } else { 195 try { 197 URLSource.urlGetUserInfo = URL .class.getMethod("getUserInfo", null); 198 String ui = (String )URLSource.urlGetUserInfo.invoke(this.url, URLSource.emptyParams); 199 URLSource.checkedURLClass = true; 200 URLSource.urlSupportsGetUserInfo = true; 201 return ui; 202 } catch (Exception e){ 203 } 204 URLSource.checkedURLClass = true; 205 URLSource.urlSupportsGetUserInfo = false; 206 URLSource.urlGetUserInfo = null; 207 return null; 208 } 209 } 210 211 214 public String getSystemId() { 215 return this.systemId; 216 } 217 218 224 public SourceValidity getValidity() { 225 final long lm = this.getLastModified(); 226 if (lm == -1) { 227 return null; 228 } else { 229 return new TimeStampValidity(lm); 230 } 231 } 232 233 237 public void discardValidity() { 238 this.connection = null; 240 this.gotInfos = false; 241 } 242 243 248 protected InputSource getInputSource() 249 throws IOException 250 { 251 InputSource newObject = new InputSource (this.getInputStream()); 252 newObject.setSystemId(this.systemId); 253 return newObject; 254 } 255 256 263 public void toSAX(ContentHandler handler) 264 throws SAXException 265 { 266 Parser parser = null; 267 try { 268 parser = (Parser)this.manager.lookup(Parser.ROLE); 269 270 parser.parse(this.getInputSource(), handler); 271 } catch (SAXException e) { 272 throw e; 274 } catch (Exception e){ 275 throw new SAXException ("Exception during processing of " 276 + this.systemId, e); 277 } finally { 278 if (parser != null) this.manager.release(parser); 279 } 280 } 281 282 } 283 | Popular Tags |