1 17 package com.sslexplorer.vfs.webdav; 18 19 import java.io.BufferedReader ; 20 import java.io.InputStream ; 21 import java.io.InputStreamReader ; 22 import java.io.UnsupportedEncodingException ; 23 import java.security.MessageDigest ; 24 import java.text.SimpleDateFormat ; 25 import java.util.Date ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.Locale ; 29 import java.util.Map ; 30 import java.util.Properties ; 31 import java.util.StringTokenizer ; 32 import java.util.TimeZone ; 33 34 import com.maverick.util.URLUTF8Encoder; 35 import com.sslexplorer.boot.Util; 36 import com.sslexplorer.core.stringreplacement.SessionInfoReplacer; 37 import com.sslexplorer.security.SessionInfo; 38 import com.sslexplorer.vfs.utils.URI; 39 import com.sslexplorer.vfs.utils.URI.MalformedURIException; 40 41 48 public class DAVUtilities { 49 50 55 private static final String ACCEPTABLE = "ABCDEFGHIJLKMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789" + "_-!.~'()*" + ",;:$+=" + "?/@"; 71 76 private static Map MIME_TYPES = new HashMap (); 77 82 private static Properties PROPERTIES = new Properties (); 83 88 private static final String FORMAT_822 = "EEE, dd MMM yyyy HH:mm:ss 'GMT'"; 89 94 private static final TimeZone TIMEZONE_822 = TimeZone.getTimeZone("GMT"); 95 96 101 static { 102 InputStream prop = DAVUtilities.class.getResourceAsStream("webdav.props"); 103 try { 104 DAVUtilities.PROPERTIES.load(prop); 105 prop.close(); 106 } catch (Exception exception) { 107 exception.printStackTrace(); 108 } 109 110 111 InputStream mime = DAVUtilities.class.getResourceAsStream("mime.types"); 112 try { 113 InputStreamReader read = new InputStreamReader (mime); 114 BufferedReader buff = new BufferedReader (read); 115 String line = null; 116 while ((line = buff.readLine()) != null) { 117 line = line.trim(); 118 if (line.length() == 0) 119 continue; 120 if (line.charAt(0) == '#') 121 continue; 122 StringTokenizer tokenizer = new StringTokenizer (line); 123 if (tokenizer.countTokens() > 1) { 124 String type = tokenizer.nextToken(); 125 while (tokenizer.hasMoreTokens()) { 126 String extension = '.' + tokenizer.nextToken(); 127 DAVUtilities.MIME_TYPES.put(extension, type); 128 } 129 } 130 } 131 buff.close(); 132 read.close(); 133 mime.close(); 134 } catch (Exception exception) { 135 exception.printStackTrace(); 136 } 137 } 138 139 144 private DAVUtilities() { 145 super(); 146 } 147 148 156 public static String getProperty(String name) { 157 if (name == null) 158 return null; 159 return DAVUtilities.PROPERTIES.getProperty(name); 160 } 161 162 170 public static String getMimeType(String name) { 171 if (name == null) 172 return null; 173 174 Iterator iterator = DAVUtilities.MIME_TYPES.keySet().iterator(); 175 while (iterator.hasNext()) { 176 String extension = (String ) iterator.next(); 177 if (name.endsWith(extension)) { 178 return (String ) DAVUtilities.MIME_TYPES.get(extension); 179 } 180 } 181 182 return null; 183 } 184 185 193 public static String getStatusMessage(int status) { 194 switch (status) { 195 196 case 100: 197 return "100 Continue"; 198 case 101: 199 return "101 Switching Protocols"; 200 case 200: 201 return "200 OK"; 202 case 201: 203 return "201 Created"; 204 case 202: 205 return "202 Accepted"; 206 case 203: 207 return "203 Non-Authoritative Information"; 208 case 204: 209 return "204 No Content"; 210 case 205: 211 return "205 Reset Content"; 212 case 206: 213 return "206 Partial Content"; 214 case 300: 215 return "300 Multiple Choices"; 216 case 301: 217 return "301 Moved Permanently"; 218 case 302: 219 return "302 Found"; 220 case 303: 221 return "303 See Other"; 222 case 304: 223 return "304 Not Modified"; 224 case 305: 225 return "305 Use Proxy"; 226 case 306: 227 return "306 (Unused)"; 228 case 307: 229 return "307 Temporary Redirect"; 230 case 400: 231 return "400 Bad Request"; 232 case 401: 233 return "401 Unauthorized"; 234 case 402: 235 return "402 Payment Required"; 236 case 403: 237 return "403 Forbidden"; 238 case 404: 239 return "404 Not Found"; 240 case 405: 241 return "405 Method Not Allowed"; 242 case 406: 243 return "406 Not Acceptable"; 244 case 407: 245 return "407 Proxy Authentication Required"; 246 case 408: 247 return "408 Request Timeout"; 248 case 409: 249 return "409 Conflict"; 250 case 410: 251 return "410 Gone"; 252 case 411: 253 return "411 Length Required"; 254 case 412: 255 return "412 Precondition Failed"; 256 case 413: 257 return "413 Request Entity Too Large"; 258 case 414: 259 return "414 Request-URI Too Long"; 260 case 415: 261 return "415 Unsupported Media Type"; 262 case 416: 263 return "416 Requested Range Not Satisfiable"; 264 case 417: 265 return "417 Expectation Failed"; 266 case 500: 267 return "500 Internal Server Error"; 268 case 501: 269 return "501 Not Implemented"; 270 case 502: 271 return "502 Bad Gateway"; 272 case 503: 273 return "503 Service Unavailable"; 274 case 504: 275 return "504 Gateway Timeout"; 276 case 505: 277 return "505 HTTP Version Not Supported"; 278 279 280 case 102: 281 return "102 Processing"; 282 case 207: 283 return "207 Multi-Status"; 284 case 422: 285 return "422 Unprocessable Entity"; 286 case 423: 287 return "423 Locked"; 288 case 424: 289 return "424 Failed Dependency"; 290 case 507: 291 return "507 Insufficient Storage"; 292 293 294 default: 295 return null; 296 } 297 } 298 299 307 public static String format(Object object) { 308 if (object == null) 309 return null; 310 if (object instanceof String ) 311 return ((String ) object); 312 if (object instanceof Date ) { 313 SimpleDateFormat formatter = new SimpleDateFormat (FORMAT_822, Locale.ENGLISH); 314 formatter.setTimeZone(TIMEZONE_822); 315 return formatter.format((Date ) object); 316 } 317 return (object.toString()); 318 } 319 320 328 public static String toHexString(byte buffer[]) { 329 char output[] = new char[buffer.length * 2]; 330 int position = 0; 331 for (int x = 0; x < buffer.length; x++) { 332 output[position++] = DAVUtilities.toHexDigit(buffer[x] >> 4); 333 output[position++] = DAVUtilities.toHexDigit(buffer[x]); 334 } 335 return new String (output); 336 } 337 338 346 public static String toHexString(long number) { 347 char output[] = new char[16]; 348 output[0] = DAVUtilities.toHexDigit((int) (number >> 60)); 349 output[1] = DAVUtilities.toHexDigit((int) (number >> 56)); 350 output[2] = DAVUtilities.toHexDigit((int) (number >> 52)); 351 output[3] = DAVUtilities.toHexDigit((int) (number >> 48)); 352 output[4] = DAVUtilities.toHexDigit((int) (number >> 44)); 353 output[5] = DAVUtilities.toHexDigit((int) (number >> 40)); 354 output[6] = DAVUtilities.toHexDigit((int) (number >> 36)); 355 output[7] = DAVUtilities.toHexDigit((int) (number >> 32)); 356 output[8] = DAVUtilities.toHexDigit((int) (number >> 28)); 357 output[9] = DAVUtilities.toHexDigit((int) (number >> 24)); 358 output[10] = DAVUtilities.toHexDigit((int) (number >> 20)); 359 output[11] = DAVUtilities.toHexDigit((int) (number >> 16)); 360 output[12] = DAVUtilities.toHexDigit((int) (number >> 12)); 361 output[13] = DAVUtilities.toHexDigit((int) (number >> 8)); 362 output[14] = DAVUtilities.toHexDigit((int) (number >> 4)); 363 output[15] = DAVUtilities.toHexDigit((int) (number)); 364 return new String (output); 365 } 366 367 375 public static String toHexString(int number) { 376 char output[] = new char[8]; 377 output[0] = DAVUtilities.toHexDigit((int) (number >> 28)); 378 output[1] = DAVUtilities.toHexDigit((int) (number >> 24)); 379 output[2] = DAVUtilities.toHexDigit((int) (number >> 20)); 380 output[3] = DAVUtilities.toHexDigit((int) (number >> 16)); 381 output[4] = DAVUtilities.toHexDigit((int) (number >> 12)); 382 output[5] = DAVUtilities.toHexDigit((int) (number >> 8)); 383 output[6] = DAVUtilities.toHexDigit((int) (number >> 4)); 384 output[7] = DAVUtilities.toHexDigit((int) (number)); 385 return new String (output); 386 } 387 388 396 public static String toHexString(char number) { 397 char output[] = new char[4]; 398 output[0] = DAVUtilities.toHexDigit((int) (number >> 12)); 399 output[1] = DAVUtilities.toHexDigit((int) (number >> 8)); 400 output[2] = DAVUtilities.toHexDigit((int) (number >> 4)); 401 output[3] = DAVUtilities.toHexDigit((int) (number)); 402 return new String (output); 403 } 404 405 413 public static String toHexString(byte number) { 414 char output[] = new char[2]; 415 output[0] = DAVUtilities.toHexDigit((int) (number >> 4)); 416 output[1] = DAVUtilities.toHexDigit((int) (number)); 417 return new String (output); 418 } 419 420 429 private static char toHexDigit(int number) { 430 switch (number & 0x0F) { 431 case 0x00: 432 return '0'; 433 case 0x01: 434 return '1'; 435 case 0x02: 436 return '2'; 437 case 0x03: 438 return '3'; 439 case 0x04: 440 return '4'; 441 case 0x05: 442 return '5'; 443 case 0x06: 444 return '6'; 445 case 0x07: 446 return '7'; 447 case 0x08: 448 return '8'; 449 case 0x09: 450 return '9'; 451 case 0x0A: 452 return 'A'; 453 case 0x0B: 454 return 'B'; 455 case 0x0C: 456 return 'C'; 457 case 0x0D: 458 return 'D'; 459 case 0x0E: 460 return 'E'; 461 case 0x0F: 462 return 'F'; 463 } 464 String message = "Invalid HEX digit " + Integer.toHexString(number); 465 throw new IllegalArgumentException (message); 466 } 467 468 474 public static String encodePath(String path) { 475 return encodePath(path, false, "UTF-8"); 476 } 477 478 public static String encodePath(String path, boolean encodeSlash) { 479 return encodePath(path, encodeSlash, "UTF-8"); 480 } 481 482 public static String encodePath(String path, String charset) { 483 return encodePath(path, false, charset); 484 } 485 493 public static URI processAndEncodeURI(String uri, SessionInfo session, String charset) throws MalformedURIException { 494 String path = session == null ? uri : SessionInfoReplacer.replace(session, uri); 496 URI nuri = new URI(path); 497 if(nuri.getUserinfo() != null) { 498 nuri.setUserinfo(encodeURIUserInfo(nuri.getUserinfo())); 499 } 500 if(nuri.getPath() != null && !nuri.getPath().equals("")) { 501 nuri.setPath(encodePath(nuri.getPath(), charset)); 502 } 503 return nuri; 504 } 505 506 508 public static URI processAndEncodeURI(String uri, SessionInfo session) throws MalformedURIException { 509 return processAndEncodeURI(uri, session, "UTF-8"); 510 } 511 512 519 public static String encodePath(String path, boolean encodeSlash, String charset) { 520 521 StringBuffer buffer = new StringBuffer (); 522 byte encoded[]; 523 try { 524 if(charset==null) 525 encoded = path.getBytes(); 526 else 527 encoded = path.getBytes(charset); 528 for (int x = 0; x < encoded.length; x++) { 529 if (((int) encoded[x] == '%' && encodeSlash) || ACCEPTABLE.indexOf((int) encoded[x]) < 0) { 530 buffer.append('%'); 531 buffer.append(DAVUtilities.toHexString(encoded[x])); 532 continue; 533 } 534 buffer.append((char) encoded[x]); 535 } 536 } catch (UnsupportedEncodingException e) { 537 e.printStackTrace(); 538 return path; 539 } 540 541 return buffer.toString(); 542 } 543 544 550 public static String encodeURIUserInfo(String uriUserInfo) { 551 int idx = uriUserInfo.indexOf(':'); 552 if(idx != -1) { 553 return URLUTF8Encoder.encode(uriUserInfo.substring(0, idx), true) + ":" + 554 URLUTF8Encoder.encode(uriUserInfo.substring(idx + 1), true); 555 } 556 return Util.urlEncode(uriUserInfo); 557 } 558 559 566 public static String getETAG(String path, Date lastModified) { 567 StringBuffer etag = new StringBuffer (); 568 etag.append('"'); 569 570 571 try { 572 MessageDigest digester = MessageDigest.getInstance("MD5"); 573 digester.reset(); 574 digester.update(path.getBytes("UTF8")); 575 etag.append(DAVUtilities.toHexString(digester.digest())); 576 etag.append('-'); 577 } catch (Exception e) { 578 } 580 581 582 etag.append(DAVUtilities.toHexString(path.hashCode())); 583 584 585 if (lastModified != null) { 586 etag.append('-'); 587 etag.append(DAVUtilities.toHexString(lastModified.getTime())); 588 } 589 590 591 etag.append('"'); 592 return (etag.toString()); 593 } 594 595 601 public static String stripFirstPath(String path) { 602 if (path.length() < 1) { 603 return path; 604 } 605 int idx = path.indexOf('/', 1); 606 return idx == -1 ? path : path.substring(idx); 607 } 608 609 615 public static String stripLeadingSlash(String path) { 616 while (path != null && path.startsWith("/")) { 617 path = path.substring(1); 618 } 619 return path; 620 } 621 622 628 public static String stripTrailingSlash(String path) { 629 while (path.endsWith("/")) { 630 path = path.substring(0, path.length() - 1); 631 } 632 return path; 633 } 634 635 640 public static String stripUserInfo(String uri) { 641 642 int idx1 = uri.indexOf("//"); 643 int idx2 = uri.indexOf('@'); 644 645 if(idx1 > -1 && idx2 > -1 && idx1 < idx2) { 646 return uri.substring(0, idx1+2) + uri.substring(idx2); 647 } else 648 return uri; 649 650 } 651 652 661 public static String basename(String path, char separator) { 662 if (path.equals("")) { 663 return path; 664 } 665 while (path.endsWith(String.valueOf(separator))) { 666 path = path.substring(0, path.length() - 1); 667 } 668 int idx = path.lastIndexOf(separator); 669 return idx == -1 ? path : path.substring(idx + 1); 670 } 671 672 679 public static String concatenatePaths(String original, String append) { 680 if (append != null) { 681 if (original.endsWith("/")) { 682 original = original.concat(stripLeadingSlash(append)); 683 } else { 684 if (append.startsWith("/")) { 685 original = original.concat(append); 686 } else { 687 original = original.concat("/".concat(append)); 688 } 689 } 690 } 691 return original; 692 } 693 694 700 public static String getParentPath(String path) { 701 path = stripTrailingSlash(path); 702 String parent = null; 703 if (!path.equals("")) { 704 int idx = path.lastIndexOf("/"); 705 if (idx == -1) { 706 parent = "/"; 707 } else { 708 parent = path.substring(0, idx + 1); 709 } 710 } 711 return parent; 712 } 713 714 722 public static String dirname(String path) { 723 String s = stripTrailingSlash(path); 724 if (s.equals("")) { 725 return null; 726 } 727 return s.substring(0, s.lastIndexOf("/")); 728 } 729 } 730 | Popular Tags |