1 18 package org.apache.batik.util; 19 20 import java.io.BufferedInputStream ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.net.HttpURLConnection ; 24 import java.net.MalformedURLException ; 25 import java.net.URL ; 26 import java.net.URLConnection ; 27 import java.util.Iterator ; 28 import java.util.LinkedList ; 29 import java.util.List ; 30 import java.util.zip.GZIPInputStream ; 31 import java.util.zip.InflaterInputStream ; 32 import java.util.zip.ZipException ; 33 34 40 public class ParsedURLData { 41 42 String HTTP_USER_AGENT_HEADER = "User-Agent"; 43 44 String HTTP_ACCEPT_HEADER = "Accept"; 45 String HTTP_ACCEPT_LANGUAGE_HEADER = "Accept-Language"; 46 String HTTP_ACCEPT_ENCODING_HEADER = "Accept-Encoding"; 47 48 protected static List acceptedEncodings = new LinkedList (); 49 static { 50 acceptedEncodings.add("gzip"); 51 } 52 53 57 public final static byte GZIP_MAGIC[] = {(byte)0x1f, (byte)0x8b}; 58 59 66 public static InputStream checkGZIP(InputStream is) 67 throws IOException { 68 69 if (!is.markSupported()) 70 is = new BufferedInputStream (is); 71 byte data[] = new byte[2]; 72 try { 73 is.mark(2); 74 is.read(data); 75 is.reset(); 76 } catch (Exception ex) { 77 is.reset(); 78 return is; 79 } 80 if ((data[0] == GZIP_MAGIC[0]) && 81 (data[1] == GZIP_MAGIC[1])) 82 return new GZIPInputStream (is); 83 84 if (((data[0]&0x0F) == 8) && 85 ((data[0]>>>4) <= 7)) { 86 int chk = ((((int)data[0])&0xFF)*256+ 88 (((int)data[1])&0xFF)); 89 if ((chk %31) == 0) { 90 try { 91 is.mark(100); 95 InputStream ret = new InflaterInputStream (is); 96 if (!ret.markSupported()) 97 ret = new BufferedInputStream (ret); 98 ret.mark(2); 99 ret.read(data); 100 is.reset(); 101 ret = new InflaterInputStream (is); 102 return ret; 103 } catch (ZipException ze) { 104 is.reset(); 105 return is; 106 } 107 } 108 } 109 110 return is; 111 } 112 113 119 public String protocol = null; 120 public String host = null; 121 public int port = -1; 122 public String path = null; 123 public String ref = null; 124 public String contentType = null; 125 public String contentEncoding = null; 126 127 public InputStream stream = null; 128 public boolean hasBeenOpened = false; 129 130 133 public ParsedURLData() { 134 } 135 136 139 public ParsedURLData(URL url) { 140 protocol = url.getProtocol(); 141 if ((protocol != null) && (protocol.length() == 0)) 142 protocol = null; 143 144 host = url.getHost(); 145 if ((host != null) && (host.length() == 0)) 146 host = null; 147 148 port = url.getPort(); 149 150 path = url.getFile(); 151 if ((path != null) && (path.length() == 0)) 152 path = null; 153 154 ref = url.getRef(); 155 if ((ref != null) && (ref.length() == 0)) 156 ref = null; 157 } 158 159 163 protected URL buildURL() throws MalformedURLException { 164 165 171 if ((protocol != null) && (host != null)) { 172 String file = ""; 173 if (path != null) 174 file = path; 175 if (port == -1) 176 return new URL (protocol, host, file); 177 178 return new URL (protocol, host, port, file); 179 } 180 181 return new URL (toString()); 183 } 184 185 188 public int hashCode() { 189 int hc = port; 190 if (protocol != null) 191 hc ^= protocol.hashCode(); 192 if (host != null) 193 hc ^= host.hashCode(); 194 195 if (path != null) { 199 int len = path.length(); 200 if (len > 20) 201 hc ^= path.substring(len-20).hashCode(); 202 else 203 hc ^= path.hashCode(); 204 } 205 if (ref != null) { 206 int len = ref.length(); 207 if (len > 20) 208 hc ^= ref.substring(len-20).hashCode(); 209 else 210 hc ^= ref.hashCode(); 211 } 212 213 return hc; 214 } 215 216 219 public boolean equals(Object obj) { 220 if (obj == null) return false; 221 if (! (obj instanceof ParsedURLData)) 222 return false; 223 224 ParsedURLData ud = (ParsedURLData)obj; 225 if (ud.port != port) 226 return false; 227 228 if (ud.protocol==null) { 229 if (protocol != null) 230 return false; 231 } else if (protocol == null) 232 return false; 233 else if (!ud.protocol.equals(protocol)) 234 return false; 235 236 if (ud.host==null) { 237 if (host !=null) 238 return false; 239 } else if (host == null) 240 return false; 241 else if (!ud.host.equals(host)) 242 return false; 243 244 if (ud.ref==null) { 245 if (ref !=null) 246 return false; 247 } else if (ref == null) 248 return false; 249 else if (!ud.ref.equals(ref)) 250 return false; 251 252 if (ud.path==null) { 253 if (path !=null) 254 return false; 255 } else if (path == null) 256 return false; 257 else if (!ud.path.equals(path)) 258 return false; 259 260 return true; 261 } 262 263 267 public String getContentType(String userAgent) { 268 if (contentType != null) 269 return contentType; 270 271 if (!hasBeenOpened) { 272 try { 273 openStreamInternal(userAgent, null, null); 274 } catch (IOException ioe) { } 275 } 276 277 return contentType; 278 } 279 280 284 public String getContentEncoding(String userAgent) { 285 if (contentEncoding != null) 286 return contentEncoding; 287 288 if (!hasBeenOpened) { 289 try { 290 openStreamInternal(userAgent, null, null); 291 } catch (IOException ioe) { } 292 } 293 294 return contentEncoding; 295 } 296 297 302 public boolean complete() { 303 try { 304 buildURL(); 305 } catch (MalformedURLException mue) { 306 return false; 307 } 308 return true; 309 } 310 311 321 public InputStream openStream(String userAgent, Iterator mimeTypes) 322 throws IOException { 323 InputStream raw = openStreamInternal(userAgent, mimeTypes, 324 acceptedEncodings.iterator()); 325 if (raw == null) 326 return null; 327 stream = null; 328 329 return checkGZIP(raw); 330 } 331 332 341 public InputStream openStreamRaw(String userAgent, Iterator mimeTypes) 342 throws IOException { 343 344 InputStream ret = openStreamInternal(userAgent, mimeTypes, null); 345 stream = null; 346 return ret; 347 } 348 349 protected InputStream openStreamInternal(String userAgent, 350 Iterator mimeTypes, 351 Iterator encodingTypes) 352 throws IOException { 353 if (stream != null) 354 return stream; 355 356 hasBeenOpened = true; 357 358 URL url = null; 359 try { 360 url = buildURL(); 361 } catch (MalformedURLException mue) { 362 throw new IOException 363 ("Unable to make sense of URL for connection"); 364 } 365 366 if (url == null) 367 return null; 368 369 URLConnection urlC = url.openConnection(); 370 if (urlC instanceof HttpURLConnection ) { 371 if (userAgent != null) 372 urlC.setRequestProperty(HTTP_USER_AGENT_HEADER, userAgent); 373 374 if (mimeTypes != null) { 375 String acceptHeader = ""; 376 while (mimeTypes.hasNext()) { 377 acceptHeader += mimeTypes.next(); 378 if (mimeTypes.hasNext()) 379 acceptHeader += ","; 380 } 381 urlC.setRequestProperty(HTTP_ACCEPT_HEADER, acceptHeader); 382 } 383 384 if (encodingTypes != null) { 385 String encodingHeader = ""; 386 while (encodingTypes.hasNext()) { 387 encodingHeader += encodingTypes.next(); 388 if (encodingTypes.hasNext()) 389 encodingHeader += ","; 390 } 391 urlC.setRequestProperty(HTTP_ACCEPT_ENCODING_HEADER, 392 encodingHeader); 393 } 394 395 contentType = urlC.getContentType(); 396 contentEncoding = urlC.getContentEncoding(); 397 } 398 399 return (stream = urlC.getInputStream()); 400 } 401 402 406 public String getPortStr() { 407 String portStr =""; 408 if (protocol != null) 409 portStr += protocol + ":"; 410 411 if ((host != null) || (port != -1)) { 412 portStr += "//"; 413 if (host != null) portStr += host; 414 if (port != -1) portStr += ":" + port; 415 } 416 417 return portStr; 418 } 419 420 protected boolean sameFile(ParsedURLData other) { 421 if (this == other) return true; 422 423 if ((port == other.port) && 426 ((path == other.path) 427 || ((path!=null) && path.equals(other.path))) && 428 ((host == other.host) 429 || ((host!=null) && host.equals(other.host))) && 430 ((protocol == other.protocol) 431 || ((protocol!=null) && protocol.equals(other.protocol)))) 432 return true; 433 434 return false; 435 } 436 437 438 441 public String toString() { 442 String ret = getPortStr(); 443 if (path != null) 444 ret += path; 445 446 if (ref != null) 447 ret += "#" + ref; 448 449 return ret; 450 } 451 } 452 453 | Popular Tags |