Skip to content

Commit 43d0b57

Browse files
author
NoteFox
committed
FTP Server runs
Connections can be established can't read / write to given Dir, so still working on that, but for tests, it's enough for now
1 parent 3d26c31 commit 43d0b57

6 files changed

Lines changed: 192 additions & 78 deletions

File tree

Log.txt

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/main/java/Playground.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import java.io.File;
2+
3+
public class Playground {
4+
public static void main(String[] args) {
5+
File file = new File("./maps/map/helluw.txt");
6+
System.out.println(file.exists());
7+
8+
}
9+
}
Lines changed: 118 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,153 @@
11
package Server.FTPServer;
22

3+
import Server.StaticVariables;
34
import org.apache.ftpserver.FtpServer;
5+
import org.apache.ftpserver.FtpServerConfigurationException;
46
import org.apache.ftpserver.FtpServerFactory;
57
import org.apache.ftpserver.ftplet.*;
68
import org.apache.ftpserver.listener.ListenerFactory;
9+
import org.apache.ftpserver.ssl.SslConfigurationFactory;
710
import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
11+
import org.apache.ftpserver.usermanager.UserManagerFactory;
812
import org.apache.ftpserver.usermanager.impl.BaseUser;
913
import org.apache.ftpserver.usermanager.impl.WritePermission;
1014

