Skip to content

Commit f87313c

Browse files
author
NoteFox
authored
Merge pull request #2 from OpenHistoricalDataMap/NoFileFoundBug
No file found bug
2 parents 0cba004 + 40a9328 commit f87313c

10 files changed

Lines changed: 344 additions & 65 deletions

File tree

out/META-INF/MANIFEST.MF

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Manifest-Version: 1.0
2+
Main-Class: Server.SpringClass
3+

pom.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@
2323
<groupId>org.springframework.boot</groupId>
2424
<artifactId>spring-boot-starter-data-rest</artifactId>
2525
</dependency>
26+
2627
<dependency>
2728
<groupId>org.springframework.boot</groupId>
2829
<artifactId>spring-boot-starter-web</artifactId>
2930
</dependency>
31+
3032
<dependency>
3133
<groupId>org.springframework.boot</groupId>
3234
<artifactId>spring-boot-starter-web-services</artifactId>
@@ -38,7 +40,6 @@
3840
<version>1.1.1</version>
3941
</dependency>
4042

41-
4243
<dependency>
4344
<groupId>org.springframework.boot</groupId>
4445
<artifactId>spring-boot-starter-test</artifactId>
@@ -50,11 +51,18 @@
5051
</exclusion>
5152
</exclusions>
5253
</dependency>
54+
5355
<dependency>
5456
<groupId>DemoProject</groupId>
5557
<artifactId>demo</artifactId>
5658
<version>0.0.1-SNAPSHOT</version>
5759
</dependency>
60+
61+
<dependency>
62+
<groupId>commons-net</groupId>
63+
<artifactId>commons-net</artifactId>
64+
<version>3.6</version>
65+
</dependency>
5866
</dependencies>
5967

