KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jftp > net > FtpURLConnection


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

16 package net.sf.jftp.net;
17
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.io.OutputStream JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.net.URLConnection JavaDoc;
23
24 import net.sf.jftp.system.logging.Log;
25
26
27 /**
28  *
29  */

30 public class FtpURLConnection extends URLConnection JavaDoc
31 {
32     private FtpConnection connection = null;
33     private String JavaDoc username = "ftp";
34     private String JavaDoc password = "none@no.no";
35     private int loginFlag = 0;
36
37     public FtpURLConnection(URL JavaDoc u)
38     {
39         super(u);
40
41         // parse the url for username and password and the remote directory
42
connection = new FtpConnection(u.getHost());
43
44         String JavaDoc userInfo = u.getUserInfo();
45
46         if(userInfo != null)
47         {
48             int index = userInfo.indexOf(":");
49
50             if(index != -1)
51             {
52                 username = userInfo.substring(0, index);
53                 password = userInfo.substring(index + 1);
54             }
55         }
56
57         //System.out.println(u.getPath());
58
//System.out.println(u.getPort());
59
//System.out.println(u.getUserInfo());
60
//System.out.println(u.getAuthority());
61
Log.debug("URL: " + u.toString());
62     }
63
64     public void connect() throws IOException JavaDoc
65     {
66         loginFlag = connection.login(username, password);
67
68         if(loginFlag != FtpConnection.LOGIN_OK)
69         {
70             return;
71         }
72
73         //System.out.println(url.getPath());
74
connection.chdir(url.getPath());
75     }
76
77     public FtpConnection getFtpConnection()
78     {
79         return connection;
80     }
81
82     public InputStream JavaDoc getInputStream() throws IOException JavaDoc
83     {
84         return null;
85     }
86
87     public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc
88     {
89         return null;
90     }
91
92     public String JavaDoc getUser()
93     {
94         return username;
95     }
96
97     public String JavaDoc getPass()
98     {
99         return password;
100     }
101
102     public String JavaDoc getHost()
103     {
104         return url.getHost();
105     }
106
107     public int getPort()
108     {
109         int ret = url.getPort();
110
111         if(ret <= 0)
112         {
113             return 21;
114         }
115         else
116         {
117             return ret;
118         }
119     }
120
121     public int getLoginResponse()
122     {
123         return loginFlag;
124     }
125
126     public boolean loginSucceeded()
127     {
128         if(loginFlag == FtpConnection.LOGIN_OK)
129         {
130             return true;
131         }
132
133         return false;
134     }
135
136     public static void main(String JavaDoc[] args)
137     {
138         try
139         {
140             URLConnection JavaDoc uc = new FtpURLConnection(new URL JavaDoc("ftp://ftp:pass@localhost/pub"));
141             uc.connect();
142         }
143         catch(IOException JavaDoc ioe)
144         {
145         }
146     }
147 }
148
Popular Tags