KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > 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 com.knowgate.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 139;
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         if( spec.equals( "smb://" )) {
80             spec = "smb:////";
81             limit += 2;
82         } else if( spec.startsWith( "smb://" ) == false &&
83                     host != null && host.length() == 0 ) {
84             spec = "//" + spec;
85             limit += 2;
86         }
87         super.parseURL( u, spec, start, limit );
88         userinfo = u.getUserInfo();
89         path = u.getPath();
90         ref = u.getRef();
91         try {
92             userinfo = unescape( userinfo );
93         } catch( UnsupportedEncodingException JavaDoc uee ) {
94         }
95         if (ref != null) {
96             path += '#' + ref;
97         }
98         setURL( u, "smb://", u.getHost(), getDefaultPort(),
99                     u.getAuthority(), userinfo,
100                     path, u.getQuery(), null );
101     }
102 }
103
Popular Tags