6068
<build>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package FTPClient;
2+
3+
import org.apache.commons.net.ftp.FTPFile;
4+
5+
import java.io.IOException;
6+
7+
public interface FTPInterface {
8+
9+
/**
10+
* if called, connects to a server
11+
*
12+
* @param server server ip/hostname
13+
* @param port FTPServer port
14+
* @param user user to log in with
15+
* @param pass passwort used to log in
16+
* @return
17+
* 0 = successful connection,
18+
* 1 = FTP server refused connection,
19+
* 2 = Could not login to FTP Server (probably wrong password),
20+
* 3 = Socket exception thrown, Server not found,
21+
* 4 = IO Exception
22+
*/
23+
int connect(String server, int port, String user, String pass);
24+
25+
/**
26+
* checking if connected
27+
* @return
28+
*/
29+
boolean isConnected();
30+
31+
/**
32+
* gives back the File list, given in current working dir
33+
* @return FTPFiles in current dir
34+
* @throws IOException couldn't read from current dir
35+
*/
36+
FTPFile[] getFileList() throws IOException;
37+
38+
/**
39+
* call to download a File from current dir
40+
* @param remoteFileName file to download from Server
41+
* @param localFile file to write to
42+
* @throws IOException couldn't download from current dir
43+
*/
44+
void downloadFile(String remoteFileName, String localFile) throws IOException;
45+
46+
/**
47+
* closes connection
48+
* pls always use at the end !!!!
49+
*/
50+
void closeConnection();
51+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package FTPClient;
2+
3+
import org.apache.commons.net.ftp.FTPClient;
4+
import org.apache.commons.net.ftp.FTPFile;
5+
import org.apache.commons.net.ftp.FTPReply;
6+
7+
import java.io.*;
8+
import java.net.SocketException;
9+
import java.text.SimpleDateFormat;
10+
import java.util.Date;
11+
12+
public class FtpClientImpl extends Thread implements FTPInterface{
13+
14+
public FtpClientImpl(OutputStream os) {
15+
logStream = new PrintStream(os);
16+
}
17+
18+
public FtpClientImpl() {
19+
LOGGING_OVER_FILE = false;
20+
}
21+
22+
private FTPClient client;
23+
24+
private PrintStream logStream;
25+
private String LOG_TAG = "FTP Client";
26+
private boolean LOGGING_OVER_FILE = true;
27+
28+
@Override
29+
public int connect(String server, int port, String user, String pass) {
30+
if( client == null ) {
31+
log("Getting passive FTP client");
32+
client = new FTPClient();
33+
34+
try {
35+
36+
client.connect(server, port);
37+
// After connection attempt, you should check the reply code to verify
38+
// success.
39+
int reply = client.getReplyCode();
40+
41+
if(!FTPReply.isPositiveCompletion(reply)) {
42+
client.disconnect();
43+
log(" ERROR : FTP server refused connection.");
44+
return 1;
45+
}
46+
47+
//after connecting to the server set the local passive mode
48+
client.enterLocalPassiveMode();
49+
50+
//send username and password to login to the server
51+
if( !client.login(user, pass) ) {
52+
log(" ERROR - Could not login to FTP Server");
53+
return 2;
54+
}
55+
} catch (SocketException e) {
56+
String message = "Socket exception thrown, Server not found";
57+
log("ERROR :" + message+"\n" + e);
58+
return 3;
59+
} catch (IOException e) {
60+
String message = "IO Exception";
61+
log("ERROR :" + message+"\n" + e);
62+
return 4;
63+
}
64+
}
65+
return 0;
66+
}
67+
68+
@Override
69+
public boolean isConnected() {
70+
return client.isConnected();
71+
}
72+
73+
@Override
74+
public FTPFile[] getFileList() throws IOException {
75+
if(client == null) {
76+
System.out.println("INFO : First initialize the FTPClient by calling 'initFTPPassiveClient()'");
77+
return null;
78+
}
79+
80+
log("DEBUG : Getting file listing for current director");
81+
FTPFile[] files = client.listFiles("");
82+
return files;
83+
}
84+
85+
@Override
86+
public void downloadFile(String remoteFileName, String downloadPath) throws IOException {
87+
// Download File using retrieveFile(String, OutputStream)
88+
OutputStream fileOutputStream = new BufferedOutputStream(new FileOutputStream(new File(downloadPath)));
89+
boolean success = client.retrieveFile(remoteFileName, fileOutputStream);
90+
fileOutputStream.close();
91+
92+
if (success)
93+
log("INFO : File " + remoteFileName + " has been downloaded successfully.");
94+
else
95+
log("ERROR : couldn't download " + remoteFileName + "from server!");
96+
97+
}
98+
99+
@Override
100+
public void closeConnection() {
101+
if( client == null ) {
102+
log("Nothing to close, the FTPClient wasn't initialized");
103+
return;
104+
}
105+
106+
//be polite and logout & close the connection before the application finishes
107+
try {
108+
client.logout();
109+
client.disconnect();
110+
} catch (IOException e) {
111+
String message = "Could not logout";
112+
log(message+"\n");
113+
}
114+
}
115+
116+
private void log(String msg) {
117+
118+
if (!LOGGING_OVER_FILE) {
119+
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
120+
Date date = new Date(System.currentTimeMillis());
121+
System.out.println(LOG_TAG + " : " + formatter.format(date) + " | " + msg);
122+
return;
123+
}
124+
125+
if (logStream == null) {
126+
throw new NullPointerException("ERROR: PrintStream for webServiceLog not initialized");
127+
}
128+
129+
130+
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
131+
Date date = new Date(System.currentTimeMillis());
132+
logStream.println(LOG_TAG + " : " + formatter.format(date) + " | " + msg);
133+
}
134+
135+
136+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package FTPClient;
2+
3+
import org.apache.commons.net.ftp.FTPFile;
4+
5+
import java.io.IOException;
6+
7+
public class TestMain {
8+
static FtpClientImpl client = new FtpClientImpl();
9+
10+
public static void main(String[] args) {
11+
client.connect("localhost", 5000, "ohdmOffViewer", "H!3r0glyph Sat3llite Era$er");
12+
FTPFile[] files = new FTPFile[0];
13+
try {
14+
files = client.getFileList();
15+
} catch (IOException e) {
16+
e.printStackTrace();
17+
}
18+
19+
System.out.println("File list : \n");
20+
for (FTPFile f :
21+
files) {
22+
System.out.println(f.getName());
23+
}
24+
25+
String downloadableFile = files[0].getName();
26+
try {
27+
client.downloadFile(downloadableFile, downloadableFile);
28+
} catch (IOException e) {
29+
e.printStackTrace();
30+
}
31+
32+
client.closeConnection();
33+
}
34+
}

src/main/java/Playground.java

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,15 @@
1-
import org.apache.ftpserver.FtpServer;
2-
import org.apache.ftpserver.FtpServerFactory;
3-
import org.apache.ftpserver.ftplet.FtpException;
4-
import org.apache.ftpserver.listener.ListenerFactory;
5-
import org.apache.ftpserver.ssl.SslConfigurationFactory;
6-
import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
1+
import Server.StaticVariables;
72

8-
import java.io.File;
3+
import java.io.*;
94

10-
public class Playground {
11-
public static void main(String[] args) throws FtpException {
12-
FtpServerFactory serverFactory = new FtpServerFactory();
5+
import static Server.StaticVariables.*;
136

14-
ListenerFactory factory = new ListenerFactory();
7+
class Playground {
8+
public static void main(String[] args) {
9+
StaticVariables.init();
1510

16-
// set the port of the listener
17-
factory.setPort(2221);
18-
19-
// define SSL configuration
20-
SslConfigurationFactory ssl = new SslConfigurationFactory();
21-
ssl.setKeystoreFile(new File("ftpserver.jks"));
22-
ssl.setKeystorePassword("password");
23-
24-
// set the SSL configuration for the listener
25-
factory.setSslConfiguration(ssl.createSslConfiguration());
26-
factory.setImplicitSsl(true);
27-
28-
// replace the default listener
29-
serverFactory.addListener("default", factory.createListener());
30-
31-
PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
32-
userManagerFactory.setFile(new File("users.properties"));
33-
34-
serverFactory.setUserManager(userManagerFactory.createUserManager());
35-
36-
// start the server
37-
FtpServer server = serverFactory.createServer();
38-
//mFtpServer = server;
39-
try {
40-
server.start();
41-
} catch (FtpException e) {
42-
e.printStackTrace();
43-
}
11+
System.out.println(standardUserName);
12+
System.out.println(ftpPort);
13+
System.out.println(logDefaultDir);
4414
}
45-
}
15+
}

src/main/java/Server/FTPServer/FTPService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void ftpServerSetUp() throws IOException {
6767
serverFactory = new FtpServerFactory();
6868
listenerFactory = new ListenerFactory();
6969

70-
defineSSLConf();
70+
//defineSSLConf();
7171
defineListener();
7272
setUpUser();
7373

@@ -95,7 +95,7 @@ private void defineSSLConf() {
9595
}
9696

9797
private void defineListener() {
98-
listenerFactory.setPort(ftpPort);// set the port of the listener (choose your desired port, not 1234)
98+
listenerFactory.setPort(ftpPort); // set the port of the listener (choose your desired port, not 1234)
9999
serverFactory.addListener("default", listenerFactory.createListener()); // adding a "default Listener"
100100
userManagerFactory = new PropertiesUserManagerFactory(); // adding a new UserManagementClass
101101
userManagerFactory.setFile(new File(ftpServiceUserPropertiesFile));//choose any. We're telling the FTP-server where to read its user list

src/main/java/Server/Service.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public int download_map(Coords[] coords, String date, String mapName) {
116116
'-t', f'{date}', '-o', f'{OSM_DIR}{mapname}.osm']
117117
*/
118118

119-
String[] template = {"java", "-jar", OHDM_DIR, "OHDMConverter.jar", "-r", "db_inter.txt", "-p",
119+
String[] template = {"java", "-jar", OHDM_DIR, "OHDMConverter.jar", "-r", "./db_inter.txt", "-p",
120120
"\"POLYGON((" + rearrangeCoordsForScript(coords) + "))", "-t", "\"" + date + "\"",
121121
"-o", OSM_DIR + mapName + ".osm"};
122122

0 commit comments

Comments
 (0)