Skip to content

Commit 40a9328

Browse files
author
NoteFox
committed
reads now options from init.txt
1 parent 5c6b33f commit 40a9328

6 files changed

Lines changed: 113 additions & 132 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+

src/main/java/Playground.java

Lines changed: 10 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,15 @@
1-
import FTPClient.FtpClientImpl;
2-
import org.apache.commons.net.ftp.FTPClient;
3-
import org.apache.commons.net.ftp.FTPFile;
4-
import org.apache.commons.net.ftp.FTPReply;
1+
import Server.StaticVariables;
52

6-
import java.io.IOException;
7-
import java.net.InetAddress;
8-
import java.net.SocketException;
3+
import java.io.*;
94

10-
public class Playground {
11-
static FTPClient client = null;
12-
static String server = "localhost";
13-
static int port = 5000;
14-
static String user = "ohdmOffViewer";
15-
static String pass = "H!3r0glyph Sat3llite Era$er";
16-
public static void main(String [] args) {
5+
import static Server.StaticVariables.*;
176

18-
//initialise the client
19-
initPassiveClient();
7+
class Playground {
8+
public static void main(String[] args) {
9+
StaticVariables.init();
2010

21-
//do stuff
22-
FTPFile[] files = listFiles("./");
23-
if( files != null ) {
24-
System.err.println("Listing Files:");
25-
for( FTPFile f : files) {
26-
System.err.println(f.getName());
27-
}
28-
}
29-
30-
//close the client
31-
close();
32-
}
33-
34-
/**
35-
* getPassiveClient retrive a FTPClient object that's set to local passive mode
36-
*
37-
* @return FTPClient
38-
*/
39-
public static FTPClient initPassiveClient() {
40-
if( client == null ) {
41-
System.out.println("Getting passive FTP client");
42-
client = new FTPClient();
43-
44-
try {
45-
client.connect(server, port);
46-
// After connection attempt, you should check the reply code to verify
47-
// success.
48-
int reply = client.getReplyCode();
49-
50-
if(!FTPReply.isPositiveCompletion(reply)) {
51-
client.disconnect();
52-
System.err.println("FTP server refused connection.");
53-
System.exit(0);
54-
}
55-
56-
//after connecting to the server set the local passive mode
57-
//client.enterLocalPassiveMode();
58-
client.enterRemoteActiveMode(InetAddress.getByName(server), port);
59-
60-
//send username and password to login to the server
61-
if( !client.login(user, pass) ) {
62-
System.err.println("Could not login to FTP Server");
63-
System.exit(0);
64-
}
65-
} catch (SocketException e) {
66-
e.printStackTrace();
67-
} catch (IOException e) {
68-
e.printStackTrace();
69-
}
70-
}
71-
72-
return client;
73-
}
74-
75-
public static void close() {
76-
if( client == null ) {
77-
System.err.println("Nothing to close, the FTPClient wasn't initialized");
78-
return;
79-
}
80-
81-
//be polite and logout & close the connection before the application finishes
82-
try {
83-
client.logout();
84-
client.disconnect();
85-
} catch (IOException e) {
86-
String message = "Could not logout";
87-
System.err.println(message+"\n");
88-
}
89-
}
90-
91-
/**
92-
* listFiles uses the FTPClient to retrieve files in the specified directory
93-
*
94-
* @return array of FTPFile objects
95-
*/
96-
private static FTPFile[] listFiles(String dir) {
97-
if( client == null ) {
98-
System.err.println("First initialize the FTPClient by calling 'initFTPPassiveClient()'");
99-
return null;
100-
}
101-
102-
try {
103-
System.out.println("DEBUG: Getting file listing for current director");
104-
FTPFile[] files = client.listFiles(dir);
105-
106-
return files;
107-
} catch (IOException e) {
108-
String message = "";
109-
System.err.println(message+"\n");
110-
}
111-
112-
return null;
11+
System.out.println(standardUserName);
12+
System.out.println(ftpPort);
13+
System.out.println(logDefaultDir);
11314
}
114-
}
15+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

src/main/java/Server/SpringClass.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
package Server;
22

3-
import Classes.CommandReturn;
43
import Classes.Coords;
54
import Classes.QueueRequest;
65
import Server.FTPServer.FTPService;
7-
import ch.qos.logback.core.encoder.EchoEncoder;
86
import org.springframework.boot.SpringApplication;
97
import org.springframework.boot.autoconfigure.SpringBootApplication;
8+
import org.springframework.context.annotation.Configuration;
109
import org.springframework.web.bind.annotation.GetMapping;
1110
import org.springframework.web.bind.annotation.RequestParam;
1211
import org.springframework.web.bind.annotation.RestController;
1312

1413
import java.io.*;
15-
import java.text.SimpleDateFormat;
1614
import java.util.ArrayList;
17-
import java.util.Date;
1815
import java.util.List;
16+
import static Server.StaticVariables.*;
1917

2018
@SpringBootApplication
2119
@RestController
@@ -28,6 +26,9 @@ public class SpringClass {
2826
public static Thread ftpThread;
2927

3028
public static void main(String[] args) {
29+
// initializes Static Variables by reading them from init.txt
30+
StaticVariables.init();
31+
3132
try {
3233
setUpLogger();
3334
} catch (IOException e) {
@@ -42,6 +43,7 @@ public static void main(String[] args) {
4243
ftpThread = new Thread(ftpInstance);
4344
ftpThread.start();
4445

46+
System.getProperties().put("server.port", webPort);
4547
SpringApplication.run(SpringClass.class, args);
4648
}
4749

@@ -57,9 +59,7 @@ private static void setUpLogger() throws IOException {
5759
}
5860
}
5961

60-
/**
61-
* method to queue a map into the "REQUEST QUEUE"
62-
*/
62+
/** method to queue a map into the "REQUEST QUEUE" */
6363
public void queue_map(QueueRequest q) {
6464
serviceInstance.WORK_QUEUE.add(q);
6565
if (!serviceInstance.watcherWorking)
@@ -94,8 +94,8 @@ public String ftpServiceStatus() {
9494
// 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
9595
@GetMapping("/request")
9696
public String request(@RequestParam(value = "name", defaultValue = "noname") String mapname,
97-
@RequestParam(value = "coords") String coords,
98-
@RequestParam(value = "date", defaultValue = "insert") String date) {
97+
@RequestParam(value = "coords", defaultValue = "13.005,15.123_13.005,15.123_13.005,15.123_13.005,15.123_13.005,15.123") String coords,
98+
@RequestParam(value = "date", defaultValue = "2000-12-11") String date) {
9999

100100
/**
101101
endpoint for map requests
@@ -164,7 +164,7 @@ public String request(@RequestParam(value = "name", defaultValue = "noname") Str
164164

165165
@GetMapping("/test")
166166
public String test() {
167-
return ("<head> <title> PageTitle </title> </head> <body> <h1> This is a Heading </h1> <p> This is a paragraph.</p> </body>");
167+
return ("<head> <title> Running ? </title> </head> <body> <h1> Am I running? </h1> <p> i guess i do </p> </body>");
168168
}
169169

170170

Lines changed: 88 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,104 @@
11
package Server;
22

3+
import java.io.*;
4+
35
public class StaticVariables {
4-
public static final String logDefaultDir = "./log";
6+
public static final String defaultInitFile = "./init.txt";
7+
// ---------------------------- will be read from File ----------------------------//
8+
public static String logDefaultDir;
9+
public static String mapsDefaultDir;
10+
public static String ftpDefaultDir;
11+
12+
public static boolean doesFTPServiceLog;
13+
public static boolean doesWebServiceLog;
514

6-
public static final boolean doesFTPServiceLog = true;
7-
public static final boolean doesWebServiceLog = true;
15+
public static int webPort;
16+
public static int ftpPort;
817

18+
// default values !!!! ALWAYS CHANGE IN FILE !!!!
19+
public static String standardUserName = "ohdmOffViewer";
20+
public static String standardUserPassword = "H!3r0glyph Sat3llite Era$er";
21+
// -------------------------------------------------------------------------------//
922
public static final String ftpLogFile = logDefaultDir + "/ftpLog.txt";
1023
public static final String webServiceLogFile = logDefaultDir + "/webServiceLog.txt";
1124

12-
public static final String mapsDefalutDir = "./maps";
13-
public static final String osmDir = mapsDefalutDir + "/osm";
14-
public static final String mapDir = mapsDefalutDir + "/map";
25+
public static final String osmDir = mapsDefaultDir + "/osm";
26+
public static final String mapDir = mapsDefaultDir + "/map";
1527
public static final String ohdmDir = "";
1628

1729
public static final String ftpServiceMapDir = mapDir;
18-
public static final String ftpDefaultDir = "./ftp";
1930
public static final String ftpServiceUserPropertiesFile = ftpDefaultDir + "/userList.properties";
2031

21-
public static final int ftpPort = 5000;
2232

23-
// will later be read from a File instead of being HardCoded
24-
public static final String standardUserName = "ohdmOffViewer";
25-
public static final String standardUserPassword = "H!3r0glyph Sat3llite Era$er";
33+
public static void init() {
34+
if (!new File(defaultInitFile).exists()) {
35+
giveStandardValues();
36+
return;
37+
}
38+
39+
BufferedReader dis = null;
40+
try { dis = new BufferedReader(new InputStreamReader(new FileInputStream(new File(defaultInitFile)))); }
41+
catch (FileNotFoundException e) { e.printStackTrace(); }
42+
43+
// readString
44+
String readString = "";
45+
try { while (dis.ready()) readString += dis.readLine() + "\n"; }
46+
catch (IOException e) { e.printStackTrace(); }
47+
48+
// cleanup String
49+
String usableLines = "";
50+
for (String s: readString.split("\n")) if (!(s.startsWith("//") || s.startsWith("[") || s.isEmpty())) usableLines += s + "\n";
51+
52+
// split names and values
53+
String[][] valuesSplit = new String[usableLines.split("\n").length][2];
54+
for (int i = 0; i < usableLines.split("\n").length; i++) {
55+
valuesSplit[i][0] = usableLines.split("\n")[i].split("=")[0].trim();
56+
valuesSplit[i][1] = usableLines.split("\n")[i].split("=")[1].trim();
57+
}
58+
59+
for (String[] s :
60+
valuesSplit) {
61+
assignValue(s);
62+
}
63+
}
64+
65+
private static void giveStandardValues() {
66+
doesFTPServiceLog = true;
67+
doesWebServiceLog = true;
68+
69+
logDefaultDir = "./log";
70+
mapsDefaultDir = "./maps";
71+
ftpDefaultDir = "./ftp";
72+
73+
webPort = 5001;
74+
ftpPort = 5000;
75+
76+
standardUserName = "ohdmOffViewer";
77+
standardUserPassword = "H!3r0glyph Sat3llite Era$er";
78+
}
79+
80+
private static void assignValue(String[] s) {
81+
switch (s[0]) {
82+
case "logDefaultDir": logDefaultDir = s[1]; break;
83+
84+
case "mapsDefaultDir": mapsDefaultDir = s[1]; break;
85+
86+
case "ftpDefaultDir": ftpDefaultDir = s[1]; break;
87+
88+
case "doesFTPServiceLog": doesFTPServiceLog = Boolean.parseBoolean(s[1]); break;
89+
90+
case "doesWebServiceLog": doesWebServiceLog = Boolean.parseBoolean(s[1]); break;
91+
92+
case "webPort": try { webPort = Integer.parseInt(s[1]); } catch (NumberFormatException e) { webPort = 5001; } break;
93+
94+
case "ftpPort": try { ftpPort = Integer.parseInt(s[1]); } catch (NumberFormatException e) { ftpPort = 5000; } break;
95+
96+
case "standardUserName": standardUserName = s[1]; break;
97+
98+
case "standardUserPassword": standardUserPassword = s[1]; break;
2699

100+
default:
101+
System.err.println("couldn't find " + s[0] + " in list | Value = " + s[1]);
102+
}
103+
}
27104
}

0 commit comments

Comments
 (0)