1115
import java.io.File;
16+
import java.io.FileOutputStream;
1217
import java.io.IOException;
13-
import java.util.ArrayList;
14-
import java.util.HashMap;
15-
import java.util.List;
16-
import java.util.Map;
18+
import java.io.PrintStream;
19+
import java.text.FieldPosition;
20+
import java.text.SimpleDateFormat;
21+
import java.util.*;
22+
23+
import static Server.StaticVariables.*;
24+
25+
public class FTPService implements Runnable {
26+
27+
private FtpServer server;
28+
29+
private FtpServerFactory serverFactory;
30+
private ListenerFactory listenerFactory;
31+
32+
private UserManager um;
33+
private PropertiesUserManagerFactory userManagerFactory;
34+
35+
private boolean LOGGING = true;
36+
private File logFile = new File(ftpLogFile);
37+
private PrintStream logStream;
38+
39+
private String LOG_TAG = "FTPService";
40+
41+
@Override
42+
public void run() {
43+
try {
44+
logFile.createNewFile();
45+
logStream = new PrintStream(new FileOutputStream(logFile));
46+
} catch (IOException e) {
47+
e.printStackTrace();
48+
}
49+
50+
try {
51+
ftpServerSetUp();
52+
} catch (IOException e) {
53+
e.printStackTrace();
54+
}
55+
56+
server = serverFactory.createServer();
57+
try {
58+
server.start(); //Your FTP server starts listening for incoming FTP-connections, using the configuration options previously set
59+
} catch (FtpException ex) {
60+
ex.printStackTrace();
61+
}
62+
}
63+
64+
public void ftpServerSetUp() throws IOException {
1765

18-
public class FTPService {
19-
public void ftpServer() {
2066
//setting up a server Factory
21-
FtpServerFactory serverFactory = new FtpServerFactory();
22-
ListenerFactory factory = new ListenerFactory();
67+
serverFactory = new FtpServerFactory();
68+
listenerFactory = new ListenerFactory();
69+
70+
//defineSSLConf();
71+
defineListener();
72+
setUpUser();
2373

24-
factory.setPort(5000);// set the port of the listener (choose your desired port, not 1234)
25-
serverFactory.addListener("default", factory.createListener()); // adding a "default Listener"
26-
PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory(); // adding a new UserManagementClass
27-
userManagerFactory.setFile(new File("~/ftp/userList.properties"));//choose any. We're telling the FTP-server where to read its user list
74+
serverFactory.setUserManager(um);
75+
Map<String, Ftplet> m = new HashMap<>();
76+
m.put("miaFtplet", new FtpLetImpl());
77+
serverFactory.setFtplets(m);
78+
79+
Map<String, Ftplet> mappa = serverFactory.getFtplets();
80+
81+
log(String.valueOf(mappa.size()));
82+
log("Thread #" + Thread.currentThread().getId());
83+
log(mappa.toString());
84+
}
85+
86+
private void defineSSLConf() {
87+
// define SSL configuration
88+
SslConfigurationFactory ssl = new SslConfigurationFactory();
89+
ssl.setKeystoreFile(new File("src/main/resources/ftpServer.jks"));
90+
ssl.setKeystorePassword("password");
91+
92+
// set the SSL configuration for the listener
93+
listenerFactory.setSslConfiguration(ssl.createSslConfiguration());
94+
listenerFactory.setImplicitSsl(true);
95+
96+
// replace the default listener
97+
serverFactory.addListener("default", listenerFactory.createListener());
98+
PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
99+
userManagerFactory.setFile(new File("myusers.properties"));
100+
serverFactory.setUserManager(userManagerFactory.createUserManager());
101+
}
102+
103+
private void defineListener() {
104+
listenerFactory.setPort(ftpPort);// set the port of the listener (choose your desired port, not 1234)
105+
serverFactory.addListener("default", listenerFactory.createListener()); // adding a "default Listener"
106+
userManagerFactory = new PropertiesUserManagerFactory(); // adding a new UserManagementClass
107+
userManagerFactory.setFile(new File(ftpServiceUserPropertiesFile));//choose any. We're telling the FTP-server where to read its user list
28108
userManagerFactory.setPasswordEncryptor(new PasswordEncryptorsImpl()); // encrypts passwords of users by using the EncrImpl
109+
serverFactory.setUserManager(userManagerFactory.createUserManager());
110+
}
29111

112+
private void setUpUser() throws IOException {
30113
//Let's add a user, since our userList.properties file is empty on our first test run
31114
BaseUser user = new BaseUser();
32115

33-
user.setName("ohdmOffViewer");
34-
user.setPassword("H!3r0glyph Sat3llite Era$er");
35-
user.setHomeDirectory("/map");
116+
user.setName(standardUserName);
117+
user.setPassword(standardUserPassword);
118+
user.setHomeDirectory(ftpServiceMapDir);
36119

37120
List<Authority> authorities = new ArrayList<>();
38121
authorities.add(new WritePermission());
39122
user.setAuthorities(authorities);
40-
UserManager um = userManagerFactory.createUserManager();
41123

124+
try {
125+
um = userManagerFactory.createUserManager();
126+
} catch (FtpServerConfigurationException e) {
127+
try {
128+
new File(ftpServiceUserPropertiesFile).createNewFile();
129+
} catch (IOException ex) {
130+
new File("./ftp").mkdir();
131+
new File(ftpServiceUserPropertiesFile).createNewFile();
132+
}
133+
um = userManagerFactory.createUserManager();
134+
}
42135
try {
43136
um.save(user);//Save the user to the user list on the filesystem
44137
} catch (FtpException e1) {
45138
//Deal with exception as you need
46139
}
140+
}
47141

48-
serverFactory.setUserManager(um);
49-
Map<String, Ftplet> m = new HashMap<>();
50-
m.put("miaFtplet", new FtpLetImpl());
142+
public void log(String msg) {
143+
if (!LOGGING)
144+
return;
51145

52-
serverFactory.setFtplets(m);
146+
if (logStream == null)
147+
throw new NullPointerException("ERROR: PrintStream for log not initialized");
53148

54-
//Map<String, Ftplet> mappa = serverFactory.getFtplets();
55-
//System.out.println(mappa.size());
56-
//System.out.println("Thread #" + Thread.currentThread().getId());
57-
//System.out.println(mappa.toString());
58-
59-
FtpServer server = serverFactory.createServer();
60-
try {
61-
server.start();//Your FTP server starts listening for incoming FTP-connections, using the configuration options previously set
62-
} catch (FtpException ex) {
63-
//Deal with exception as you need
64-
}
149+
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
150+
Date date = new Date(System.currentTimeMillis());
151+
logStream.println(LOG_TAG + formatter.format(date) + " | " + msg);
65152
}
66153
}

src/main/java/Server/Service.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,24 @@
1010
import java.util.Date;
1111
import java.util.List;
1212

13+
import static Server.StaticVariables.*;
14+
1315
public class Service implements Runnable, srvInterface{
1416
private String LISTEN_IP = "0.0.0.0"; // "::" ipv6 , "0.0.0.0" ipv4
1517
private int LISTEN_PORT = 5000;
1618

17-
private String OSM_DIR = "./maps/osm";
18-
private String MAP_DIR = "./maps/map";
19-
private String OHDM_DIR = "";
19+
//fetching DirStrings from StaticVariables Class
20+
private String OSM_DIR = osmDir;
21+
private String MAP_DIR = mapDir;
22+
private String OHDM_DIR = ohdmDir;
2023

2124
private boolean LOGGING = true;
22-
private File logFile = new File("Log.txt");
25+
// fetching ftpLogFile from StaticVariables Class
26+
private File logFile = new File(webServiceLogFile);
2327
private PrintStream logStream;
2428

29+
private String LOG_TAG = "WebService";
30+
2531
List<QueueRequest> WORK_QUEUE = new ArrayList<>();
2632

2733
boolean watcherWorking;
@@ -34,6 +40,7 @@ public void run() {
3440
} catch (IOException e) {
3541
e.printStackTrace();
3642
}
43+
fixFolders();
3744
watch();
3845
}
3946

@@ -92,11 +99,11 @@ public void log(String msg) {
9299
return;
93100

94101
if (logStream == null)
95-
throw new NullPointerException("ERROR: PrintStream for log not initialized");
102+
throw new NullPointerException("ERROR: PrintStream for webServiceLog not initialized");
96103

97104
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
98105
Date date = new Date(System.currentTimeMillis());
99-
logStream.println(formatter.format(date) + " | " + msg);
106+
logStream.println(LOG_TAG + " : " + formatter.format(date) + " | " + msg);
100107
}
101108

102109
public int download_map(Coords[] coords, String date, String mapName) {

src/main/java/Server/SpringClass.java

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import Classes.CommandReturn;
44
import Classes.Coords;
55
import Classes.QueueRequest;
6+
import Server.FTPServer.FTPService;
67
import ch.qos.logback.core.encoder.EchoEncoder;
78
import org.springframework.boot.SpringApplication;
89
import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -23,10 +24,18 @@ public class SpringClass {
2324
public static Service serviceInstance;
2425
public static Thread serviceThread;
2526

27+
public static FTPService ftpInstance;
28+
public static Thread ftpThread;
29+
2630
public static void main(String[] args) {
2731
serviceInstance = new Service();
2832
serviceThread = new Thread(serviceInstance);
2933
serviceThread.start();
34+
35+
ftpInstance = new FTPService();
36+
ftpThread = new Thread(ftpInstance);
37+
ftpThread.start();
38+
3039
SpringApplication.run(SpringClass.class, args);
3140
}
3241

@@ -39,25 +48,31 @@ public void queue_map(QueueRequest q) {
3948
serviceThread.interrupt();
4049
}
4150

42-
@GetMapping("/status")
43-
public String status() {
51+
@GetMapping("/service")
52+
public String webServiceStatus() {
4453
StringBuilder sb = new StringBuilder();
45-
sb.append("Service Running = " + serviceInstance.serviceRunning + "\n");
46-
sb.append("WatcherThread currently working = " + serviceInstance.watcherWorking + "\n");
47-
sb.append("current Queue length = " + serviceInstance.WORK_QUEUE.size() + "\n");
48-
sb.append("QUEUE LIST: \n");
54+
sb.append("<head> <title> WebService status</title> </head>");
55+
sb.append("<p> Service Running = " + serviceInstance.serviceRunning + "</p>");
56+
sb.append("<p> WatcherThread currently working = " + serviceInstance.watcherWorking + "</p>");
57+
sb.append("<p> current Queue length = " + serviceInstance.WORK_QUEUE.size() + "</p>");
58+
sb.append("<p> QUEUE LIST: </p>");
4959
int i = 0;
5060
for (QueueRequest r: serviceInstance.WORK_QUEUE) {
51-
sb.append(r.getMapName()).append("\n");
52-
sb.append("-------------------------\n");
53-
sb.append(" - Position : " + i);
54-
sb.append(" - Date : " + r.getDate());
55-
sb.append(" - Classes.Coords : \n" + r.getPrintableCoordsString());
61+
sb.append("<p>" + r.getMapName() + "</p>");
62+
sb.append("<p> ------------------------- </p>");
63+
sb.append("<p> - Position : " + i + "</p>");
64+
sb.append("<p> - Date : " + r.getDate() + "</p>");
65+
sb.append("<p> - Classes.Coords : \n" + r.getPrintableCoordsString() + "</p>");
5666
i++;
5767
}
5868
return sb.toString();
5969
}
6070

71+
@GetMapping("/ftp")
72+
public String ftpServiceStatus() {
73+
return ("yeah... working on that");
74+
}
75+
6176
// 192.168.178.35:8080/request?name=mapname&coords=13.005,15.123_13.005,15.123_13.005,15.123_13.005,15.123_13.005,15.123&date=2117-12-11
6277
@GetMapping("/request")
6378
public String request(@RequestParam(value = "name", defaultValue = "noname") String mapname,
@@ -128,4 +143,11 @@ public String request(@RequestParam(value = "name", defaultValue = "noname") Str
128143
}
129144
return null;
130145
}
146+
147+
@GetMapping("/test")
148+
public String test() {
149+
return ("<head> <title> PageTitle </title> </head> <body> <h1> This is a Heading </h1> <p> This is a paragraph.</p> </body>");
150+
}
151+
152+
131153
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package Server;
2+
3+
public class StaticVariables {
4+
public static final String ftpLogFile = "ftpLog.txt";
5+
public static final String webServiceLogFile = "webServiceLog.txt";
6+
7+
public static final String osmDir = "./maps/osm";
8+
public static final String mapDir = "./maps/map";
9+
public static final String ohdmDir = "";
10+
11+
public static final String ftpServiceMapDir = mapDir;
12+
public static final String ftpServiceUserPropertiesFile = "./ftp/userList.properties";
13+
public static final int ftpPort = 5000;
14+
15+
// will later be read from a File instead of being HardCoded
16+
public static final String standardUserName = "ohdmOffViewer";
17+
public static final String standardUserPassword = "H!3r0glyph Sat3llite Era$er";
18+
19+
}

0 commit comments

Comments
 (0)