KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcifs > smb > Handler


1 /* jcifs smb client library in Java
2  * Copyright (C) 2000 "Michael B. Allen" <jcifs at samba dot org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package jcifs.smb;
20
21 import java.net.URL JavaDoc;
22 import java.net.URLConnection JavaDoc;
23 import java.net.URLStreamHandler JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.UnsupportedEncodingException JavaDoc;
26
27 import java.io.PrintStream JavaDoc;
28
29 public class Handler extends URLStreamHandler JavaDoc {
30
31     static final URLStreamHandler JavaDoc SMB_HANDLER = new Handler();
32
33     static String JavaDoc unescape( String JavaDoc str ) throws NumberFormatException JavaDoc, UnsupportedEncodingException JavaDoc {
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                     /* Get ASCII hex value and convert to platform dependant
58                      * encoding like EBCDIC perhaps
59                      */

60                     b[0] = (byte)(Integer.parseInt( str.substring( i, i + 2 ), 16 ) & 0xFF);
61                     out[j++] = (new String JavaDoc( b, 0, 1, "ASCII" )).charAt( 0 );
62                     i++;
63                     state = 0;
64             }
65         }
66
67         return new String JavaDoc( out, 0, j );
68     }
69
70     protected int getDefaultPort() {
71         return SmbConstants.DEFAULT_PORT;
72     }
73     public URLConnection JavaDoc openConnection( URL JavaDoc u ) throws IOException JavaDoc {
74         return new SmbFile( u );
75     }
76     protected void parseURL( URL JavaDoc u, String JavaDoc spec, int start, int limit ) {
77         String JavaDoc host = u.getHost();
78         String JavaDoc 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 JavaDoc 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