001: /* jcifs smb client library in Java
002: * Copyright (C) 2000 "Michael B. Allen" <jcifs at samba dot org>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.knowgate.jcifs.smb;
020:
021: import java.net.URL;
022: import java.net.URLConnection;
023: import java.net.URLStreamHandler;
024: import java.io.IOException;
025: import java.io.UnsupportedEncodingException;
026:
027: import java.io.PrintStream;
028:
029: public class Handler extends URLStreamHandler {
030:
031: static final URLStreamHandler SMB_HANDLER = new Handler();
032:
033: static String unescape(String str) throws NumberFormatException,
034: UnsupportedEncodingException {
035: char ch;
036: int i, j, state, len;
037: char[] out;
038: byte[] b = new byte[1];
039:
040: if (str == null) {
041: return null;
042: }
043:
044: len = str.length();
045: out = new char[len];
046: state = 0;
047: for (i = j = 0; i < len; i++) {
048: switch (state) {
049: case 0:
050: ch = str.charAt(i);
051: if (ch == '%') {
052: state = 1;
053: } else {
054: out[j++] = ch;
055: }
056: break;
057: case 1:
058: /* Get ASCII hex value and convert to platform dependant
059: * encoding like EBCDIC perhaps
060: */
061: b[0] = (byte) (Integer.parseInt(
062: str.substring(i, i + 2), 16) & 0xFF);
063: out[j++] = (new String(b, 0, 1, "ASCII")).charAt(0);
064: i++;
065: state = 0;
066: }
067: }
068:
069: return new String(out, 0, j);
070: }
071:
072: protected int getDefaultPort() {
073: return 139;
074: }
075:
076: public URLConnection openConnection(URL u) throws IOException {
077: return new SmbFile(u);
078: }
079:
080: protected void parseURL(URL u, String spec, int start, int limit) {
081: String host = u.getHost();
082: String userinfo, path, ref;
083: if (spec.equals("smb://")) {
084: spec = "smb:////";
085: limit += 2;
086: } else if (spec.startsWith("smb://") == false && host != null
087: && host.length() == 0) {
088: spec = "//" + spec;
089: limit += 2;
090: }
091: super .parseURL(u, spec, start, limit);
092: userinfo = u.getUserInfo();
093: path = u.getPath();
094: ref = u.getRef();
095: try {
096: userinfo = unescape(userinfo);
097: } catch (UnsupportedEncodingException uee) {
098: }
099: if (ref != null) {
100: path += '#' + ref;
101: }
102: setURL(u, "smb://", u.getHost(), getDefaultPort(), u
103: .getAuthority(), userinfo, path, u.getQuery(), null);
104: }
105: }
|