1 18 19 package jcifs.smb; 20 21 import java.net.URL ; 22 import java.net.URLConnection ; 23 import java.net.URLStreamHandler ; 24 import java.io.IOException ; 25 import java.io.UnsupportedEncodingException ; 26 27 import java.io.PrintStream ; 28 29 public class Handler extends URLStreamHandler { 30 31 static final URLStreamHandler SMB_HANDLER = new Handler(); 32 33 static String unescape( String str ) throws NumberFormatException , UnsupportedEncodingException { 34 char ch; 35 int i, j, state, len; 36 char[] out; 37 byte[] b = new byte[1]; 38 39 if( str == null ) { 40 return null; 41 } 42 43 len = str.length(); 44 out = new char[len]; 45 state = 0; 46 for( i = j = 0; i < len; i++ ) { 47 switch( state ) { 48 case 0: 49 ch = str.charAt( i ); 50 if( ch == '%' ) { 51 state = 1; 52 } else { 53 out[j++] = ch; 54 } 55 break; 56 case 1: 57 60 b[0] = (byte)(Integer.parseInt( str.substring( i, i + 2 ), 16 ) & 0xFF); 61 out[j++] = (new String ( b, 0, 1, "ASCII" )).charAt( 0 ); 62 i++; 63 state = 0; 64 } 65 } 66 67 return new String ( out, 0, j ); 68 } 69 70 protected int getDefaultPort() { 71 return SmbConstants.DEFAULT_PORT; 72 } 73 public URLConnection openConnection( URL u ) throws IOException { 74 return new SmbFile( u ); 75 } 76 protected void parseURL( URL u, String spec, int start, int limit ) { 77 String host = u.getHost(); 78 String userinfo, path, ref; 79 int port; 80 81 if( spec.equals( "smb://" )) { 82 spec = "smb:////"; 83 limit += 2; 84 } else if( spec.startsWith( "smb://" ) == false && 85 host != null && host.length() == 0 ) { 86 spec = "//" + spec; 87 limit += 2; 88 } 89 super.parseURL( u, spec, start, limit ); 90 userinfo = u.getUserInfo(); 91 path = u.getPath(); 92 ref = u.getRef(); 93 try { 94 userinfo = unescape( userinfo ); 95 } catch( UnsupportedEncodingException uee ) { 96 } 97 if (ref != null) { 98 path += '#' + ref; 99 } 100 port = u.getPort(); 101 if( port == -1 ) { 102 port = getDefaultPort(); 103 } 104 setURL( u, "smb", u.getHost(), port, 105 u.getAuthority(), userinfo, 106 path, u.getQuery(), null ); 107 } 108 } 109 | Popular Tags |