1 package net.matuschek.http; 2 3 import java.net.URL ; 4 import java.util.Date ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 import java.util.StringTokenizer ; 8 import java.util.Vector ; 9 import net.matuschek.util.MD5; 10 11 14 15 22 public class HttpDoc { 23 24 private byte[] content; 25 26 32 private Vector <HttpHeader> httpHeader; 33 34 35 private List links; 36 37 private int httpReturnCode=0; 38 private URL url; 39 40 41 private boolean cached = false; 42 43 private final static int HTTP_REDIRECTSTART=300; 44 private final static int HTTP_REDIRECTEND=399; 45 46 47 48 52 public HttpDoc() { 53 httpHeader = new Vector <HttpHeader>(); 54 } 55 56 62 public byte[] getContent() { 63 return content; 64 } 65 66 71 public void setContent(byte[] content) { 72 this.content = content; 73 removeHeader(HttpHeader.CONTENT_MD5); 75 } 76 77 public void setHttpCode(String httpCode) { 78 StringTokenizer st = new StringTokenizer (httpCode," "); 79 if (st.countTokens() < 2) { 81 return; 82 } 83 84 st.nextToken(); 85 String codeStr = st.nextToken(); 86 87 try { 88 httpReturnCode = Integer.parseInt(codeStr); 89 } catch (NumberFormatException e) { 90 } 92 } 93 94 public void setHttpCode(int code) { 95 httpReturnCode = code; 96 } 97 98 103 public int getHttpCode() { 104 return httpReturnCode; 105 } 106 107 113 public void addHeader(HttpHeader header) { 114 httpHeader.add(header); 115 } 116 117 122 public Vector getHttpHeader() { 123 return httpHeader; 124 } 125 126 132 public HttpHeader getHttpHeader(String headerName) { 133 for (Iterator iter = httpHeader.iterator(); iter.hasNext();) { 134 HttpHeader header = (HttpHeader) iter.next(); 135 if (header.getName().equals(headerName)) { 136 return header; 137 } 138 } 139 return null; 140 } 141 142 148 public String getHeaderValue(String headerName) { 149 HttpHeader header = getHeader(headerName); 150 return header != null ? header.getValue() : null; 151 } 152 153 160 public void setHeaderValue(String headerName, String headerValue) { 161 HttpHeader header = getHeader(headerName); 162 if (header == null) { 163 header = new HttpHeader(headerName, headerValue); 164 addHeader(header); 165 } else { 166 header.setValue(headerValue); 167 } 168 } 169 170 176 public String getLocation() { 177 HttpHeader location= getHeader(HttpHeader.LOCATION); 178 if (location == null) { 179 return ""; 180 } else { 181 return location.getValue(); 182 } 183 } 184 185 190 public boolean isRedirect() { 191 if ((httpReturnCode >= HTTP_REDIRECTSTART) && 192 (httpReturnCode <= HTTP_REDIRECTEND)) { 193 return true; 194 } else { 195 return false; 196 } 197 } 198 199 202 public boolean isOk() { 203 return (httpReturnCode == HttpConstants.HTTP_OK); 204 } 205 206 209 public boolean isNotModified() { 210 return (getHttpCode() == HttpConstants.HTTP_NOTMODIFIED); 211 } 212 213 216 public boolean isNotFound() { 217 return (httpReturnCode == HttpConstants.HTTP_NOTFOUND); 218 } 219 220 223 public boolean isUnauthorized() { 224 return (httpReturnCode == HttpConstants.HTTP_UNAUTHORIZED); 225 } 226 227 232 public HttpHeader getHeader(String name) { 233 for (int i=0; i<httpHeader.size(); i++) { 234 HttpHeader h = (HttpHeader)httpHeader.elementAt(i); 235 if (name.equalsIgnoreCase(h.getName())) { 236 return h; 237 } 238 } 239 return null; 240 } 241 242 247 public HttpHeader removeHeader(String name) { 248 HttpHeader header = getHeader(name); 249 if (header != null) { 250 httpHeader.remove(header); 251 } 252 return header; 253 } 254 255 262 public Vector getHttpHeaders() { 263 return httpHeader; 264 } 265 266 267 273 public boolean isHTML() { 274 HttpHeader ct = getHeader(HttpHeader.CONTENT_TYPE); 275 if (ct==null) { 276 return false; 277 } else { 278 if (ct.getValue().toLowerCase().startsWith("text/html")) { 279 return true; 280 } 281 } 282 return false; 283 } 284 285 290 public boolean isJavaScript(){ 291 HttpHeader ct = getHeader(HttpHeader.CONTENT_TYPE); 292 if (ct==null) { 293 return false; 294 } else { 295 if (ct.getValue().equalsIgnoreCase("text/x-javascript")) { 296 return true; 297 } 298 } 299 return false; 300 } 301 302 303 310 public String toString() { 311 StringBuffer res = new StringBuffer (); 312 313 res.append(url.toString()+"\n\n"); 314 315 for (int i=0; i<httpHeader.size(); i++) { 316 HttpHeader h = (HttpHeader)httpHeader.elementAt(i); 317 res.append(h.toString()); 318 res.append("\n"); 319 } 320 res.append("\n"); 321 if (content != null) { 322 res.append(new String (content)); 323 } 324 325 return res.toString(); 326 } 327 333 public URL getURL() { 334 return url; 335 } 336 337 338 343 public void setURL(URL url) { 344 this.url = url; 345 } 346 347 352 public long getLastModifiedAsMilliSeconds() { 353 String value = getHeaderValue(HttpHeader.LAST_MODIFIED); 354 return value != null ? HTTPDateTool.parseDate(value) : -1; 355 } 356 357 362 public long getDateAsMilliSeconds() { 363 String value = getHeaderValue(HttpHeader.DATE); 364 return value != null ? HTTPDateTool.parseDate(value) : -1; 365 } 366 367 372 public void setLastModified(long d) { 373 String dateString = HTTPDateTool.rfc1123Format.format(new Date (d)); 374 setHeaderValue(HttpHeader.LAST_MODIFIED, dateString); 375 } 376 377 382 public void setDate(long d) { 383 String dateString = HTTPDateTool.rfc1123Format.format(new Date (d)); 384 setHeaderValue(HttpHeader.DATE, dateString); 385 } 386 387 393 protected static String getContentMD5(byte[] content) { 394 if ((content == null) || (content.length == 0)) { 395 return "00000000000000000000000000000000"; 396 } 397 MD5 md5 = new MD5(); 398 md5.Update(content); 399 return md5.asHex(); 400 } 401 402 409 public String getContentMD5() { 410 HttpHeader md5Header = getHeader(HttpHeader.CONTENT_MD5); 411 String md5; 412 if (md5Header != null) { 413 md5 = md5Header.getValue(); 414 } else { 415 md5 = getContentMD5(getContent()); 416 md5Header = new HttpHeader(HttpHeader.CONTENT_MD5, md5); 417 addHeader(md5Header); 418 } 419 return md5; 420 } 421 422 423 427 public void setCached(boolean cached) { 428 this.cached = cached; 429 } 430 431 435 public boolean isCached() { 436 return cached; 437 } 438 439 443 public void setLinks(List links) { 444 this.links = links; 445 } 446 447 451 public List getLinks() { 452 return links; 453 } 454 455 } 456 | Popular Tags |