View Javadoc

1   package org.opensync.engine.server.protocol;
2   
3   import java.io.IOException;
4   
5   import org.opensync.engine.server.DefaultProtocol;
6   import org.opensync.engine.server.Protocol;
7   import org.opensync.engine.server.OpenSyncException;
8   import org.opensync.engine.server.connector.FileConnector;
9   
10  import com.enterprisedt.net.ftp.FTPClient;
11  import com.enterprisedt.net.ftp.FTPConnectMode;
12  import com.enterprisedt.net.ftp.FTPException;
13  import com.enterprisedt.net.ftp.FTPTransferType;
14  
15  /***
16   * The protocol used to get information from a ftp file server
17   */
18  
19  public class FTPProtocol extends DefaultProtocol{
20  
21    /***
22     * Construct a FTPProtocl
23     * 
24     */
25    public FTPProtocol() {
26      setName(Protocol.FTP);
27    }
28    /***
29     * Put the <code>fileContent</code> in the file <code>fileName</code> int
30     * the <code>folder</code> directory
31     * 
32     * @param	fileContent the file content
33     * @param	fileName the file name
34     * @exception	OpenSyncException
35     * @exception	IOException
36     * 	
37     */
38    public void putView(String fileContent,String fileName)throws OpenSyncException,IOException{
39      try {
40        FileConnector fileConnector = (FileConnector)connector;
41        FTPClient ftp = new FTPClient(fileConnector.getUrl(), 21);
42        ftp.login(fileConnector.getUser(), fileConnector.getPassword());
43        ftp.setType(FTPTransferType.ASCII);
44        ftp.setConnectMode(FTPConnectMode.PASV);
45        ftp.chdir(folder);
46        ftp.put(fileContent.getBytes(),fileName);
47        ftp.quit();
48      }
49      catch (FTPException ex) {
50        throw new OpenSyncException(ex.getMessage());
51      }
52    }
53    /***
54     * Get the content of the file<code>fileName</code>
55     * 
56     * @param	fileName the file name (not use *)
57     * @exception	OpenSyncException
58     * @exception	IOException
59     */
60    public String[] getViews(String fileName)throws OpenSyncException,IOException{
61      try {
62        FileConnector fileConnector = (FileConnector)connector;
63        FTPClient ftp = new FTPClient(fileConnector.getUrl(), 21);
64        ftp.login(fileConnector.getUser(), fileConnector.getPassword());
65        ftp.setType(FTPTransferType.ASCII);
66        ftp.setConnectMode(FTPConnectMode.PASV);
67        ftp.chdir(folder);
68        byte[] bytes = ftp.get(fileName);
69        ftp.quit();
70        return new String[]{new String(bytes)};
71      }
72      catch (FTPException ex) {
73        throw new OpenSyncException(ex.getMessage());
74      }
75    }
76  }