1 15 16 package com.ibm.webdav; 17 18 import java.io.IOException ; 19 20 32 import java.util.Properties ; 33 34 import sun.misc.BASE64Decoder; 35 import sun.misc.BASE64Encoder; 36 37 38 63 public class HTTPHeaders extends Properties { 64 public static HTTPHeaders defaults = new HTTPHeaders(); 65 66 static { 67 defaults.put("connection", "close"); 68 69 defaults.put("accept", "text/xml"); 70 71 } 74 75 static BASE64Encoder base64encoder = new BASE64Encoder(); 77 static BASE64Decoder base64decoder = new BASE64Decoder(); 78 79 81 public HTTPHeaders() { 82 super(); 83 } 84 85 88 public HTTPHeaders(HTTPHeaders defaults) { 89 super(defaults); 90 } 91 92 94 public String accept() { 95 return getProperty("accept"); 96 } 97 98 100 public void accept(String value) { 101 put("accept", value); 102 } 103 104 106 public String acceptCharset() { 107 return getProperty("accept-charset"); 108 } 109 110 112 public void acceptCharset(String value) { 113 put("accept-charset", value); 114 } 115 116 118 public String acceptEncoding() { 119 return getProperty("accept-encoding"); 120 } 121 122 124 public void acceptEncoding(String value) { 125 put("accept-encoding", value); 126 } 127 128 130 public String acceptLanguage() { 131 return getProperty("accept-language"); 132 } 133 134 136 public void acceptLanguage(String value) { 137 put("accept-language", value); 138 } 139 140 142 public String acceptRanges() { 143 return getProperty("accept-ranges"); 144 } 145 146 148 public void acceptRanges(String value) { 149 put("accept-ranges", value); 150 } 151 152 154 public String age() { 155 return getProperty("age"); 156 } 157 158 160 public void age(String value) { 161 put("age", value); 162 } 163 164 166 public String allow() { 167 return getProperty("allow"); 168 } 169 170 172 public void allow(String value) { 173 put("allow", value); 174 } 175 176 178 public String authorization() { 179 return getProperty("authorization"); 180 } 181 182 185 public void authorization(String value) { 186 if (value != null) { 187 put("authorization", value); 188 } else { 189 remove("authorization"); 190 } 191 } 192 193 195 public String cacheControl() { 196 return getProperty("cache-control"); 197 } 198 199 201 public void cacheControl(String value) { 202 put("cache-control", value); 203 } 204 205 207 public String connection() { 208 return getProperty("connection"); 209 } 210 211 213 public void connection(String value) { 214 put("connection", value); 215 } 216 217 222 public boolean containsKey(Object keyi) throws IllegalArgumentException { 223 String key = null; 224 225 if (keyi instanceof String ) { 226 key = ((String ) keyi).toLowerCase(); 227 } else { 228 throw new IllegalArgumentException ( 229 "internal programming error: header must be string: " + 230 keyi); 231 } 232 233 return super.containsKey(key); 234 } 235 236 238 public String contentEncoding() { 239 return getProperty("content-encoding"); 240 } 241 242 244 public void contentEncoding(String value) { 245 put("content-encoding", value); 246 } 247 248 250 public String contentLanguage() { 251 return getProperty("content-language"); 252 } 253 254 256 public void contentLanguage(String value) { 257 put("content-language", value); 258 } 259 260 262 public long contentLength() { 263 return Long.parseLong(getProperty("content-length", "-1")); 264 } 265 266 268 public void contentLength(long value) { 269 put("content-length", (new Long (value)).toString()); 270 } 271 272 274 public String contentLocation() { 275 return getProperty("content-location"); 276 } 277 278 280 public void contentLocation(String value) { 281 put("content-location", value); 282 } 283 284 287 public String contentType() { 288 return getProperty("content-type"); 289 } 290 291 294 public void contentType(String value) { 295 put("content-type", value); 296 } 297 298 301 public String date() { 302 return getProperty("date"); 303 } 304 305 307 public void date(String value) { 308 put("date", value); 309 } 310 311 313 public String DAV() { 314 return getProperty("dav"); 315 } 316 317 319 public void DAV(String value) { 320 put("dav", value); 321 } 322 323 325 public String depth() { 326 return getProperty("depth"); 327 } 328 329 331 public void depth(String value) { 332 put("depth", value); 333 } 334 335 337 public String destination() { 338 return getProperty("destination"); 339 } 340 341 343 public void destination(String value) { 344 put("destination", value); 345 } 346 347 349 public String etag() { 350 return getProperty("etag"); 351 } 352 353 355 public void etag(String value) { 356 put("etag", value); 357 } 358 359 361 public String expires() { 362 return getProperty("expires"); 363 } 364 365 367 public void expires(String value) { 368 put("expires", value); 369 } 370 371 375 public Object get(Object keyi) { 376 String key = null; 377 378 if (keyi instanceof String ) { 379 key = ((String ) keyi).toLowerCase(); 380 } else { 381 throw new IllegalArgumentException ( 382 "internal programming error: header must be string: " + 383 keyi); 384 } 385 386 return super.get(key); 387 } 388 389 392 public String getAuthorizationId() { 393 if (authorization() == null) { 394 return null; 395 } 396 397 String authorization = authorization().trim(); 398 String id = null; 399 400 if (authorization.startsWith("Basic") || 401 authorization.startsWith("BASIC")) { 402 String basicCookie = authorization.substring(6); 403 404 try { 405 id = new String (base64decoder.decodeBuffer(basicCookie)); 406 } catch (IOException exc) { 407 } 408 409 int colon = id.indexOf(':'); 410 411 if (colon > 0) { 412 id = id.substring(0, colon); 413 } 414 } 415 416 return id; 417 } 418 419 422 public String getPassword() { 423 if (authorization() == null) { 424 return null; 425 } 426 427 String authorization = authorization().trim(); 428 String pw = null; 429 430 if (authorization.startsWith("Basic") || 431 authorization.startsWith("BASIC")) { 432 String basicCookie = authorization.substring(6); 433 434 try { 435 pw = new String (base64decoder.decodeBuffer(basicCookie)); 436 } catch (IOException exc) { 437 } 438 439 int colon = pw.indexOf(':'); 440 441 if (colon > 0) { 442 pw = pw.substring(colon + 1); 443 } 444 } 445 446 return pw; 447 } 448 449 454 public int getTimeout() { 455 String t = timeout(); 456 int timeout = -1; 457 458 if ((t == null) || t.equals("Infinity")) { 459 timeout = -1; 460 } else if (t.startsWith("Second-")) { 461 timeout = new Integer (t.substring(7)).intValue(); 462 } 463 464 return timeout; 466 } 467 468 470 public String host() { 471 return getProperty("host"); 472 } 473 474 476 public void host(String value) { 477 put("host", value); 478 } 479 480 482 public String lastModified() { 483 return getProperty("last-modified"); 484 } 485 486 488 public void lastModified(String value) { 489 put("last-modified", value); 490 } 491 492 494 public String location() { 495 return getProperty("location"); 496 } 497 498 500 public void location(String value) { 501 put("location", value); 502 } 503 504 507 public String lockToken() { 508 String lockToken = getProperty("lock-token"); 509 510 if (lockToken.charAt(0) == '<') { 511 lockToken = lockToken.substring(1, lockToken.length() - 1); 512 } 513 514 return lockToken; 515 } 516 517 522 public void lockToken(String value) { 523 put("lock-token", "<" + value + ">"); 525 } 526 527 530 public String overwrite() { 531 return getProperty("overwrite"); 532 } 533 534 537 public void overwrite(String value) { 538 put("overwrite", value); 539 } 540 541 545 public Precondition precondition() throws WebDAVException { 546 String ifHeader = getProperty("if"); 547 Precondition precondition = null; 548 549 if (ifHeader != null) { 550 precondition = new Precondition(ifHeader); 551 } 552 553 return precondition; 554 } 555 556 560 public void precondition(Precondition value) { 561 if (value != null) { 562 put("if", value.toString()); 563 } else { 564 remove("if"); 565 } 566 } 567 568 572 public void precondition(String value) { 573 if (value != null) { 574 put("if", value); 575 } else { 576 remove("if"); 577 } 578 } 579 580 584 public Object put(Object keyi, Object value) { 585 String key = null; 586 587 if (keyi instanceof String ) { 588 key = ((String ) keyi).toLowerCase(); 589 } else { 590 throw new IllegalArgumentException ( 591 "internal programming error: header must be string: " + 592 keyi); 593 } 594 595 return super.put(key, value); 596 } 597 598 600 public String referer() { 601 return getProperty("referer"); 602 } 603 604 606 public void referer(String value) { 607 put("referer", value); 608 } 609 610 614 public Object remove(Object keyi) { 615 String key = null; 616 617 if (keyi instanceof String ) { 618 key = ((String ) keyi).toLowerCase(); 619 } else { 620 throw new IllegalArgumentException ( 621 "internal programming error: header must be string: " + 622 keyi); 623 } 624 625 return super.remove(key); 626 } 627 628 631 public String server() { 632 return getProperty("server"); 633 } 634 635 638 public void server(String value) { 639 put("server", value); 640 } 641 642 646 public void setBasicAuthorization(String userid, String password) { 647 String authString = base64encoder.encode( 648 (userid + ":" + password).getBytes()); 649 authorization("Basic " + authString); 650 } 651 652 657 public void setTimeout(int value) { 658 if (value == -1) { 659 timeout("Infinity"); 660 } else { 661 timeout("Second-" + value); 662 } 663 } 664 665 667 public String statusURI() { 668 return getProperty("status-uri"); 669 } 670 671 673 public void statusURI(String value) { 674 put("status-uri", value); 675 } 676 677 679 public String timeout() { 680 return getProperty("timeout"); 681 } 682 683 685 public void timeout(String value) { 686 put("timeout", value); 687 } 688 689 691 public String userAgent() { 692 return getProperty("user-agent"); 693 } 694 695 697 public void userAgent(String value) { 698 put("user-agent", value); 699 } 700 } | Popular Tags |