001: /*
002: * This program is free software; you can redistribute it and/or
003: * modify it under the terms of the GNU General Public License
004: * as published by the Free Software Foundation; either version 2
005: * of the License, or (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016: package net.sf.jftp.net;
017:
018: import java.io.IOException;
019: import java.io.InputStream;
020: import java.io.OutputStream;
021: import java.net.URL;
022: import java.net.URLConnection;
023:
024: import net.sf.jftp.system.logging.Log;
025:
026: /**
027: *
028: */
029: public class FtpURLConnection extends URLConnection {
030: private FtpConnection connection = null;
031: private String username = "ftp";
032: private String password = "none@no.no";
033: private int loginFlag = 0;
034:
035: public FtpURLConnection(URL u) {
036: super (u);
037:
038: // parse the url for username and password and the remote directory
039: connection = new FtpConnection(u.getHost());
040:
041: String userInfo = u.getUserInfo();
042:
043: if (userInfo != null) {
044: int index = userInfo.indexOf(":");
045:
046: if (index != -1) {
047: username = userInfo.substring(0, index);
048: password = userInfo.substring(index + 1);
049: }
050: }
051:
052: //System.out.println(u.getPath());
053: //System.out.println(u.getPort());
054: //System.out.println(u.getUserInfo());
055: //System.out.println(u.getAuthority());
056: Log.debug("URL: " + u.toString());
057: }
058:
059: public void connect() throws IOException {
060: loginFlag = connection.login(username, password);
061:
062: if (loginFlag != FtpConnection.LOGIN_OK) {
063: return;
064: }
065:
066: //System.out.println(url.getPath());
067: connection.chdir(url.getPath());
068: }
069:
070: public FtpConnection getFtpConnection() {
071: return connection;
072: }
073:
074: public InputStream getInputStream() throws IOException {
075: return null;
076: }
077:
078: public OutputStream getOutputStream() throws IOException {
079: return null;
080: }
081:
082: public String getUser() {
083: return username;
084: }
085:
086: public String getPass() {
087: return password;
088: }
089:
090: public String getHost() {
091: return url.getHost();
092: }
093:
094: public int getPort() {
095: int ret = url.getPort();
096:
097: if (ret <= 0) {
098: return 21;
099: } else {
100: return ret;
101: }
102: }
103:
104: public int getLoginResponse() {
105: return loginFlag;
106: }
107:
108: public boolean loginSucceeded() {
109: if (loginFlag == FtpConnection.LOGIN_OK) {
110: return true;
111: }
112:
113: return false;
114: }
115:
116: public static void main(String[] args) {
117: try {
118: URLConnection uc = new FtpURLConnection(new URL(
119: "ftp://ftp:pass@localhost/pub"));
120: uc.connect();
121: } catch (IOException ioe) {
122: }
123: }
124: }
|