001: /*
002: * This file is part of the QuickServer library
003: * Copyright (C) 2003-2005 QuickServer.org
004: *
005: * Use, modification, copying and distribution of this software is subject to
006: * the terms and conditions of the GNU Lesser General Public License.
007: * You should have received a copy of the GNU LGP License along with this
008: * library; if not, you can download a copy from <http://www.quickserver.org/>.
009: *
010: * For questions, suggestions, bug-reports, enhancement-requests etc.
011: * visit http://www.quickserver.org
012: *
013: */
014:
015: package ftpserver;
016:
017: import org.quickserver.net.*;
018: import org.quickserver.net.server.*;
019:
020: import org.quickserver.util.pool.PoolableObject;
021: import org.apache.commons.pool.BasePoolableObjectFactory;
022: import org.apache.commons.pool.PoolableObjectFactory;
023:
024: import java.io.*;
025: import java.net.*;
026:
027: public class Data implements ClientData, PoolableObject {
028: boolean isTransferring = false;
029: boolean isRenameFrom = false;
030: boolean isStop = false;
031: boolean isPassive = false;
032:
033: Socket socket = null;
034: ServerSocket server = null; //if passive
035: int socketPort = -1;
036: int serverPort = -1;
037: boolean closeDataServer = true;
038: String ip = null;
039:
040: //pipelining
041: boolean wasQuit = false;
042: boolean isProcessing = false;
043: int noOfBytesTransferred = -1;
044: int startPosition = 0; //for REST
045:
046: String root = null; //no ending slash \
047: String wDir = "/"; //WORKING DIRECTORY
048:
049: String account = null; //ACCOUNT
050: String username = ""; //USERNAME
051: boolean binary = false;
052: char type = 'A'; //ASCII,IMAGE
053: char typeSub = 'N'; //Non-print,
054: String mode = "Stream"; //Stream
055: String structure = "File"; //File, Record
056:
057: public void sendFile(String file) throws Exception {
058: isStop = false;
059: if (socket == null)
060: throw new IOException("No client connected");
061: String input = "";
062: int i = 0;
063: FileInputStream fin = null;
064: BufferedOutputStream out = null;
065: try {
066: out = new BufferedOutputStream(socket.getOutputStream());
067: fin = new FileInputStream(file);
068: /*
069: if(data.type == 'A') {
070: PrintWriter rout=new PrintWriter(out);
071: BufferedReader br = new BufferedReader(new InputStreamReader(fin));
072: while(true) {
073: input = br.readLine();
074: if(input==null)
075: break;
076: rout.
077: }
078: rout.flush();
079: //rout.close();
080: } else {
081:
082: }*/
083: while (true) {
084: i = fin.read();
085: if (i == -1 || isStop == true) //if aborted
086: break;
087: out.write(i);
088: }
089: out.flush();
090: } catch (Exception e) {
091: throw e;
092: } finally {
093: if (fin != null)
094: fin.close();
095: }
096: }
097:
098: public void sendData(String sdata) throws Exception {
099: //System.out.print("Sending data on datacon ");
100: if (socket == null) {
101: Thread.currentThread().sleep(500);
102: if (socket == null) {
103: throw new IOException("No client connected");
104: }
105: }
106: BufferedOutputStream out = null;
107: try {
108: out = new BufferedOutputStream(socket.getOutputStream());
109: out.write(sdata.getBytes(), 0, sdata.length());
110: out.flush();
111: } catch (Exception e) {
112: throw e;
113: }
114: }
115:
116: public void startDataServer(ServerSocket acceptSocket,
117: ftpserver.Data data) {
118: new DataServer(acceptSocket, data);
119: }
120:
121: //-- PoolableObject
122: public void clean() {
123: isTransferring = false;
124: isRenameFrom = false;
125: isStop = false;
126: isPassive = false;
127: socket = null;
128: server = null;
129: socketPort = -1;
130: serverPort = -1;
131: closeDataServer = true;
132: ip = null;
133: wasQuit = false;
134: isProcessing = false;
135: noOfBytesTransferred = -1;
136: startPosition = 0;
137: root = null;
138: wDir = "/";
139: account = null;
140: username = "";
141: binary = false;
142: type = 'A';
143: typeSub = 'N';
144: mode = "Stream";
145: structure = "File";
146: }
147:
148: public boolean isPoolable() {
149: return true;
150: }
151:
152: public PoolableObjectFactory getPoolableObjectFactory() {
153: return new BasePoolableObjectFactory() {
154: public Object makeObject() {
155: return new Data();
156: }
157:
158: public void passivateObject(Object obj) {
159: Data ed = (Data) obj;
160: ed.clean();
161: }
162:
163: public void destroyObject(Object obj) {
164: if (obj == null)
165: return;
166: passivateObject(obj);
167: obj = null;
168: }
169:
170: public boolean validateObject(Object obj) {
171: if (obj == null)
172: return false;
173: else
174: return true;
175: }
176: };
177: }
178: }
179:
180: class DataServer extends Thread {
181: ServerSocket acceptSocket = null;
182: ftpserver.Data data = null;
183:
184: public DataServer(ServerSocket acceptSocket, ftpserver.Data data) {
185: super ("DataServer");
186: this .acceptSocket = acceptSocket;
187: this .data = data;
188: setDaemon(true);
189: this .data.isStop = false;
190: start();
191: }
192:
193: public void run() {
194: try {
195: data.closeDataServer = false;
196: data.socket = acceptSocket.accept();
197: //System.out.println("Data con opened.");
198: while (data != null && data.closeDataServer == false
199: && data.isStop == false) {
200: Thread.sleep(300);
201: }
202: if (data.socket != null) {
203: data.socket.close();
204: }
205: } catch (IOException e) {
206: System.err.println("Error at dataServer :" + e);
207: } catch (InterruptedException e) {
208: System.err.println("Thread Error at dataServer :" + e);
209: } finally {
210: try {
211: acceptSocket.close();
212: } catch (Exception re) {
213: System.err.println("BAD Error at dataServer :" + re);
214: }
215: }
216: data.socket = null;
217: data.server = null;
218: }
219:
220: }
|