1 16 package org.apache.cocoon.components.source; 17 18 import org.apache.avalon.framework.component.ComponentManager; 19 20 import org.apache.cocoon.ProcessingException; 21 import org.apache.cocoon.ResourceNotFoundException; 22 23 import org.apache.excalibur.source.SourceParameters; 24 import org.apache.excalibur.source.SourceUtil; 25 26 import java.io.File ; 27 import java.io.FileInputStream ; 28 import java.io.FileNotFoundException ; 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 import java.lang.reflect.Method ; 32 import java.net.HttpURLConnection ; 33 import java.net.JarURLConnection ; 34 import java.net.URL ; 35 import java.net.URLConnection ; 36 import java.util.Iterator ; 37 import java.util.jar.JarEntry ; 38 39 46 public class URLSource extends AbstractStreamSource { 47 48 49 private final String FILE = "file:"; 50 51 52 private long lastModificationDate; 53 54 55 private long contentLength; 56 57 58 private boolean isHTMLContent = false; 59 60 61 private String systemId; 62 63 64 private URL url; 65 66 67 private URLConnection connection; 68 69 70 private boolean isFile; 71 72 73 private boolean gotInfos; 74 75 76 private SourceParameters postParameters; 77 78 81 public URLSource(URL url, ComponentManager manager) 82 throws IOException { 83 super(manager); 84 this.systemId = url.toExternalForm(); 85 this.isFile = systemId.startsWith(FILE); 86 if (this.isFile == true) { 87 if (systemId.endsWith(".htm") || systemId.endsWith(".html")) { 88 this.isHTMLContent = true; 89 } 90 } 91 this.url = url; 92 this.gotInfos = false; 93 } 94 95 protected boolean isHTMLContent() { 96 return this.isHTMLContent; 97 } 98 99 103 private void getInfos() { 104 if (!this.gotInfos) { 105 if (this.isFile) { 106 File file = new File (systemId.substring(FILE.length())); 107 this.lastModificationDate = file.lastModified(); 108 this.contentLength = file.length(); 109 } else { 110 if (this.postParameters == null) { 111 try { 112 if (this.connection == null) { 113 this.connection = this.url.openConnection(); 114 String userInfo = this.getUserInfo(); 115 if (this.url.getProtocol().startsWith("http") && userInfo != null) { 116 this.connection.setRequestProperty("Authorization","Basic "+SourceUtil.encodeBASE64(userInfo)); 117 } 118 } 119 if(this.connection instanceof JarURLConnection ) { 120 JarEntry entry = ((JarURLConnection )this.connection).getJarEntry(); 121 this.lastModificationDate = entry.getTime(); 122 } else { 123 this.lastModificationDate = this.connection.getLastModified(); 124 } 125 this.contentLength = this.connection.getContentLength(); 126 } catch (IOException ignore) { 127 this.lastModificationDate = 0; 128 this.contentLength = -1; 129 } 130 } else { 131 this.lastModificationDate = 0; 133 this.contentLength = -1; 134 } 135 } 136 this.gotInfos = true; 137 } 138 } 139 140 144 public long getLastModified() { 145 this.getInfos(); 146 return this.lastModificationDate; 147 } 148 149 153 public long getContentLength() { 154 this.getInfos(); 155 return this.contentLength; 156 } 157 158 165 public InputStream getInputStream() 166 throws IOException , ProcessingException { 167 this.getInfos(); 168 try { 169 InputStream input = null; 170 if ( this.isFile ) { 171 input = new FileInputStream (this.systemId.substring(FILE.length())); 172 } else { 173 if (this.connection == null) { 174 this.connection = this.url.openConnection(); 175 176 String userInfo = this.getUserInfo(); 177 if (this.url.getProtocol().startsWith("http") && userInfo != null) { 178 this.connection.setRequestProperty("Authorization","Basic "+SourceUtil.encodeBASE64(userInfo)); 179 } 180 if (this.connection instanceof HttpURLConnection 182 && this.postParameters != null) { 183 StringBuffer buffer = new StringBuffer (2000); 184 String key; 185 Iterator i = postParameters.getParameterNames(); 186 Iterator values; 187 String value; 188 boolean first = true; 189 while ( i.hasNext() ) { 190 key = (String )i.next(); 191 values = this.postParameters.getParameterValues(key); 192 while (values.hasNext() == true) { 193 value = SourceUtil.encode((String )values.next()); 194 if (first == false) buffer.append('&'); 195 first = false; 196 buffer.append(key.toString()); 197 buffer.append('='); 198 buffer.append(value); 199 } 200 } 201 HttpURLConnection httpCon = (HttpURLConnection )connection; 202 httpCon.setDoInput(true); 203 204 if (buffer.length() > 1) { String postString = buffer.toString(); 206 httpCon.setRequestMethod("POST"); httpCon.setDoOutput(true); 208 httpCon.setRequestProperty("Content-type", "application/x-www-form-urlencoded"); 209 210 httpCon.setRequestProperty("Content-length", Integer.toString(postString.length())); 212 java.io.OutputStream out = new java.io.BufferedOutputStream (httpCon.getOutputStream()); 213 out.write(postString.getBytes()); 214 out.close(); 215 } 216 if ("text/html".equals(httpCon.getContentType()) == true) { 217 this.isHTMLContent = true; 218 } 219 input = httpCon.getInputStream(); 220 this.connection = null; return input; 222 } 223 } 224 if ("text/html".equals(this.connection.getContentType()) == true) { 225 this.isHTMLContent = true; 226 } 227 input = this.connection.getInputStream(); 228 this.connection = null; } 230 return input; 231 } catch (FileNotFoundException e) { 232 throw new ResourceNotFoundException("Resource not found " 233 + this.systemId, e); 234 } 235 } 236 237 private static boolean checkedURLClass = false; 238 private static boolean urlSupportsGetUserInfo = false; 239 private static Method urlGetUserInfo = null; 240 private static Object [] emptyParams = new Object [0]; 241 242 246 private String getUserInfo() { 247 if (URLSource.checkedURLClass) { 248 if (URLSource.urlSupportsGetUserInfo) { 249 try { 250 return (String ) URLSource.urlGetUserInfo.invoke(this.url, URLSource.emptyParams); 251 } catch (Exception e){ 252 } 254 } 255 return null; 256 } else { 257 try { 259 URLSource.urlGetUserInfo = URL .class.getMethod("getUserInfo", null); 260 String ui = (String )URLSource.urlGetUserInfo.invoke(this.url, URLSource.emptyParams); 261 URLSource.checkedURLClass = true; 262 URLSource.urlSupportsGetUserInfo = true; 263 return ui; 264 } catch (Exception e){ 265 } 266 URLSource.checkedURLClass = true; 267 URLSource.urlSupportsGetUserInfo = false; 268 URLSource.urlGetUserInfo = null; 269 return null; 270 } 271 } 272 273 276 public String getSystemId() { 277 return this.systemId; 278 } 279 280 284 public void refresh() { 285 this.connection = null; 287 this.gotInfos = false; 288 } 289 290 public void recycle() { 291 refresh(); 292 } 293 294 297 public void setPostParameters(SourceParameters pars) { 298 this.postParameters = pars; 299 } 300 301 } 302 | Popular Tags |